-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.h
More file actions
98 lines (75 loc) · 2.62 KB
/
Copy pathwrapper.h
File metadata and controls
98 lines (75 loc) · 2.62 KB
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
#ifndef REFLECTION_WRAPPER_H
#define REFLECTION_WRAPPER_H
#include <functional>
#include <memory>
namespace reflection {
template<typename Result, typename ...Args>
class IMethod {
public:
virtual ~IMethod() {};
Result operator()(Args... args) {
return invoke(args...);
}
private:
virtual Result invoke(Args... args) = 0;
};
template<typename Result, typename ...Args>
class SimpleMethod : public IMethod<Result, Args...> {
public:
SimpleMethod(Result (*func)(Args ...)) : method(func) {}
private:
virtual Result invoke(Args... args) override {
return method(args ...);
}
Result (*method)(Args ...);
};
template<typename Object, typename Result, typename ...Args>
class MemberMethod : public IMethod<Result, Args...> {
public:
MemberMethod(Object *obj, Result (Object::*func)(Args ...)) :
obj(obj), method(func) {}
private:
virtual Result invoke(Args... args) override {
return ((*obj).*method)(args ...);
}
std::shared_ptr<Object> obj;
Result (Object::*method)(Args ...);
};
template<typename Result, typename ...Args>
struct Wrapper {
public:
Wrapper() {}
template<typename Object>
Wrapper(Object *obj, Result (Object::*func)(Args...)) {
iMethod = std::shared_ptr<IMethod<Result, Args...>>(
new MemberMethod<Object, Result, Args...>(obj, func));
}
Wrapper(Result (*func)(Args...)) {
iMethod = std::shared_ptr<IMethod<Result, Args...>>(
new SimpleMethod<Result, Args...>(func));
}
~Wrapper() {
iMethod.reset();
}
Result operator()(Args... args) {
return (*iMethod)(args...);
}
Wrapper &operator=(Result (*func)(Args...)) {
iMethod = std::shared_ptr<IMethod<Result, Args...>>(
new SimpleMethod<Result, Args...>(func));
return *this;
}
private:
std::shared_ptr<IMethod<Result, Args...>> iMethod;
};
template<typename Object, typename Result, typename ...Args>
auto
wrapper(Object &x, Result(Object::*fun)(Args...)) -> std::shared_ptr<Wrapper<Result, Args...>> {
return std::make_shared<Wrapper<Result, Args ...>>(&x, fun);
}
template<typename Result, typename ...Args>
auto wrapper(Result (*fun)(Args...)) -> std::shared_ptr<Wrapper<Result, Args...>> {
return std::make_shared<Wrapper<Result, Args ...>>(fun);
}
}
#endif //REFLECTION_WRAPPER_H