diff options
author | Peter Powell <petpow@saberuk.com> | 2019-10-19 17:26:30 +0100 |
---|---|---|
committer | Peter Powell <petpow@saberuk.com> | 2019-10-21 12:06:37 +0100 |
commit | 318b40dd70c769e1909f6acc7c8edea4d6e344dc (patch) | |
tree | 34c407068c0911ee445f461da1fd4e797d8e63c2 /include/event.h | |
parent | 274f40653f48f47b71cb3b653f75727c6c2dc912 (diff) |
Allow event providers to know when a listener subs or unsubs.
Diffstat (limited to 'include/event.h')
-rw-r--r-- | include/event.h | 32 |
1 files 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. */ |