D++ (DPP)
C++ Discord API Bot Library
dpp::event_router_t< T > Class Template Reference

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. More...

#include <event_router.h>

Public Member Functions

 event_router_t ()=default
 Construct a new event_router_t object. More...
 
 ~event_router_t ()
 Destructor. Will cancel any coroutine awaiting on events. More...
 
void call (const T &event) const
 Call all attached listeners. Listeners may cancel, by calling the event.cancel method. More...
 
void call (T &&event) const
 Call all attached listeners. Listeners may cancel, by calling the event.cancel method. More...
 
bool empty () const
 Returns true if the container of listeners is empty, i.e. there is nothing listening for this event right now. More...
 
 operator bool () const
 Returns true if any listeners are attached. More...
 
template<typename F >
event_handle 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. More...
 
template<typename F >
event_handle attach (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. More...
 
bool detach (const event_handle &handle)
 Detach a listener from the event using a previously obtained ID. More...
 

Protected Member Functions

void set_warning_callback (std::function< void(const T &)> warning_function)
 Next handle to be given out by the event router. More...
 
void handle (const T &event) const
 Handle an event. This function should only be used without coro enabled, otherwise use handle_coro. More...
 

Friends

class cluster
 

Detailed Description

template<class T>
class dpp::event_router_t< 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:

// Declare an event that takes log_t as its parameter
event_router_t<log_t> my_event;
// Attach a listener to the event
event_handle id = my_event([&](const log_t& cc) {
std::cout << cc.message << "\n";
});
// Construct a log_t and call the event (listeners will receive the log_t object)
log_t lt;
lt.message = "foo";
my_event.call(lt);
// Detach from an event using the handle returned by operator()
my_event.detach(id);
size_t event_handle
A returned event handle for an event which was attached.
Definition: event_router.h:159
Template Parameters
Ttype of single parameter passed to event lambda derived from event_dispatch_t

Constructor & Destructor Documentation

◆ event_router_t()

template<class T >
dpp::event_router_t< T >::event_router_t ( )
default

Construct a new event_router_t object.

◆ ~event_router_t()

template<class T >
dpp::event_router_t< T >::~event_router_t ( )
inline

Destructor. Will cancel any coroutine awaiting on events.

Exceptions
!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.

Member Function Documentation

◆ attach()

template<class T >
template<typename F >
event_handle dpp::event_router_t< T >::attach ( 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.

Parameters
funCallable to attach to event
Returns
event_handle An event handle unique to this event, used to detach the listener from the event later if necessary.

◆ call() [1/2]

template<class T >
void dpp::event_router_t< T >::call ( const T &  event) const
inline

Call all attached listeners. Listeners may cancel, by calling the event.cancel method.

Parameters
eventClass to pass as parameter to all listeners.

◆ call() [2/2]

template<class T >
void dpp::event_router_t< T >::call ( T &&  event) const
inline

Call all attached listeners. Listeners may cancel, by calling the event.cancel method.

Parameters
eventClass to pass as parameter to all listeners.

◆ detach()

template<class T >
bool dpp::event_router_t< T >::detach ( const event_handle handle)
inline

Detach a listener from the event using a previously obtained ID.

Warning
You cannot call this within an event handler.
Parameters
handleAn ID obtained from operator()
Return values
trueThe event was successfully detached
falseThe ID is invalid (possibly already detached, or does not exist)

◆ empty()

template<class T >
bool dpp::event_router_t< T >::empty ( ) const
inline

Returns true if the container of listeners is empty, i.e. there is nothing listening for this event right now.

Return values
trueif there are no listeners
falseif there are some listeners

◆ handle()

template<class T >
void dpp::event_router_t< T >::handle ( const T &  event) const
inlineprotected

Handle an event. This function should only be used without coro enabled, otherwise use handle_coro.

◆ operator bool()

template<class T >
dpp::event_router_t< T >::operator bool ( ) const
inline

Returns true if any listeners are attached.

This is the boolean opposite of event_router_t::empty().

Return values
trueif listeners are attached
falseif no listeners are attached

◆ operator()()

template<class T >
template<typename F >
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.

See also
attach
Parameters
funCallable to attach to event
Returns
event_handle An event handle unique to this event, used to detach the listener from the event later if necessary.

◆ set_warning_callback()

template<class T >
void dpp::event_router_t< T >::set_warning_callback ( std::function< void(const T &)>  warning_function)
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

Parameters
warning_functionA checking function to call

Friends And Related Function Documentation

◆ cluster

template<class T >
friend class cluster
friend
D++ Library version 10.1.0D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0