Skip to content

Commit 4399d38

Browse files
author
Sebastiano Merlino
committed
Solved bug on event_supplier registering.
This includes additional binders having being crafted to support the use case.
1 parent 51f4ce9 commit 4399d38

File tree

3 files changed

+172
-11
lines changed

3 files changed

+172
-11
lines changed

src/httpserver/binders.hpp

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,53 @@ namespace details
119119
}
120120
};
121121

122+
template<typename RET_TYPE=void>
123+
class functor_zero
124+
{
125+
private:
126+
typedef RET_TYPE (*static_function)();
127+
typedef RET_TYPE (*void_static_function)();
128+
typedef RET_TYPE (generic_class::*generic_mem)();
129+
typedef binder<generic_mem,
130+
static_function, void_static_function> binder_type;
131+
binder_type _binder;
132+
133+
RET_TYPE exec_static() const
134+
{
135+
return (*(_binder.get_static_func()))();
136+
}
137+
138+
functor_zero& operator=(const functor_zero&)
139+
{
140+
return *this;
141+
}
142+
public:
143+
typedef functor_zero type;
144+
functor_zero() { }
145+
146+
template <typename X, typename Y>
147+
functor_zero(Y* pmem, RET_TYPE(X::*func)()):
148+
_binder(reinterpret_cast<X*>(pmem), func)
149+
{
150+
}
151+
152+
template <typename X, typename Y>
153+
functor_zero(Y* pmem, RET_TYPE(X::*func)() const ):
154+
_binder(reinterpret_cast<X*>(pmem), func)
155+
{
156+
}
157+
158+
functor_zero(RET_TYPE(*func)() ):
159+
_binder(this, &functor_zero::exec_static, func)
160+
{
161+
}
162+
163+
RET_TYPE operator() () const
164+
{
165+
return (_binder.exec()->*(_binder.get_mem_ptr()))();
166+
}
167+
};
168+
122169
template<typename PAR1, typename RET_TYPE=void>
123170
class functor_one
124171
{
@@ -149,6 +196,12 @@ namespace details
149196
{
150197
}
151198

199+
template <typename X, typename Y>
200+
functor_one(Y* pmem, RET_TYPE(X::*func)(PAR1 p1) const ):
201+
_binder(reinterpret_cast<X*>(pmem), func)
202+
{
203+
}
204+
152205
functor_one(RET_TYPE(*func)(PAR1 p1) ):
153206
_binder(this, &functor_one::exec_static, func)
154207
{
@@ -189,11 +242,18 @@ namespace details
189242
{
190243
}
191244

245+
template <typename X, typename Y>
246+
functor_two(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2) const ):
247+
_binder(reinterpret_cast<X*>(pmem), func)
248+
{
249+
}
250+
192251
template <typename X, typename Y>
193252
functor_two(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2) ):
194253
_binder(reinterpret_cast<X*>(pmem), func)
195254
{
196255
}
256+
197257
functor_two(RET_TYPE(*func)(PAR1 p1, PAR2 p2) ):
198258
_binder(this, &functor_two::exec_static, func)
199259
{
@@ -204,6 +264,110 @@ namespace details
204264
return (_binder.exec()->*(_binder.get_mem_ptr()))(p1, p2);
205265
}
206266
};
267+
268+
template<typename PAR1, typename PAR2, typename PAR3, typename RET_TYPE=void>
269+
class functor_three
270+
{
271+
private:
272+
typedef RET_TYPE (*static_function)(PAR1 p1, PAR2 p2, PAR3 p3);
273+
typedef RET_TYPE (*void_static_function)(PAR1 p1, PAR2 p2, PAR3 p3);
274+
275+
typedef RET_TYPE
276+
(generic_class::*generic_mem)(PAR1 p1, PAR2 p2, PAR3 p3);
277+
278+
typedef binder<
279+
generic_mem, static_function, void_static_function
280+
> binder_type;
281+
282+
binder_type _binder;
283+
284+
RET_TYPE exec_static(PAR1 p1, PAR2 p2, PAR3 p3) const
285+
{
286+
return (*(_binder.get_static_func()))(p1, p2, p3);
287+
}
288+
public:
289+
typedef functor_three type;
290+
functor_three() { }
291+
292+
functor_three(const functor_three& o):
293+
_binder(o._binder)
294+
{
295+
}
296+
297+
template <typename X, typename Y>
298+
functor_three(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3) const ):
299+
_binder(reinterpret_cast<X*>(pmem), func)
300+
{
301+
}
302+
303+
template <typename X, typename Y>
304+
functor_three(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3) ):
305+
_binder(reinterpret_cast<X*>(pmem), func)
306+
{
307+
}
308+
309+
functor_three(RET_TYPE(*func)(PAR1 p1, PAR2 p2, PAR3 p3) ):
310+
_binder(this, &functor_three::exec_static, func)
311+
{
312+
}
313+
314+
RET_TYPE operator() (PAR1 p1, PAR2 p2, PAR3 p3) const
315+
{
316+
return (_binder.exec()->*(_binder.get_mem_ptr()))(p1, p2, p3);
317+
}
318+
};
319+
320+
template<typename PAR1, typename PAR2, typename PAR3, typename PAR4, typename RET_TYPE=void>
321+
class functor_four
322+
{
323+
private:
324+
typedef RET_TYPE (*static_function)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4);
325+
typedef RET_TYPE (*void_static_function)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4);
326+
327+
typedef RET_TYPE
328+
(generic_class::*generic_mem)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4);
329+
330+
typedef binder<
331+
generic_mem, static_function, void_static_function
332+
> binder_type;
333+
334+
binder_type _binder;
335+
336+
RET_TYPE exec_static(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) const
337+
{
338+
return (*(_binder.get_static_func()))(p1, p2, p3, p4);
339+
}
340+
public:
341+
typedef functor_four type;
342+
functor_four() { }
343+
344+
functor_four(const functor_four& o):
345+
_binder(o._binder)
346+
{
347+
}
348+
349+
template <typename X, typename Y>
350+
functor_four(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) const ):
351+
_binder(reinterpret_cast<X*>(pmem), func)
352+
{
353+
}
354+
355+
template <typename X, typename Y>
356+
functor_four(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) ):
357+
_binder(reinterpret_cast<X*>(pmem), func)
358+
{
359+
}
360+
361+
functor_four(RET_TYPE(*func)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) ):
362+
_binder(this, &functor_four::exec_static, func)
363+
{
364+
}
365+
366+
RET_TYPE operator() (PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) const
367+
{
368+
return (_binder.exec()->*(_binder.get_mem_ptr()))(p1, p2, p3, p4);
369+
}
370+
};
207371
}
208372
}}
209373

