Skip to main content

Dispatcher.ixx File

Dispatches events to registered callbacks. More...

Included Headers

#include <functional> #include <memory> #include <typeindex> #include <helios.event.Event>

Namespaces Index

namespacehelios
namespaceevent

Event system. More...

Classes Index

classDispatcher

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

Description

Dispatches events to registered callbacks.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file Dispatcher.ixx
3 * @brief Dispatches events to registered callbacks.
4 */
5module;
6
7#include <functional>
8#include <memory>
9#include <typeindex>
10
11export module helios.event.Dispatcher;
12
13import helios.event.Event;
14
15
16export namespace helios::event {
17
18 /**
19 * @brief A generic event Dispatcher for type-safe event handling.
20 *
21 * The `Dispatcher` allows interested entities to subscribe to specific
22 * event types and dispatch events in a decoupled manner.
23 * Using `std::type_index` makes sure that only relevant handlers receive
24 * specific event instances.
25 *
26 */
27 class Dispatcher {
28
29 private:
30 /**
31 * @brief Internal map to store registered callbacks for various event types.
32 */
33 std::unordered_map<std::type_index, std::vector<std::function<void(const Event&)>>> callbacks_;
34
35 public:
36 /**
37 * @brief Subscribes a callback to a specific event type.
38 *
39 * Registers a `std::function` to be invoked when an event of `EventType`
40 * is dispatched. The callback will receive the dispatched event as a const reference.
41 *
42 * @tparam EventType The specific type of event to subscribe to. Must derive from `Event`.
43 * @param callback The callback function to be executed when the event is dispatched. The
44 * function should accept `const EventType&` as its single argument.
45 */
46 template<typename EventType>
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 }
59
60
61 /**
62 * @brief Dispatches an event to all subscribed callbacks (i.e. listeners) of the
63 * specific EventType.
64 *
65 * This method will take ownership of the event to dispatch.
66 * Once this method returns, the unique_ptr to the `Event` will be a nullptr.
67 *
68 * @param event A unique_ptr to the event instance to be dispatched.
69 */
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 }
79
80 };
81
82}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.