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
event_dispatcher.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <cstdint>
26#include <functional>
27#include <memory>
28#include <unordered_map>
29#include <utility>
30#include <vector>
31
32#include "utility/event/event.hpp"
33
34namespace utility::event
35{
36
49 {
50 private:
51 using Handler = std::function<void(const Event &)>;
52
53 std::unordered_map<std::size_t, std::vector<Handler>> _handlers;
54
55 template<typename EventType> static std::size_t typeKey(void) noexcept
56 {
57 static const EventType *marker = nullptr;
58 return reinterpret_cast<std::size_t>(&marker);
59 }
60
61 public:
65 EventDispatcher(void) = default;
66
70 ~EventDispatcher(void) = default;
71
78 template<InheritFromEvent EventType, typename Callable>
79 void addListener(Callable &&listener)
80 {
81 auto key = typeKey<EventType>();
82 _handlers[key].emplace_back([fn = std::forward<Callable>(listener)](
83 const Event &event) mutable {
84 fn(static_cast<const EventType &>(event));
85 });
86 }
87
94 template<InheritFromEvent EventType>
95 void emit(const EventType &event) const
96 {
97 auto key = typeKey<EventType>();
98 auto it = _handlers.find(key);
99 if (it == _handlers.end()) {
100 return;
101 }
102 for (const auto &handler: it->second) {
103 handler(event);
104 }
105 }
106
111 template<InheritFromEvent EventType> void clearListeners(void)
112 {
113 _handlers.erase(typeKey<EventType>());
114 }
115
119 void clearAll(void) noexcept
120 {
121 _handlers.clear();
122 }
123 };
124
125} // namespace utility::event
Dispatches typed events to registered listeners.
void clearAll(void) noexcept
Remove all listeners for every event type.
EventDispatcher(void)=default
Default constructor.
void addListener(Callable &&listener)
Register a listener for a concrete event type.
void emit(const EventType &event) const
Emit an event to all listeners registered for its concrete type.
void clearListeners(void)
Remove all listeners registered for a concrete event type.
~EventDispatcher(void)=default
Default destructor.
Base class for events in the event system.
Definition event.hpp:38