1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
#ifndef INCLUDED_GRAS_DETAIL_CALLABLE_HPP
#define INCLUDED_GRAS_DETAIL_CALLABLE_HPP
#include <typeinfo>
namespace gras
{
/***********************************************************************
* Registration entry base class
**********************************************************************/
struct GRAS_API CallableRegistryEntry
{
CallableRegistryEntry(void);
virtual ~CallableRegistryEntry(void);
virtual PMCC call(const PMCC *args) = 0;
};
/***********************************************************************
* Registration for return with 0 args
**********************************************************************/
template <typename ClassType, typename ReturnType>
struct CallableRegistryEntryImpl0 : CallableRegistryEntry
{
typedef ReturnType(ClassType::*Fcn)(void);
CallableRegistryEntryImpl0(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
PMCC call(const PMCC *args)
{
return PMC_M((_obj->*_fcn)());
}
ClassType *_obj; Fcn _fcn;
};
template <typename ClassType, typename ReturnType>
void Callable::register_call(const std::string &key, ReturnType(ClassType::*fcn)(void))
{
ClassType *obj = dynamic_cast<ClassType *>(this);
void *fr = new CallableRegistryEntryImpl0<ClassType, ReturnType>(obj, fcn);
_register_call(key, fr);
}
template <typename ClassType>
struct CallableRegistryEntryImplVoid0 : CallableRegistryEntry
{
typedef void(ClassType::*Fcn)(void);
CallableRegistryEntryImplVoid0(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
PMCC call(const PMCC *args)
{
(_obj->*_fcn)(); return PMCC();
}
ClassType *_obj; Fcn _fcn;
};
template <typename ClassType>
void Callable::register_call(const std::string &key, void(ClassType::*fcn)(void))
{
ClassType *obj = dynamic_cast<ClassType *>(this);
void *fr = new CallableRegistryEntryImplVoid0<ClassType>(obj, fcn);
_register_call(key, fr);
}
/***********************************************************************
* Registration for return with 1 args
**********************************************************************/
template <typename ClassType, typename ReturnType, typename Arg0>
struct CallableRegistryEntryImpl1 : CallableRegistryEntry
{
typedef ReturnType(ClassType::*Fcn)(const Arg0 &);
CallableRegistryEntryImpl1(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
PMCC call(const PMCC *args)
{
return PMC_M((_obj->*_fcn)(args[0].safe_as<Arg0>()));
}
ClassType *_obj; Fcn _fcn;
};
template <typename ClassType, typename ReturnType, typename Arg0>
void Callable::register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &))
{
ClassType *obj = dynamic_cast<ClassType *>(this);
void *fr = new CallableRegistryEntryImpl1<ClassType, ReturnType, Arg0>(obj, fcn);
_register_call(key, fr);
}
template <typename ClassType, typename Arg0>
struct CallableRegistryEntryImplVoid1 : CallableRegistryEntry
{
typedef void(ClassType::*Fcn)(const Arg0 &);
CallableRegistryEntryImplVoid1(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
PMCC call(const PMCC *args)
{
(_obj->*_fcn)(args[0].safe_as<Arg0>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
};
template <typename ClassType, typename Arg0>
void Callable::register_call(const std::string &key, void(ClassType::*fcn)(const Arg0 &))
{
ClassType *obj = dynamic_cast<ClassType *>(this);
void *fr = new CallableRegistryEntryImplVoid1<ClassType, Arg0>(obj, fcn);
_register_call(key, fr);
}
/***********************************************************************
* Registration for return with 2 args
**********************************************************************/
template <typename ClassType, typename ReturnType, typename Arg0, typename Arg1>
struct CallableRegistryEntryImpl2 : CallableRegistryEntry
{
typedef ReturnType(ClassType::*Fcn)(const Arg0 &, const Arg1 &);
CallableRegistryEntryImpl2(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
PMCC call(const PMCC *args)
{
return PMC_M((_obj->*_fcn)(args[0].safe_as<Arg0>(), args[1].safe_as<Arg1>()));
}
ClassType *_obj; Fcn _fcn;
};
template <typename ClassType, typename ReturnType, typename Arg0, typename Arg1>
void Callable::register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &, const Arg1 &))
{
ClassType *obj = dynamic_cast<ClassType *>(this);
void *fr = new CallableRegistryEntryImpl2<ClassType, ReturnType, Arg0, Arg1>(obj, fcn);
_register_call(key, fr);
}
template <typename ClassType, typename Arg0, typename Arg1>
struct CallableRegistryEntryImplVoid2 : CallableRegistryEntry
{
typedef void(ClassType::*Fcn)(const Arg0 &, const Arg1 &);
CallableRegistryEntryImplVoid2(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
PMCC call(const PMCC *args)
{
(_obj->*_fcn)(args[0].safe_as<Arg0>(), args[1].safe_as<Arg1>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
};
template <typename ClassType, typename Arg0, typename Arg1>
void Callable::register_call(const std::string &key, void(ClassType::*fcn)(const Arg0 &, const Arg1 &))
{
ClassType *obj = dynamic_cast<ClassType *>(this);
void *fr = new CallableRegistryEntryImplVoid2<ClassType, Arg0, Arg1>(obj, fcn);
_register_call(key, fr);
}
/***********************************************************************
* Registration for return with 3 args
**********************************************************************/
template <typename ClassType, typename ReturnType, typename Arg0, typename Arg1, typename Arg2>
struct CallableRegistryEntryImpl3 : CallableRegistryEntry
{
typedef ReturnType(ClassType::*Fcn)(const Arg0 &, const Arg1 &, const Arg2 &);
CallableRegistryEntryImpl3(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
PMCC call(const PMCC *args)
{
return PMC_M((_obj->*_fcn)(args[0].safe_as<Arg0>(), args[1].safe_as<Arg1>(), args[2].safe_as<Arg2>()));
}
ClassType *_obj; Fcn _fcn;
};
template <typename ClassType, typename ReturnType, typename Arg0, typename Arg1, typename Arg2>
void Callable::register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &, const Arg1 &, const Arg2 &))
{
ClassType *obj = dynamic_cast<ClassType *>(this);
void *fr = new CallableRegistryEntryImpl3<ClassType, ReturnType, Arg0, Arg1, Arg2>(obj, fcn);
_register_call(key, fr);
}
template <typename ClassType, typename Arg0, typename Arg1, typename Arg2>
struct CallableRegistryEntryImplVoid3 : CallableRegistryEntry
{
typedef void(ClassType::*Fcn)(const Arg0 &, const Arg1 &, const Arg2 &);
CallableRegistryEntryImplVoid3(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
PMCC call(const PMCC *args)
{
(_obj->*_fcn)(args[0].safe_as<Arg0>(), args[1].safe_as<Arg1>(), args[2].safe_as<Arg2>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
};
template <typename ClassType, typename Arg0, typename Arg1, typename Arg2>
void Callable::register_call(const std::string &key, void(ClassType::*fcn)(const Arg0 &, const Arg1 &, const Arg2 &))
{
ClassType *obj = dynamic_cast<ClassType *>(this);
void *fr = new CallableRegistryEntryImplVoid3<ClassType, Arg0, Arg1, Arg2>(obj, fcn);
_register_call(key, fr);
}
/***********************************************************************
* Call implementations with 0 args
**********************************************************************/
template <typename ReturnType>
ReturnType Callable::x(const std::string &key)
{
PMCC args[0];
PMCC r = _handle_call(key, args);
return r.safe_as<ReturnType>();
}
inline
void Callable::x(const std::string &key)
{
PMCC args[0];
_handle_call(key, args);
}
/***********************************************************************
* Call implementations with 1 args
**********************************************************************/
template <typename ReturnType, typename Arg0>
ReturnType Callable::x(const std::string &key, const Arg0 &a0)
{
PMCC args[1];
args[0] = PMC_M(a0);
PMCC r = _handle_call(key, args);
return r.safe_as<ReturnType>();
}
template <typename Arg0>
void Callable::x(const std::string &key, const Arg0 &a0)
{
PMCC args[1];
args[0] = PMC_M(a0);
_handle_call(key, args);
}
/***********************************************************************
* Call implementations with 2 args
**********************************************************************/
template <typename ReturnType, typename Arg0, typename Arg1>
ReturnType Callable::x(const std::string &key, const Arg0 &a0, const Arg1 &a1)
{
PMCC args[2];
args[0] = PMC_M(a0);
args[1] = PMC_M(a1);
PMCC r = _handle_call(key, args);
return r.safe_as<ReturnType>();
}
template <typename Arg0, typename Arg1>
void Callable::x(const std::string &key, const Arg0 &a0, const Arg1 &a1)
{
PMCC args[2];
args[0] = PMC_M(a0);
args[1] = PMC_M(a1);
_handle_call(key, args);
}
/***********************************************************************
* Call implementations with 3 args
**********************************************************************/
template <typename ReturnType, typename Arg0, typename Arg1, typename Arg2>
ReturnType Callable::x(const std::string &key, const Arg0 &a0, const Arg1 &a1, const Arg2 &a2)
{
PMCC args[3];
args[0] = PMC_M(a0);
args[1] = PMC_M(a1);
args[2] = PMC_M(a2);
PMCC r = _handle_call(key, args);
return r.safe_as<ReturnType>();
}
template <typename Arg0, typename Arg1, typename Arg2>
void Callable::x(const std::string &key, const Arg0 &a0, const Arg1 &a1, const Arg2 &a2)
{
PMCC args[3];
args[0] = PMC_M(a0);
args[1] = PMC_M(a1);
args[2] = PMC_M(a2);
_handle_call(key, args);
}
} //namespace gras
#endif /*INCLUDED_GRAS_DETAIL_CALLABLE_HPP*/
|