dpp::event_router_t
event_router.h
class T
std::variant< regular_handler_t, std::function< task_dummy(T)> > using dpp::event_router_t< T >::event_handler_abi_t = std::variant<regular_handler_t, std::function<task_dummy(T)>> event_handler_abi_t dpp::event_router_t::event_handler_abi_t

Type that event handlers will be stored as with DPP_CORO off. This is the ABI DPP_CORO has to match.

std::variant< regular_handler_t, task_handler_t > using dpp::event_router_t< T >::event_handler_t = std::variant<regular_handler_t, task_handler_t> event_handler_t dpp::event_router_t::event_handler_t

Type that event handlers are stored as.

std::function< void(const T &)> using dpp::event_router_t< T >::regular_handler_t = std::function<void(const T&)> regular_handler_t dpp::event_router_t::regular_handler_t

Non-coro event handler type.

std::function< dpp::task< void >(const T &)> using dpp::event_router_t< T >::task_handler_t = std::function<dpp::task<void>(const T&)> task_handler_t dpp::event_router_t::task_handler_t

dpp::task coro event handler

class friend class cluster cluster dpp::event_router_t::cluster cluster
class friend class detail::event_router::awaitable< T > detail::event_router::awaitable< T > dpp::event_router_t::awaitable< T > detail::event_router::awaitable< T >
std::vector< detail::event_router::awaitable< T > * > std::vector<detail::event_router::awaitable<T> *> dpp::event_router_t< T >::coro_awaiters coro_awaiters dpp::event_router_t::coro_awaiters

Vector containing the awaitables currently being awaited on for this event router.

std::shared_mutex std::shared_mutex dpp::event_router_t< T >::coro_mutex coro_mutex dpp::event_router_t::coro_mutex

Mutex for messing with coro_awaiters.

std::map< event_handle, event_handler_t > std::map<event_handle, event_handler_t> dpp::event_router_t< T >::dispatch_container dispatch_container dpp::event_router_t::dispatch_container

Container of event listeners keyed by handle, as handles are handed out sequentially they will always be called in they order they are bound to the event as std::map is an ordered container.

std::shared_mutex std::shared_mutex dpp::event_router_t< T >::mutex mutex dpp::event_router_t::mutex

Thread safety mutex.

std::function< void(const T &)> std::function<void(const T&)> dpp::event_router_t< T >::warning warning dpp::event_router_t::warning

A function to be called whenever the method is called, to check some condition that is required for this event to trigger correctly.

dpp::event_router_t< T >::DPP_CHECK_ABI_COMPAT
(event_handler_t, event_handler_abi_t) event_handle next_handle
DPP_CHECK_ABI_COMPAT
dpp::event_router_t::DPP_CHECK_ABI_COMPAT
event_handler_t
event_handler_abi_t

Identifier for the next event handler, will be given to the user on attaching a handler.

void void dpp::event_router_t< T >::attach_awaiter (detail::event_router::awaitable< T > *awaiter) attach_awaiter dpp::event_router_t::attach_awaiter detail::event_router::awaitable< T > * awaiter

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

void void dpp::event_router_t< T >::detach_coro (void *handle) detach_coro dpp::event_router_t::detach_coro void * handle

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

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

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

dpp::job dpp::job dpp::event_router_t< T >::handle_coro (T event) const handle_coro dpp::event_router_t::handle_coro T event

Handle an event as a coroutine, ensuring the lifetime of the event object.

void void dpp::event_router_t< T >::resume_awaiters (const T &event) const resume_awaiters dpp::event_router_t::resume_awaiters const T & event

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"

void void dpp::event_router_t< T >::set_warning_callback (std::function< void(const T &)> warning_function) set_warning_callback dpp::event_router_t::set_warning_callback std::function< void(const T &)> warning_function

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

dpp::event_router_t< T >::event_router_t
()=default
event_router_t
dpp::event_router_t::event_router_t

Construct a new event_router_t object.

dpp::event_router_t< T >::~event_router_t
()
~event_router_t
dpp::event_router_t::~event_router_t

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.