src/httpserver/details/event_tuple.hpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define _EVENT_TUPLE_HPP_
2727

2828
#include "httpserver/event_supplier.hpp"
29+
#include "httpserver/binders.hpp"
2930

3031
namespace httpserver {
3132

@@ -45,23 +46,19 @@ namespace details
4546
MHD_socket*
4647
);
4748

48-
typedef struct timeval(*get_timeout_ptr)();
49-
typedef void(*dispatch_events_ptr)();
50-
supply_events_ptr supply_events;
51-
get_timeout_ptr get_timeout;
52-
dispatch_events_ptr dispatch_events;
49+
binders::functor_four<fd_set*, fd_set*, fd_set*, MHD_socket*, void> supply_events;
50+
binders::functor_zero<struct timeval> get_timeout;
51+
binders::functor_zero<void> dispatch_events;
5352

5453
event_tuple();
5554

5655
friend class ::httpserver::webserver;
5756
public:
5857
template<typename T>
5958
event_tuple(event_supplier<T>* es):
60-
supply_events(std::bind1st(std::mem_fun(&T::supply_events),es)),
61-
get_timeout(std::bind1st(std::mem_fun(&T::get_timeout), es)),
62-
dispatch_events(std::bind1st(
63-
std::mem_fun(&T::dispatch_events), es)
64-
)
59+
supply_events(binders::functor_four<fd_set*, fd_set*, fd_set*, MHD_socket*, void>(es, &T::supply_events)),
60+
get_timeout(binders::functor_zero<struct timeval>(es, &T::get_timeout)),
61+
dispatch_events(binders::functor_zero<void>(es, &T::dispatch_events))
6562
{
6663
}
6764
};

src/httpserver/webserver.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class webserver
175175
event_supplier<T>* ev_supplier
176176
)
177177
{
178-
register_event_supplier(id, details::event_tuple(&ev_supplier));
178+
register_event_supplier(id, details::event_tuple(ev_supplier));
179179
}
180180

181181
void remove_event_supplier(const std::string& id);

0 commit comments

Comments
 (0)