11#include <unordered_map>
15#include <spdlog/spdlog.h>
18namespace Imago::Engine
59 static const EventTypeId id = detail::next_event_type_id();
77 using ErasedFn = void(*)();
83 ErasedFn systemAddress;
88 std::unordered_map<EventTypeId, std::vector<Subscriber>> _subscribers;
93 uint32_t _broadcastDepth = 0;
104 static std::vector<Subscriber>::iterator find_subscriber(std::vector<Subscriber>& subscribers, ErasedFn system);
112 template <
typename T>
136 template <
typename T>
137 void emit(
const T& event);
144 template <
typename T>
156 template <
typename T>
170 template <
typename T>
177 auto it = _subscribers.find(
id);
178 if (it == _subscribers.end())
return;
181 std::vector<Subscriber>& subscribers = it->second;
185 for (
const Subscriber& subscriber : subscribers) {
186 subscriber.invoke(_ctx, &event);
191 template <
typename T>
195 static_assert(!std::is_const_v<T> && !std::is_reference_v<T>,
"[Broadcaster] Event type T must not be const or a reference");
199 assert(_broadcastDepth == 0 &&
"[Broadcaster] you are trying to subscribe to an event while the Broadcaster is emitting!");
203 assert(system !=
nullptr &&
"[Broadcaster] you are trying to subscribe a null system to an event!");
209 std::vector<Subscriber>& subscribers = _subscribers[id];
216 ErasedFn erasedSystem =
reinterpret_cast<ErasedFn
>(system);
217 if (find_subscriber(subscribers, erasedSystem) != subscribers.end())
return;
220 Subscriber subscriber = {
223 system(ctx, *
static_cast<const T*
>(event));
226 subscribers.push_back(subscriber);
229 template <
typename T>
233 static_assert(!std::is_const_v<T> && !std::is_reference_v<T>,
"[Broadcaster] Event type T must not be const or a reference");
237 assert(_broadcastDepth == 0 &&
"[Broadcaster] you are trying to unsubscribe from an event while the Broadcaster is emitting!");
242 auto it = _subscribers.find(
id);
243 if (it == _subscribers.end()) {
244 spdlog::warn(
"[Broadcaster] You are trying to unsubscribe from an event that the system is already not subscribed to");
249 std::vector<Subscriber>& subscribers = it->second;
252 ErasedFn erasedSystem =
reinterpret_cast<ErasedFn
>(system);
258 auto newEnd = std::remove_if(subscribers.begin(), subscribers.end(), [erasedSystem](
const Subscriber& s) { return s.systemAddress == erasedSystem ; });
262 if (newEnd == subscribers.end()) {
263 spdlog::warn(
"[Broadcaster] You are trying to unsubscribe from an event that the system is already not subscribed to");
268 subscribers.erase(newEnd, subscribers.end());
EventTypeId get_event_type_id()
Returns the EventTypeId for the event of type T.
Definition Broadcaster.hpp:57
uint32_t EventTypeId
Alias for event ids in event storage.
Definition Broadcaster.hpp:25
void emit(const T &event)
Call every system subscribed to EventType T in subcription order.
Definition Broadcaster.hpp:171
void(*)(SceneContext &, const T &) SystemFn
Function pointer to a system that recieves SceneContext and an EventType and returns nothing.
Definition Broadcaster.hpp:113
void unsubscribe(SystemFn< T > system)
Removes the system function pointer from the list for EventType T.
Definition Broadcaster.hpp:230
void subscribe(SystemFn< T > system)
Adds the system function pointer to the list for EventType T. Called during scene initialization.
Definition Broadcaster.hpp:192
Broadcaster(SceneContext &ctx)
Construct a new Broadcaster object with the current SceneContext.
Definition Broadcaster.cpp:9
void clear()
Drops every subscription for every event type.
Definition Broadcaster.cpp:18
Gives systems access to the persistent engine objects they could need.
Definition SceneContext.hpp:30