HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
Public Types | Public Member Functions
v1::observable< T > Class Template Reference

#include <hikogui/observable.hpp>

Public Types

using value_type = T
 
using impl_type = detail::observable_impl<value_type>
 
using proxy_type = impl_type::proxy_type
 
using const_proxy_type = impl_type::const_proxy_type
 
using reference = value_type&
 
using const_reference = value_type const&
 
using notifier_type = notifier<void(value_type)>
 
using token_type = notifier_type::token_type
 
using awaiter_type = notifier_type::awaiter_type
 

Public Member Functions

 observable () noexcept
 Construct an observable.
 
 observable (observable const &other) noexcept
 Construct an observable and chain it to another.
 
observableoperator= (observable const &other) noexcept
 Chain with another observable.
 
token_type subscribe (callback_flags flags, std::invocable< value_type > auto &&callback) noexcept
 
token_type subscribe (std::invocable< value_type > auto &&callback) noexcept
 
awaiter_type operator co_await () const noexcept
 
const_proxy_type const_proxy () const noexcept
 proxy a constant reference to the shared value.
 
proxy_type proxy () const noexcept
 proxy a constant reference to the shared value.
 
proxy_type proxy () noexcept
 proxy a writable reference to the shared-value.
 
const_reference operator* () const noexcept
 Dereference to the value.
 
const_proxy_type operator-> () const noexcept
 Member select.
 
 observable (std::convertible_to< value_type > auto &&value) noexcept
 Construct an observable with its value set.
 
observableoperator= (std::convertible_to< value_type > auto &&value) noexcept
 Assign a new value.
 
auto operator++ () noexcept
 Pre increment.
 
auto operator-- () noexcept
 Pre decrement.
 
auto operator++ (int) noexcept
 Post increment.
 
auto operator-- (int) noexcept
 Post decrement.
 
auto operator+= (auto &&rhs) noexcept
 Inplace add.
 
auto operator-= (auto &&rhs) noexcept
 Inplace subtract.
 
auto operator*= (auto &&rhs) noexcept
 Inplace multiply.
 
auto operator/= (auto &&rhs) noexcept
 Inplace divide.
 
auto operator%= (auto &&rhs) noexcept
 Inplace remainder.
 
auto operator&= (auto &&rhs) noexcept
 Inplace bitwise and.
 
auto operator|= (auto &&rhs) noexcept
 Inplace bitwise or.
 
auto operator^= (auto &&rhs) noexcept
 Inplace bitwise xor.
 
auto operator<<= (auto &&rhs) noexcept
 Inplace shift left.
 
auto operator>>= (auto &&rhs) noexcept
 Inplace shift right.
 

Detailed Description

template<typename T>
class v1::observable< T >

An observable value.

Typically owners of an observable will subscribe a callback that will be called when the observed value has changed.

Multiple observables will be able to share the same value by assigning observables to each other, as shown in the example below. The callback subscribtion on each observable will not change with these assignments.

auto a = observable<int>{1};
auto b = observable<int>{5};
auto c = observable<int>{42};
auto d = observable<int>{9};
a = b; // both 'a' and 'b' share the value 5.
b = c; // 'a', 'b' and 'c' all share the value 42.
b = d; // 'a', 'b', 'c' and 'd' all share the value 9.
An observable value.
Definition observable.hpp:359

Access to the value is done through either a proxy or const-proxy. Only one proxy xor multiple const-proxies can be outstanding at a time. This is checked on debug builds.

The const-proxy is cheap and possibly completely optimized away for accessing the value read-only. The (non-const) proxy may be quite expensive as it makes a copy of the value to compare against to determine if change-notification is needed.

For this reason almost all operators of observable will work on const-proxies.

If you want to modify the value, or make multiple modifications you may explicitly use the proxy() function to get a proxy object and dereference it.

Template Parameters
TThe type of the value to be observed.

Constructor & Destructor Documentation

◆ observable() [1/3]

template<typename T >
v1::observable< T >::observable ( )
inlinenoexcept

Construct an observable.

The observer is created with a value that is default constructed.

◆ observable() [2/3]

template<typename T >
v1::observable< T >::observable ( observable< T > const & other)
inlinenoexcept

Construct an observable and chain it to another.

The new observable will share a value with the other observable.

Parameters
otherThe other observable to share the value with.

◆ observable() [3/3]

template<typename T >
v1::observable< T >::observable ( std::convertible_to< value_type > auto && value)
inlinenoexcept

Construct an observable with its value set.

Parameters
valueThe value to assign to the shared-value.

Member Function Documentation

◆ const_proxy()

template<typename T >
const_proxy_type v1::observable< T >::const_proxy ( ) const
inlinenoexcept

proxy a constant reference to the shared value.

In reality the reference is a proxy object which makes available operations to be used with the shared value. This proxy object will make sure any state is done atomically.

Returns
A reference to the shared value.

◆ operator%=()

