From 027e67b32a9cca3cac4a186e73fdcc42faeab8c4 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 16 Nov 2024 15:21:49 +0100 Subject: added eventManager reference to interfaces --- src/crepe/api/EventManager.cpp | 49 +++++++++++------------------------------- 1 file changed, 13 insertions(+), 36 deletions(-) (limited to 'src/crepe/api/EventManager.cpp') diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index e881d49..a04f08b 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -8,10 +8,10 @@ EventManager & EventManager::get_instance() { } void EventManager::dispatch_events() { - for (std::vector, int, - std::type_index>>::iterator event_it - = this->events_queue.begin(); - event_it != this->events_queue.end();) { + using HandlersMap = std::unordered_map>>; + using HandlersVec = std::vector>; + + for (auto event_it = this->events_queue.begin(); event_it != this->events_queue.end();) { std::unique_ptr & event = std::get<0>(*event_it); int channel = std::get<1>(*event_it); std::type_index event_type = std::get<2>(*event_it); @@ -19,29 +19,13 @@ void EventManager::dispatch_events() { bool event_handled = false; if (channel) { - std::unordered_map< - std::type_index, - std::unordered_map< - int, std::vector>>>:: - iterator handlers_it - = subscribers_by_event_id.find(event_type); + auto handlers_it = subscribers_by_event_id.find(event_type); if (handlers_it != subscribers_by_event_id.end()) { - std::unordered_map< - int, std::vector>> & - handlers_map - = handlers_it->second; - std::unordered_map< - int, std::vector>>:: - iterator handlers - = handlers_map.find(channel); + HandlersMap & handlers_map = handlers_it->second; + auto handlers = handlers_map.find(channel); if (handlers != handlers_map.end()) { - std::vector> & - callbacks - = handlers->second; - for (std::vector>:: - iterator handler_it - = callbacks.begin(); - handler_it != callbacks.end(); ++handler_it) { + HandlersVec & callbacks = handlers->second; + for (auto handler_it = callbacks.begin(); handler_it != callbacks.end(); ++handler_it) { if ((*handler_it)->exec(*event)) { event_it = events_queue.erase(event_it); event_handled = true; @@ -52,18 +36,10 @@ void EventManager::dispatch_events() { } } else { // Handle event for all channels - std::unordered_map< - std::type_index, - std::vector>>::iterator - handlers_it - = this->subscribers.find(event_type); + auto handlers_it = this->subscribers.find(event_type); if (handlers_it != this->subscribers.end()) { - std::vector> & handlers - = handlers_it->second; - for (std::vector>:: - iterator handler_it - = handlers.begin(); - handler_it != handlers.end(); ++handler_it) { + HandlersVec & handlers = handlers_it->second; + for (auto handler_it = handlers.begin(); handler_it != handlers.end(); ++handler_it) { // remove event from queue since and continue when callback returns true if ((*handler_it)->exec(*event)) { event_it = this->events_queue.erase(event_it); @@ -79,3 +55,4 @@ void EventManager::dispatch_events() { } } } + -- cgit v1.2.3