diff options
author | Adam <Adam@anope.org> | 2011-08-25 16:21:21 -0400 |
---|---|---|
committer | Adam <Adam@anope.org> | 2011-09-10 01:58:38 -0400 |
commit | f025d1b495cee3aadb6b7b45652d1cab3b7b7b6d (patch) | |
tree | 02df5a3f12104996af87d1b6c5ec9db9d45b6bde /include | |
parent | 8c4417cad180f3c5448c5b7509b1bf2235e3077e (diff) |
Made service_reference type safe
Diffstat (limited to 'include')
-rw-r--r-- | include/access.h | 2 | ||||
-rw-r--r-- | include/commands.h | 2 | ||||
-rw-r--r-- | include/modules.h | 33 | ||||
-rw-r--r-- | include/oper.h | 2 | ||||
-rw-r--r-- | include/services.h | 33 |
5 files changed, 34 insertions, 38 deletions
diff --git a/include/access.h b/include/access.h index fd2b60356..0c730c9ff 100644 --- a/include/access.h +++ b/include/access.h @@ -44,7 +44,7 @@ enum ChannelAccess class ChanAccess; -class CoreExport AccessProvider : public Service +class CoreExport AccessProvider : public Service<AccessProvider> { public: AccessProvider(Module *o, const Anope::string &n); diff --git a/include/commands.h b/include/commands.h index 227483f63..02a89f416 100644 --- a/include/commands.h +++ b/include/commands.h @@ -62,7 +62,7 @@ struct CoreExport CommandSource /** Every services command is a class, inheriting from Command. */ -class CoreExport Command : public Service, public Flags<CommandFlag> +class CoreExport Command : public Service<Command>, public Flags<CommandFlag> { Anope::string desc; std::vector<Anope::string> syntax; diff --git a/include/modules.h b/include/modules.h index 5ba1be938..a34b7f64d 100644 --- a/include/modules.h +++ b/include/modules.h @@ -1076,16 +1076,10 @@ enum Implementation I_END }; -class Service; - /** Used to manage modules. */ class CoreExport ModuleManager { - private: - /** A map of service providers - */ - static std::map<Anope::string, Service *> ServiceProviders; public: /** Event handler hooks. * This needs to be public to be used by FOREACH_MOD and friends. @@ -1197,29 +1191,6 @@ class CoreExport ModuleManager */ static void UnloadAll(); - /** Register a service - * @param s The service - * @return true if it was successfully registeed, else false (service name colision) - */ - static bool RegisterService(Service *s); - - /** Unregister a service - * @param s The service - * @return true if it was unregistered successfully - */ - static bool UnregisterService(Service *s); - - /** Get a service - * @param name The service name - * @return The services, or NULL - */ - static Service *GetService(const Anope::string &name); - - /** Get the existing service key names - * @return The keys - */ - static std::vector<Anope::string> GetServiceKeys(); - private: /** Call the module_delete function to safely delete the module * @param m the module to delete @@ -1254,7 +1225,7 @@ class service_reference : public dynamic_reference<T> Anope::string name; public: - service_reference(const Anope::string &n) : dynamic_reference<T>(static_cast<T *>(ModuleManager::GetService(n))), name(n) + service_reference(const Anope::string &n) : dynamic_reference<T>(NULL), name(n) { } @@ -1267,7 +1238,7 @@ class service_reference : public dynamic_reference<T> } if (!this->ref) { - this->ref = static_cast<T *>(ModuleManager::GetService(this->name)); + this->ref = Service<T>::FindService(this->name); if (this->ref) this->ref->AddReference(this); } diff --git a/include/oper.h b/include/oper.h index 228d1cf50..8c587794b 100644 --- a/include/oper.h +++ b/include/oper.h @@ -30,7 +30,7 @@ class CoreExport XLine sockaddrs GetIP() const; }; -class CoreExport XLineManager : public Service +class CoreExport XLineManager : public Service<XLineManager> { char type; protected: diff --git a/include/services.h b/include/services.h index 9719ab13e..d6db4e9ed 100644 --- a/include/services.h +++ b/include/services.h @@ -347,17 +347,42 @@ template<typename T, size_t Size = 32> class Flags class Module; -class CoreExport Service : public Base +template<typename T> class CoreExport Service : public Base { + static Anope::map<T *> services; public: + static T* FindService(const Anope::string &n) + { + typename Anope::map<T *>::iterator it = Service<T>::services.find(n); + if (it != Service<T>::services.end()) + return it->second; + return NULL; + } + + static std::vector<Anope::string> GetServiceKeys() + { + std::vector<Anope::string> keys; + for (typename Anope::map<T *>::iterator it = Service<T>::services.begin(), it_end = Service<T>::services.end(); it != it_end; ++it) + keys.push_back(it->first); + return keys; + } + Module *owner; Anope::string name; - Service(Module *o, const Anope::string &n); + Service(Module *o, const Anope::string &n) : owner(o), name(n) + { + if (Service<T>::services.find(n) != Service<T>::services.end()) + throw ModuleException("Service with name " + n + " already exists"); + Service<T>::services[n] = static_cast<T *>(this); + } - virtual ~Service(); + virtual ~Service() + { + Service<T>::services.erase(this->name); + } }; - +template<typename T> Anope::map<T *> Service<T>::services; #include "sockets.h" #include "socketengine.h" |