typename F
event_handle event_handle dpp::event_router_t< T >::attach (F &&fun) attach dpp::event_router_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&)`, where T is the event type for this event router.

fun

Callable to attach to event

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

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

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

event

Class to pass as parameter to all listeners.

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

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

event

Class to pass as parameter to all listeners.

bool bool dpp::event_router_t< T >::detach (const event_handle &handle) detach dpp::event_router_t::detach const event_handle & handle

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

You cannot call this within an event handler.

handle

An ID obtained from operator()

true

The event was successfully detached

false

The ID is invalid (possibly already detached, or does not exist)

bool bool dpp::event_router_t< T >::empty () const empty dpp::event_router_t::empty

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

dpp::event_router_t< T >::operator bool
() const
operator bool
dpp::event_router_t::operator bool

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

auto auto dpp::event_router_t< T >::operator co_await () noexcept operator co_await dpp::event_router_t::operator co_await

Obtain an awaitable object that refers to any event. It can be co_await-ed to wait for the next event.

Example:

    dpp::task<>my_handler(constdpp::slashcommand_t&event){
    co_awaitevent.co_reply(dpp::message().add_component(dpp::component().add_component().set_label("clickme!").set_id("test")));

    dpp::button_click_tb=co_awaitc->on_message_create;

    //dosomethingonbuttonclick
    }

This can be combined with dpp::when_any and other awaitables, for example dpp::cluster::co_sleep to create expiring buttons.

On resumption the awaiter will be given a reference to the event. This means that variable may become dangling at the next co_await, be careful and save it in a variable if you need to.

awaitable An awaitable object that can be co_await-ed to await an event matching the condition.

typename F
event_handle event_handle dpp::event_router_t< T >::operator() (F &&fun) operator() dpp::event_router_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&)`, where T is the event type for this event router.

This has the exact same behavior as using attach.

attach

fun

Callable to attach to event

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

typename Predicate
auto auto dpp::event_router_t< T >::when (Predicate &&pred) when dpp::event_router_t::when Predicate && pred

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:

    dpp::task<>my_handler(constdpp::slashcommand_t&event){
    co_awaitevent.co_reply(dpp::message().add_component(dpp::component().add_component().set_label("clickme!").set_id("test")));

    dpp::button_click_tb=co_awaitc->on_button_click.with([](constdpp::button_click_t&event){returnevent.custom_id=="test";});

    //dosomethingonbuttonclick
    }

This can be combined with dpp::when_any and other awaitables, for example dpp::cluster::co_sleep to create expiring buttons.

On resumption the awaiter will be given a reference to the event. This means that variable may become dangling at the next co_await, be careful and save it in a variable if you need to.

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.

awaitable An awaitable object that can be co_await-ed to await an event matching the condition.

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:

    //Declareaneventthattakeslog_tasitsparameter
    event_router_t<log_t>my_event;

    //Attachalistenertotheevent
    event_handleid=my_event([&](constlog_t&cc){
    std::cout<<cc.message<<"\n";
    });

    //Constructalog_tandcalltheevent(listenerswillreceivethelog_tobject)
    log_tlt;
    lt.message="foo";
    my_event.call(lt);

    //Detachfromaneventusingthehandlereturnedbyoperator()
    my_event.detach(id);

T

type of single parameter passed to event lambda derived from event_dispatch_t

dpp::event_router_t< T >
dpp::event_router_t< dpp::autocomplete_t >
< dpp::autocomplete_t >
dpp::event_router_t< dpp::automod_rule_create_t >
< dpp::automod_rule_create_t >
dpp::event_router_t< dpp::automod_rule_delete_t >
< dpp::automod_rule_delete_t >
dpp::event_router_t< dpp::automod_rule_execute_t >
< dpp::automod_rule_execute_t >
dpp::event_router_t< dpp::automod_rule_update_t >
< dpp::automod_rule_update_t >
dpp::event_router_t< dpp::button_click_t >
< dpp::button_click_t >
dpp::event_router_t< dpp::channel_create_t >
< dpp::channel_create_t >
dpp::event_router_t< dpp::channel_delete_t >
< dpp::channel_delete_t >
dpp::event_router_t< dpp::channel_pins_update_t >
< dpp::channel_pins_update_t >
dpp::event_router_t< dpp::channel_update_t >
< dpp::channel_update_t >
dpp::event_router_t< dpp::entitlement_create_t >
< dpp::entitlement_create_t >
dpp::event_router_t< dpp::entitlement_delete_t >
< dpp::entitlement_delete_t >
dpp::event_router_t< dpp::entitlement_update_t >
< dpp::entitlement_update_t >
dpp::event_router_t< dpp::form_submit_t >
< dpp::form_submit_t >
dpp::event_router_t< dpp::guild_audit_log_entry_create_t >
< dpp::guild_audit_log_entry_create_t >
dpp::event_router_t< dpp::guild_ban_add_t >
< dpp::guild_ban_add_t >
dpp::event_router_t< dpp::guild_ban_remove_t >
< dpp::guild_ban_remove_t >
dpp::event_router_t< dpp::guild_create_t >
< dpp::guild_create_t >
dpp::event_router_t< dpp::guild_delete_t >
< dpp::guild_delete_t >
dpp::event_router_t< dpp::guild_emojis_update_t >
< dpp::guild_emojis_update_t >
dpp::event_router_t< dpp::guild_integrations_update_t >
< dpp::guild_integrations_update_t >
dpp::event_router_t< dpp::guild_join_request_delete_t >
< dpp::guild_join_request_delete_t >
dpp::event_router_t< dpp::guild_member_add_t >
< dpp::guild_member_add_t >
dpp::event_router_t< dpp::guild_member_remove_t >
< dpp::guild_member_remove_t >
dpp::event_router_t< dpp::guild_member_update_t >
< dpp::guild_member_update_t >
dpp::event_router_t< dpp::guild_members_chunk_t >
< dpp::guild_members_chunk_t >
dpp::event_router_t< dpp::guild_role_create_t >
< dpp::guild_role_create_t >
dpp::event_router_t< dpp::guild_role_delete_t >
< dpp::guild_role_delete_t >
dpp::event_router_t< dpp::guild_role_update_t >
< dpp::guild_role_update_t >
dpp::event_router_t< dpp::guild_scheduled_event_create_t >
< dpp::guild_scheduled_event_create_t >
dpp::event_router_t< dpp::guild_scheduled_event_delete_t >
< dpp::guild_scheduled_event_delete_t >
dpp::event_router_t< dpp::guild_scheduled_event_update_t >
< dpp::guild_scheduled_event_update_t >
dpp::event_router_t< dpp::guild_scheduled_event_user_add_t >
< dpp::guild_scheduled_event_user_add_t >
dpp::event_router_t< dpp::guild_scheduled_event_user_remove_t >
< dpp::guild_scheduled_event_user_remove_t >
dpp::event_router_t< dpp::guild_stickers_update_t >
< dpp::guild_stickers_update_t >
dpp::event_router_t< dpp::guild_update_t >
< dpp::guild_update_t >
dpp::event_router_t< dpp::integration_create_t >
< dpp::integration_create_t >
dpp::event_router_t< dpp::integration_delete_t >
< dpp::integration_delete_t >
dpp::event_router_t< dpp::integration_update_t >
< dpp::integration_update_t >
dpp::event_router_t< dpp::interaction_create_t >
< dpp::interaction_create_t >
dpp::event_router_t< dpp::invite_create_t >
< dpp::invite_create_t >
dpp::event_router_t< dpp::invite_delete_t >
< dpp::invite_delete_t >
dpp::event_router_t< dpp::log_t >
< dpp::log_t >
dpp::event_router_t< dpp::message_context_menu_t >
< dpp::message_context_menu_t >
dpp::event_router_t< dpp::message_create_t >
< dpp::message_create_t >
dpp::event_router_t< dpp::message_delete_bulk_t >
< dpp::message_delete_bulk_t >
dpp::event_router_t< dpp::message_delete_t >
< dpp::message_delete_t >
dpp::event_router_t< dpp::message_poll_vote_add_t >
< dpp::message_poll_vote_add_t >
dpp::event_router_t< dpp::message_poll_vote_remove_t >
< dpp::message_poll_vote_remove_t >
dpp::event_router_t< dpp::message_reaction_add_t >
< dpp::message_reaction_add_t >
dpp::event_router_t< dpp::message_reaction_remove_all_t >
< dpp::message_reaction_remove_all_t >
dpp::event_router_t< dpp::message_reaction_remove_emoji_t >
< dpp::message_reaction_remove_emoji_t >
dpp::event_router_t< dpp::message_reaction_remove_t >
< dpp::message_reaction_remove_t >
dpp::event_router_t< dpp::message_update_t >
< dpp::message_update_t >
dpp::event_router_t< dpp::presence_update_t >
< dpp::presence_update_t >
dpp::event_router_t< dpp::ready_t >
< dpp::ready_t >
dpp::event_router_t< dpp::resumed_t >
< dpp::resumed_t >
dpp::event_router_t< dpp::select_click_t >
< dpp::select_click_t >
dpp::event_router_t< dpp::slashcommand_t >
< dpp::slashcommand_t >
dpp::event_router_t< dpp::socket_close_t >
< dpp::socket_close_t >
dpp::event_router_t< dpp::stage_instance_create_t >
< dpp::stage_instance_create_t >
dpp::event_router_t< dpp::stage_instance_delete_t >
< dpp::stage_instance_delete_t >
dpp::event_router_t< dpp::stage_instance_update_t >
< dpp::stage_instance_update_t >
dpp::event_router_t< dpp::thread_create_t >
< dpp::thread_create_t >
dpp::event_router_t< dpp::thread_delete_t >
< dpp::thread_delete_t >
dpp::event_router_t< dpp::thread_list_sync_t >
< dpp::thread_list_sync_t >
dpp::event_router_t< dpp::thread_member_update_t >
< dpp::thread_member_update_t >
dpp::event_router_t< dpp::thread_members_update_t >
< dpp::thread_members_update_t >
dpp::event_router_t< dpp::thread_update_t >
< dpp::thread_update_t >
dpp::event_router_t< dpp::typing_start_t >
< dpp::typing_start_t >
dpp::event_router_t< dpp::user_context_menu_t >
< dpp::user_context_menu_t >
dpp::event_router_t< dpp::user_update_t >
< dpp::user_update_t >
dpp::event_router_t< dpp::voice_buffer_send_t >
< dpp::voice_buffer_send_t >
dpp::event_router_t< dpp::voice_channel_effect_send_t >
< dpp::voice_channel_effect_send_t >
dpp::event_router_t< dpp::voice_client_disconnect_t >
< dpp::voice_client_disconnect_t >
dpp::event_router_t< dpp::voice_client_platform_t >
< dpp::voice_client_platform_t >
dpp::event_router_t< dpp::voice_client_speaking_t >
< dpp::voice_client_speaking_t >
dpp::event_router_t< dpp::voice_ready_t >
< dpp::voice_ready_t >
dpp::event_router_t< dpp::voice_receive_t >
< dpp::voice_receive_t >
dpp::event_router_t< dpp::voice_server_update_t >
< dpp::voice_server_update_t >
dpp::event_router_t< dpp::voice_state_update_t >
< dpp::voice_state_update_t >
dpp::event_router_t< dpp::voice_track_marker_t >
< dpp::voice_track_marker_t >
dpp::event_router_t< dpp::webhooks_update_t >
< dpp::webhooks_update_t >
- dpp::event_router_t
  attach
