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 
33 #include <cxxtools/callable.h>
34 #include <cxxtools/function.h>
35 #include <cxxtools/method.h>
36 #include <string>
37 #include <vector>
38 #include <map>
39 
40 namespace cxxtools
41 {
43  {
45  ServiceRegistry& operator=(const ServiceRegistry&) { return *this; }
46 
47  public:
49  { }
50 
52 
53  template <typename R>
54  void registerFunction(const std::string& name, R (*fn)())
55  {
56  ServiceProcedure* proc = new BasicServiceProcedure<R>(callable(fn));
57  this->registerProcedure(name, proc);
58  }
59 
60  template <typename R, typename A1>
61  void registerFunction(const std::string& name, R (*fn)(A1))
62  {
63  ServiceProcedure* proc = new BasicServiceProcedure<R, A1>(callable(fn));
64  this->registerProcedure(name, proc);
65  }
66 
67  template <typename R, typename A1, typename A2>
68  void registerFunction(const std::string& name, R (*fn)(A1, A2))
69  {
70  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2>(callable(fn));
71  this->registerProcedure(name, proc);
72  }
73 
74  template <typename R, typename A1, typename A2, typename A3>
75  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3))
76  {
77  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3>(callable(fn));
78  this->registerProcedure(name, proc);
79  }
80 
81  template <typename R, typename A1, typename A2, typename A3, typename A4>
82  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4))
83  {
84  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4>(callable(fn));
85  this->registerProcedure(name, proc);
86  }
87 
88  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
89  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5))
90  {
91  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5>(callable(fn));
92  this->registerProcedure(name, proc);
93  }
94 
95  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
96  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6))
97  {
98  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6>(callable(fn));
99  this->registerProcedure(name, proc);
100  }
101 
102  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
103  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7))
104  {
105  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7>(callable(fn));
106  this->registerProcedure(name, proc);
107  }
108 
109  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
110  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7, A8))
111  {
112  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7, A8>(callable(fn));
113  this->registerProcedure(name, proc);
114  }
115 
116  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
117  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7, A8, A9))
118  {
119  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7, A8, A9>(callable(fn));
120  this->registerProcedure(name, proc);
121  }
122 
123  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
124  void registerFunction(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10))
125  {
126  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>(callable(fn));
127  this->registerProcedure(name, proc);
128  }
129 
130  template <typename R>
131  void registerCallable(const std::string& name, const Callable<R>& cb)
132  {
133  ServiceProcedure* proc = new BasicServiceProcedure<R>(cb);
134  this->registerProcedure(name, proc);
135  }
136 
137  template <typename R, typename A1>
138  void registerCallable(const std::string& name, const Callable<R, A1>& cb)
139  {
140  ServiceProcedure* proc = new BasicServiceProcedure<R, A1>(cb);
141  this->registerProcedure(name, proc);
142  }
143 
144  template <typename R, typename A1, typename A2>
145  void registerCallable(const std::string& name, const Callable<R, A1, A2>& cb)
146  {
147  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2>(cb);
148  this->registerProcedure(name, proc);
149  }
150 
151  template <typename R, typename A1, typename A2, typename A3>
152  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3>& cb)
153  {
154  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3>(cb);
155  this->registerProcedure(name, proc);
156  }
157 
158  template <typename R, typename A1, typename A2, typename A3, typename A4>
159  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4>& cb)
160  {
161  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4>(cb);
162  this->registerProcedure(name, proc);
163  }
164 
165  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
166  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5>& cb)
167  {
168  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5>(cb);
169  this->registerProcedure(name, proc);
170  }
171 
172  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
173  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6>& cb)
174  {
175  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6>(cb);
176  this->registerProcedure(name, proc);
177  }
178 
179  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
180  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7>& cb)
181  {
182  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7>(cb);
183  this->registerProcedure(name, proc);
184  }
185 
186  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
187  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7, A8>& cb)
188  {
189  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7, A8>(cb);
190  this->registerProcedure(name, proc);
191  }
192 
193  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
194  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7, A8, A9>& cb)
195  {
196  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7, A8, A9>(cb);
197  this->registerProcedure(name, proc);
198  }
199 
200  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
201  void registerCallable(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>& cb)
202  {
203  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>(cb);
204  this->registerProcedure(name, proc);
205  }
206 
207  template <typename R, class C>
208  void registerMethod(const std::string& name, C& obj, R (C::*method)() )
209  {
210  ServiceProcedure* proc = new BasicServiceProcedure<R>( callable(obj, method) );
211  this->registerProcedure(name, proc);
212  }
213 
214  template <typename R, class C, typename A1>
215  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1) )
216  {
217  ServiceProcedure* proc = new BasicServiceProcedure<R, A1>( callable(obj, method) );
218  this->registerProcedure(name, proc);
219  }
220 
221  template <typename R, class C, typename A1, typename A2>
222  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2) )
223  {
224  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2>( callable(obj, method) );
225  this->registerProcedure(name, proc);
226  }
227 
228  template <typename R, class C, typename A1, typename A2, typename A3>
229  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3) )
230  {
231  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3>( callable(obj, method) );
232  this->registerProcedure(name, proc);
233  }
234 
235  template <typename R, class C, typename A1, typename A2, typename A3, typename A4>
236  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4) )
237  {
238  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4>( callable(obj, method) );
239  this->registerProcedure(name, proc);
240  }
241 
242  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5>
243  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5) )
244  {
245  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5>( callable(obj, method) );
246  this->registerProcedure(name, proc);
247  }
248 
249  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
250  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6) )
251  {
252  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6>( callable(obj, method) );
253  this->registerProcedure(name, proc);
254  }
255 
256  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
257  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7) )
258  {
259  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7>( callable(obj, method) );
260  this->registerProcedure(name, proc);
261  }
262 
263  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
264  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7, A8) )
265  {
266  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7, A8>( callable(obj, method) );
267  this->registerProcedure(name, proc);
268  }
269 
270  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
271  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7, A8, A9) )
272  {
273  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7, A8, A9>( callable(obj, method) );
274  this->registerProcedure(name, proc);
275  }
276 
277  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>
278  void registerMethod(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10) )
279  {
280  ServiceProcedure* proc = new BasicServiceProcedure<R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>( callable(obj, method) );
281  this->registerProcedure(name, proc);
282  }
283 
284  ServiceProcedure* getProcedure(const std::string& name) const;
285 
286  void releaseProcedure(ServiceProcedure* proc) const;
287 
288  std::vector<std::string> getProcedureNames() const;
289 
290  protected:
291  void registerProcedure(const std::string& name, ServiceProcedure* proc);
292 
293  private:
294  typedef std::map<std::string, ServiceProcedure*> ProcedureMap;
295  ProcedureMap _procedures;
296  };
297 
298 }
299 
300 #endif // CXXTOOLS_SERVICEREGISTRY_H
301