Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
event_bus.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <algorithm>
6#include <cstddef>
7#include <cstdint>
8#include <functional>
9#include <memory>
10#include <typeindex>
11#include <typeinfo>
12#include <unordered_map>
13#include <utility>
14#include <vector>
15
16namespace aegis2d::core {
17
19using SubscriptionId = std::uint64_t;
20
31class EventBus {
32public:
34 template <typename Event>
35 using Handler = std::function<void(const Event&)>;
36
40 template <typename Event>
42 const SubscriptionId id = ++last_id_;
43 handlers_[std::type_index{typeid(Event)}].push_back(
44 std::make_shared<TypedSubscription<Event>>(id, std::move(handler)));
45 return id;
46 }
47
52 for (auto& [type, subscriptions] : handlers_) {
53 const auto found = std::ranges::find(subscriptions, id, &Subscription::id_of);
54 if (found != subscriptions.end()) {
55 (*found)->deactivate();
56 subscriptions.erase(found);
57 return true;
58 }
59 }
60 return false;
61 }
62
65 template <typename Event>
66 void publish(const Event& event) {
67 const auto found = handlers_.find(std::type_index{typeid(Event)});
68 if (found == handlers_.end() || found->second.empty()) {
69 return;
70 }
71 const std::vector<std::shared_ptr<Subscription>> subscriptions = found->second;
72 for (const auto& subscription : subscriptions) {
73 if (subscription->active()) {
74 dynamic_cast<const TypedSubscription<Event>&>(*subscription).call(event);
75 }
76 }
77 }
78
81 template <typename Event>
82 [[nodiscard]] std::size_t subscriber_count() const {
83 const auto found = handlers_.find(std::type_index{typeid(Event)});
84 return found == handlers_.end() ? 0 : found->second.size();
85 }
86
88 void clear() {
89 for (auto& [type, subscriptions] : handlers_) {
90 for (const auto& subscription : subscriptions) {
91 subscription->deactivate();
92 }
93 }
94 handlers_.clear();
95 }
96
97private:
98 class Subscription {
99 public:
100 explicit Subscription(SubscriptionId id) noexcept : id_{id} {}
101 Subscription(const Subscription&) = delete;
102 Subscription& operator=(const Subscription&) = delete;
103 Subscription(Subscription&&) = delete;
104 Subscription& operator=(Subscription&&) = delete;
105 virtual ~Subscription() = default;
106
107 [[nodiscard]] static SubscriptionId id_of(const std::shared_ptr<Subscription>& self) {
108 return self->id_;
109 }
110
111 [[nodiscard]] bool active() const noexcept { return active_; }
112
113 void deactivate() noexcept { active_ = false; }
114
115 private:
116 SubscriptionId id_;
117 bool active_ = true;
118 };
119
120 template <typename Event>
121 class TypedSubscription final : public Subscription {
122 public:
123 TypedSubscription(SubscriptionId id, Handler<Event> handler)
124 : Subscription{id}, handler_{std::move(handler)} {}
125
126 void call(const Event& event) const { handler_(event); }
127
128 private:
129 Handler<Event> handler_;
130 };
131
132 std::unordered_map<std::type_index, std::vector<std::shared_ptr<Subscription>>> handlers_;
133 SubscriptionId last_id_ = 0;
134};
135
136} // namespace aegis2d::core
Lets systems react to events without knowing who sends them.
Definition event_bus.hpp:31
std::function< void(const Event &)> Handler
Callback for events of type Event.
Definition event_bus.hpp:35
std::size_t subscriber_count() const
Number of handlers registered for events of type Event.
Definition event_bus.hpp:82
SubscriptionId subscribe(Handler< Event > handler)
Registers a handler for events of type Event.
Definition event_bus.hpp:41
void publish(const Event &event)
Calls every handler of type Event with event, before returning.
Definition event_bus.hpp:66
bool unsubscribe(SubscriptionId id)
Removes a handler.
Definition event_bus.hpp:51
void clear()
Removes every handler of every event type.
Definition event_bus.hpp:88
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11
std::uint64_t SubscriptionId
Handle returned by EventBus::subscribe(), used to unsubscribe. Never 0.
Definition event_bus.hpp:19
Definition endpoint.hpp:85