Pothos  0.4.1-gb758ed46
The Pothos dataflow programming software suite
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 {
68  if (not getReferenceToWrapper() or not getPointerToWrapper() or not getSharedToWrapper())
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, typename... ArgsType>
95 {
96  this->registerClass<ClassType>();
97  this->registerConstructor(Callable::factory<ClassType, ArgsType...>());
98  this->registerStaticMethod("new", Callable::factoryNew<ClassType, ArgsType...>());
99  this->registerStaticMethod("shared", Callable::factoryShared<ClassType, ArgsType...>());
100  return *this;
101 }
102 
103 template <typename ReturnType, typename... ArgsType>
104 ManagedClass &ManagedClass::registerStaticMethod(const std::string &name, ReturnType(*method)(ArgsType...))
105 {
106  this->registerStaticMethod(name, Callable(method));
107  return *this;
108 }
109 
110 template <typename ReturnType, typename ClassType, typename... ArgsType>
111 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(ArgsType...))
112 {
113  this->registerClass<ClassType>();
114  this->registerMethod(name, Callable(method));
115  return *this;
116 }
117 
118 template <typename ReturnType, typename ClassType, typename... ArgsType>
119 ManagedClass &ManagedClass::registerMethod(const std::string &name, ReturnType(ClassType::*method)(ArgsType...) const)
120 {
121  this->registerClass<ClassType>();
122  this->registerMethod(name, Callable(method));
123  return *this;
124 }
125 
126 template <typename ClassType>
128 {
129  this->registerClass<ClassType>();
130  this->registerOpaqueConstructor(Callable::factory<ClassType, const Object *, const size_t>());
131  return *this;
132 }
133 
134 } //namespace Pothos
ManagedClass & registerOpaqueConstructor(void)
Definition: ClassImpl.hpp:127
ManagedClass & registerMethod(const std::string &name, ReturnType(ClassType::*method)(ArgsType...))
Definition: ClassImpl.hpp:111
Definition: Callable.hpp:30
Definition: Testing.hpp:134
ManagedClass & registerConstructor(void)
Definition: ClassImpl.hpp:94
Definition: Class.hpp:24
ManagedClass & registerBaseClass(void)
Definition: ClassImpl.hpp:79
ManagedClass & registerClass(void)
Definition: ClassImpl.hpp:66
ManagedClass & registerField(const std::string &name, ValueType ClassType::*member)
Definition: ClassImpl.hpp:85
ManagedClass & registerStaticMethod(const std::string &name, ReturnType(*method)(ArgsType...))
Definition: ClassImpl.hpp:104