From 48fb3f48831b1db187942592343eb4a2dc6662fc Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 12 Nov 2024 12:45:19 +0100 Subject: dispatch and queue working --- src/crepe/api/EventManager.h | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/crepe/api/EventManager.h') diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 91120fd..be9a947 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -24,12 +24,13 @@ public: void unsubscribe(const EventHandler &, int eventId); template void trigger_event(const EventType & event, int channel); - void queue_event(std::unique_ptr && event, int channel); + template + void queue_event(EventType&& event, int channel); void dispatch_events(); private: EventManager() = default; - std::vector, int>> events_queue; + std::vector, int, std::type_index>> events_queue; std::unordered_map>> subscribers; std::unordered_map>>> subscribers_by_event_id; }; @@ -51,6 +52,19 @@ void EventManager::subscribe(EventHandler && callback, int channel){ subscribers[event_type].emplace_back(std::move(handler)); } } +template +void EventManager::queue_event(EventType&& event, int channel) { + std::type_index event_type = std::type_index(typeid(EventType)); + + std::unique_ptr event_ptr = std::make_unique(std::forward(event)); + + std::tuple, int, std::type_index> tuple( + std::move(event_ptr), + channel, + event_type + ); + events_queue.push_back(std::move(tuple)); +} template void EventManager::trigger_event(const EventType & event, int eventId) { @@ -79,7 +93,6 @@ template void EventManager::unsubscribe(const EventHandler & callback, int channel) { std::type_index event_type(typeid(EventType)); const std::string handler_name = callback.target_type().name(); - std::cout << "unsubcribe name: " << handler_name << std::endl; if (channel) { auto subscriber_list = subscribers_by_event_id.find(event_type); @@ -90,7 +103,6 @@ void EventManager::unsubscribe(const EventHandler & callback, int cha auto& callbacks = handlers->second; for (auto it = callbacks.begin(); it != callbacks.end(); ++it) { if ((*it)->get_type() == handler_name) { - std::cout << "successfully erased an event" << std::endl; it = callbacks.erase(it); return; } @@ -103,7 +115,6 @@ void EventManager::unsubscribe(const EventHandler & callback, int cha auto& handlers = handlers_it->second; for (auto it = handlers.begin(); it != handlers.end(); ++it) { if ((*it)->get_type() == handler_name) { - std::cout << "successfully erased an event" << std::endl; it = handlers.erase(it); return; } -- cgit v1.2.3