forked from reiver-dev/cppcallback
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelegate.hpp
More file actions
161 lines (121 loc) · 3.55 KB
/
delegate.hpp
File metadata and controls
161 lines (121 loc) · 3.55 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
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
#ifndef DELEGATE_HPP
#define DELEGATE_HPP
#define CB_FORWARD(type, obj) static_cast<type &&>(obj)
namespace CB {
namespace detail {
/// Simple wrapper to object and his member function
template<typename>
class Delegate;
template<typename R, typename... ARGS>
class Delegate<R (ARGS...)> {
public:
using sig_type = R (ARGS...);
using this_type = Delegate<sig_type>;
using func_t = R (*)(void *, ARGS...);
Delegate()
: m_obj(0)
, m_func(0)
{
//
}
Delegate(const this_type &o)
: m_obj(o.m_obj)
, m_func(o.m_func)
{
//
}
this_type& operator=(const this_type &o)
{
m_obj = o.m_obj;
m_func = o.m_func;
return *this;
}
explicit operator bool()
{
return m_func != 0 ? m_obj : 0;
}
R operator()(ARGS... args) const {
return m_func(m_obj, args...);
}
R operator()(ARGS... args) const volatile {
return m_func(m_obj, args...);
}
private:
void *m_obj;
func_t m_func;
Delegate(void *obj, func_t func)
: m_obj(obj)
, m_func(func)
{
//
}
template<typename _T, typename _R, typename... _ARGS>
friend struct DelegateWrapper;
template<typename _T, typename _R, typename... _ARGS>
friend struct DelegateConstWrapper;
template<typename _R, typename... _ARGS>
friend struct DelegateFFWrapper;
};
template<typename T, typename R, typename... ARGS>
struct DelegateWrapper {
using func_t = R (*)(void *, ARGS...);
using delegate_t = Delegate<R (ARGS...)>;
template<R (T::*MFUN)(ARGS...)>
static delegate_t bind(T *obj)
{
struct _ {
static R wrapper(void *obj, ARGS... args)
{
return (static_cast<T *>(obj)->*MFUN)(CB_FORWARD(ARGS, args)...);
}
};
return delegate_t(obj, _::wrapper);
}
};
template<typename T, typename R, typename... ARGS>
struct DelegateConstWrapper {
using func_t = R (*)(void *, ARGS...);
using delegate_t = Delegate<R (ARGS...)>;
template<R (T::*MFUN)(ARGS...) const >
static delegate_t bind(const T *const obj)
{
struct _ {
static R wrapper(void *obj, ARGS... args)
{
return (static_cast<const T *const>(obj)->*MFUN)(CB_FORWARD(ARGS, args)...);
}
};
return delegate_t(const_cast<T *>(obj), _::wrapper);
}
};
template<typename R, typename... ARGS>
struct DelegateFFWrapper {
using func_t = R (*)(void *, ARGS...);
using delegate_t = Delegate<R (ARGS...)>;
template<R (*FUNC)(ARGS...)>
static delegate_t bind()
{
struct _ {
static R wrapper(void *, ARGS... args)
{
return FUNC(CB_FORWARD(ARGS, args)...);
}
};
return delegate_t(nullptr, _::wrapper);
}
};
template<class T, typename R, typename ... ARGS>
DelegateWrapper<T, R, ARGS...> create_delegate(R (T::*)(ARGS...));
template<class T, typename R, typename ... ARGS>
DelegateConstWrapper<T, R, ARGS...> create_delegate(R (T::*)(ARGS...) const);
template<typename R, typename ... ARGS>
DelegateFFWrapper<R, ARGS...> create_delegate(R (*)(ARGS...));
}
using detail::Delegate;
}
#undef CB_FORWARD
#define __CB_DELEGATE_INIT(instance, func) decltype(CB::detail::create_delegate(func))::bind<func>(instance)
#define __CB_DELEGATE_INIT_FF(func) decltype(CB::detail::create_delegate(func))::bind<func>()
#define CB_DELEGATE_INIT __CB_DELEGATE_INIT
#define CB_DELEGATE_FF __CB_DELEGATE_INIT_FF
#endif /* DELEGATE_HPP */