Skip to main content

Dispatcher Class

A generic event Dispatcher for type-safe event handling. More...

Declaration

class helios::event::Dispatcher { ... }

Public Member Functions Index

template <typename EventType>
voidsubscribe (std::function< void(const EventType &)> callback)

Subscribes a callback to a specific event type. More...

voiddispatch (std::unique_ptr< const Event > event)

Dispatches an event to all subscribed callbacks (i.e. listeners) of the specific EventType. More...

Private Member Attributes Index

std::unordered_map< std::type_index, std::vector< std::function< void(const Event &)> > >callbacks_

Internal map to store registered callbacks for various event types. More...

Description

A generic event Dispatcher for type-safe event handling.

The `Dispatcher` allows interested entities to subscribe to specific event types and dispatch events in a decoupled manner. Using `std::type_index` makes sure that only relevant handlers receive specific event instances.

Definition at line 27 of file Dispatcher.ixx.

Public Member Functions

dispatch()

void helios::event::Dispatcher::dispatch (std::unique_ptr< const Event > event)
inline

Dispatches an event to all subscribed callbacks (i.e. listeners) of the specific EventType.

This method will take ownership of the event to dispatch. Once this method returns, the unique_ptr to the `Event` will be a nullptr.

Parameters
event

A unique_ptr to the event instance to be dispatched.

Definition at line 70 of file Dispatcher.ixx.

70 void dispatch(std::unique_ptr<const Event> event) {
71 const auto idx = std::type_index(typeid(*event));
72
73 if (const auto cb = callbacks_.find(idx); cb != callbacks_.end()) {
74 for (const auto& callback : cb->second) {
75 callback(*event);
76 }
77 }
78 }

subscribe()

template <typename EventType>
void helios::event::Dispatcher::subscribe (std::function< void(const EventType &)> callback)
inline

Subscribes a callback to a specific event type.

Registers a `std::function` to be invoked when an event of `EventType` is dispatched. The callback will receive the dispatched event as a const reference.

Template Parameters
EventType

The specific type of event to subscribe to. Must derive from `Event`.

Parameters
callback

The callback function to be executed when the event is dispatched. The function should accept `const EventType&` as its single argument.

Definition at line 47 of file Dispatcher.ixx.

47 void subscribe(std::function<void(const EventType&)> callback) {
48 static_assert(std::is_base_of_v<Event, EventType>, "EventType is not of type Event");
49
50 const auto idx = std::type_index(typeid(EventType));
51
52 auto wrapper = [callback](const Event& event) {
53 const auto& typedEvent = static_cast<const EventType&>(event);
54 callback(typedEvent);
55 };
56
57 callbacks_[idx].push_back(wrapper);
58 }

Referenced by helios::app::controller::BasicWindowRenderingController::subscribeTo.

Private Member Attributes

callbacks_

std::unordered_map<std::type_index, std::vector<std::function<void(const Event&)> > > helios::event::Dispatcher::callbacks_

Internal map to store registered callbacks for various event types.

Definition at line 33 of file Dispatcher.ixx.

33 std::unordered_map<std::type_index, std::vector<std::function<void(const Event&)>>> callbacks_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.