Handles routing of an event to multiple listeners. More...
#include "event_router.h"
Public Member Functions | |
event_router_t ()=default | |
Construct a new event_router_t object. More... | |
~event_router_t () | |
Destructor. More... | |
template<typename F > | |
event_handle | attach (F &&fun) |
Attach a callable to the event, adding a listener. More... | |
void | call (const T &event) const |
Call all attached listeners. More... | |
void | call (T &&event) const |
Call all attached listeners. More... | |
bool | detach (const event_handle &handle) |
Detach a listener from the event using a previously obtained ID. More... | |
bool | empty () const |
Returns true if the container of listeners is empty, i.e. More... | |
operator bool () const | |
Returns true if any listeners are attached. More... | |
auto | operator co_await () noexcept |
Obtain an awaitable object that refers to any event. More... | |
template<typename F > | |
event_handle | operator() (F &&fun) |
Attach a callable to the event, adding a listener. More... | |
template<typename Predicate > | |
auto | when (Predicate &&pred) |
Obtain an awaitable object that refers to an event with a certain condition. More... | |
Protected Member Functions | |
void | attach_awaiter (detail::event_router::awaitable< T > *awaiter) |
Attach a suspended coroutine to this event router via detail::event_router::awaitable. More... | |
void | detach_coro (void *handle) |
Detach an awaiting coroutine handle from this event router. More... | |
void | handle (const T &event) const |
Handle an event. More... | |
dpp::job | handle_coro (T event) const |
Handle an event as a coroutine, ensuring the lifetime of the event object. More... | |
void | resume_awaiters (const T &event) const |
Resume any awaiter whose predicate matches this event, or is null. More... | |
void | set_warning_callback (std::function< void(const T &)> warning_function) |
Next handle to be given out by the event router. More... | |
Friends | |
class | cluster |
class | detail::event_router::awaitable< T > |
Handles routing of an event to multiple listeners.
Multiple listeners may attach to the event_router_t by means of operator(). Passing a lambda into operator() attaches to the event.
Dispatchers of the event may call the call() method to cause all listeners to receive the event.
The empty() method will return true if there are no listeners attached to the event_router_t (this can be used to save time by not constructing objects that nobody will ever see).
The detach() method removes an existing listener from the event, using the event_handle ID returned by operator().
This class is used by the library to route all websocket events to listening code.
Example:
T | type of single parameter passed to event lambda derived from event_dispatch_t |
|
default |
Construct a new event_router_t object.
|
inline |
Destructor.
Will cancel any coroutine awaiting on events.
! | Cancelling a coroutine will throw a dpp::task_cancelled_exception to it. This will be caught in this destructor, however, make sure no other exceptions are thrown in the coroutine after that or it will terminate. |
Attach a callable to the event, adding a listener.
The callable should either be of the form void(const T&)
or dpp::task<void>(const T&)
(the latter requires DPP_CORO to be defined), where T is the event type for this event router.
fun | Callable to attach to event |
|
inlineprotected |
Attach a suspended coroutine to this event router via detail::event_router::awaitable.
It will be resumed and detached when an event satisfying its condition completes, or it is cancelled.
This is for internal usage only, the user way to do this is to co_await it (which will call this when suspending) This guarantees that the coroutine is indeed suspended and thus can be resumed at any time
awaiter | Awaiter to attach |
|
inline |
Call all attached listeners.
Listeners may cancel, by calling the event.cancel method.
event | Class to pass as parameter to all listeners. |
|
inline |
Call all attached listeners.
Listeners may cancel, by calling the event.cancel method.
event | Class to pass as parameter to all listeners. |
|
inline |
Detach a listener from the event using a previously obtained ID.
handle | An ID obtained from operator() |
true | The event was successfully detached |
false | The ID is invalid (possibly already detached, or does not exist) |
|
inlineprotected |
Detach an awaiting coroutine handle from this event router.
This is mostly called when a detail::event_router::awaitable is cancelled.
handle | Coroutine handle to find in the attached coroutines |
|
inline |
Returns true if the container of listeners is empty, i.e.
there is nothing listening for this event right now.
true | if there are no listeners |
false | if there are some listeners |
|
inlineprotected |
Handle an event.
This function should only be used without coro enabled, otherwise use handle_coro.
|
inlineprotected |
Handle an event as a coroutine, ensuring the lifetime of the event object.
|
inline |
Returns true if any listeners are attached.
This is the boolean opposite of event_router_t::empty().
true | if listeners are attached |
false | if no listeners are attached |
|
inlinenoexcept |
Obtain an awaitable object that refers to any event.
It can be co_await-ed to wait for the next event.
Example:
Example:
This can be combined with dpp::when_any and other awaitables, for example dpp::cluster::co_sleep to create expiring buttons.
event_handle dpp::event_router_t< T >::operator() | ( | F && | fun | ) |
Attach a callable to the event, adding a listener.
The callable should either be of the form void(const T&)
or dpp::task<void>(const T&)
(the latter requires DPP_CORO to be defined), where T is the event type for this event router.
This has the exact same behavior as using attach.
fun | Callable to attach to event |
|
inlineprotected |
Resume any awaiter whose predicate matches this event, or is null.
event | Event to compare and pass to accepting awaiters |
If state == none (was never awaited), do nothing If state == waiting, prevent resumption, resume on our end If state == resuming || cancelling, ignore
Technically only cancelling || waiting should be possible here We do this by trying to exchange "waiting" with "resuming". If that returns false, this is presumed to be "cancelling"
|
inlineprotected |
Next handle to be given out by the event router.
Set the warning callback object used to check that this event is capable of running properly
warning_function | A checking function to call |
|
inline |
Obtain an awaitable object that refers to an event with a certain condition.
It can be co_await-ed to wait for the next event that satisfies this condition. On resumption the awaiter will be given a reference to the event, saving it in a variable is recommended to avoid variable lifetime issues.
Example:
This can be combined with dpp::when_any and other awaitables, for example dpp::cluster::co_sleep to create expiring buttons.
pred | Predicate to check the event against. This should be a callable of the form bool(const T&) where T is the event type, returning true if the event is to match. |
|
friend |
|
friend |