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
-
Args | Observer 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.
template<typename ... Args>
template<typename Callable >
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] | observer | An observer callable that will be subscribed to notifications from this subject. |
- Template Parameters
-
Callable | Type 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.
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] | observer | An observer callable that will be subscribed to notifications from this subject and immediately invoked with the provided arguments. |
[in] | arguments | Arguments to pass to the observer when called. |
- Template Parameters
-
Callable | Type 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()
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] | arguments | Arguments 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.