-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosure_impl.h
169 lines (134 loc) · 4.62 KB
/
closure_impl.h
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
/*
Closure library 1.1
file "closure_impl.h"
Written by Ivan Yankov aka _Winnie ([email protected])
Many thanks to Wolfhound
*/
//====================================================================================================
//general template
template <class F>
struct Closure;
//forward declaration of specialization
template <TEMPLATE_PARAM_LIST>
struct Closure<R(PARAM_TYPE_LIST)>;
//====================================================================================================
namespace winnie { namespace detail
{
//this template generate function, which call "pmem" member function
template <class T, TEMPLATE_PARAM_LIST, R (T::*pmem)(PARAM_TYPE_LIST)>
R ProxyFunc(void *p_this PARAM_FORM_ARG_LIST_COMMA)
{
return (static_cast<T *>(p_this) ->* pmem)( PARAM_ARG_LIST);
}
//this template generate function, which call "pmem" const member function
template <class T, TEMPLATE_PARAM_LIST, R (T::*pmem)(PARAM_TYPE_LIST)const>
R ProxyFuncConst(void *p_this PARAM_FORM_ARG_LIST_COMMA)
{
return (static_cast<T *>(p_this) ->* pmem)(PARAM_ARG_LIST);
}
namespace CLOSURE_NUM
{
//base for Closure
//for example, if CLOSURE_NUM is Closure2, then
//Closure2::ClosureBase is superclass for Closure<int(long, long)>
template <TEMPLATE_PARAM_LIST>
struct ClosureBase
{
typedef R (*p_proxy_type)(void * PARAM_FORM_ARG_LIST_COMMA);
void *p_this;
p_proxy_type p_proxy;
R operator()(PARAM_FORM_ARG_LIST) { return p_proxy(p_this PARAM_ARG_LIST_COMMA); }
};
} //namespace CLOSURE_NUM
}} //namespace detail
//====================================================================================================
template <TEMPLATE_PARAM_LIST>
struct Closure<R(PARAM_TYPE_LIST)>: public winnie::detail::CLOSURE_NUM::ClosureBase<R PARAM_TYPE_LIST_COMMA>
{
//default initialization
Closure()
{
this->p_this= 0;
this->p_proxy= 0;
}
//initialization whith proxy function "p_proxy_function" and context "p_this".
//signature of p_proxy_function should match template parameters of Closure<...>
//and take additional parameter void *
Closure(
void *p_this,
typename winnie::detail::CLOSURE_NUM::ClosureBase<R PARAM_TYPE_LIST_COMMA>::p_proxy_type p_proxy_function)
{
this->p_this= p_this;
this->p_proxy= p_proxy_function;
}
private:
struct dummy { void nonnull() {} };
typedef void (dummy::*safe_bool)();
public:
//return value which can be implicitly casted to bool.
//true, if object initialized with some non-NULL function
//false, otherwise
operator safe_bool() const
{
if(this->p_proxy)
return &dummy::nonnull;
else
return 0;
}
bool operator!() const
{
return !safe_bool(*this);
}
};
//====================================================================================================
namespace winnie { namespace detail
{
namespace CLOSURE_NUM
{
template <class T, TEMPLATE_PARAM_LIST>
struct CreateClosureHelper
{
template <R (T::*p_mem)(PARAM_TYPE_LIST)>
Closure<R(PARAM_TYPE_LIST)> Init(T *p_this)
{
Closure<R(PARAM_TYPE_LIST)> c(
p_this,
ProxyFunc<T, R PARAM_TYPE_LIST_COMMA , p_mem>);
return c;
}
};
template <class T, TEMPLATE_PARAM_LIST>
struct CreateClosureHelperConst
{
template <R (T::*p_mem)(PARAM_TYPE_LIST)const>
Closure<R(PARAM_TYPE_LIST)> Init(const T *p_this)
{
Closure<R(PARAM_TYPE_LIST)> c(
const_cast<void*>(static_cast<const void*>(p_this)),
ProxyFuncConst<T, R PARAM_TYPE_LIST_COMMA, p_mem>);
return c;
}
};
} //namespace CLOSURE_NUM
//helper function, to deduce return and parameters types of given pointer to member function
template <class T, TEMPLATE_PARAM_LIST>
CLOSURE_NUM::CreateClosureHelper<T, R PARAM_TYPE_LIST_COMMA> CreateClosure(R (T::*)(PARAM_TYPE_LIST))
{
return CLOSURE_NUM::CreateClosureHelper<T, R PARAM_TYPE_LIST_COMMA>();
}
//helper function, to deduce return and parameters types of given pointer to member const function
template <class T, TEMPLATE_PARAM_LIST>
CLOSURE_NUM::CreateClosureHelperConst<T, R PARAM_TYPE_LIST_COMMA> CreateClosure(R (T::*)(PARAM_TYPE_LIST)const)
{
return CLOSURE_NUM::CreateClosureHelperConst<T, R PARAM_TYPE_LIST_COMMA>();
}
}} //namespace detail
//====================================================================================================
#undef TEMPLATE_PARAM_LIST
#undef PARAM_TYPE_LIST
#undef PARAM_TYPE_LIST_COMMA
#undef PARAM_FORM_ARG_LIST
#undef PARAM_FORM_ARG_LIST_COMMA
#undef PARAM_ARG_LIST
#undef PARAM_ARG_LIST_COMMA
#undef CLOSURE_NUM