Skip to main content

EventManager.ixx File

High-level event manager dispatching events to listeners. More...

Included Headers

#include <functional> #include <memory> #include <utility> #include <format> #include <helios.util.log.Logger> #include <helios.util.log.LogManager> #include <helios.event.Dispatcher> #include <helios.event.EventQueue> #include <helios.event.Event>

Namespaces Index

namespacehelios
namespaceevent

Event system. More...

Classes Index

classEventManager

An abstract EventManager managing the buffering and dispatching of events, acting as a central hub for event management. More...

Macro Definitions Index

#defineHELIOS_LOG_SCOPE   "helios::event::EventManager"

Description

High-level event manager dispatching events to listeners.

Macro Definitions

HELIOS_LOG_SCOPE

#define HELIOS_LOG_SCOPE   "helios::event::EventManager"

Definition at line 21 of file EventManager.ixx.

21#define HELIOS_LOG_SCOPE "helios::event::EventManager"

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file EventManager.ixx
3 * @brief High-level event manager dispatching events to listeners.
4 */
5module;
6
7#include <functional>
8#include <memory>
9#include <utility>
10#include <format>
11
12export module helios.event.EventManager;
13
14import helios.event.Event;
15import helios.event.EventQueue;
16import helios.event.Dispatcher;
17
18import helios.util.log.LogManager;
19import helios.util.log.Logger;
20
21#define HELIOS_LOG_SCOPE "helios::event::EventManager"
22export namespace helios::event {
23
24 /**
25 * @brief Defines the posting policy for events.
26 *
27 * This enum specifies how events should be handled when posted
28 * to the EventQueue.
29 */
30 enum PostPolicy {
31 /**
32 * @brief Policy for removing an event in favor of a newer one based
33 * on specific criteria.
34 */
36
37 /**
38 * @brief Policy for simply adding an event to the underlying EventQueue.
39 */
41
42 /**
43 * @brief Number of entries in this enumeration.
44 */
46 };
47
48
49 /**
50 * @brief An abstract EventManager managing the buffering and dispatching of
51 * events, acting as a central hub for event management.
52 *
53 * Concrete implementations take care of `dispatchAll()`, that is, the
54 * logic for dispatching all events to the subscribed callbacks.
55 */
57
58 protected:
59
61
62 /**
63 * @brief A unique_ptr to the EventQueue this EventManager owns.
64 */
65 std::unique_ptr<EventQueue> eventQueue_;
66
67 /**
68 * @brief A unique_ptr to the Dispatcher this EventManager owns.
69 */
70 std::unique_ptr<Dispatcher> dispatcher_;
71
72 EventManager() = default;
73
74 public:
75
76 virtual ~EventManager() = default;
77
78 /**
79 * @brief Creates a new EventManager using the specified EventQueue and Dispatcher.
80 *
81 * The EventManger takes onwership of the EventQueue and the Dispatcher.
82 *
83 * @param eventQueue
84 * @param dispatcher
85 */
86 explicit EventManager(
87 std::unique_ptr<EventQueue> eventQueue,
88 std::unique_ptr<Dispatcher> dispatcher
89 ) :
90 eventQueue_(std::move(eventQueue)),
91 dispatcher_(std::move(dispatcher))
92 {}
93
94
95 /**
96 * @brief Posts the event to the queue using the APPEND policy,
97 * passing ownership of the event to the underlying queue.
98 *
99 * Calling this function is functionally identical to calling post()
100 * with the second argument set to APPEND.
101 *
102 * @param event The event to post to the EventQueue.
103 *
104 * @return EventManager
105 */
106 EventManager& post(std::unique_ptr<const Event> event) {
107 return post(std::move(event), APPEND, nullptr);
108 }
109
110
111 /**
112 * @brief Posts the event to the queue, passing ownership of the event
113 * to the underlying queue.
114 *
115 * Calling this function is identical to calling post() with the third argument
116 * func set to nullptr, relying on the default implementations.
117 *
118 * @param event The event to post to the EventQueue.
119 * @param policy The policy to use for posting the event.
120 *
121 * @return EventManager
122 */
123 EventManager& post(std::unique_ptr<const Event> event, PostPolicy policy) {
124 logger_.debug(std::format("Posting Event {0}", event->toString()));
125 return post(std::move(event), policy, nullptr);
126 }
127
128
129 /**
130 * @brief Posts the event to the underlying EventQueue using the specified
131 * policy and the specified func.
132 *
133 * Implementing classes are responsible to treating func according to the
134 * specified policy, e.g. APPEND for adding the event to the queue,
135 * LATEST_WINS for removing a particular event in favor of the submitted
136 * event.
137 *
138 * @param event A unique_ptr to the event to be posted. Will be the first argument
139 * passed to func.
140 * @param policy The post policy for the event (@see PostPolicy)
141 * @param func The function used with the PostPolicy.
142 * @return
143 */
145 std::unique_ptr<const Event> event,
146 PostPolicy policy,
147 const std::function<bool(
148 const std::unique_ptr<const Event>& event,
149 const std::unique_ptr<const Event>& evt)>& func
150 ) = 0;
151
152
153 /**
154 * @brief Dispatches all events currently held in the event queue.
155 *
156 * This method retrieves and removes events one by one from the underlying
157 * `EventQueue`, passing ownership to the underlying Dispatcher.
158 *
159 * @return EventManager
160 */
161 virtual EventManager& dispatchAll() = 0;
162
163
164 /**
165 * @brief Subscribes a callback to the specified EventType.
166 *
167 * Delegates to the subscribe() method of the underlying dispatcher.
168 *
169 * @tparam EventType The specific tyoe of the event to subscribe to.
170 * @param callback The callback function that should be called when an event of the
171 * specific type is triggered.
172 *
173 * @return EventManager
174 */
175 template<typename EventType>
176 EventManager& subscribe(std::function<void(const EventType&)> callback) {
177 dispatcher_->subscribe(callback);
178
179 return *this;
180 }
181
182
183 /**
184 * @brief Returns a reference to the dispatcher used with this EventManager.
185 *
186 * Provides direct access to the dispatcher, allowing for modification
187 * of the Dispatcher, e.g. managing subscriptions.
188 *
189 * @return Dispatcher
190 */
191 [[nodiscard]] Dispatcher& dispatcher() const noexcept {
192 return *dispatcher_;
193 }
194
195
196 };
197
198}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.