Observable Reference
Generic observable objects for C++
Public Types | Public Member Functions | Public Attributes | Friends | List of all members
observable::value< ValueType > Class Template Reference

Description

template<typename ValueType>
class observable::value< ValueType >

Get notified when a value-type changes.

When setting a new value, if the new value is different than the existing one, any subscribed observers will be notified.

Equality will be checked using std::equal_to<ValueType> if the ValueType is EqualityComparable, else all values will be assumed to be unequal.

Warning
None of the methods in this class can be safely called concurrently.
Template Parameters
ValueTypeThe value-type that will be stored inside the observable. This type will need to be at least movable.

Public Types

using value_type = ValueType
 The observable value's stored value type. More...
 

Public Member Functions

 value ()=default
 Create a default-constructed observable value. More...
 
 value (ValueType initial_value) noexcept(std::is_nothrow_move_constructible< ValueType >::value)
 Create an initialized observable value. More...
 
template<typename EqualityComparator >
 value (ValueType initial_value, EqualityComparator equal) noexcept(std::is_nothrow_move_constructible< ValueType >::value &&std::is_nothrow_move_constructible< EqualityComparator >::value)
 Create an initialized observable value. More...
 
template<typename UpdaterType >
 value (std::unique_ptr< UpdaterType > &&ud)
 Create an initialized value that will be updated by the provided value_updater. More...
 
 operator ValueType const & () const noexcept
 Convert the observable value to its stored value type. More...
 
auto get () const noexcept -> ValueType const &
 Retrieve the stored value. More...
 
template<typename Callable >
auto subscribe (Callable &&observer) const
 Subscribe to changes to the observable value. More...
 
template<typename Callable >
auto subscribe_and_call (Callable &&observer) const
 Subscribe to changes to the observable value and also call the observer callback immediately with the current value. More...
 
void set (ValueType new_value)
 Set a new value, possibly notifying any subscribed observers. More...
 
auto operator= (ValueType new_value) -> value &
 Set a new value. More...
 
 ~value ()
 Destructor. More...
 
 value (value< ValueType > const &)=delete
 Observable values are not copy-constructible. More...
 
auto operator= (value< ValueType > const &) -> value< ValueType > &=delete
 Observable values are not copy-assignable. More...
 
template<typename = std::enable_if_t<std::is_move_constructible<ValueType>::value>>
 value (value< ValueType > &&other) noexcept(std::is_nothrow_move_constructible< ValueType >::value)
 Observable values are move-constructible. More...
 
template<typename = std::enable_if_t<std::is_move_assignable<ValueType>::value>>
auto operator= (value< ValueType > &&other) noexcept(std::is_nothrow_move_assignable< ValueType >::value) -> value< ValueType > &
 Observable values are move-assignable. More...
 

Public Attributes

subject< void(value< ValueType > &), value< ValueType > > moved
 Subject notified after the value has been moved. More...
 
subject< void(), value< ValueType > > destroyed
 Subject notified before the value is destroyed. More...
 

Friends

template<typename , typename ... >
class value
 

Member Typedef Documentation

◆ value_type

template<typename ValueType >
using observable::value< ValueType >::value_type = ValueType

The observable value's stored value type.

Constructor & Destructor Documentation

◆ value() [1/6]

template<typename ValueType >
observable::value< ValueType >::value ( )
default

Create a default-constructed observable value.

Depending on the value type, the stored value will be either uninitialized or it will be default constructed.

◆ value() [2/6]

template<typename ValueType >
observable::value< ValueType >::value ( ValueType  initial_value)
inlineexplicitnoexcept

Create an initialized observable value.

Parameters
initial_valueThe observable's initial value.

◆ value() [3/6]

template<typename ValueType >
template<typename EqualityComparator >
observable::value< ValueType >::value ( ValueType  initial_value,
EqualityComparator  equal 
)
inlinenoexcept

Create an initialized observable value.

Parameters
initial_valueThe observable's initial value.
equalA functor to be used for comparing values. The functor must have a signature compatible with the one below:

bool(ValueType const &, ValueType const &)

The comparator must return true if both of its parameters are equal.

◆ value() [4/6]

template<typename ValueType >
template<typename UpdaterType >
observable::value< ValueType >::value ( std::unique_ptr< UpdaterType > &&  ud)
inlineexplicit

