From 6fd7cec7d4bbf5aeb361b3f1337671bb0f9af61b Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 28 Nov 2024 10:13:11 +0100 Subject: manager mediator refactor --- src/crepe/manager/EventManager.hpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/crepe/manager/EventManager.hpp (limited to 'src/crepe/manager/EventManager.hpp') diff --git a/src/crepe/manager/EventManager.hpp b/src/crepe/manager/EventManager.hpp new file mode 100644 index 0000000..a5f4556 --- /dev/null +++ b/src/crepe/manager/EventManager.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "EventManager.h" + +namespace crepe { + +template +subscription_t EventManager::subscribe(const EventHandler & callback, + event_channel_t channel) { + subscription_counter++; + std::type_index event_type = typeid(EventType); + std::unique_ptr> handler + = std::make_unique>(callback); + std::vector & handlers = this->subscribers[event_type]; + handlers.emplace_back(CallbackEntry{ + .callback = std::move(handler), .channel = channel, .id = subscription_counter}); + return subscription_counter; +} + +template +void EventManager::queue_event(const EventType & event, event_channel_t channel) { + static_assert(std::is_base_of::value, + "EventType must derive from Event"); + this->events_queue.push_back(QueueEntry{ + .event = std::make_unique(event), + .channel = channel, + .type = typeid(EventType), + }); +} + +template +void EventManager::trigger_event(const EventType & event, event_channel_t channel) { + this->handle_event(typeid(EventType), channel, event); +} + +} // namespace crepe -- cgit v1.2.3