Pothos  0.2.1-g9f04573d
The Pothos dataflow programming software suite
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
ClassImpl.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 #include <Pothos/Config.hpp>
14 #include <Pothos/Managed/Class.hpp>
15 #include <functional> //std::reference_wrapper
16 
17 namespace Pothos {
18 
19 namespace Detail {
20 
21 template <typename T>
22 std::reference_wrapper<T> referenceToWrapper(T &v)
23 {
24  return std::ref(v);
25 }
26 
27 template <typename T>
28 std::reference_wrapper<T> pointerToWrapper(T *v)
29 {
30  return std::ref(*v);
31 }
32 
33 template <typename T>
34 std::reference_wrapper<T> sharedToWrapper(std::shared_ptr<T> &v)
35 {
36  return std::ref(*v);
37 }
38 
39 template <typename T, typename Base>
40 std::reference_wrapper<Base> convertToBase(T &v)
41 {
42  return std::ref<Base>(v);
43 }
44 
45 template <typename T>
46 void deleteValue(T &v)
47 {
48  delete (&v);
49 }
50 
51 template <typename ClassType, typename ValueType>
52 static const ValueType &getField(ClassType &i, const std::function<ValueType &(ClassType *)> &getRef)
53 {
54  return getRef(&i);
55 }
56 
57 template <typename ClassType, typename ValueType>
58 static void setField(ClassType &i, const std::function<ValueType &(ClassType *)> &getRef, const ValueType &v)
59 {
60  getRef(&i) = v;
61 }
62 
63 } //namespace Detail
64 
65 template <typename ClassType>
67 {
69  {
70  registerReferenceToWrapper(Callable(&Detail::referenceToWrapper<ClassType>));
71  registerPointerToWrapper(Callable(&Detail::pointerToWrapper<ClassType>));
72  registerSharedToWrapper(Callable(&Detail::sharedToWrapper<ClassType>));
73  registerMethod("delete", Callable(&Detail::deleteValue<ClassType>));
74  }
75  return *this;
76 }
77 
78 template <typename ClassType, typename BaseClassType>
80 {
81  return this->registerToBaseClass(Callable(&Detail::convertToBase<ClassType, BaseClassType>));
82 }
83 
84 template <typename ClassType, typename ValueType>
85 ManagedClass &ManagedClass::registerField(const std::string &name, ValueType ClassType::*member)
86 {
87  std::function<ValueType &(ClassType *)> getRef = std::mem_fn(member);
88  this->registerMethod("get:"+name, Callable(&Detail::getField<ClassType, ValueType>).bind(getRef, 1));
89  this->registerMethod("set:"+name, Callable(&Detail::setField<ClassType, ValueType>).bind(getRef, 1));
90  return *this;
91 }
92 
93 template <typename ClassType>
95 {
96  this->registerClass<ClassType>();
97  this->registerConstructor(Callable::factory<ClassType>());
98  this->registerStaticMethod("new", Callable::factoryNew<ClassType>());
99  this->registerStaticMethod("shared", Callable::factoryShared<ClassType>());
100  return *this;
101 }
102 
103 template <typename ReturnType>
104 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)())
105 {
106  this->registerStaticMethod(name, Callable(method));
107  return *this;
108 }
109 
110 template <typename ReturnType, typename ClassType>
111 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)())
112 {
113  this->registerClass<ClassType>();
114  this->registerMethod(name, Callable(method));
115  return *this;
116 }
117 
118 template <typename ReturnType, typename ClassType>
119 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)() const)
120 {
121  this->registerClass<ClassType>();
122  this->registerMethod(name, Callable(method));
123  return *this;
124 }
125 
126 template <typename ClassType, typename A0>
128 {
129  this->registerClass<ClassType>();
130  this->registerConstructor(Callable::factory<ClassType, A0>());
131  this->registerStaticMethod("new", Callable::factoryNew<ClassType, A0>());
132  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, A0>());
133  return *this;
134 }
135 
136 template <typename A0, typename ReturnType>
137 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)(A0))
138 {
139  this->registerStaticMethod(name, Callable(method));
140  return *this;
141 }
142 
143 template <typename A0, typename ReturnType, typename ClassType>
144 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0))
145 {
146  this->registerClass<ClassType>();
147  this->registerMethod(name, Callable(method));
148  return *this;
149 }
150 
151 template <typename A0, typename ReturnType, typename ClassType>
152 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0) const)
153 {
154  this->registerClass<ClassType>();
155  this->registerMethod(name, Callable(method));
156  return *this;
157 }
158 
159 template <typename ClassType, typename A0, typename A1>
161 {
162  this->registerClass<ClassType>();
163  this->registerConstructor(Callable::factory<ClassType, A0, A1>());
164  this->registerStaticMethod("new", Callable::factoryNew<ClassType, A0, A1>());
165  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, A0, A1>());
166  return *this;
167 }
168 
169 template <typename A0, typename A1, typename ReturnType>
170 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)(A0, A1))
171 {
172  this->registerStaticMethod(name, Callable(method));
173  return *this;
174 }
175 
176 template <typename A0, typename A1, typename ReturnType, typename ClassType>
177 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1))
178 {
179  this->registerClass<ClassType>();
180  this->registerMethod(name, Callable(method));
181  return *this;
182 }
183 
184 template <typename A0, typename A1, typename ReturnType, typename ClassType>
185 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1) const)
186 {
187  this->registerClass<ClassType>();
188  this->registerMethod(name, Callable(method));
189  return *this;
190 }
191 
192 template <typename ClassType, typename A0, typename A1, typename A2>
194 {
195  this->registerClass<ClassType>();
196  this->registerConstructor(Callable::factory<ClassType, A0, A1, A2>());
197  this->registerStaticMethod("new", Callable::factoryNew<ClassType, A0, A1, A2>());
198  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, A0, A1, A2>());
199  return *this;
200 }
201 
202 template <typename A0, typename A1, typename A2, typename ReturnType>
203 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)(A0, A1, A2))
204 {
205  this->registerStaticMethod(name, Callable(method));
206  return *this;
207 }
208 
209 template <typename A0, typename A1, typename A2, typename ReturnType, typename ClassType>
210 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2))
211 {
212  this->registerClass<ClassType>();
213  this->registerMethod(name, Callable(method));
214  return *this;
215 }
216 
217 template <typename A0, typename A1, typename A2, typename ReturnType, typename ClassType>
218 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2) const)
219 {
220  this->registerClass<ClassType>();
221  this->registerMethod(name, Callable(method));
222  return *this;
223 }
224 
225 template <typename ClassType, typename A0, typename A1, typename A2, typename A3>
227 {
228  this->registerClass<ClassType>();
229  this->registerConstructor(Callable::factory<ClassType, A0, A1, A2, A3>());
230  this->registerStaticMethod("new", Callable::factoryNew<ClassType, A0, A1, A2, A3>());
231  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, A0, A1, A2, A3>());
232  return *this;
233 }
234 
235 template <typename A0, typename A1, typename A2, typename A3, typename ReturnType>
236 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)(A0, A1, A2, A3))
237 {
238  this->registerStaticMethod(name, Callable(method));
239  return *this;
240 }
241 
242 template <typename A0, typename A1, typename A2, typename A3, typename ReturnType, typename ClassType>
243 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3))
244 {
245  this->registerClass<ClassType>();
246  this->registerMethod(name, Callable(method));
247  return *this;
248 }
249 
250 template <typename A0, typename A1, typename A2, typename A3, typename ReturnType, typename ClassType>
251 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3) const)
252 {
253  this->registerClass<ClassType>();
254  this->registerMethod(name, Callable(method));
255  return *this;
256 }
257 
258 template <typename ClassType, typename A0, typename A1, typename A2, typename A3, typename A4>
260 {
261  this->registerClass<ClassType>();
262  this->registerConstructor(Callable::factory<ClassType, A0, A1, A2, A3, A4>());
263  this->registerStaticMethod("new", Callable::factoryNew<ClassType, A0, A1, A2, A3, A4>());
264  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, A0, A1, A2, A3, A4>());
265  return *this;
266 }
267 
268 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename ReturnType>
269 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)(A0, A1, A2, A3, A4))
270 {
271  this->registerStaticMethod(name, Callable(method));
272  return *this;
273 }
274 
275 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename ReturnType, typename ClassType>
276 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3, A4))
277 {
278  this->registerClass<ClassType>();
279  this->registerMethod(name, Callable(method));
280  return *this;
281 }
282 
283 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename ReturnType, typename ClassType>
284 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3, A4) const)
285 {
286  this->registerClass<ClassType>();
287  this->registerMethod(name, Callable(method));
288  return *this;
289 }
290 
291 template <typename ClassType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5>
293 {
294  this->registerClass<ClassType>();
295  this->registerConstructor(Callable::factory<ClassType, A0, A1, A2, A3, A4, A5>());
296  this->registerStaticMethod("new", Callable::factoryNew<ClassType, A0, A1, A2, A3, A4, A5>());
297  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, A0, A1, A2, A3, A4, A5>());
298  return *this;
299 }
300 
301 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename ReturnType>
302 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)(A0, A1, A2, A3, A4, A5))
303 {
304  this->registerStaticMethod(name, Callable(method));
305  return *this;
306 }
307 
308 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename ReturnType, typename ClassType>
309 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3, A4, A5))
310 {
311  this->registerClass<ClassType>();
312  this->registerMethod(name, Callable(method));
313  return *this;
314 }
315 
316 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename ReturnType, typename ClassType>
317 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3, A4, A5) const)
318 {
319  this->registerClass<ClassType>();
320  this->registerMethod(name, Callable(method));
321  return *this;
322 }
323 
324 template <typename ClassType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
326 {
327  this->registerClass<ClassType>();
328  this->registerConstructor(Callable::factory<ClassType, A0, A1, A2, A3, A4, A5, A6>());
329  this->registerStaticMethod("new", Callable::factoryNew<ClassType, A0, A1, A2, A3, A4, A5, A6>());
330  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, A0, A1, A2, A3, A4, A5, A6>());
331  return *this;
332 }
333 
334 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename ReturnType>
335 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)(A0, A1, A2, A3, A4, A5, A6))
336 {
337  this->registerStaticMethod(name, Callable(method));
338  return *this;
339 }
340 
341 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename ReturnType, typename ClassType>
342 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3, A4, A5, A6))
343 {
344  this->registerClass<ClassType>();
345  this->registerMethod(name, Callable(method));
346  return *this;
347 }
348 
349 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename ReturnType, typename ClassType>
350 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3, A4, A5, A6) const)
351 {
352  this->registerClass<ClassType>();
353  this->registerMethod(name, Callable(method));
354  return *this;
355 }
356 
357 template <typename ClassType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
359 {
360  this->registerClass<ClassType>();
361  this->registerConstructor(Callable::factory<ClassType, A0, A1, A2, A3, A4, A5, A6, A7>());
362  this->registerStaticMethod("new", Callable::factoryNew<ClassType, A0, A1, A2, A3, A4, A5, A6, A7>());
363  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, A0, A1, A2, A3, A4, A5, A6, A7>());
364  return *this;
365 }
366 
367 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename ReturnType>
368 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)(A0, A1, A2, A3, A4, A5, A6, A7))
369 {
370  this->registerStaticMethod(name, Callable(method));
371  return *this;
372 }
373 
374 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename ReturnType, typename ClassType>
375 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3, A4, A5, A6, A7))
376 {
377  this->registerClass<ClassType>();
378  this->registerMethod(name, Callable(method));
379  return *this;
380 }
381 
382 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename ReturnType, typename ClassType>
383 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3, A4, A5, A6, A7) const)
384 {
385  this->registerClass<ClassType>();
386  this->registerMethod(name, Callable(method));
387  return *this;
388 }
389 
390 template <typename ClassType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
392 {
393  this->registerClass<ClassType>();
394  this->registerConstructor(Callable::factory<ClassType, A0, A1, A2, A3, A4, A5, A6, A7, A8>());
395  this->registerStaticMethod("new", Callable::factoryNew<ClassType, A0, A1, A2, A3, A4, A5, A6, A7, A8>());
396  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, A0, A1, A2, A3, A4, A5, A6, A7, A8>());
397  return *this;
398 }
399 
400 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename ReturnType>
401 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)(A0, A1, A2, A3, A4, A5, A6, A7, A8))
402 {
403  this->registerStaticMethod(name, Callable(method));
404  return *this;
405 }
406 
407 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename ReturnType, typename ClassType>
408 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3, A4, A5, A6, A7, A8))
409 {
410  this->registerClass<ClassType>();
411  this->registerMethod(name, Callable(method));
412  return *this;
413 }
414 
415 template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename ReturnType, typename ClassType>
416 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(A0, A1, A2, A3, A4, A5, A6, A7, A8) const)
417 {
418  this->registerClass<ClassType>();
419  this->registerMethod(name, Callable(method));
420  return *this;
421 }
422 
423 
424 template <typename ClassType>
426 {
427  this->registerClass<ClassType>();
428  this->registerOpaqueConstructor(Callable::factory<ClassType, const Object *, const size_t>());
429  return *this;
430 }
431 
432 } //namespace Pothos
ManagedClass & registerOpaqueConstructor(void)
Definition: ClassImpl.hpp:425
ManagedClass & registerPointerToWrapper(const Callable &toPointer)
const Callable & getReferenceToWrapper(void) const
Definition: Callable.tmpl.hpp:30
Definition: Class.hpp:24
ManagedClass & registerToBaseClass(const Callable &toBase)
ManagedClass & registerMethod(const std::string &name, ReturnType(ClassType::*method)())
Definition: ClassImpl.hpp:111
ManagedClass & registerReferenceToWrapper(const Callable &toPointer)
ManagedClass & registerBaseClass(void)
Definition: ClassImpl.hpp:79
ManagedClass & registerClass(void)
Definition: ClassImpl.hpp:66
const Callable & getPointerToWrapper(void) const
ManagedClass & registerField(const std::string &name, ValueType ClassType::*member)
Definition: ClassImpl.hpp:85
ManagedClass & registerStaticMethod(const std::string &name, ReturnType(*method)())
Definition: ClassImpl.hpp:104
const Callable & getSharedToWrapper(void) const
ManagedClass & registerSharedToWrapper(const Callable &toPointer)
ManagedClass & registerConstructor(void)
Definition: ClassImpl.hpp:94