Pothos  0.2.1-g9f04573d
The Pothos dataflow programming software suite
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
ClassImpl.tmpl.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>
66 ManagedClass &ManagedClass::registerClass(void)
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>
79 ManagedClass &ManagedClass::registerBaseClass(void)
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 #for $NARGS in range($MAX_ARGS)
94 template <typename ClassType, $expand('typename A%d', $NARGS)>
95 ManagedClass &ManagedClass::registerConstructor(void)
96 {
97  this->registerClass<ClassType>();
98  this->registerConstructor(Callable::factory<ClassType, $expand('A%d', $NARGS)>());
99  this->registerStaticMethod("new", Callable::factoryNew<ClassType, $expand('A%d', $NARGS)>());
100  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, $expand('A%d', $NARGS)>());
101  return *this;
102 }
103 
104 template <$expand('typename A%d', $NARGS), typename ReturnType>
105 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)($expand('A%d', $NARGS)))
106 {
107  this->registerStaticMethod(name, Callable(method));
108  return *this;
109 }
110 
111 template <$expand('typename A%d', $NARGS), typename ReturnType, typename ClassType>
112 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)($expand('A%d', $NARGS)))
113 {
114  this->registerClass<ClassType>();
115  this->registerMethod(name, Callable(method));
116  return *this;
117 }
118 
119 template <$expand('typename A%d', $NARGS), typename ReturnType, typename ClassType>
120 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)($expand('A%d', $NARGS)) const)
121 {
122  this->registerClass<ClassType>();
123  this->registerMethod(name, Callable(method));
124  return *this;
125 }
126 
127 #end for
128 
129 template <typename ClassType>
130 ManagedClass &ManagedClass::registerOpaqueConstructor(void)
131 {
132  this->registerClass<ClassType>();
133  this->registerOpaqueConstructor(Callable::factory<ClassType, const Object *, const size_t>());
134  return *this;
135 }
136 
137 } //namespace Pothos
ManagedClass & registerOpaqueConstructor(void)
Definition: ClassImpl.hpp:425
ManagedClass & registerPointerToWrapper(const Callable &toPointer)
const Callable & getReferenceToWrapper(void) const
Definition: Callable.tmpl.hpp:30
static Callable factoryShared(void)
Definition: CallableImpl.hpp:1006
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
static Callable factoryNew(void)
Definition: CallableImpl.hpp:1000
const Callable & getPointerToWrapper(void) const
ManagedClass & registerField(const std::string &name, ValueType ClassType::*member)
Definition: ClassImpl.hpp:85
static Callable factory(void)
Definition: CallableImpl.hpp:994
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