TeiaCareSDK  v0.1.0
TeiaCareSDK is a collection of reusable C++ components
tc::sdk::observable< T > Class Template Reference

Utility class to observe a value with a user provided callback. More...

#include <observable.hpp>

Public Member Functions

 observable (const T &t, const std::function< void(T)> &callback)
 Constructor. More...
 
 observable (T &&t, const std::function< void(T)> &callback)
 Constructor. More...
 
 ~observable ()=default
 Destructor. More...
 
bool callback_enabled () const
 Check if the callback is currently enabled. More...
 
void callback_enabled (bool is_enabled)
 Enable or disable the callback temporarily. More...
 
 operator T const () const
 Return the observed value. More...
 
 operator T ()
 Return the observed value. More...
 
value () const
 Return the observed value. More...
 
observableoperator= (T &&t)
 Update the observed value. More...
 
observableoperator= (const T &t)
 Update the observed value. More...
 

Protected Member Functions

void set (const T &value)
 
void set (T &&value)
 

Detailed Description

template<std::equality_comparable T>
class tc::sdk::observable< T >

Utility class to observe a value with a user provided callback.

Template Parameters
TObserved item type
CallbackTUser defined callback type

The callback is invoked every time the observed value is updated. The observable callback receives as a parameter the updated observed value. Thread safe.

Note
T must be Equality Comparable, i.e. it must implement the equality comparison operator:
bool operator==(T const&) const { }
Examples
example_observable.cpp.

Definition at line 45 of file observable.hpp.

Constructor & Destructor Documentation

◆ observable() [1/2]

template<std::equality_comparable T>
tc::sdk::observable< T >::observable ( const T &  t,
const std::function< void(T)> &  callback 
)
inlineexplicit

Constructor.

Template Parameters
Tinitial value for the observed value
CallbackTcallable object that is invoked every time the value is changed

Creates a tc::sdk::observable instance.

Definition at line 55 of file observable.hpp.

◆ observable() [2/2]

template<std::equality_comparable T>
tc::sdk::observable< T >::observable ( T &&  t,
const std::function< void(T)> &  callback 
)
inlineexplicit

Constructor.

Template Parameters
Tinitial value for the observed value
CallbackTcallable object that is invoked every time the value is changed

Creates a tc::sdk::observable instance.

Definition at line 71 of file observable.hpp.

◆ ~observable()

template<std::equality_comparable T>
tc::sdk::observable< T >::~observable ( )
default

Destructor.

Destructs this.

Member Function Documentation

◆ callback_enabled() [1/2]

template<std::equality_comparable T>
bool tc::sdk::observable< T >::callback_enabled ( ) const
inline

Check if the callback is currently enabled.

Returns
true if the callback is currently enabled

Definition at line 91 of file observable.hpp.

◆ callback_enabled() [2/2]

template<std::equality_comparable T>
void tc::sdk::observable< T >::callback_enabled ( bool  is_enabled)
inline

Enable or disable the callback temporarily.

Parameters
is_enabledenable or disable callback

Definition at line 101 of file observable.hpp.

◆ operator T const()

template<std::equality_comparable T>
tc::sdk::observable< T >::operator T const ( ) const
inline

Return the observed value.

Returns
const observed value

Definition at line 111 of file observable.hpp.

◆ operator T()

template<std::equality_comparable T>
tc::sdk::observable< T >::operator T ( )
inline

Return the observed value.

Returns
observed value

Definition at line 120 of file observable.hpp.

◆ value()

template<std::equality_comparable T>
T tc::sdk::observable< T >::value ( ) const
inline

Return the observed value.

Returns
the observed value

Definition at line 129 of file observable.hpp.

◆ operator=() [1/2]

template<std::equality_comparable T>
observable& tc::sdk::observable< T >::operator= ( T &&  t)
inline

Update the observed value.

Template Parameters
tthe updated value
Returns
a reference to self

If the parameter value is different from the observed one the callback is invoked.

Definition at line 142 of file observable.hpp.

◆ operator=() [2/2]

template<std::equality_comparable T>
observable& tc::sdk::observable< T >::operator= ( const T &  t)
inline

Update the observed value.

Template Parameters
tthe updated value
Returns
a reference to self

If the parameter value is different from the observed one the callback is invoked.

Definition at line 155 of file observable.hpp.


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