diff options
| author | 2020-11-10 23:14:21 +0000 | |
|---|---|---|
| committer | 2020-11-10 23:14:21 +0000 | |
| commit | ba3dd9cedcca2f3cc6781f09410e3cf2cf696e43 (patch) | |
| tree | 86fee6a331b262932973ac453169a1b566d60a2a /include | |
| parent | Convert FIRST_MOD_RESULT_CUSTOM to a variadic function. (diff) | |
Convert FOREACH_MOD_CUSTOM to a variadic function.
Diffstat (limited to 'include')
| -rw-r--r-- | include/event.h | 34 | ||||
| -rw-r--r-- | include/modules/server.h | 4 |
2 files changed, 20 insertions, 18 deletions
diff --git a/include/event.h b/include/event.h index af75f4495..f27f69409 100644 --- a/include/event.h +++ b/include/event.h @@ -83,6 +83,10 @@ class Events::ModuleEventProvider OnUnsubscribe(subscriber); } + /** Run the given hook provided by a module. */ + template<typename Class, typename... FunArgs, typename... FwdArgs> + inline void Call(void (Class::*function)(FunArgs...), FwdArgs&&... args) const; + /** * Run the given hook provided by a module until some module returns MOD_RES_ALLOW or MOD_RES_DENY. * If no module does that, result is set to MOD_RES_PASSTHRU. @@ -184,27 +188,25 @@ inline bool Events::ModuleEventProvider::ElementComp::operator()(Events::ModuleE return std::less<ModuleEventListener*>()(lhs, rhs); } -/** - * Run the given hook provided by a module - * - * FOREACH_MOD_CUSTOM(accountevprov, AccountEventListener, OnAccountChange, MOD_RESULT, (user, newaccount)) - */ -#define FOREACH_MOD_CUSTOM(prov, listenerclass, func, params) do { \ - const ::Events::ModuleEventProvider::SubscriberList& _handlers = (prov).GetSubscribers(); \ - for (::Events::ModuleEventProvider::SubscriberList::const_iterator _i = _handlers.begin(); _i != _handlers.end(); ++_i) \ - { \ - listenerclass* _t = static_cast<listenerclass*>(*_i); \ - const Module* _m = _t->GetModule(); \ - if (_m && !_m->dying) \ - _t->func params ; \ - } \ -} while (0); +template<typename Class, typename... FunArgs, typename... FwdArgs> +inline void Events::ModuleEventProvider::Call(void (Class::*function)(FunArgs...), FwdArgs&&... args) const +{ + for (const auto& subscriber : subscribers) + { + const Module* mod = subscriber->GetModule(); + if (!mod || mod->dying) + continue; + + Class* klass = static_cast<Class*>(subscriber); + (klass->*function)(std::forward<FwdArgs>(args)...); + } +} template<typename Class, typename... FunArgs, typename... FwdArgs> inline ModResult Events::ModuleEventProvider::FirstResult(ModResult (Class::*function)(FunArgs...), FwdArgs&&... args) const { ModResult result; - for (ModuleEventListener* subscriber : subscribers) + for (const auto& subscriber : subscribers) { const Module* mod = subscriber->GetModule(); if (!mod || mod->dying) diff --git a/include/modules/server.h b/include/modules/server.h index d1b68e003..3ed06c355 100644 --- a/include/modules/server.h +++ b/include/modules/server.h @@ -91,14 +91,14 @@ class ServerProtocol::MessageEventListener * @param name The name of the command which was sent. * @param tags The tags which will be sent with the message. */ - virtual void OnBuildMessage(User* source, const char* name, ClientProtocol::TagMap& tags) { } + virtual void OnBuildUserMessage(User* source, const char* name, ClientProtocol::TagMap& tags) { } /** Fired when a server message is being sent by a server. * @param source The server who sent the message. * @param name The name of the command which was sent. * @param tags The tags which will be sent with the message. */ - virtual void OnBuildMessage(Server* source, const char* name, ClientProtocol::TagMap& tags) { } + virtual void OnBuildServerMessage(Server* source, const char* name, ClientProtocol::TagMap& tags) { } }; class ServerProtocol::SyncEventListener |
