properly add unsub

This commit is contained in:
Green Sky 2024-10-24 12:31:22 +02:00
parent a42ce1a9e0
commit a38c3a3305
No known key found for this signature in database

View File

@ -22,18 +22,12 @@ struct EventProviderI {
~SubscriptionReference(void) { ~SubscriptionReference(void) {
for (const enumType et : _subs) { for (const enumType et : _subs) {
auto& o_vec = _ep._subscribers.at(static_cast<size_t>(et)); _ep.unsubscribe(_object, 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;
}
}
} }
} }
SubscriptionReference& subscribe(const enumType event_type) { SubscriptionReference& subscribe(const enumType event_type) {
_ep._subscribers.at(static_cast<size_t>(event_type)).push_back(_object); _ep.subscribe(_object, event_type);
_subs.push_back(event_type); _subs.push_back(event_type);
return *this; return *this;
} }
@ -41,11 +35,20 @@ struct EventProviderI {
virtual ~EventProviderI(void) {}; virtual ~EventProviderI(void) {};
// TODO: unsub
virtual void subscribe(EventI* object, const enumType event_type) { virtual void subscribe(EventI* object, const enumType event_type) {
_subscribers.at(static_cast<size_t>(event_type)).push_back(object); _subscribers.at(static_cast<size_t>(event_type)).push_back(object);
} }
virtual void unsubscribe(EventI* object, const enumType event_type) {
auto& o_vec = _subscribers.at(static_cast<size_t>(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) { SubscriptionReference newSubRef(EventI* object) {
return SubscriptionReference{*this, object}; return SubscriptionReference{*this, object};
} }