utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
utility::event::EventDispatcher Class Reference

Dispatches typed events to registered listeners. More...

#include <headers/utility/event/event_dispatcher.hpp>

Public Member Functions

 EventDispatcher (void)=default
 Default constructor.
 
 ~EventDispatcher (void)=default
 Default destructor.
 
template<InheritFromEvent EventType, typename Callable >
void addListener (Callable &&listener)
 Register a listener for a concrete event type.
 
template<InheritFromEvent EventType>
void emit (const EventType &event) const
 Emit an event to all listeners registered for its concrete type.
 
template<InheritFromEvent EventType>
void clearListeners (void)
 Remove all listeners registered for a concrete event type.
 
void clearAll (void) noexcept
 Remove all listeners for every event type.
 

Detailed Description

Dispatches typed events to registered listeners.

Listeners are registered per concrete event type. When an event is emitted, all listeners registered for that type are invoked with a strongly typed reference. Events are dispatched to the concrete type derived from the base Event pointer via a runtime type check.

The dispatcher owns no events; it only forwards them. It is safe to register and emit from the same thread.

Definition at line 48 of file event_dispatcher.hpp.

Member Function Documentation

◆ addListener()

template<InheritFromEvent EventType, typename Callable >
void utility::event::EventDispatcher::addListener ( Callable &&  listener)
inline

Register a listener for a concrete event type.

Template Parameters
EventTypeThe concrete event type (must derive from Event).
Parameters
listenerCallable invoked with a const reference to the event.
Note
The listener is invoked synchronously on the emitting thread.

Definition at line 79 of file event_dispatcher.hpp.

◆ clearAll()

void utility::event::EventDispatcher::clearAll ( void  )
inlinenoexcept

Remove all listeners for every event type.

Definition at line 119 of file event_dispatcher.hpp.

◆ clearListeners()

template<InheritFromEvent EventType>
void utility::event::EventDispatcher::clearListeners ( void  )
inline

Remove all listeners registered for a concrete event type.

Template Parameters
EventTypeThe concrete event type.

Definition at line 111 of file event_dispatcher.hpp.

◆ emit()

template<InheritFromEvent EventType>
void utility::event::EventDispatcher::emit ( const EventType &  event) const
inline

Emit an event to all listeners registered for its concrete type.

Template Parameters
EventTypeThe concrete event type.
Parameters
eventThe event to dispatch.

Definition at line 95 of file event_dispatcher.hpp.


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