From 318b40dd70c769e1909f6acc7c8edea4d6e344dc Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Sat, 19 Oct 2019 17:26:30 +0100 Subject: Allow event providers to know when a listener subs or unsubs. --- include/event.h | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/include/event.h b/include/event.h index b00e44e38..806b55309 100644 --- a/include/event.h +++ b/include/event.h @@ -57,7 +57,23 @@ class Events::ModuleEventProvider : public ServiceProvider, private dynamic_refe */ const SubscriberList& GetSubscribers() const { return prov->subscribers; } - friend class ModuleEventListener; + /** Subscribes a listener to this event. + * @param subscriber The listener to subscribe. + */ + void Subscribe(ModuleEventListener* subscriber) + { + subscribers.insert(subscriber); + OnSubscribe(subscriber); + } + + /** Unsubscribes a listener from this event. + * @param subscriber The listener to unsubscribe. + */ + void Unsubscribe(ModuleEventListener* subscriber) + { + subscribers.erase(subscriber); + OnUnsubscribe(subscriber); + } private: void OnCapture() CXX11_OVERRIDE @@ -67,6 +83,16 @@ class Events::ModuleEventProvider : public ServiceProvider, private dynamic_refe subscribers.clear(); } + /** Called when a listener subscribes to this event. + * @param subscriber The listener which subscribed. + */ + virtual void OnSubscribe(ModuleEventListener* subscriber) { } + + /** Called when a listener unsubscribes from this event. + * @param subscriber The listener which unsubscribed. + */ + virtual void OnUnsubscribe(ModuleEventListener* subscriber) { } + /** Reference to the active provider for this event. In case multiple event providers * exist for the same event, only one of them contains the list of subscribers. * To handle the case when we are not the ones with the list, we get it from the provider @@ -95,7 +121,7 @@ class Events::ModuleEventListener : private dynamic_reference_base::CaptureHook */ void OnCapture() CXX11_OVERRIDE { - prov->subscribers.insert(this); + prov->Subscribe(this); } public: @@ -119,7 +145,7 @@ class Events::ModuleEventListener : private dynamic_reference_base::CaptureHook ~ModuleEventListener() { if (prov) - prov->subscribers.erase(this); + prov->Unsubscribe(this); } /** Retrieves the module which created this listener. */ -- cgit v1.2.3