Refactor folder item management and move NotifyValue to a new utility header
This commit is contained in:
55
src/util/notify.h
Normal file
55
src/util/notify.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef YAZE_UTIL_NOTIFY_H
|
||||
#define YAZE_UTIL_NOTIFY_H
|
||||
|
||||
namespace yaze {
|
||||
namespace util {
|
||||
|
||||
/**
|
||||
* @class NotifyValue
|
||||
* @brief A class to manage a value that can be modified and notify when it
|
||||
* changes.
|
||||
*/
|
||||
template <typename T>
|
||||
class NotifyValue {
|
||||
public:
|
||||
NotifyValue() : value_(), modified_(false), temp_value_() {}
|
||||
NotifyValue(const T &value)
|
||||
: value_(value), modified_(false), temp_value_() {}
|
||||
|
||||
void set(const T &value) {
|
||||
value_ = value;
|
||||
modified_ = true;
|
||||
}
|
||||
|
||||
const T &get() {
|
||||
modified_ = false;
|
||||
return value_;
|
||||
}
|
||||
|
||||
T &mutable_get() {
|
||||
modified_ = false;
|
||||
temp_value_ = value_;
|
||||
return temp_value_;
|
||||
}
|
||||
|
||||
void apply_changes() {
|
||||
if (temp_value_ != value_) {
|
||||
value_ = temp_value_;
|
||||
modified_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void operator=(const T &value) { set(value); }
|
||||
operator T() { return get(); }
|
||||
|
||||
bool modified() const { return modified_; }
|
||||
|
||||
private:
|
||||
T value_;
|
||||
bool modified_;
|
||||
T temp_value_;
|
||||
};
|
||||
} // namespace util
|
||||
} // namespace yaze
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user