From 80c74b90a3e44e25a4fa9fdd25bf0aa9efbaf6fd Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 11 Nov 2024 16:12:09 +0100 Subject: interfaces working but unsubscribe broken --- src/crepe/api/EventManager.h | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/crepe/api/EventManager.h') diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 8c0685a..88a386a 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -20,7 +20,8 @@ public: } template void subscribe(EventHandler && callback, int channel = 0); - void unsubscribe(std::type_index eventType, const std::string & handlerName,int channel); + template + void unsubscribe(const EventHandler &, int eventId); template void trigger_event(const EventType & event, int channel); void queue_event(std::unique_ptr && event, int channel); @@ -74,3 +75,39 @@ void EventManager::trigger_event(const EventType & event, int eventId) { } } } +template +void EventManager::unsubscribe(const EventHandler & callback, int channel) { + std::type_index event_type(typeid(EventType)); + 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); + if (subscriber_list != subscribers_by_event_id.end()) { + auto& handlers_map = subscriber_list->second; + auto handlers = handlers_map.find(channel); + if (handlers != handlers_map.end()) { + 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; + } + } + } + } + } else { + auto handlers_it = subscribers.find(event_type); + if (handlers_it != subscribers.end()) { + 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