Create an initialized value that will be updated by the provided value_updater.

Parameters
udA value_updater that will be stored by the value.

◆ ~value()

template<typename ValueType >
observable::value< ValueType >::~value ( )
inline

Destructor.

◆ value() [5/6]

template<typename ValueType >
observable::value< ValueType >::value ( value< ValueType > const &  )
delete

Observable values are not copy-constructible.

◆ value() [6/6]

template<typename ValueType >
template<typename = std::enable_if_t<std::is_move_constructible<ValueType>::value>>
observable::value< ValueType >::value ( value< ValueType > &&  other)
inlinenoexcept

Observable values are move-constructible.

Member Function Documentation

◆ operator ValueType const &()

template<typename ValueType >
observable::value< ValueType >::operator ValueType const & ( ) const
inlineexplicitnoexcept

Convert the observable value to its stored value type.

◆ get()

template<typename ValueType >
auto observable::value< ValueType >::get ( ) const -> ValueType const &
inlinenoexcept

Retrieve the stored value.

◆ subscribe()

template<typename ValueType >
template<typename Callable >
auto observable::value< ValueType >::subscribe ( Callable &&  observer) const
inline

Subscribe to changes to the observable value.

These subscriptions will be triggered whenever the stored value changes.

Parameters
[in]observerA callable that will be called whenever the value changes. The observer must satisfy the Callable concept.
Template Parameters
CallableA callable taking no parameters or a callable taking one parameter that will be called with the new value:
  • void() – will be called when the value changes but will not receive the new value.
  • void(T const &) or void(T) – will be called with the new value. The expression T { value.get() } must be correct.
See also
subject<void(Args ...)>::subscribe()

◆ subscribe_and_call()

template<typename ValueType >
template<typename Callable >
auto observable::value< ValueType >::subscribe_and_call ( Callable &&  observer) const
inline

Subscribe to changes to the observable value and also call the observer callback immediately with the current value.

If the observer throws an exception during the initial call, it will not be subscribed.

Note
The observer is not subscribed during the initial call.
Parameters
[in]observerA callable that will be called whenever the value changes. The observer must satisfy the Callable concept.
Template Parameters
CallableA callable taking no parameters or a callable taking one parameter that will be called with the new value:
  • void() – will be called when the value changes but will not receive the new value.
  • void(T const &) or void(T) – will be called with the new value. The expression T { value.get() } must be correct.
See also
subscribe()

◆ set()

template<typename ValueType >
void observable::value< ValueType >::set ( ValueType  new_value)
inline

Set a new value, possibly notifying any subscribed observers.

If the new value compares equal to the existing value, this method has no effect. The comparison is performed using the EqualityComparator.

Parameters
new_valueThe new value to set.
Exceptions
readonly_valueif the value has an associated updater.
See also
subject<void(Args ...)>::notify()

◆ operator=() [1/3]

template<typename ValueType >
auto observable::value< ValueType >::operator= ( ValueType  new_value) -> value &
inline

Set a new value.

Will just call set(ValueType &&).

See also
set(ValueType &&)

◆ operator=() [2/3]

template<typename ValueType >
auto observable::value< ValueType >::operator= ( value< ValueType > const &  ) -> value< ValueType > &=delete
delete

Observable values are not copy-assignable.

◆ operator=() [3/3]

template<typename ValueType >
template<typename = std::enable_if_t<std::is_move_assignable<ValueType>::value>>
auto observable::value< ValueType >::operator= ( value< ValueType > &&  other) -> value<ValueType> &
inlinenoexcept

Observable values are move-assignable.

Friends And Related Function Documentation

◆ value

template<typename ValueType >
template<typename , typename ... >
friend class value
friend

Member Data Documentation

◆ moved

template<typename ValueType >
subject<void(value<ValueType> &), value<ValueType> > observable::value< ValueType >::moved

Subject notified after the value has been moved.

The subject's parameter is a reference to the value instance that has been moved into.

Warning
The subject of the moved-into value will be notified, not the moved-from value's subject.

◆ destroyed

template<typename ValueType >
subject<void(), value<ValueType> > observable::value< ValueType >::destroyed

Subject notified before the value is destroyed.