template class EE::ObservableValue¶
Overview¶
Owns a value and notifies scoped observers after the value changes. More…
#include <observablevalue.hpp> template <typename T> class ObservableValue { public: // typedefs typedef std::function<void(const T&)> Callback; // structs struct State; // classes class Connection; class WeakHandle; // construction ObservableValue(); ObservableValue(T value); ObservableValue(const ObservableValue&); ObservableValue(ObservableValue&&); // methods ObservableValue& operator=(const ObservableValue&); ObservableValue& operator=(ObservableValue&&); const T& get() const; void set(const T& value); void set(T&& value); ObservableValue& operator=(const T& value); ObservableValue& operator=(T&& value); const T& operator*() const; const T* operator->() const; operator const T &() const; Connection observe(Callback callback); WeakHandle weakHandle() const; };
Detailed Documentation¶
Owns a value and notifies scoped observers after the value changes.
ObservableValue is a deliberately small synchronous primitive. Assignment and set() notify observers immediately on the calling thread. Observer callbacks use snapshot semantics: changes to the observer list during a notification take effect on the next notification.
The class is non-copyable. Moving it transfers the value and its existing observers, allowing handles and UI bindings to keep observing the moved-to instance. ObservableValue and all of its connections must be used from a single owning thread.
Use ObservableValue for model or application state whose observers are not known by the model. A configuration object, for example, can publish changes without depending on UIWidget; a live UI may attach with UIValueBinding and disappear safely later.
struct ApplicationConfig { ObservableValue<bool> showLineNumbers{ true }; }; ApplicationConfig config; auto connection = config.showLineNumbers.observe( []( bool enabled ) { updateEditorPolicy( enabled ); } ); config.showLineNumbers = false;