diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-20 16:35:58 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-20 16:35:58 +0100 |
commit | 5f76ad1dde34fc0cf7b8ea63befa8917da94fe5c (patch) | |
tree | a393216d0e3beb6d226c6081350a83d1a897748f | |
parent | ce14236bd08469737185962d0be11d72c442b60e (diff) |
save before big refactor
-rw-r--r-- | src/crepe/api/Event.h | 1 | ||||
-rw-r--r-- | src/crepe/api/EventManager.h | 27 | ||||
-rw-r--r-- | src/crepe/api/EventManager.hpp | 29 |
3 files changed, 21 insertions, 36 deletions
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 <string> 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> event; - int channel = 0; + int channel = CHANNEL_ALL; std::type_index type; int priority = 0; }; struct CallbackEntry { std::unique_ptr<IEventHandlerWrapper> 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<QueueEntry> events_queue; //! Registered event handlers. - std::unordered_map<std::type_index, std::vector<std::unique_ptr<IEventHandlerWrapper>>> - subscribers; - //! Event handlers indexed by event ID. - std::unordered_map< - std::type_index, - std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>> - subscribers_by_event_id; + std::unordered_map<std::type_index,std::vector<CallbackEntry>> 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 <typename EventType> void EventManager::subscribe(const EventHandler<EventType> & callback, int channel, int priority) { - using HandlersMap - = std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>; - using HandlersVec = std::vector<std::unique_ptr<IEventHandlerWrapper>>; + using HandlersVec = std::vector<CallbackEntry>; std::type_index event_type = typeid(EventType); std::unique_ptr<EventHandlerWrapper<EventType>> handler = std::make_unique<EventHandlerWrapper<EventType>>(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 <typename EventType> @@ -33,12 +24,12 @@ void EventManager::queue_event(const EventType & event, int channel,int priority auto event_ptr = std::make_unique<EventType>(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); |