From a38c3a33055e91c9d9ceed507c0e3c2f63dc7fa4 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Thu, 24 Oct 2024 12:31:22 +0200 Subject: [PATCH] properly add unsub --- solanaceae/util/event_provider.hpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/solanaceae/util/event_provider.hpp b/solanaceae/util/event_provider.hpp index cc68c12..00c0da5 100644 --- a/solanaceae/util/event_provider.hpp +++ b/solanaceae/util/event_provider.hpp @@ -22,18 +22,12 @@ struct EventProviderI { ~SubscriptionReference(void) { for (const enumType et : _subs) { - auto& o_vec = _ep._subscribers.at(static_cast(et)); - for (auto o_it = o_vec.cbegin(); o_it != o_vec.cend(); o_it++) { - if (*o_it == _object) { - o_vec.erase(o_it); - break; - } - } + _ep.unsubscribe(_object, et); } } SubscriptionReference& subscribe(const enumType event_type) { - _ep._subscribers.at(static_cast(event_type)).push_back(_object); + _ep.subscribe(_object, event_type); _subs.push_back(event_type); return *this; } @@ -41,11 +35,20 @@ struct EventProviderI { virtual ~EventProviderI(void) {}; - // TODO: unsub virtual void subscribe(EventI* object, const enumType event_type) { _subscribers.at(static_cast(event_type)).push_back(object); } + virtual void unsubscribe(EventI* object, const enumType event_type) { + auto& o_vec = _subscribers.at(static_cast(event_type)); + for (auto o_it = o_vec.cbegin(); o_it != o_vec.cend(); o_it++) { + if (*o_it == object) { + o_vec.erase(o_it); + break; + } + } + } + SubscriptionReference newSubRef(EventI* object) { return SubscriptionReference{*this, object}; }