cppexpose  1.0.0.b785e04f23b8
C++ library for type introspection, reflection, and scripting interface
function_helpers.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 
5 #include <functional>
6 #include <string>
7 #include <vector>
8 
10 
11 
12 namespace cppexpose
13 {
14 
15 
16 class Object;
17 
18 
23 namespace helper
24 {
25 
26 
31 template<typename T, size_t POS>
32 struct CPPEXPOSE_TEMPLATE_API ArgValue {
33  static int get(const std::vector<Variant> & args) {
34  // Assume signed integral type by default
35  int value = 0;
36  if (POS < args.size()) {
37  value = args[POS].value<int>();
38  }
39  return value;
40  }
41 };
42 
43 template<typename T, size_t POS>
44 struct CPPEXPOSE_TEMPLATE_API ArgValue<const T &, POS> {
45  static T get(const std::vector<Variant> & args) {
46  return ArgValue<T, POS>::get(args);
47  }
48 };
49 
50 template<size_t POS>
51 struct CPPEXPOSE_TEMPLATE_API ArgValue<float, POS> {
52  static float get(const std::vector<Variant> & args) {
53  float value = 0.0f;
54  if (POS < args.size()) {
55  value = (float)args[POS].value<double>();
56  }
57  return value;
58  }
59 };
60 
61 template<size_t POS>
62 struct CPPEXPOSE_TEMPLATE_API ArgValue<double, POS> {
63  static double get(const std::vector<Variant> & args) {
64  double value = 0.0f;
65  if (POS < args.size()) {
66  value = args[POS].value<double>();
67  }
68  return value;
69  }
70 };
71 
72 template<size_t POS>
73 struct CPPEXPOSE_TEMPLATE_API ArgValue<bool, POS> {
74  static bool get(const std::vector<Variant> & args) {
75  bool value = false;
76  if (POS < args.size()) {
77  value = args[POS].value<bool>();
78  }
79  return value;
80  }
81 };
82 
83 template<size_t POS>
84 struct CPPEXPOSE_TEMPLATE_API ArgValue<std::string, POS> {
85  static std::string get(const std::vector<Variant> & args) {
86  std::string value;
87  if (POS < args.size()) {
88  value = args[POS].value<std::string>();
89  }
90  return value;
91  }
92 };
93 
94 template<size_t POS>
95 struct CPPEXPOSE_TEMPLATE_API ArgValue<Variant, POS> {
96  static Variant get(const std::vector<Variant> & args) {
97  Variant value;
98  if (POS < args.size()) {
99  value = args[POS];
100  }
101  return value;
102  }
103 };
104 
105 template<size_t POS>
106 struct CPPEXPOSE_TEMPLATE_API ArgValue<const Variant &, POS> {
107  static Variant get(const std::vector<Variant> & args) {
108  return ArgValue<Variant, POS>::get(args);
109  }
110 };
111 
112 template<typename T, size_t POS>
113 struct CPPEXPOSE_TEMPLATE_API ArgValue<const std::vector<T> &, POS> {
114  static std::vector<T> get(const std::vector<Variant> & args) {
115  if (POS < args.size()) {
116  return args[POS].toVector<T>();
117  } else {
118  return std::vector<T>();
119  }
120  }
121 };
122 
123 template<size_t POS>
124 struct CPPEXPOSE_TEMPLATE_API ArgValue<const std::vector<Variant> &, POS> {
125  static std::vector<Variant> get(const std::vector<Variant> & args) {
126  std::vector<Variant> list;
127  for (size_t i=POS; i<args.size(); i++) {
128  list.push_back(args[i]);
129  }
130  return list;
131  }
132 };
133 
134 template<size_t POS>
135 struct CPPEXPOSE_TEMPLATE_API ArgValue<cppexpose::Object *, POS> {
136  static cppexpose::Object * get(const std::vector<Variant> & args) {
137  cppexpose::Object * obj = nullptr;
138  if (POS < args.size()) {
139  obj = args[POS].value<cppexpose::Object *>();
140  }
141  return obj;
142  }
143 };
144 
145 template<size_t POS>
146 struct CPPEXPOSE_TEMPLATE_API ArgValue<const cppexpose::Object *, POS> {
147  static const cppexpose::Object * get(const std::vector<Variant> & args) {
149  }
150 };
151 
152 
157 template<size_t... I>
158 struct CPPEXPOSE_TEMPLATE_API Seq {};
159 
164 template<int N, size_t... I>
165 struct CPPEXPOSE_TEMPLATE_API GenSeq : GenSeq<N-1, N-1, I...> {};
166 
167 template<size_t... I>
168 struct CPPEXPOSE_TEMPLATE_API GenSeq<0, I...> { typedef Seq<I...> Type; };
169 
174 template<size_t N, typename T, typename... Arguments>
175 struct CPPEXPOSE_TEMPLATE_API PickType : PickType<N-1, Arguments...> {};
176 
177 template<typename T, typename... Arguments>
178 struct CPPEXPOSE_TEMPLATE_API PickType<0, T, Arguments...> { typedef T Type; };
179 
184 template<size_t I, typename... Arguments>
185 struct CPPEXPOSE_TEMPLATE_API ArgValueGen
186 {
187  typedef typename PickType<I, Arguments...>::Type T;
189 };
190 
195 template <typename RET, typename... Arguments>
196 class CPPEXPOSE_TEMPLATE_API CallFunction
197 {
198 public:
199  typedef std::function<RET (Arguments...)> FuncPtr;
200 
201  static Variant call(FuncPtr func, Arguments... args) {
202  return Variant( func(args...) );
203  }
204 };
205 
210 template <typename... Arguments>
211 class CPPEXPOSE_TEMPLATE_API CallFunction<void, Arguments...>
212 {
213 public:
214  typedef std::function<void (Arguments...)> FuncPtr;
215 
216  static Variant call(FuncPtr func, Arguments... args) {
217  func(args...);
218  return Variant();
219  }
220 };
221 
226 template <typename T, typename RET, typename... Arguments>
227 class CPPEXPOSE_TEMPLATE_API CallMethod
228 {
229 public:
230  typedef RET (T::*MethodPtr) (Arguments...);
231 
232  static Variant call(T * obj, MethodPtr method, Arguments... args) {
233  return Variant((obj->*method)(args...));
234  }
235 };
236 
241 template <typename T, typename... Arguments>
242 class CPPEXPOSE_TEMPLATE_API CallMethod<T, void, Arguments...>
243 {
244 public:
245  typedef void (T::*MethodPtr) (Arguments...);
246 
247  static Variant call(T * obj, MethodPtr method, Arguments... args) {
248  (obj->*method)(args...);
249  return Variant();
250  }
251 };
252 
257 template <typename T, typename RET, typename... Arguments>
258 class CPPEXPOSE_TEMPLATE_API CallConstMethod
259 {
260 public:
261  typedef RET (T::*MethodPtr) (Arguments...) const;
262 
263  static Variant call(const T * obj, MethodPtr method, Arguments... args) {
264  return Variant((obj->*method)(args...));
265  }
266 };
267 
272 template <typename T, typename... Arguments>
273 class CPPEXPOSE_TEMPLATE_API CallConstMethod<T, void, Arguments...>
274 {
275 public:
276  typedef void (T::*MethodPtr) (Arguments...) const;
277 
278  static Variant call(const T * obj, MethodPtr method, Arguments... args) {
279  (obj->*method)(args...);
280  return Variant();
281  }
282 };
283 
284 
285 } // namespace helper
286 
287 
288 } // namespace cppexpose
Generate ArgValue class for types and index (e.g., ArgValueGen<2, float, int, double>::Type = ArgValu...
Definition: function_helpers.h:185
static Variant call(T *obj, MethodPtr method, Arguments...args)
Definition: function_helpers.h:247
static Variant call(FuncPtr func, Arguments...args)
Definition: function_helpers.h:201
static Variant call(const T *obj, MethodPtr method, Arguments...args)
Definition: function_helpers.h:278
Pick type by index (e.g., PickType<1, void, int, float>::Type = int)
Definition: function_helpers.h:175
Definition: function_helpers.h:12
ArgValue< T, I > Type
Definition: function_helpers.h:188
static Variant call(const T *obj, MethodPtr method, Arguments...args)
Definition: function_helpers.h:263
Template for parsing typed arguments from a list of variants.
Definition: function_helpers.h:32
Generate a sequence of numbers (e.g., Seq<0, 1, 2>)
Definition: function_helpers.h:158
PickType< I, Arguments... >::Type T
Definition: function_helpers.h:187
Template for calling a static function with a return value.
Definition: function_helpers.h:196
Sequence generator (e.g., GenSec<3>::Type = Seq<0, 1, 2>)
Definition: function_helpers.h:165
static Variant call(FuncPtr func, Arguments...args)
Definition: function_helpers.h:216
std::function< void(Arguments...)> FuncPtr
Definition: function_helpers.h:214
Seq< I... > Type
Definition: function_helpers.h:168
T Type
Definition: function_helpers.h:178
Template for calling a const member function with a return value.
Definition: function_helpers.h:258
std::function< RET(Arguments...)> FuncPtr
Definition: function_helpers.h:199
static int get(const std::vector< Variant > &args)
Definition: function_helpers.h:33
Base class for reflection-enabled objects.
Definition: Object.h:24
Template for calling a member function with a return value.
Definition: function_helpers.h:227
static Variant call(T *obj, MethodPtr method, Arguments...args)
Definition: function_helpers.h:232