.. index:: pair: class; EE::ObservableValue .. _doxid-class_e_e_1_1_observable_value: template class EE::ObservableValue ================================== .. toctree:: :hidden: struct_EE_ObservableValue_State.rst class_EE_ObservableValue_Connection.rst class_EE_ObservableValue_WeakHandle.rst Overview ~~~~~~~~ Owns a value and notifies scoped observers after the value changes. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block #include template class ObservableValue { public: // typedefs typedef std::function :target:`Callback`; // structs struct :ref:`State`; // classes class :ref:`Connection`; class :ref:`WeakHandle`; // construction :target:`ObservableValue`(); :target:`ObservableValue`(T value); :target:`ObservableValue`(const ObservableValue&); :target:`ObservableValue`(ObservableValue&&); // methods ObservableValue& :target:`operator=`(const ObservableValue&); ObservableValue& :target:`operator=`(ObservableValue&&); const T& :target:`get`() const; void :target:`set`(const T& value); void :target:`set`(T&& value); ObservableValue& :target:`operator=`(const T& value); ObservableValue& :target:`operator=`(T&& value); const T& :target:`operator*`() const; const T* :target:`operator->`() const; :target:`operator const T &`() const; :ref:`Connection` :target:`observe`(:ref:`Callback` callback); :ref:`WeakHandle` :target:`weakHandle`() const; }; .. _details-class_e_e_1_1_observable_value: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ Owns a value and notifies scoped observers after the value changes. :ref:`ObservableValue ` is a deliberately small synchronous primitive. Assignment and :ref:`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 :ref:`UI ` bindings to keep observing the moved-to instance. :ref:`ObservableValue ` and all of its connections must be used from a single owning thread. Use :ref:`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 :ref:`UI ` may attach with UIValueBinding and disappear safely later. .. ref-code-block:: cpp struct ApplicationConfig { ObservableValue showLineNumbers{ true }; }; ApplicationConfig config; auto connection = config.showLineNumbers.observe( []( bool enabled ) { updateEditorPolicy( enabled ); } ); config.showLineNumbers = false;