serviceregistry.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_SERVICEREGISTRY_H
30 #define CXXTOOLS_SERVICEREGISTRY_H
31 
32 #include <cxxtools/api.h>
34 #include <cxxtools/callable.h>
35 #include <string>
36 #include <vector>
37 #include <map>
38 
39 namespace cxxtools
40 {
42  {
44  ServiceRegistry& operator=(const ServiceRegistry&) { return *this; }
45 
46  public:
48  { }
49 
50  ~ServiceRegistry();
51 
52  template <typename R>
53  void registerFunction(const std::string& name, R (*fn)())
54  {
55  ServiceProcedure* proc = new BasicServiceProcedure<R>(callable(fn));
56  this->registerProcedure(name, proc);
57  }
58 
59  template <typename R, typename A1>
60  void registerFunction(const std::string& name, R (*fn)(A1))
61  {
62  ServiceProcedure* proc = new BasicServiceProcedure<R, A1>(callable(fn));
63  this->registerProcedure(name, proc);
64  }
65 
66  template <typename R, typename A1, typename A2>
67  void registerFunction(const std::string& name, R (*fn)(A1, A2))
68  {
69  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2>(callable(fn));
70  this->registerProcedure(name, proc);
71  }
72 
73  template <typename R, typename A1, typename A2, typename A3>
74  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3))
75  {
77  this->registerProcedure(name, proc);
78  }
79 
80  template <typename R, typename A1, typename A2, typename A3, typename A4>
81  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4))
82  {
84  this->registerProcedure(name, proc);
85  }
86 
87  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
88  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5))
89  {
91  this->registerProcedure(name, proc);
92  }
93 
94  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
95  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6))
96  {
98  this->registerProcedure(name, proc);
99  }
100 
101  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
102  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7))
103  {
105  this->registerProcedure(name, proc);
106  }
107 
108  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
109  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7, A8))
110  {
112  this->registerProcedure(name, proc);
113  }
114 
115  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
116  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7, A8, A9))
117  {
119  this->registerProcedure(name, proc);
120  }
121 
122  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
123  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10))
124  {
126  this->registerProcedure(name, proc);
127  }
128 
129  template <typename R>
130  void registerCallable(const std::string& name, const Callable<R>& cb)
131  {
133  this->registerProcedure(name, proc);
134  }
135 
136  template <typename R, typename A1>
137  void registerCallable(const std::string& name, const Callable<R, A1>& cb)
138  {
140  this->registerProcedure(name, proc);
141  }
142 
143  template <typename R, typename A1, typename A2>
144  void registerCallable(const std::string& name, const Callable<R, A1, A2>& cb)
145  {
147  this->registerProcedure(name, proc);
148  }
149 
150  template <typename R, typename A1, typename A2, typename A3>
151  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3>& cb)
152  {
154  this->registerProcedure(name, proc);
155  }
156 
157  template <typename R, typename A1, typename A2, typename A3, typename A4>
158  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4>& cb)
159  {
161  this->registerProcedure(name, proc);
162  }
163 
164  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
165  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5>& cb)
166  {
168  this->registerProcedure(name, proc);
169  }
170 
171  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
172  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6>& cb)
173  {
175  this->registerProcedure(name, proc);
176  }
177 
178  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
179  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7>& cb)
180  {
182  this->registerProcedure(name, proc);
183  }
184 
185  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
186  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7, A8>& cb)
187  {
189  this->registerProcedure(name, proc);
190  }
191 
192  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
193  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7, A8, A9>& cb)
194  {
196  this->registerProcedure(name, proc);
197  }
198 
199  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
200  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>& cb)
201  {
203  this->registerProcedure(name, proc);
204  }
205 
206  template <typename R, class C>
207  void registerMethod(const std::string& name, C& obj, R (C::*method)() )
208  {
209  ServiceProcedure* proc = new BasicServiceProcedure<R>( callable(obj, method) );
210  this->registerProcedure(name, proc);
211  }
212 
213  template <typename R, class C, typename A1>
214  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1) )
215  {
216  ServiceProcedure* proc = new BasicServiceProcedure<R, A1>( callable(obj, method) );
217  this->registerProcedure(name, proc);
218  }
219 
220  template <typename R, class C, typename A1, typename A2>
221  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2) )
222  {
223  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2>( callable(obj, method) );
224  this->registerProcedure(name, proc);
225  }
226 
227  template <typename R, class C, typename A1, typename A2, typename A3>
228  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3) )
229  {
230  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3>( callable(obj, method) );
231  this->registerProcedure(name, proc);
232  }
233 
234  template <typename R, class C, typename A1, typename A2, typename A3, typename A4>
235  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4) )
236  {
237  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4>( callable(obj, method) );
238  this->registerProcedure(name, proc);
239  }
240 
241  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5>
242  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5) )
243  {
244  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5>( callable(obj, method) );
245  this->registerProcedure(name, proc);
246  }
247 
248  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
249  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6) )
250  {
251  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6>( callable(obj, method) );
252  this->registerProcedure(name, proc);
253  }
254 
255  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
256  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7) )
257  {
258  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7>( callable(obj, method) );
259  this->registerProcedure(name, proc);
260  }
261 
262  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
263  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7, A8) )
264  {
266  this->registerProcedure(name, proc);
267  }
268 
269  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
270  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7, A8, A9) )
271  {
273  this->registerProcedure(name, proc);
274  }
275 
276  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
277  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10) )
278  {
280  this->registerProcedure(name, proc);
281  }
282 
283  ServiceProcedure* getProcedure(const std::string& name) const;
284 
285  void releaseProcedure(ServiceProcedure* proc) const;
286 
287  std::vector<std::string> getProcedureNames() const;
288 
289  protected:
290  void registerProcedure(const std::string& name, ServiceProcedure* proc);
291 
292  private:
293  typedef std::map<std::string, ServiceProcedure*> ProcedureMap;
294  ProcedureMap _procedures;
295  };
296 
297 }
298 
299 #endif // CXXTOOLS_SERVICEREGISTRY_H
300