Anope IRC Services  Version 2.0
service.h
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  */
12 
13 #ifndef SERVICE_H
14 #define SERVICE_H
15 
16 #include "services.h"
17 #include "anope.h"
18 #include "base.h"
19 
25 class CoreExport Service : public virtual Base
26 {
27  static std::map<Anope::string, std::map<Anope::string, Service *> > Services;
28  static std::map<Anope::string, std::map<Anope::string, Anope::string> > Aliases;
29 
30  static Service *FindService(const std::map<Anope::string, Service *> &services, const std::map<Anope::string, Anope::string> *aliases, const Anope::string &n)
31  {
32  std::map<Anope::string, Service *>::const_iterator it = services.find(n);
33  if (it != services.end())
34  return it->second;
35 
36  if (aliases != NULL)
37  {
38  std::map<Anope::string, Anope::string>::const_iterator it2 = aliases->find(n);
39  if (it2 != aliases->end())
40  return FindService(services, aliases, it2->second);
41  }
42 
43  return NULL;
44  }
45 
46  public:
47  static Service *FindService(const Anope::string &t, const Anope::string &n)
48  {
49  std::map<Anope::string, std::map<Anope::string, Service *> >::const_iterator it = Services.find(t);
50  if (it == Services.end())
51  return NULL;
52 
53  std::map<Anope::string, std::map<Anope::string, Anope::string> >::const_iterator it2 = Aliases.find(t);
54  if (it2 != Aliases.end())
55  return FindService(it->second, &it2->second, n);
56 
57  return FindService(it->second, NULL, n);
58  }
59 
60  static std::vector<Anope::string> GetServiceKeys(const Anope::string &t)
61  {
62  std::vector<Anope::string> keys;
63  std::map<Anope::string, std::map<Anope::string, Service *> >::iterator it = Services.find(t);
64  if (it != Services.end())
65  for (std::map<Anope::string, Service *>::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
66  keys.push_back(it2->first);
67  return keys;
68  }
69 
70  static void AddAlias(const Anope::string &t, const Anope::string &n, const Anope::string &v)
71  {
72  std::map<Anope::string, Anope::string> &smap = Aliases[t];
73  smap[n] = v;
74  }
75 
76  static void DelAlias(const Anope::string &t, const Anope::string &n)
77  {
78  std::map<Anope::string, Anope::string> &smap = Aliases[t];
79  smap.erase(n);
80  if (smap.empty())
81  Aliases.erase(t);
82  }
83 
85  /* Service type, which should be the class name (eg "Command") */
87  /* Service name, commands are usually named service/command */
89 
90  Service(Module *o, const Anope::string &t, const Anope::string &n) : owner(o), type(t), name(n)
91  {
92  this->Register();
93  }
94 
95  virtual ~Service()
96  {
97  this->Unregister();
98  }
99 
100  void Register()
101  {
102  std::map<Anope::string, Service *> &smap = Services[this->type];
103  if (smap.find(this->name) != smap.end())
104  throw ModuleException("Service " + this->type + " with name " + this->name + " already exists");
105  smap[this->name] = this;
106  }
107 
108  void Unregister()
109  {
110  std::map<Anope::string, Service *> &smap = Services[this->type];
111  smap.erase(this->name);
112  if (smap.empty())
113  Services.erase(this->type);
114  }
115 };
116 
119 template<typename T>
120 class ServiceReference : public Reference<T>
121 {
124 
125  public:
127 
128  ServiceReference(const Anope::string &t, const Anope::string &n) : type(t), name(n)
129  {
130  }
131 
132  inline void operator=(const Anope::string &n)
133  {
134  this->name = n;
135  this->invalid = true;
136  }
137 
138  operator bool() anope_override
139  {
140  if (this->invalid)
141  {
142  this->invalid = false;
143  this->ref = NULL;
144  }
145  if (!this->ref)
146  {
147  /* This really could be dynamic_cast in every case, except for when a module
148  * creates its own service type (that other modules must include the header file
149  * for), as the core is not compiled with it so there is no RTTI for it.
150  */
151  this->ref = static_cast<T *>(::Service::FindService(this->type, this->name));
152  if (this->ref)
153  this->ref->AddReference(this);
154  }
155  return this->ref;
156  }
157 };
158 
160 {
162  public:
163  ServiceAlias(const Anope::string &type, const Anope::string &from, const Anope::string &to) : t(type), f(from)
164  {
165  Service::AddAlias(type, from, to);
166  }
167 
169  {
170  Service::DelAlias(t, f);
171  }
172 };
173 
174 #endif // SERVICE_H
175 
Anope::string t
Definition: service.h:161
Definition: base.h:17
Anope::string type
Definition: service.h:122
T * ref
Definition: base.h:52
static std::map< Anope::string, std::map< Anope::string, Service * > > Services
Definition: service.h:27
static Service * FindService(const std::map< Anope::string, Service * > &services, const std::map< Anope::string, Anope::string > *aliases, const Anope::string &n)
Definition: service.h:30
virtual ~Service()
Definition: service.h:95
Definition: base.h:49
static std::vector< Anope::string > GetServiceKeys(const Anope::string &t)
Definition: service.h:60
Service(Module *o, const Anope::string &t, const Anope::string &n)
Definition: service.h:90
Anope::string type
Definition: service.h:86
static void AddAlias(const Anope::string &t, const Anope::string &n, const Anope::string &v)
Definition: service.h:70
Anope::string name
Definition: service.h:123
void operator=(const Anope::string &n)
Definition: service.h:132
#define anope_override
Definition: services.h:56
static void DelAlias(const Anope::string &t, const Anope::string &n)
Definition: service.h:76
Anope::string f
Definition: service.h:161
#define CoreExport
Definition: services.h:62
Anope::string name
Definition: service.h:88
void Unregister()
Definition: service.h:108
void Register()
Definition: service.h:100
Anope::string name
Definition: access.cpp:22
ServiceReference(const Anope::string &t, const Anope::string &n)
Definition: service.h:128
static std::map< Anope::string, std::map< Anope::string, Anope::string > > Aliases
Definition: service.h:28
ServiceAlias(const Anope::string &type, const Anope::string &from, const Anope::string &to)
Definition: service.h:163
~ServiceAlias()
Definition: service.h:168
static Service * FindService(const Anope::string &t, const Anope::string &n)
Definition: service.h:47
bool invalid
Definition: base.h:37
Module * owner
Definition: service.h:84