forked from etr/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinders.hpp
More file actions
374 lines (314 loc) · 12.4 KB
/
binders.hpp
File metadata and controls
374 lines (314 loc) · 12.4 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#if !defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION)
#error "Only <httpserver.hpp> or <httpserverpp> can be included directly."
#endif
#ifndef _BINDERS_HPP_
#define _BINDERS_HPP_
namespace httpserver {
namespace details
{
namespace binders
{
class generic_class;
const int MEMFUNC_SIZE = sizeof(void (generic_class::*)());
template<int N>
struct converter
{
template<typename X,
typename func_type,
typename generic_mem_func_type>
inline static generic_class* convert(
X* pmem,
func_type func,
generic_mem_func_type &bound
)
{
return 0;
}
};
template<>
struct converter<MEMFUNC_SIZE>
{
template<typename X,
typename func_type,
typename generic_mem_func_type>
inline static generic_class* convert(
X* pmem,
func_type func,
generic_mem_func_type &bound
)
{
bound = reinterpret_cast<generic_mem_func_type>(func);
return reinterpret_cast<generic_class*>(pmem);
}
};
template<typename generic_mem,
typename static_function,
typename void_static_function>
class binder
{
private:
typedef void (generic_class::*generic_mem_fun)();
typedef void (*generic_mem_ptr)();
generic_class *pmem;
generic_mem_fun _pfunc;
generic_mem_ptr _spfunc;
public:
binder()
{
}
binder(const binder& o):
pmem(o.pmem),
_pfunc(o._pfunc),
_spfunc(o._spfunc)
{
}
template<typename X, typename Y>
binder(X* pmem, Y fun):
pmem(converter<sizeof(fun)>::convert(pmem, fun, _pfunc)),
_spfunc(0)
{
}
template<class DC, class parent_invoker>
binder(DC* pp, parent_invoker invoker, static_function fun):
pmem(converter<sizeof(invoker)>::convert(pp, invoker, _pfunc)),
_spfunc(reinterpret_cast<generic_mem_ptr>(fun))
{
}
inline generic_class* exec() const
{
return pmem;
}
inline generic_mem get_mem_ptr() const
{
return reinterpret_cast<const generic_mem>(_pfunc);
}
inline void_static_function get_static_func() const
{
return reinterpret_cast<void_static_function>(_spfunc);
}
};
template<typename RET_TYPE=void>
class functor_zero
{
private:
typedef RET_TYPE (*static_function)();
typedef RET_TYPE (*void_static_function)();
typedef RET_TYPE (generic_class::*generic_mem)();
typedef binder<generic_mem,
static_function, void_static_function> binder_type;
binder_type _binder;
RET_TYPE exec_static() const
{
return (*(_binder.get_static_func()))();
}
functor_zero& operator=(const functor_zero&)
{
return *this;
}
public:
typedef functor_zero type;
functor_zero() { }
template <typename X, typename Y>
functor_zero(Y* pmem, RET_TYPE(X::*func)()):
_binder(reinterpret_cast<X*>(pmem), func)
{
}
template <typename X, typename Y>
functor_zero(Y* pmem, RET_TYPE(X::*func)() const ):
_binder(reinterpret_cast<X*>(pmem), func)
{
}
functor_zero(RET_TYPE(*func)() ):
_binder(this, &functor_zero::exec_static, func)
{
}
RET_TYPE operator() () const
{
return (_binder.exec()->*(_binder.get_mem_ptr()))();
}
};
template<typename PAR1, typename RET_TYPE=void>
class functor_one
{
private:
typedef RET_TYPE (*static_function)(PAR1 p1);
typedef RET_TYPE (*void_static_function)(PAR1 p1);
typedef RET_TYPE (generic_class::*generic_mem)(PAR1 p1);
typedef binder<generic_mem,
static_function, void_static_function> binder_type;
binder_type _binder;
RET_TYPE exec_static(PAR1 p1) const
{
return (*(_binder.get_static_func()))(p1);
}
functor_one& operator=(const functor_one&)
{
return *this;
}
public:
typedef functor_one type;
functor_one() { }
template <typename X, typename Y>
functor_one(Y* pmem, RET_TYPE(X::*func)(PAR1 p1) ):
_binder(reinterpret_cast<X*>(pmem), func)
{
}
template <typename X, typename Y>
functor_one(Y* pmem, RET_TYPE(X::*func)(PAR1 p1) const ):
_binder(reinterpret_cast<X*>(pmem), func)
{
}
functor_one(RET_TYPE(*func)(PAR1 p1) ):
_binder(this, &functor_one::exec_static, func)
{
}
RET_TYPE operator() (PAR1 p1) const
{
return (_binder.exec()->*(_binder.get_mem_ptr()))(p1);
}
};
template<typename PAR1, typename PAR2, typename RET_TYPE=void>
class functor_two
{
private:
typedef RET_TYPE (*static_function)(PAR1 p1, PAR2 p2);
typedef RET_TYPE (*void_static_function)(PAR1 p1, PAR2 p2);
typedef RET_TYPE
(generic_class::*generic_mem)(PAR1 p1, PAR2 p2);
typedef binder<
generic_mem, static_function, void_static_function
> binder_type;
binder_type _binder;
RET_TYPE exec_static(PAR1 p1, PAR2 p2) const
{
return (*(_binder.get_static_func()))(p1, p2);
}
public:
typedef functor_two type;
functor_two() { }
functor_two(const functor_two& o):
_binder(o._binder)
{
}
template <typename X, typename Y>
functor_two(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2) const ):
_binder(reinterpret_cast<X*>(pmem), func)
{
}
template <typename X, typename Y>
functor_two(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2) ):
_binder(reinterpret_cast<X*>(pmem), func)
{
}
functor_two(RET_TYPE(*func)(PAR1 p1, PAR2 p2) ):
_binder(this, &functor_two::exec_static, func)
{
}
RET_TYPE operator() (PAR1 p1, PAR2 p2) const
{
return (_binder.exec()->*(_binder.get_mem_ptr()))(p1, p2);
}
};
template<typename PAR1, typename PAR2, typename PAR3, typename RET_TYPE=void>
class functor_three
{
private:
typedef RET_TYPE (*static_function)(PAR1 p1, PAR2 p2, PAR3 p3);
typedef RET_TYPE (*void_static_function)(PAR1 p1, PAR2 p2, PAR3 p3);
typedef RET_TYPE
(generic_class::*generic_mem)(PAR1 p1, PAR2 p2, PAR3 p3);
typedef binder<
generic_mem, static_function, void_static_function
> binder_type;
binder_type _binder;
RET_TYPE exec_static(PAR1 p1, PAR2 p2, PAR3 p3) const
{
return (*(_binder.get_static_func()))(p1, p2, p3);
}
public:
typedef functor_three type;
functor_three() { }
functor_three(const functor_three& o):
_binder(o._binder)
{
}
template <typename X, typename Y>
functor_three(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3) const ):
_binder(reinterpret_cast<X*>(pmem), func)
{
}
template <typename X, typename Y>
functor_three(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3) ):
_binder(reinterpret_cast<X*>(pmem), func)
{
}
functor_three(RET_TYPE(*func)(PAR1 p1, PAR2 p2, PAR3 p3) ):
_binder(this, &functor_three::exec_static, func)
{
}
RET_TYPE operator() (PAR1 p1, PAR2 p2, PAR3 p3) const
{
return (_binder.exec()->*(_binder.get_mem_ptr()))(p1, p2, p3);
}
};
template<typename PAR1, typename PAR2, typename PAR3, typename PAR4, typename RET_TYPE=void>
class functor_four
{
private:
typedef RET_TYPE (*static_function)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4);
typedef RET_TYPE (*void_static_function)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4);
typedef RET_TYPE
(generic_class::*generic_mem)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4);
typedef binder<
generic_mem, static_function, void_static_function
> binder_type;
binder_type _binder;
RET_TYPE exec_static(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) const
{
return (*(_binder.get_static_func()))(p1, p2, p3, p4);
}
public:
typedef functor_four type;
functor_four() { }
functor_four(const functor_four& o):
_binder(o._binder)
{
}
template <typename X, typename Y>
functor_four(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) const ):
_binder(reinterpret_cast<X*>(pmem), func)
{
}
template <typename X, typename Y>
functor_four(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) ):
_binder(reinterpret_cast<X*>(pmem), func)
{
}
functor_four(RET_TYPE(*func)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) ):
_binder(this, &functor_four::exec_static, func)
{
}
RET_TYPE operator() (PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) const
{
return (_binder.exec()->*(_binder.get_mem_ptr()))(p1, p2, p3, p4);
}
};
}
}}
#endif