diff options
author | Adam <Adam@anope.org> | 2012-11-22 00:50:33 -0500 |
---|---|---|
committer | Adam <Adam@anope.org> | 2012-11-22 00:50:33 -0500 |
commit | d33a0f75a5c0c584fbb7cc0076da36d494f39494 (patch) | |
tree | 7b2274cc833c793c0f5595660cbd4d715de52ffd /include/service.h | |
parent | 368d469631763e9c8bf399980d0ac7c5b5664d39 (diff) |
Pretty large coding style cleanup, in source doc
cleanup, and allow protocol mods to depend on each
other
Diffstat (limited to 'include/service.h')
-rw-r--r-- | include/service.h | 84 |
1 files changed, 70 insertions, 14 deletions
diff --git a/include/service.h b/include/service.h index 95c650076..f2f553eb2 100644 --- a/include/service.h +++ b/include/service.h @@ -1,4 +1,5 @@ /* + * * (C) 2003-2012 Anope Team * Contact us at team@anope.org * @@ -6,6 +7,7 @@ * * Based on the original code of Epona by Lara. * Based on the original code of Services by Andy Church. + * */ #ifndef SERVICE_H @@ -15,18 +17,34 @@ #include "anope.h" #include "modules.h" +/** Anything that inherits from this class can be referred to + * using ServiceReference. Any interfaces provided by modules, + * such as commands, use this. This is also used for modules + * that publish a service (m_ssl, etc). + */ class CoreExport Service : public virtual Base { - static std::map<Anope::string, std::map<Anope::string, Service *> > services; + static std::map<Anope::string, std::map<Anope::string, Service *> > Services; + static std::map<Anope::string, std::map<Anope::string, Anope::string> > Aliases; public: static Service *FindService(const Anope::string &t, const Anope::string &n) { - std::map<Anope::string, std::map<Anope::string, Service *> >::iterator it = services.find(t); - if (it != services.end()) + std::map<Anope::string, std::map<Anope::string, Service *> >::iterator it = Services.find(t); + if (it != Services.end()) { - std::map<Anope::string, Service *>::iterator it2 = it->second.find(n); - if (it2 != it->second.end()) - return it2->second; + Anope::string name = n; + + std::map<Anope::string, std::map<Anope::string, Anope::string> >::iterator it2 = Aliases.find(t); + if (it2 != Aliases.end()) + { + std::map<Anope::string, Anope::string>::iterator it3 = it2->second.find(n); + if (it3 != it2->second.end()) + name = it3->second; + } + + std::map<Anope::string, Service *>::iterator it4 = it->second.find(name); + if (it4 != it->second.end()) + return it4->second; } return NULL; @@ -35,15 +53,31 @@ class CoreExport Service : public virtual Base static std::vector<Anope::string> GetServiceKeys(const Anope::string &t) { std::vector<Anope::string> keys; - std::map<Anope::string, std::map<Anope::string, Service *> >::iterator it = services.find(t); - if (it != services.end()) + std::map<Anope::string, std::map<Anope::string, Service *> >::iterator it = Services.find(t); + if (it != Services.end()) for (std::map<Anope::string, Service *>::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) keys.push_back(it2->first); return keys; } + static void AddAlias(const Anope::string &t, const Anope::string &n, const Anope::string &v) + { + std::map<Anope::string, Anope::string> &smap = Aliases[t]; + smap[n] = v; + } + + static void DelAlias(const Anope::string &t, const Anope::string &n) + { + std::map<Anope::string, Anope::string> &smap = Aliases[t]; + smap.erase(n); + if (smap.empty()) + Aliases.erase(t); + } + Module *owner; + /* Service type, which should be the class name (eg "Command") */ Anope::string type; + /* Service name, commands are usually named service/command */ Anope::string name; Service(Module *o, const Anope::string &t, const Anope::string &n) : owner(o), type(t), name(n) @@ -58,7 +92,7 @@ class CoreExport Service : public virtual Base void Register() { - std::map<Anope::string, Service *> &smap = services[this->type]; + std::map<Anope::string, Service *> &smap = Services[this->type]; if (smap.find(this->name) != smap.end()) throw ModuleException("Service " + this->type + " with name " + this->name + " already exists"); smap[this->name] = this; @@ -66,29 +100,32 @@ class CoreExport Service : public virtual Base void Unregister() { - std::map<Anope::string, Service *> &smap = services[this->type]; + std::map<Anope::string, Service *> &smap = Services[this->type]; smap.erase(this->name); if (smap.empty()) - services.erase(this->type); + Services.erase(this->type); } }; +/** Like Reference, but used to refer to Services. + */ template<typename T> -class service_reference : public dynamic_reference<T> +class ServiceReference : public Reference<T> { Anope::string type; Anope::string name; public: - service_reference() : dynamic_reference<T>(NULL) { } + ServiceReference() { } - service_reference(const Anope::string &t, const Anope::string &n) : dynamic_reference<T>(NULL), type(t), name(n) + ServiceReference(const Anope::string &t, const Anope::string &n) : type(t), name(n) { } inline void operator=(const Anope::string &n) { this->name = n; + this->invalid = true; } operator bool() anope_override @@ -100,6 +137,10 @@ class service_reference : public dynamic_reference<T> } if (!this->ref) { + /* This really could be dynamic_cast in every case, except for when a module + * creates its own service type (that other modules must include the header file + * for), as the core is not compiled with it so there is no RTTI for it. + */ this->ref = static_cast<T *>(Service::FindService(this->type, this->name)); if (this->ref) this->ref->AddReference(this); @@ -108,5 +149,20 @@ class service_reference : public dynamic_reference<T> } }; +class ServiceAlias +{ + Anope::string t, f; + public: + ServiceAlias(const Anope::string &type, const Anope::string &from, const Anope::string &to) : t(type), f(from) + { + Service::AddAlias(type, from, to); + } + + ~ServiceAlias() + { + Service::DelAlias(t, f); + } +}; + #endif // SERVICE_H |