service.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  *
00011  */
00012 
00013 #ifndef SERVICE_H
00014 #define SERVICE_H
00015 
00016 #include "services.h"
00017 #include "anope.h"
00018 #include "modules.h"
00019 
00025 class CoreExport Service : public virtual Base
00026 {
00027         static std::map<Anope::string, std::map<Anope::string, Service *> > Services;
00028         static std::map<Anope::string, std::map<Anope::string, Anope::string> > Aliases;
00029 
00030         static Service *FindService(const std::map<Anope::string, Service *> &services, const std::map<Anope::string, Anope::string> *aliases, const Anope::string &n)
00031         {
00032                 std::map<Anope::string, Service *>::const_iterator it = services.find(n);
00033                 if (it != services.end())
00034                         return it->second;
00035 
00036                 if (aliases != NULL)
00037                 {
00038                         std::map<Anope::string, Anope::string>::const_iterator it2 = aliases->find(n);
00039                         if (it2 != aliases->end())
00040                                 return FindService(services, aliases, it2->second);
00041                 }
00042 
00043                 return NULL;
00044         }
00045 
00046  public:
00047         static Service *FindService(const Anope::string &t, const Anope::string &n)
00048         {
00049                 std::map<Anope::string, std::map<Anope::string, Service *> >::const_iterator it = Services.find(t);
00050                 if (it == Services.end())
00051                         return NULL;
00052 
00053                 std::map<Anope::string, std::map<Anope::string, Anope::string> >::const_iterator it2 = Aliases.find(t);
00054                 if (it2 != Aliases.end())
00055                         return FindService(it->second, &it2->second, n);
00056 
00057                 return FindService(it->second, NULL, n);
00058         }
00059 
00060         static std::vector<Anope::string> GetServiceKeys(const Anope::string &t)
00061         {
00062                 std::vector<Anope::string> keys;
00063                 std::map<Anope::string, std::map<Anope::string, Service *> >::iterator it = Services.find(t);
00064                 if (it != Services.end())
00065                         for (std::map<Anope::string, Service *>::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
00066                                 keys.push_back(it2->first);
00067                 return keys;
00068         }
00069 
00070         static void AddAlias(const Anope::string &t, const Anope::string &n, const Anope::string &v)
00071         {
00072                 std::map<Anope::string, Anope::string> &smap = Aliases[t];
00073                 smap[n] = v;
00074         }
00075 
00076         static void DelAlias(const Anope::string &t, const Anope::string &n)
00077         {
00078                 std::map<Anope::string, Anope::string> &smap = Aliases[t];
00079                 smap.erase(n);
00080                 if (smap.empty())
00081                         Aliases.erase(t);
00082         }
00083 
00084         Module *owner;
00085         /* Service type, which should be the class name (eg "Command") */
00086         Anope::string type;
00087         /* Service name, commands are usually named service/command */
00088         Anope::string name;
00089 
00090         Service(Module *o, const Anope::string &t, const Anope::string &n) : owner(o), type(t), name(n)
00091         {
00092                 this->Register();
00093         }
00094 
00095         virtual ~Service()
00096         {
00097                 this->Unregister();
00098         }
00099 
00100         void Register()
00101         {
00102                 std::map<Anope::string, Service *> &smap = Services[this->type];
00103                 if (smap.find(this->name) != smap.end())
00104                         throw ModuleException("Service " + this->type + " with name " + this->name + " already exists");
00105                 smap[this->name] = this;
00106         }
00107 
00108         void Unregister()
00109         {
00110                 std::map<Anope::string, Service *> &smap = Services[this->type];
00111                 smap.erase(this->name);
00112                 if (smap.empty())
00113                         Services.erase(this->type);
00114         }
00115 };
00116 
00119 template<typename T>
00120 class ServiceReference : public Reference<T>
00121 {
00122         Anope::string type;
00123         Anope::string name;
00124 
00125  public:
00126         ServiceReference() { }
00127 
00128         ServiceReference(const Anope::string &t, const Anope::string &n) : type(t), name(n)
00129         {
00130         }
00131 
00132         inline void operator=(const Anope::string &n)
00133         {
00134                 this->name = n;
00135                 this->invalid = true;
00136         }
00137 
00138         operator bool() anope_override
00139         {
00140                 if (this->invalid)
00141                 {
00142                         this->invalid = false;
00143                         this->ref = NULL;
00144                 }
00145                 if (!this->ref)
00146                 {
00147                         /* This really could be dynamic_cast in every case, except for when a module
00148                          * creates its own service type (that other modules must include the header file
00149                          * for), as the core is not compiled with it so there is no RTTI for it.
00150                          */
00151                         this->ref = static_cast<T *>(Service::FindService(this->type, this->name));
00152                         if (this->ref)
00153                                 this->ref->AddReference(this);
00154                 }
00155                 return this->ref;
00156         }
00157 };
00158 
00159 class ServiceAlias
00160 {
00161         Anope::string t, f;
00162  public:
00163         ServiceAlias(const Anope::string &type, const Anope::string &from, const Anope::string &to) : t(type), f(from)
00164         {
00165                 Service::AddAlias(type, from, to);
00166         }
00167 
00168         ~ServiceAlias()
00169         {
00170                 Service::DelAlias(t, f);
00171         }
00172 };
00173 
00174 #endif // SERVICE_H
00175