Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
util::ObservableValue< T > Class Template Reference

An observable value container that notifies observers when the value changes. More...

#include <ObservableValue.hpp>

Inheritance diagram for util::ObservableValue< T >:
Collaboration diagram for util::ObservableValue< T >:

Public Member Functions

 ObservableValue (std::convertible_to< T > auto &&value)
 Constructs ObservableValue with initial value.
 ObservableValue ()
 Constructs ObservableValue with default initial value.
 ObservableValue (ObservableValue const &)=delete
 ObservableValue (ObservableValue &&)=default
ObservableValueoperator= (ObservableValue const &)=delete
ObservableValueoperator= (ObservableValue &&)=default
ObservableValueoperator= (std::convertible_to< T > auto &&val)
 Assignment operator that updates value and notifies observers.
ObservableGuard operator-> ()
 Provides deferred notification access to the value.
 operator T const & () const
 Implicit conversion to const reference of the value.
T const & get () const
 Explicitly gets the current value.
void set (std::convertible_to< T > auto &&val)
 Sets a new value and notifies observers if changed.
void forceNotify () override
 Forces notification of all observers with the current value.
Public Member Functions inherited from util::impl::ObservableValueBase< T >
boost::signals2::connection observe (std::invocable< T const & > auto &&fn)
 Registers an observer callback for value changes.
bool hasObservers () const
 Checks if there are any active observers.

Additional Inherited Members

Protected Member Functions inherited from util::impl::ObservableValueBase< T >
void notifyObservers (T const &value)
 Notifies all observers with the given value.
Protected Attributes inherited from util::impl::ObservableValueBase< T >
boost::signals2::signal< void(T const &)> onUpdate_

Detailed Description

template<Observable T>
requires (not SomeAtomic<T>)
class util::ObservableValue< T >

An observable value container that notifies observers when the value changes.

ObservableValue wraps a value of type T and provides a mechanism to observe changes to that value. When the value is modified (and actually changes), all registered observers are notified.

Template Parameters
TThe type of value to observe. Must satisfy the Observable concept.
Thread Safety
  • Observer subscription/unsubscription (observe() and connection.disconnect()) are thread-safe
  • Value modification operations (set(), operator=) are NOT thread-safe and require external synchronization
  • Observer callbacks are invoked synchronously on the same thread that triggered the value change
  • If observers need to perform work on different threads, they must handle dispatch themselves (e.g., using an async execution context or message queue)
Exception Handling
  • If an observer callback throws an exception, the exception will propagate to the caller
  • The value will still be updated even if observers throw exceptions
  • No guarantee is made about whether other observers will be called if one throws
  • It is the caller's responsibility to handle exceptions from observer callbacks

Constructor & Destructor Documentation

◆ ObservableValue()

template<Observable T>
util::ObservableValue< T >::ObservableValue ( std::convertible_to< T > auto && value)
inline

Constructs ObservableValue with initial value.

Parameters
valueInitial value (must be convertible to T)

Member Function Documentation

◆ forceNotify()

template<Observable T>
void util::ObservableValue< T >::forceNotify ( )
inlineoverridevirtual

Forces notification of all observers with the current value.

This method will notify all observers with the current value regardless of whether the value has changed since the last notification.

Implements util::impl::ObservableValueBase< T >.

◆ get()

template<Observable T>
T const & util::ObservableValue< T >::get ( ) const
inlinenodiscard

Explicitly gets the current value.

Returns
Const reference to the stored value

◆ operator T const &()

template<Observable T>
util::ObservableValue< T >::operator T const & ( ) const
inlinenodiscard

Implicit conversion to const reference of the value.

Returns
Const reference to the stored value

◆ operator->()

template<Observable T>
ObservableGuard util::ObservableValue< T >::operator-> ( )
inlinenodiscard

Provides deferred notification access to the value.

Returns an ObservableGuard that allows modification of the value with notification deferred until the guard is destroyed.

Returns
ObservableGuard for deferred notification

◆ operator=()

template<Observable T>
ObservableValue & util::ObservableValue< T >::operator= ( std::convertible_to< T > auto && val)
inline

Assignment operator that updates value and notifies observers.

Updates the stored value and notifies observers if the new value differs from the current value (using operator!=).

Parameters
valNew value (must be convertible to T)
Returns
Reference to this object for chaining
Exceptions
Anyexception thrown by observer callbacks will propagate

◆ set()

template<Observable T>
void util::ObservableValue< T >::set ( std::convertible_to< T > auto && val)
inline

Sets a new value and notifies observers if changed.

Updates the stored value and notifies all observers if the new value differs from the current value (using operator!=). If the values are equal, no notification occurs.

Parameters
valNew value (must be convertible to T)
Exceptions
Anyexception thrown by observer callbacks will propagate
Thread Safety
  • This method is NOT thread-safe and requires external synchronization for concurrent access
  • Observer callbacks are invoked synchronously on the calling thread

The documentation for this class was generated from the following file: