eventbus
dp::event_bus Class Reference

A central event handler class that connects event handlers with the events. More...

#include <event_bus.hpp>

Public Member Functions

 event_bus ()=default
 
template<typename EventType , typename EventHandler , typename = std::enable_if_t<std::is_invocable_v<EventHandler> || std::is_invocable_v<EventHandler, EventType>>>
handler_registration register_handler (EventHandler &&handler)
 Register an event handler for a given event type. More...
 
template<typename EventType , typename ClassType , typename MemberFunction >
handler_registration register_handler (ClassType *class_instance, MemberFunction &&function) noexcept
 Register an event handler for a given event type. More...
 
template<typename EventType , typename = std::enable_if_t<!std::is_pointer_v<EventType>>>
void fire_event (EventType &&evt) noexcept
 Fire an event to notify event handlers. More...
 
bool remove_handler (const handler_registration &registration) noexcept
 Remove a given handler from the event bus. More...
 
void remove_handlers () noexcept
 Remove all handlers from event bus. More...
 
std::size_t handler_count () noexcept
 Get the number of handlers registered with the event bus. More...
 

Detailed Description

A central event handler class that connects event handlers with the events.

Constructor & Destructor Documentation

◆ event_bus()

dp::event_bus::event_bus ( )
default

Member Function Documentation

◆ fire_event()

template<typename EventType , typename = std::enable_if_t<!std::is_pointer_v<EventType>>>
void dp::event_bus::fire_event ( EventType &&  evt)
inlinenoexcept

Fire an event to notify event handlers.

Template Parameters
EventTypeThe event type
Parameters
evtThe event to pass to all event handlers.

◆ handler_count()

std::size_t dp::event_bus::handler_count ( )
inlinenoexcept

Get the number of handlers registered with the event bus.

Returns
The total number of handlers.

◆ register_handler() [1/2]

template<typename EventType , typename ClassType , typename MemberFunction >
handler_registration dp::event_bus::register_handler ( ClassType *  class_instance,
MemberFunction &&  function 
)
inlinenoexcept

Register an event handler for a given event type.

Template Parameters
EventTypeThe event type
ClassTypeEvent handler class
MemberFunctionEvent handler member function
Parameters
class_instanceInstance of ClassType that will handle the event.
functionPointer to the MemberFunction of the ClassType.
Returns
A handler_registration instance for the given handler.

◆ register_handler() [2/2]

template<typename EventType , typename EventHandler , typename = std::enable_if_t<std::is_invocable_v<EventHandler> || std::is_invocable_v<EventHandler, EventType>>>
handler_registration dp::event_bus::register_handler ( EventHandler &&  handler)
inline

Register an event handler for a given event type.

Template Parameters
EventTypeThe event type
EventHandlerThe invocable event handler type.
Parameters
handlerA callable handler of the event type. Can accept the event as param or take no params.
Returns
A handler_registration instance for the given handler.

◆ remove_handler()

bool dp::event_bus::remove_handler ( const handler_registration registration)
inlinenoexcept

Remove a given handler from the event bus.

Parameters
registrationThe registration object returned by register_handler.
Returns
true is handler removal was successful, false otherwise.

◆ remove_handlers()

void dp::event_bus::remove_handlers ( )
inlinenoexcept

Remove all handlers from event bus.


The documentation for this class was generated from the following file: