9#include <unordered_map>
47 std::vector<std::pair<
HandlerId, std::function<void(
const T&)>>>
61 auto currentEvents = std::move(
queue);
64 for (
const auto& event : currentEvents) {
65 for (
const auto& handlerPair :
handlers) {
66 handlerPair.second(event);
79 [
id](
const auto& pair) { return pair.first == id; });
92 std::unordered_map<std::type_index, std::unique_ptr<IEventQueue>>
110 template <
typename T>
112 using CleanType = std::decay_t<T>;
113 auto typeIdx = std::type_index(
typeid(CleanType));
116 queues[typeIdx] = std::make_unique<EventQueue<CleanType>>();
120 q->
queue.push_back(std::forward<T>(event));
128 template <
typename T>
130 using CleanType = std::decay_t<T>;
131 auto typeIdx = std::type_index(
typeid(CleanType));
134 queues[typeIdx] = std::make_unique<EventQueue<CleanType>>();
140 q->
handlers.push_back({id, callback});
153 for (
auto& [type, q] :
queues) {
154 q->RemoveHandler(
id);
164 for (
auto& [type, q] :
queues) {
uint32_t HandlerId
A unique identifier for a registered callback.
Definition EventBus.hpp:15
Central event dispatcher connecting isolated engine systems.
Definition EventBus.hpp:90
void DispatchEvents()
Initiates sequential dispatch of all accumulated events across all queues.
Definition EventBus.hpp:163
std::unordered_map< std::type_index, std::unique_ptr< IEventQueue > > queues
Map of event types to their queues.
Definition EventBus.hpp:93
void Publish(T &&event)
Adds an event to the corresponding type queue for future dispatch.
Definition EventBus.hpp:111
HandlerId Subscribe(std::function< void(const T &)> callback)
Registers a new subscriber callback for a specific event type.
Definition EventBus.hpp:129
HandlerId nextId
Counter for generating unique handler IDs.
Definition EventBus.hpp:94
void Unsubscribe(HandlerId id)
Removes a handler from the event bus across all queues.
Definition EventBus.hpp:152
EventBus()
Initializes the EventBus and resets the handler ID counter.
Definition EventBus.hpp:102
Typed event queue for a specific event type T.
Definition EventBus.hpp:44
void Dispatch() override
Moves events to a local buffer, clears the original queue, and notifies subscribers.
Definition EventBus.hpp:57
std::vector< T > queue
Accumulated events for the current frame.
Definition EventBus.hpp:46
void RemoveHandler(HandlerId id) override
Erases a callback from the handlers list if the ID matches.
Definition EventBus.hpp:76
std::vector< std::pair< HandlerId, std::function< void(const T &)> > > handlers
Registered callbacks paired with their IDs.
Definition EventBus.hpp:48
Abstract event queue interface for type erasure.
Definition EventBus.hpp:20
virtual void Dispatch()=0
Calls all subscribers and clears the current queue.
virtual ~IEventQueue()=default
virtual void RemoveHandler(HandlerId id)=0
Removes a handler by its unique ID.