- dpp::event_router_t
  attach_awaiter
- dpp::event_router_t
  call
- dpp::event_router_t
  call
- dpp::event_router_t
  cluster
- dpp::event_router_t
  coro_awaiters
- dpp::event_router_t
  coro_mutex
- dpp::event_router_t
  detach
- dpp::event_router_t
  detach_coro
- dpp::event_router_t
  detail::event_router::awaitable< T >
- dpp::event_router_t
  dispatch_container
- dpp::event_router_t
  DPP_CHECK_ABI_COMPAT
- dpp::event_router_t
  empty
- dpp::event_router_t
  event_handler_abi_t
- dpp::event_router_t
  event_handler_t
- dpp::event_router_t
  event_router_t
- dpp::event_router_t
  handle
- dpp::event_router_t
  handle_coro
- dpp::event_router_t
  mutex
- dpp::event_router_t
  operator bool
- dpp::event_router_t
  operator co_await
- dpp::event_router_t
  operator()
- dpp::event_router_t
  regular_handler_t
- dpp::event_router_t
  resume_awaiters
- dpp::event_router_t
  set_warning_callback
- dpp::event_router_t
  task_handler_t
- dpp::event_router_t
  warning
- dpp::event_router_t
  when
- dpp::event_router_t
  ~event_router_t