.. 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 T :target:`ValueType`; typedef std::function :target:`Callback`; // structs struct :ref:`State`; // classes class :ref:`Connection`; class :ref:`WeakHandle`; // construction :ref:`ObservableValue`(); :ref:`ObservableValue`(T value); :target:`ObservableValue`(const ObservableValue&); :target:`ObservableValue`(ObservableValue&&); // methods ObservableValue& :target:`operator=`(const ObservableValue&); ObservableValue& :target:`operator=`(ObservableValue&&); const T& :ref:`get`() const; void :ref:`set`(const T& value); void :ref:`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` :ref:`observe`(:ref:`Callback` callback); :ref:`WeakHandle` :ref:`weakHandle`() const; std::size_t :ref:`observerCount`() const; bool :ref:`isNotifying`() 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; Construction ------------ .. index:: pair: function; ObservableValue .. _doxid-class_e_e_1_1_observable_value_1af5b7bbc0cc20b466d357e4dc1f5bc1e0: .. ref-code-block:: cpp :class: doxyrest-title-code-block ObservableValue() Creates an observable containing a default-constructed value. .. index:: pair: function; ObservableValue .. _doxid-class_e_e_1_1_observable_value_1a467fd1c9a36ba40acffe8d1d74759537: .. ref-code-block:: cpp :class: doxyrest-title-code-block ObservableValue(T value) Creates an observable containing ``value``. No notification is emitted. Methods ------- .. index:: pair: function; get .. _doxid-class_e_e_1_1_observable_value_1a09b30b2f00ee8e230053faf4b2c12396: .. ref-code-block:: cpp :class: doxyrest-title-code-block const T& get() const .. rubric:: Returns: A reference to the current value. .. index:: pair: function; set .. _doxid-class_e_e_1_1_observable_value_1ad0f68766b43ac8a21855aabc3a0e2520: .. ref-code-block:: cpp :class: doxyrest-title-code-block void set(const T& value) Replaces the value and synchronously notifies observers when it changed. .. index:: pair: function; set .. _doxid-class_e_e_1_1_observable_value_1a35a74adadd32b6b2e19a77b9535d1f5b: .. ref-code-block:: cpp :class: doxyrest-title-code-block void set(T&& value) Move-replaces the value and synchronously notifies observers when it changed. .. index:: pair: function; observe .. _doxid-class_e_e_1_1_observable_value_1a3ba08b2ee0bd84be4bbf25aacc726f87: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`Connection` observe(:ref:`Callback` callback) Observes subsequent value changes. Registration does not invoke ``callback`` with the current value. Reentrant :ref:`set() ` calls are queued and delivered after the current observer snapshot completes. .. rubric:: Returns: A scoped connection; destroying it disconnects the callback. .. index:: pair: function; weakHandle .. _doxid-class_e_e_1_1_observable_value_1a2f100b43921d89134863a0b884855340: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`WeakHandle` weakHandle() const .. rubric:: Returns: A non-owning handle that expires safely with this observable. .. index:: pair: function; observerCount .. _doxid-class_e_e_1_1_observable_value_1ac0753da9b3c8f1dda95ea25e908d9a16: .. ref-code-block:: cpp :class: doxyrest-title-code-block std::size_t observerCount() const .. rubric:: Returns: The number of currently connected observers. .. index:: pair: function; isNotifying .. _doxid-class_e_e_1_1_observable_value_1a1372269fe7a7225987c7818949290d25: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool isNotifying() const .. rubric:: Returns: Whether observer callbacks are currently being delivered.