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

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...
 
void call (const 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...
 
event_handle operator() (std::function< void(const T &)> func)
 Attach a lambda to the event, adding a listener. The lambda should follow the signature specified when declaring the event object and should take exactly one parameter derived from event_dispatch_t. More...
 
event_handle attach (std::function< void(const T &)> func)
 Attach a lambda to the event, adding a listener. The lambda should follow the signature specified when declaring the event object and should take exactly one parameter derived from event_dispatch_t. More...
 
event_handle co_attach (std::function< dpp::task(T)> func)
 
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...
 

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 event_router_t::call() method to cause all listeners to receive the event.

The event_router_t::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 event_router_t::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:44
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.

Member Function Documentation

◆ attach()

template<class T >
event_handle dpp::event_router_t< T >::attach ( std::function< void(const T &)>  func)
inline

Attach a lambda to the event, adding a listener. The lambda should follow the signature specified when declaring the event object and should take exactly one parameter derived from event_dispatch_t.

Parameters
funcFunction lambda 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()

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.

◆ co_attach()

template<class T >
event_handle dpp::event_router_t< T >::co_attach ( std::function< dpp::task(T)>  func)
inline

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

Parameters
handleAn ID obtained from event_router_t::operator()
Returns
true The event was successfully detached
false The 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.

Returns
true if there are no listeners
false if there are some listeners

◆ 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().

Returns
true if listeners are attached
false if no listeners are attached

◆ operator()()

template<class T >
event_handle dpp::event_router_t< T >::operator() ( std::function< void(const T &)>  func)
inline

Attach a lambda to the event, adding a listener. The lambda should follow the signature specified when declaring the event object and should take exactly one parameter derived from event_dispatch_t.

Parameters
funcFunction lambda 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.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