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
210 lines (180 loc) · 6.7 KB
/
binders.hpp
File metadata and controls
210 lines (180 loc) · 6.7 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
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014 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 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)
{
}
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) ):
_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);
}
};
}
}}
#endif