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

Partial specialization of ObservableValue for atomic types. More...

#include <ObservableValue.hpp>

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

Public Member Functions

 ObservableValue (std::convertible_to< T > auto &&value)
 Constructs ObservableValue with initial atomic 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 atomic value and notifies observers.
get () const
 Gets the current atomic value.
 operator T () const
 Implicit conversion to the current atomic value.
void set (std::convertible_to< T > auto &&val)
 Sets a new atomic 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>
class util::ObservableValue< std::atomic< T > >

Partial specialization of ObservableValue for atomic types.

This specialization provides thread-safe observation of atomic values while maintaining atomic semantics. It avoids the issues of copying atomic values and handles race conditions properly.

Template Parameters
TThe underlying type stored in the atomic
Thread Safety
  • All operations are thread-safe
  • Observer notifications are atomic with respect to value changes
  • Multiple threads can safely modify and observe the atomic value
Performance Considerations
  • Uses atomic compare-and-swap operations for updates
  • Minimizes atomic reads during guard operations
  • Observer notifications happen outside of atomic operations when possible

Constructor & Destructor Documentation

◆ ObservableValue()

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

Constructs ObservableValue with initial atomic value.

Parameters
valueInitial value (will be stored in the atomic)

Member Function Documentation

◆ forceNotify()

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

Forces notification of all observers with the current value.

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

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

◆ get()

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

Gets the current atomic value.

Returns
Current value stored in the atomic

◆ operator T()

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

Implicit conversion to the current atomic value.

Returns
Current value stored in the atomic

◆ operator=()

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

Assignment operator that updates atomic value and notifies observers.

Uses atomic compare-and-swap to update the value and notifies observers only if the value actually changed.

Parameters
valNew value
Returns
Reference to this object for chaining

◆ set()

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

Sets a new atomic value and notifies observers if changed.

Uses atomic compare-and-swap to update the value. Notifies all observers if the value actually changed.

Parameters
valNew value

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