ServiceDefinition.h
1 /*
2  * Copyright (C) 2009-2014 by Dr. Marc Boris Duerner
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 Pt_Remoting_ServiceDefinition_h
30 #define Pt_Remoting_ServiceDefinition_h
31 
32 #include <Pt/Remoting/Api.h>
33 #include <Pt/Remoting/ServiceProcedure.h>
34 #include <Pt/System/Mutex.h>
35 #include <Pt/NonCopyable.h>
36 #include <Pt/Types.h>
37 #include <string>
38 #include <map>
39 
40 namespace Pt {
41 
42 namespace Remoting {
43 
44 class Responder;
45 
46 class PT_REMOTING_API ServiceDefinition : private NonCopyable
47 {
48  public:
49  ServiceDefinition();
50 
51  virtual ~ServiceDefinition();
52 
53  ServiceProcedure* getProcedure(const std::string& name, Responder& resp);
54 
55  void releaseProcedure(ServiceProcedure* proc);
56 
57  template <typename R>
58  void registerProcedure(const std::string& name, R (*fn)())
59  {
60  ServiceProcedureDef* proc = new BasicProcedureDef<R>(Pt::callable(fn));
61  this->registerProcedure(name, proc);
62  }
63 
64  template <typename R, typename A1>
65  void registerProcedure(const std::string& name, R (*fn)(A1))
66  {
67  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1>(Pt::callable(fn));
68  this->registerProcedure(name, proc);
69  }
70 
71  template <typename R, typename A1, typename A2>
72  void registerProcedure(const std::string& name, R (*fn)(A1, A2))
73  {
74  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2>(Pt::callable(fn));
75  this->registerProcedure(name, proc);
76  }
77 
78  template <typename R, typename A1, typename A2, typename A3>
79  void registerProcedure(const std::string& name, R (*fn)(A1, A2, A3))
80  {
81  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3>(Pt::callable(fn));
82  this->registerProcedure(name, proc);
83  }
84 
85  template <typename R, typename A1, typename A2, typename A3, typename A4>
86  void registerProcedure(const std::string& name, R (*fn)(A1, A2, A3, A4))
87  {
88  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4>(Pt::callable(fn));
89  this->registerProcedure(name, proc);
90  }
91 
92  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
93  void registerProcedure(const std::string& name, R (*fn)(A1, A2, A3, A4, A5))
94  {
95  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5>(Pt::callable(fn));
96  this->registerProcedure(name, proc);
97  }
98 
99  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
100  void registerProcedure(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6))
101  {
102  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6>(Pt::callable(fn));
103  this->registerProcedure(name, proc);
104  }
105 
106  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
107  void registerProcedure(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7))
108  {
109  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7>(Pt::callable(fn));
110  this->registerProcedure(name, proc);
111  }
112 
113  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
114  void registerProcedure(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7, A8))
115  {
116  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7, A8>(Pt::callable(fn));
117  this->registerProcedure(name, proc);
118  }
119 
120  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
121  void registerProcedure(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7, A8, A9))
122  {
123  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7, A8, A9>(Pt::callable(fn));
124  this->registerProcedure(name, proc);
125  }
126 
127  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
128  void registerProcedure(const std::string& name, R (*fn)(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10))
129  {
130  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>(Pt::callable(fn));
131  this->registerProcedure(name, proc);
132  }
133 
134  template <typename R, class C>
135  void registerProcedure(const std::string& name, C& obj, R (C::*method)() )
136  {
137  ServiceProcedureDef* proc = new BasicProcedureDef<R>( callable(obj, method) );
138  this->registerProcedure(name, proc);
139  }
140 
141  template <typename R, class C, typename A1>
142  void registerProcedure(const std::string& name, C& obj, R (C::*method)(A1) )
143  {
144  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1>( callable(obj, method) );
145  this->registerProcedure(name, proc);
146  }
147 
148  template <typename R, class C, typename A1, typename A2>
149  void registerProcedure(const std::string& name, C& obj, R (C::*method)(A1, A2) )
150  {
151  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2>( callable(obj, method) );
152  this->registerProcedure(name, proc);
153  }
154 
155  template <typename R, class C, typename A1, typename A2, typename A3>
156  void registerProcedure(const std::string& name, C& obj, R (C::*method)(A1, A2, A3) )
157  {
158  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3>( callable(obj, method) );
159  this->registerProcedure(name, proc);
160  }
161 
162  template <typename R, class C, typename A1, typename A2, typename A3, typename A4>
163  void registerProcedure(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4) )
164  {
165  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4>( callable(obj, method) );
166  this->registerProcedure(name, proc);
167  }
168 
169  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5>
170  void registerProcedure(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5) )
171  {
172  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5>( callable(obj, method) );
173  this->registerProcedure(name, proc);
174  }
175 
176  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
177  void registerProcedure(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6) )
178  {
179  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6>( callable(obj, method) );
180  this->registerProcedure(name, proc);
181  }
182 
183  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
184  void registerProcedure(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7) )
185  {
186  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7>( callable(obj, method) );
187  this->registerProcedure(name, proc);
188  }
189 
190  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
191  void registerProcedure(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7, A8) )
192  {
193  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7, A8>( callable(obj, method) );
194  this->registerProcedure(name, proc);
195  }
196 
197  template <typename R, class C, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
198  void registerProcedure(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7, A8, A9) )
199  {
200  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7, A8, A9>( callable(obj, method) );
201  this->registerProcedure(name, proc);
202  }
203 
204  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>
205  void registerProcedure(const std::string& name, C& obj, R (C::*method)(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10) )
206  {
207  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>( callable(obj, method) );
208  this->registerProcedure(name, proc);
209  }
210 
211  template <typename R>
212  void registerProcedure(const std::string& name, const Callable<R>& cb)
213  {
214  ServiceProcedureDef* proc = new BasicProcedureDef<R>(cb);
215  this->registerProcedure(name, proc);
216  }
217 
218  template <typename R, typename A1>
219  void registerProcedure(const std::string& name, const Callable<R, A1>& cb)
220  {
221  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1>(cb);
222  this->registerProcedure(name, proc);
223  }
224 
225  template <typename R, typename A1, typename A2>
226  void registerProcedure(const std::string& name, const Callable<R, A1, A2>& cb)
227  {
228  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2>(cb);
229  this->registerProcedure(name, proc);
230  }
231 
232  template <typename R, typename A1, typename A2, typename A3>
233  void registerProcedure(const std::string& name, const Callable<R, A1, A2, A3>& cb)
234  {
235  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3>(cb);
236  this->registerProcedure(name, proc);
237  }
238 
239  template <typename R, typename A1, typename A2, typename A3, typename A4>
240  void registerProcedure(const std::string& name, const Callable<R, A1, A2, A3, A4>& cb)
241  {
242  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4>(cb);
243  this->registerProcedure(name, proc);
244  }
245 
246  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
247  void registerProcedure(const std::string& name, const Callable<R, A1, A2, A3, A4, A5>& cb)
248  {
249  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5>(cb);
250  this->registerProcedure(name, proc);
251  }
252 
253  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
254  void registerProcedure(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6>& cb)
255  {
256  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6>(cb);
257  this->registerProcedure(name, proc);
258  }
259 
260  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
261  void registerProcedure(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7>& cb)
262  {
263  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7>(cb);
264  this->registerProcedure(name, proc);
265  }
266 
267  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
268  void registerProcedure(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7, A8>& cb)
269  {
270  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7, A8>(cb);
271  this->registerProcedure(name, proc);
272  }
273 
274  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
275  void registerProcedure(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7, A8, A9>& cb)
276  {
277  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7, A8, A9>(cb);
278  this->registerProcedure(name, proc);
279  }
280 
281  template <typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
282  void registerProcedure(const std::string& name, const Callable<R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>& cb)
283  {
284  ServiceProcedureDef* proc = new BasicProcedureDef<R, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>(cb);
285  this->registerProcedure(name, proc);
286  }
287 
288  template <typename A, class C>
289  void registerActiveProcedure(const std::string& name, A* (*fn)(Responder&) )
290  {
291  ServiceProcedureDef* proc = new ActiveProcedureDef<A>( callable(fn) );
292  this->registerProcedure(name, proc);
293  }
294 
295  template <typename A, class C>
296  void registerActiveProcedure(const std::string& name, C& obj, A* (C::*method)(Responder&) )
297  {
298  ServiceProcedureDef* proc = new ActiveProcedureDef<A>( callable(obj, method) );
299  this->registerProcedure(name, proc);
300  }
301 
302  template <typename A, class C>
303  void registerActiveProcedure(const std::string& name, C& obj, A* (C::*method)(Responder&) const )
304  {
305  ServiceProcedureDef* proc = new ActiveProcedureDef<A>( callable(obj, method) );
306  this->registerProcedure(name, proc);
307  }
308 
309  protected:
310  void registerProcedure(const std::string& name, ServiceProcedureDef* proc);
311 
312  System::Mutex& mutex();
313 
314  System::Mutex& mutex() const;
315 
316  private:
317  typedef std::map<std::string, ServiceProcedureDef*> ProcedureMap;
318  ProcedureMap _procedures;
319  mutable System::Mutex _mtx;
320  Pt::varint_t _r1;
321  Pt::varint_t _r2;
322 };
323 
324 } // namespace Remoting
325 
326 } // namespace Pt
327 
328 #endif // Pt_Remoting_ServiceDefinition_h
XML-RPC service procedure.
Definition: ServiceProcedure.h:44
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Mutual exclusion device.
Definition: Mutex.h:48
Dispatches requests to a service procedure.
Definition: Responder.h:49