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
|
// 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
{
struct GRAS_API CallableRegistry
{
CallableRegistry(void);
virtual ~CallableRegistry(void);
virtual PMCC call(const std::vector<PMCC> &args) = 0;
};
template <typename ClassType, typename ReturnType, typename Arg0, typename Arg1>
class CallableRegistryImpl : public CallableRegistry
{
public:
typedef ReturnType(ClassType::*Fcn0)(void);
typedef ReturnType(ClassType::*Fcn1)(const Arg0 &);
typedef ReturnType(ClassType::*Fcn2)(const Arg0 &, const Arg1 &);
CallableRegistryImpl(ClassType *obj, Fcn0 fcn0, Fcn1 fcn1 = NULL, Fcn2 fcn2 = NULL):
_obj(obj), _fcn0(fcn0), _fcn1(fcn1), _fcn2(fcn2)
{}
virtual ~CallableRegistryImpl(void){}
PMCC call(const std::vector<PMCC> &args)
{
if (_fcn0) return PMC_M((_obj->*_fcn0)());
if (_fcn1) return PMC_M((_obj->*_fcn1)(args[0].safe_as<Arg0>()));
if (_fcn2) return PMC_M((_obj->*_fcn2)(args[0].safe_as<Arg0>(), args[1].safe_as<Arg1>()));
return PMCC();//should not get here
}
private:
ClassType *_obj;
Fcn0 _fcn0; Fcn1 _fcn1; Fcn2 _fcn2;
};
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 CallableRegistryImpl<ClassType, ReturnType, int, int>(obj, fcn);
_register_call(key, fr);
}
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 CallableRegistryImpl<ClassType, ReturnType, Arg0, int>(obj, NULL, fcn);
_register_call(key, fr);
}
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 CallableRegistryImpl<ClassType, ReturnType, Arg0, Arg1>(obj, NULL, NULL, fcn);
_register_call(key, fr);
}
template <typename ReturnType> ReturnType Callable::call(const std::string &key)
{
std::vector<PMCC> args;
PMCC r = _handle_call(key, args);
return r.safe_as<ReturnType>();
}
template <typename ReturnType, typename Arg0> ReturnType Callable::call(const std::string &key, const Arg0 &a0)
{
std::vector<PMCC> args;
args.push_back(PMC_M(a0));
PMCC r = _handle_call(key, args);
return r.safe_as<ReturnType>();
}
template <typename ReturnType, typename Arg0, typename Arg1> ReturnType Callable::call(const std::string &key, const Arg0 &a0, const Arg1 &a1)
{
std::vector<PMCC> args;
args.push_back(PMC_M(a0));
args.push_back(PMC_M(a1));
PMCC r = _handle_call(key, args);
return r.safe_as<ReturnType>();
}
inline void Callable::call(const std::string &key)
{
std::vector<PMCC> args;
_handle_call(key, args);
}
template <typename Arg0> void Callable::call(const std::string &key, const Arg0 &a0)
{
std::vector<PMCC> args;
args.push_back(PMC_M(a0));
_handle_call(key, args);
}
template <typename Arg0, typename Arg1> void Callable::call(const std::string &key, const Arg0 &a0, const Arg1 &a1)
{
std::vector<PMCC> args;
args.push_back(PMC_M(a0));
args.push_back(PMC_M(a1));
_handle_call(key, args);
}
} //namespace gras
#endif /*INCLUDED_GRAS_DETAIL_CALLABLE_HPP*/
|