template<typename T >
auto v1::observable< T >::operator%= ( auto && rhs)
inlinenoexcept

Inplace remainder.

Short for *lhs.proxy() %= rhs

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator%=(rhs).

◆ operator&=()

template<typename T >
auto v1::observable< T >::operator&= ( auto && rhs)
inlinenoexcept

Inplace bitwise and.

Short for *lhs.proxy() &= rhs

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator&=(rhs).

◆ operator*()

template<typename T >
const_reference v1::observable< T >::operator* ( ) const
inlinenoexcept

Dereference to the value.

Returns
A const reference to the value.

◆ operator*=()

template<typename T >
auto v1::observable< T >::operator*= ( auto && rhs)
inlinenoexcept

Inplace multiply.

Short for *lhs.proxy() *= rhs

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator*=(rhs).

◆ operator++() [1/2]

template<typename T >
auto v1::observable< T >::operator++ ( )
inlinenoexcept

Pre increment.

Short for ++*lhs.proxy()

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator++().

◆ operator++() [2/2]

template<typename T >
auto v1::observable< T >::operator++ ( int )
inlinenoexcept

Post increment.

Short for (*lhs.proxy())++

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator++(int).

◆ operator+=()

template<typename T >
auto v1::observable< T >::operator+= ( auto && rhs)
inlinenoexcept

Inplace add.

Short for *lhs.proxy() += rhs

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator+=(rhs).

◆ operator--() [1/2]

template<typename T >
auto v1::observable< T >::operator-- ( )
inlinenoexcept

Pre decrement.

Short for --*lhs.proxy()

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator--().

◆ operator--() [2/2]

template<typename T >
auto v1::observable< T >::operator-- ( int )
inlinenoexcept

Post decrement.

Short for (*lhs.proxy())--

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator--(int).

◆ operator-=()

template<typename T >
auto v1::observable< T >::operator-= ( auto && rhs)
inlinenoexcept

Inplace subtract.

Short for *lhs.proxy() -= rhs

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator-=(rhs).

◆ operator->()

template<typename T >
const_proxy_type v1::observable< T >::operator-> ( ) const
inlinenoexcept

Member select.

Member selection is done recursive through a read-only proxy object.

Returns
A read-only proxy object used to recursively member select.

◆ operator/=()

template<typename T >
auto v1::observable< T >::operator/= ( auto && rhs)
inlinenoexcept

Inplace divide.

Short for *lhs.proxy() /= rhs

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator/=(rhs).

◆ operator<<=()

template<typename T >
auto v1::observable< T >::operator<<= ( auto && rhs)
inlinenoexcept

Inplace shift left.

Short for *lhs.proxy() <<= rhs

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator<<=(rhs).

◆ operator=() [1/2]

template<typename T >
observable & v1::observable< T >::operator= ( observable< T > const & other)
inlinenoexcept

Chain with another observable.

Replace the current shared value, with the value of the other observer. This means the other observers that share the current value will also be reseated with the new value.

Postcondition
observers sharing the same value will be modified to share the new value.
subscribers are notified
Parameters
otherThe other observable to share the value with.

◆ operator=() [2/2]

template<typename T >
observable & v1::observable< T >::operator= ( std::convertible_to< value_type > auto && value)
inlinenoexcept

Assign a new value.

Postcondition
subscribers are notified.
Parameters
valueThe value to assign to the shared-value.

◆ operator>>=()

template<typename T >
auto v1::observable< T >::operator>>= ( auto && rhs)
inlinenoexcept

Inplace shift right.

Short for *lhs.proxy() >>= rhs

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator>>=(rhs).

◆ operator^=()

template<typename T >
auto v1::observable< T >::operator^= ( auto && rhs)
inlinenoexcept

Inplace bitwise xor.

Short for *lhs.proxy() ^= rhs

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator^=(rhs).

◆ operator|=()

template<typename T >
auto v1::observable< T >::operator|= ( auto && rhs)
inlinenoexcept

Inplace bitwise or.

Short for *lhs.proxy() |= rhs

Parameters
rhsThe right hand side value.
Returns
The return value of the value_type::operator|=(rhs).

◆ proxy() [1/2]

template<typename T >
proxy_type v1::observable< T >::proxy ( ) const
inlinenoexcept

proxy a constant reference to the shared value.

In reality the reference is a proxy object which makes available operations to be used with the shared value. This proxy object will make sure any state is done atomically.

Returns
A reference to the shared value.

◆ proxy() [2/2]

template<typename T >
proxy_type v1::observable< T >::proxy ( )
inlinenoexcept

proxy a writable reference to the shared-value.

In reality the reference is a proxy object which makes available operations to be used with the shared-value. This proxy object will make sure any state is done atomically and that subscribers are notified when the proxy's lifetime has ended.

Postcondition
subscribers are notified after reference's lifetime has ended.
Returns
A reference to the shared value.

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