From a39bd397cd0052dd8e23ba5c4c5dfc9b3a11a70c Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Mon, 23 Nov 2015 12:46:59 +0100 Subject: Extract code that manipulates the ModuleManager::DataProviders map into methods --- include/modules.h | 11 +++++++++++ src/modules.cpp | 30 +++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/include/modules.h b/include/modules.h index c938e6a9d..378aec1ee 100644 --- a/include/modules.h +++ b/include/modules.h @@ -1215,6 +1215,17 @@ class CoreExport ModuleManager : public fakederef * @return A ModuleMap containing all loaded modules */ const ModuleMap& GetModules() const { return Modules; } + + /** Make a service referenceable by dynamic_references + * @param name Name that will be used by dynamic_references to find the object + * @param service Service to make referenceable by dynamic_references + */ + void AddReferent(const std::string& name, ServiceProvider* service); + + /** Make a service no longer referenceable by dynamic_references + * @param service Service to make no longer referenceable by dynamic_references + */ + void DelReferent(ServiceProvider* service); }; /** Do not mess with these functions unless you know the C preprocessor diff --git a/src/modules.cpp b/src/modules.cpp index 9e653a4ab..0223d12f1 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -564,8 +564,7 @@ void ModuleManager::AddService(ServiceProvider& item) { ModeHandler* mh = static_cast(&item); ServerInstance->Modes->AddMode(mh); - DataProviders.insert(std::make_pair((mh->GetModeType() == MODETYPE_CHANNEL ? "mode/" : "umode/") + item.name, &item)); - dynamic_reference_base::reset_all(); + AddReferent((mh->GetModeType() == MODETYPE_CHANNEL ? "mode/" : "umode/") + item.name, &item); return; } case SERVICE_METADATA: @@ -604,13 +603,7 @@ void ModuleManager::DelService(ServiceProvider& item) case SERVICE_DATA: case SERVICE_IOHOOK: { - for(std::multimap::iterator i = DataProviders.begin(); i != DataProviders.end(); ) - { - std::multimap::iterator curr = i++; - if (curr->second == &item) - DataProviders.erase(curr); - } - dynamic_reference_base::reset_all(); + DelReferent(&item); return; } default: @@ -702,3 +695,22 @@ Module* ModuleManager::Find(const std::string &name) else return modfind->second; } + +void ModuleManager::AddReferent(const std::string& name, ServiceProvider* service) +{ + DataProviders.insert(std::make_pair(name, service)); + dynamic_reference_base::reset_all(); +} + +void ModuleManager::DelReferent(ServiceProvider* service) +{ + for (std::multimap::iterator i = DataProviders.begin(); i != DataProviders.end(); ) + { + ServiceProvider* curr = i->second; + if (curr == service) + DataProviders.erase(i++); + else + ++i; + } + dynamic_reference_base::reset_all(); +} -- cgit v1.2.3 From 820f229a9efe2678d1e85578aa086a1efdd73a30 Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Mon, 23 Nov 2015 12:50:02 +0100 Subject: Add ServiceProvider::RegisterService(), call it from ModuleManager::AddService() --- include/base.h | 4 ++++ src/base.cpp | 4 ++++ src/modules.cpp | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/base.h b/include/base.h index dcbb2e5c7..8aa6269d3 100644 --- a/include/base.h +++ b/include/base.h @@ -251,6 +251,10 @@ class CoreExport ServiceProvider : public classbase ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type); virtual ~ServiceProvider(); + /** Register this service in the appropriate registrar + */ + virtual void RegisterService(); + /** If called, this ServiceProvider won't be registered automatically */ void DisableAutoRegister(); diff --git a/src/base.cpp b/src/base.cpp index 67b136ec8..63b14256e 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -95,6 +95,10 @@ ServiceProvider::~ServiceProvider() { } +void ServiceProvider::RegisterService() +{ +} + ExtensionItem::ExtensionItem(const std::string& Key, ExtensibleType exttype, Module* mod) : ServiceProvider(mod, Key, SERVICE_METADATA) , type(exttype) diff --git a/src/modules.cpp b/src/modules.cpp index 0223d12f1..3ff8aad14 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -588,7 +588,7 @@ void ModuleManager::AddService(ServiceProvider& item) return; } default: - throw ModuleException("Cannot add unknown service type"); + item.RegisterService(); } } -- cgit v1.2.3 From 42de16029da37f08ce1827352aa78b4b29d888cb Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Mon, 23 Nov 2015 12:50:35 +0100 Subject: Implement Command::RegisterService() --- include/ctables.h | 4 ++++ src/command_parse.cpp | 6 ++++++ src/modules.cpp | 4 ---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/ctables.h b/include/ctables.h index abf65f561..bc4226ea9 100644 --- a/include/ctables.h +++ b/include/ctables.h @@ -218,6 +218,10 @@ class CoreExport Command : public CommandBase */ virtual CmdResult Handle(const std::vector& parameters, User* user) = 0; + /** Register this object in the CommandParser + */ + void RegisterService() CXX11_OVERRIDE; + /** Destructor * Removes this command from the command parser */ diff --git a/src/command_parse.cpp b/src/command_parse.cpp index 7998d9cc3..c123353b0 100644 --- a/src/command_parse.cpp +++ b/src/command_parse.cpp @@ -379,6 +379,12 @@ Command::~Command() ServerInstance->Parser.RemoveCommand(this); } +void Command::RegisterService() +{ + if (!ServerInstance->Parser.AddCommand(this)) + throw ModuleException("Command already exists: " + name); +} + void CommandParser::ProcessBuffer(std::string &buffer,LocalUser *user) { if (buffer.empty()) diff --git a/src/modules.cpp b/src/modules.cpp index 3ff8aad14..d28657ca8 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -556,10 +556,6 @@ void ModuleManager::AddService(ServiceProvider& item) { switch (item.service) { - case SERVICE_COMMAND: - if (!ServerInstance->Parser.AddCommand(static_cast(&item))) - throw ModuleException("Command "+std::string(item.name)+" already exists."); - return; case SERVICE_MODE: { ModeHandler* mh = static_cast(&item); -- cgit v1.2.3 From 6fc00fa62905e5990fa9667f540e10a775832540 Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Mon, 23 Nov 2015 12:51:00 +0100 Subject: Implement ModeHandler::RegisterService() --- include/mode.h | 5 +++++ src/mode.cpp | 6 ++++++ src/modules.cpp | 7 ------- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/mode.h b/include/mode.h index eebfbedd6..d8884bf9f 100644 --- a/include/mode.h +++ b/include/mode.h @@ -168,6 +168,11 @@ class CoreExport ModeHandler : public ServiceProvider ModeHandler(Module* me, const std::string& name, char modeletter, ParamSpec params, ModeType type, Class mclass = MC_OTHER); virtual CullResult cull(); virtual ~ModeHandler(); + + /** Register this object in the ModeParser + */ + void RegisterService() CXX11_OVERRIDE; + /** * Returns true if the mode is a list mode */ diff --git a/src/mode.cpp b/src/mode.cpp index 671b5d854..6038f6f5b 100644 --- a/src/mode.cpp +++ b/src/mode.cpp @@ -90,6 +90,12 @@ bool ModeHandler::ResolveModeConflict(std::string& theirs, const std::string& ou return (theirs < ours); } +void ModeHandler::RegisterService() +{ + ServerInstance->Modes.AddMode(this); + ServerInstance->Modules.AddReferent((GetModeType() == MODETYPE_CHANNEL ? "mode/" : "umode/") + name, this); +} + ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding) { /* We're either trying to add a mode we already have or diff --git a/src/modules.cpp b/src/modules.cpp index d28657ca8..f5998f79c 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -556,13 +556,6 @@ void ModuleManager::AddService(ServiceProvider& item) { switch (item.service) { - case SERVICE_MODE: - { - ModeHandler* mh = static_cast(&item); - ServerInstance->Modes->AddMode(mh); - AddReferent((mh->GetModeType() == MODETYPE_CHANNEL ? "mode/" : "umode/") + item.name, &item); - return; - } case SERVICE_METADATA: if (!ServerInstance->Extensions.Register(static_cast(&item))) throw ModuleException("Extension " + std::string(item.name) + " already exists."); -- cgit v1.2.3 From f3667d995e439a1d75fd9b56b29b610f3d877d8b Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Mon, 23 Nov 2015 12:52:03 +0100 Subject: Implement ExtensionItem::RegisterService() --- include/extensible.h | 4 ++++ src/base.cpp | 6 ++++++ src/modules.cpp | 4 ---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/extensible.h b/include/extensible.h index a2c104377..3453f3e15 100644 --- a/include/extensible.h +++ b/include/extensible.h @@ -69,6 +69,10 @@ class CoreExport ExtensionItem : public ServiceProvider, public usecountbase /** Free the item */ virtual void free(void* item) = 0; + /** Register this object in the ExtensionManager + */ + void RegisterService() CXX11_OVERRIDE; + protected: /** Get the item from the internal map */ void* get_raw(const Extensible* container) const; diff --git a/src/base.cpp b/src/base.cpp index 63b14256e..4111451f8 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -144,6 +144,12 @@ void* ExtensionItem::unset_raw(Extensible* container) return rv; } +void ExtensionItem::RegisterService() +{ + if (!ServerInstance->Extensions.Register(this)) + throw ModuleException("Extension already exists: " + name); +} + bool ExtensionManager::Register(ExtensionItem* item) { return types.insert(std::make_pair(item->name, item)).second; diff --git a/src/modules.cpp b/src/modules.cpp index f5998f79c..ca13dad73 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -556,10 +556,6 @@ void ModuleManager::AddService(ServiceProvider& item) { switch (item.service) { - case SERVICE_METADATA: - if (!ServerInstance->Extensions.Register(static_cast(&item))) - throw ModuleException("Extension " + std::string(item.name) + " already exists."); - return; case SERVICE_DATA: case SERVICE_IOHOOK: { -- cgit v1.2.3 From 90ab8cf4258a25e8403465ed86296fc74405620d Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Mon, 23 Nov 2015 12:52:24 +0100 Subject: Add SERVICE_CUSTOM for services managed by a module --- include/base.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/base.h b/include/base.h index 8aa6269d3..c378afc1c 100644 --- a/include/base.h +++ b/include/base.h @@ -235,7 +235,9 @@ enum ServiceType { /** is a data processing provider (MD5, SQL) */ SERVICE_DATA, /** is an I/O hook provider (SSL) */ - SERVICE_IOHOOK + SERVICE_IOHOOK, + /** Service managed by a module */ + SERVICE_CUSTOM }; /** A structure defining something that a module can provide */ -- cgit v1.2.3