From 5f76ad1dde34fc0cf7b8ea63befa8917da94fe5c Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 20 Nov 2024 16:35:58 +0100 Subject: save before big refactor --- src/crepe/api/Event.h | 1 + src/crepe/api/EventManager.h | 27 ++++++++++----------------- src/crepe/api/EventManager.hpp | 29 ++++++++++------------------- 3 files changed, 21 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index 33f3add..06cf7f3 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -1,3 +1,4 @@ +// TODO discussing the location of these events #pragma once #include diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 8fd22f4..eccb0bf 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -91,35 +91,28 @@ public: */ void clear(); private: + + /** + * \brief Default constructor for the EventManager. + * + * This constructor is private to enforce the singleton pattern. + */ + EventManager() = default; struct QueueEntry { std::unique_ptr event; - int channel = 0; + int channel = CHANNEL_ALL; std::type_index type; int priority = 0; }; struct CallbackEntry { std::unique_ptr callback; - int channel = 0; - std::type_index type; + int channel = CHANNEL_ALL; int priority = 0; }; - /** - * \brief Default constructor for the EventManager. - * - * This constructor is private to enforce the singleton pattern. - */ - EventManager() = default; - //! The queue of events to be processed. std::vector events_queue; //! Registered event handlers. - std::unordered_map>> - subscribers; - //! Event handlers indexed by event ID. - std::unordered_map< - std::type_index, - std::unordered_map>>> - subscribers_by_event_id; + std::unordered_map> subscribers; }; diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp index 9090a3f..3a40336 100644 --- a/src/crepe/api/EventManager.hpp +++ b/src/crepe/api/EventManager.hpp @@ -4,26 +4,17 @@ namespace crepe { template void EventManager::subscribe(const EventHandler & callback, int channel, int priority) { - using HandlersMap - = std::unordered_map>>; - using HandlersVec = std::vector>; + using HandlersVec = std::vector; std::type_index event_type = typeid(EventType); std::unique_ptr> handler = std::make_unique>(callback); - - if (channel) { - HandlersMap & handlers_map = this->subscribers_by_event_id[event_type]; - auto handlers = handlers_map.find(channel); - if (handlers != handlers_map.end()) { - handlers->second.emplace_back(std::move(handler)); - } else { - handlers_map[channel].emplace_back(std::move(handler)); - } - } else { - HandlersVec & handlers = this->subscribers[event_type]; - handlers.emplace_back(std::move(handler)); - } + HandlersVec & handlers = this->subscribers[event_type]; + handlers.emplace_back(CallbackEntry{ + .callback = std::move(handler), + .channel = channel, + .priority = priority, + }); } template @@ -33,12 +24,12 @@ void EventManager::queue_event(const EventType & event, int channel,int priority auto event_ptr = std::make_unique(event); - this->events_queue.push_back( QueueEntry{ .event = std::move(event_ptr), .channel = channel, - .type = event_type + .type = event_type, + .priority = priority } ); } @@ -51,7 +42,7 @@ void EventManager::trigger_event(const EventType & event, int channel) { std::type_index event_type = typeid(EventType); - if (channel > 0) { + if (channel == CHANNEL_ALL) { HandlersMap & handlers_map = this->subscribers_by_event_id[event_type]; auto handlers_it = handlers_map.find(channel); -- cgit v1.2.3