Observable Reference
Generic observable objects for C++
Public Types | Public Member Functions | List of all members
observable::subject< void(Args ...)> Class Template Reference

Description

template<typename ... Args>
class observable::subject< void(Args ...)>

Store observers and provide a way to notify them when events occur.

Observers are objects that satisfy the Callable concept and can be stored inside a std::function<void(Args ...)>.

Once you call subscribe(), the observer is said to be subscribed to notifications from the subject.

Calling notify(), will call all the currently subscribed observers with the arguments provided to notify().

All methods can be safely called in parallel, from multiple threads.

Template Parameters
ArgsObserver arguments. All observer types must be storable inside a std::function<void(Args ...)>.
Warning
Even though subjects themselves are safe to use in parallel, observers need to handle being called from multiple threads too.

Public Types

using observer_type = void(Args ...)
 

Public Member Functions

template<typename Callable >
auto subscribe (Callable &&observer) -> infinite_subscription
 Subscribe an observer to notifications. More...
 
template<typename Callable , typename ... ActualArgs>
auto subscribe_and_call (Callable &&observer, ActualArgs ... arguments) -> infinite_subscription
 Subscribe an observer to notifications and immediately call it with the provided arguments. More...
 
void notify (Args ... arguments) const
 Notify all currently subscribed observers. More...
 
auto empty () const noexcept
 Return true if there are no subscribers. More...
 
 subject ()=default
 Constructor. Will create an empty subject. More...
 
 subject (subject const &)=delete
 Subjects are not copy-constructible. More...
 
auto operator= (subject const &) -> subject &=delete
 Subjects are not copy-assignable. More...
 
 subject (subject &&) noexcept=default
 Subjects are move-constructible. More...
 
auto operator= (subject &&) noexcept -> subject &=default
 Subjects are move-assignable. More...
 

Member Typedef Documentation

◆ observer_type

template<typename ... Args>
using observable::subject< void(Args ...)>::observer_type = void(Args ...)

Constructor & Destructor Documentation

◆ subject() [1/3]

template<typename ... Args>
observable::subject< void(Args ...)>::subject ( )
default

Constructor. Will create an empty subject.

◆ subject() [2/3]

template<typename ... Args>
observable::subject< void(Args ...)>::subject ( subject< void(Args ...)> const &  )
delete

Subjects are not copy-constructible.

◆ subject() [3/3]

template<typename ... Args>
observable::subject< void(Args ...)>::subject ( subject< void(Args ...)> &&  )
defaultnoexcept

Subjects are move-constructible.

Member Function Documentation

◆ subscribe()

template<typename ... Args>
template<typename Callable >
auto observable::subject< void(Args ...)>::subscribe ( Callable &&  observer) -> infinite_subscription
inline

Subscribe an observer to notifications.

You can safely call this method in parallel, from multiple threads.

This method is reentrant, you can add and remove observers from inside other, running, observers.

Parameters
[in]observerAn observer callable that will be subscribed to notifications from this subject.
Template Parameters
CallableType of the observer object. This type must satisfy the Callable concept and must be storable inside a std::function<void(Args ...)>.
Returns
An unique subscription that can be used to unsubscribe the provided observer from receiving notifications from this subject.
Warning
Observers must be valid to be called for as long as they are subscribed and there is a possibility to be called.
Observers must be safe to be called in parallel, if the notify() method will be called from multiple threads.

◆ subscribe_and_call()

template<typename ... Args>
template<typename Callable , typename ... ActualArgs>
auto observable::subject< void(Args ...)>::subscribe_and_call ( Callable &&  observer,
ActualArgs ...  arguments 
) -> infinite_subscription
inline

Subscribe an observer to notifications and immediately call it with the provided arguments.

This method works exactly like the regular subscribe except it also invokes the observer.

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

Note
The observer will not be subscribed during the initial call.
Parameters
[in]observerAn observer callable that will be subscribed to notifications from this subject and immediately invoked with the provided arguments.
[in]argumentsArguments to pass to the observer when called.
Template Parameters
CallableType of the observer callable. This type must satisfy the Callable concept and must be storable inside a std::function<void(Args...)>.
Returns
An unique subscription that can be used to unsubscribe the provided observer from receiving notifications from this subject.
Warning
Observers must be valid to be called for as long as they are subscribed and there is a possibility to be called.
Observers must be safe to be called in parallel, if the notify() method will be called from multiple threads.
See also
subscribe()

◆ notify()

template<typename ... Args>
void observable::subject< void(Args ...)>::notify ( Args ...  arguments) const
inline

Notify all currently subscribed observers.

This method will block until all subscribed observers are called. The method will call observers one-by-one in an unspecified order.

You can safely call this method in parallel, from multiple threads.

Note
Observers subscribed during a notify call, will not be called as part of the notify call during which they were added.
Observers removed during the notify call, before they themselves have been called, will not be called.

The method is reentrant; you can call notify() from inside a running observer.

Parameters
[in]argumentsArguments that will be forwarded to the subscribed observers.
Warning
All observers that will be called by notify() must remain valid to be called for the duration of the notify() call.
If notify() is called from multiple threads, all observers must be safe to call from multiple threads.

◆ empty()

template<typename ... Args>
auto observable::subject< void(Args ...)>::empty ( ) const
inlinenoexcept

Return true if there are no subscribers.

◆ operator=() [1/2]

template<typename ... Args>
auto observable::subject< void(Args ...)>::operator= ( subject< void(Args ...)> const &  ) -> subject &=delete
delete

Subjects are not copy-assignable.

◆ operator=() [2/2]

template<typename ... Args>
auto observable::subject< void(Args ...)>::operator= ( subject< void(Args ...)> &&  ) -> subject &=default
defaultnoexcept

Subjects are move-assignable.