|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +
|
| 19 | +*/ |
| 20 | + |
| 21 | +#if !defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION) |
| 22 | +#error "Only <httpserver.hpp> or <httpserverpp> can be included directly." |
| 23 | +#endif |
| 24 | + |
| 25 | +#ifndef _BINDERS_HPP_ |
| 26 | +#define _BINDERS_HPP_ |
| 27 | + |
| 28 | +namespace httpserver { |
| 29 | +namespace details |
| 30 | +{ |
| 31 | + namespace binders |
| 32 | + { |
| 33 | + class generic_class; |
| 34 | + const int MEMFUNC_SIZE = sizeof(void (generic_class::*)()); |
| 35 | + |
| 36 | + template<int N> |
| 37 | + struct converter |
| 38 | + { |
| 39 | + template<typename X, typename func_type, typename generic_mem_func_type> |
| 40 | + inline static generic_class* convert(X* pmem, func_type func, generic_mem_func_type &bound) |
| 41 | + { |
| 42 | + typedef char ERROR_your_compiler_does_not_support_this_member_pointer[N-100]; |
| 43 | + return 0; |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + template<> |
| 48 | + struct converter<MEMFUNC_SIZE> |
| 49 | + { |
| 50 | + template<typename X, typename func_type, typename generic_mem_func_type> |
| 51 | + inline static generic_class* convert(X* pmem, func_type func, generic_mem_func_type &bound) |
| 52 | + { |
| 53 | + bound = reinterpret_cast<generic_mem_func_type>(func); |
| 54 | + return reinterpret_cast<generic_class*>(pmem); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + template<typename generic_mem, typename static_function, typename void_static_function> |
| 59 | + class binder |
| 60 | + { |
| 61 | + private: |
| 62 | + typedef void (generic_class::*generic_mem_fun)(); |
| 63 | + generic_class *pmem; |
| 64 | + generic_mem_fun _pfunc; |
| 65 | + |
| 66 | + typedef void (*generic_mem_ptr)(); |
| 67 | + generic_mem_ptr _spfunc; |
| 68 | + |
| 69 | + public: |
| 70 | + template<typename X, typename Y> |
| 71 | + inline void bind(X* pmem, Y fun) |
| 72 | + { |
| 73 | + this->pmem = converter<sizeof(fun)>::convert(pmem, fun, _pfunc); |
| 74 | + _spfunc = 0; |
| 75 | + } |
| 76 | + template <class DC, class parent_invoker> |
| 77 | + inline void bind_static(DC *pp, parent_invoker invoker, static_function fun ) |
| 78 | + { |
| 79 | + if (fun==0) |
| 80 | + _pfunc=0; |
| 81 | + else |
| 82 | + bind(pp, invoker); |
| 83 | + _spfunc = reinterpret_cast<generic_mem_ptr>(fun); |
| 84 | + } |
| 85 | + inline generic_class* exec() const |
| 86 | + { |
| 87 | + return pmem; |
| 88 | + } |
| 89 | + inline generic_mem get_mem_ptr() const |
| 90 | + { |
| 91 | + return reinterpret_cast<generic_mem>(_pfunc); |
| 92 | + } |
| 93 | + inline void_static_function get_static_func() const |
| 94 | + { |
| 95 | + return reinterpret_cast<void_static_function>(_spfunc); |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + template<typename RET_TYPE=void> |
| 100 | + class functor_zero |
| 101 | + { |
| 102 | + private: |
| 103 | + typedef RET_TYPE (*static_function)(); |
| 104 | + typedef RET_TYPE (*void_static_function)(); |
| 105 | + typedef RET_TYPE (generic_class::*generic_mem)(); |
| 106 | + typedef binder<generic_mem, static_function, void_static_function> binder_type; |
| 107 | + binder_type _binder; |
| 108 | + |
| 109 | + RET_TYPE exec_static() const |
| 110 | + { |
| 111 | + return (*(_binder.get_static_func()))(); |
| 112 | + } |
| 113 | + public: |
| 114 | + typedef functor_zero type; |
| 115 | + functor_zero() { } |
| 116 | + |
| 117 | + template <typename X, typename Y> |
| 118 | + functor_zero(Y* pmem, RET_TYPE(X::*func)() ) |
| 119 | + { |
| 120 | + _binder.bind(reinterpret_cast<X*>(pmem), func); |
| 121 | + } |
| 122 | + functor_zero(RET_TYPE(*func)() ) |
| 123 | + { |
| 124 | + bind(func); |
| 125 | + } |
| 126 | + template < class X, class Y > |
| 127 | + inline void bind(Y* pmem, RET_TYPE(X::*func)()) |
| 128 | + { |
| 129 | + _binder.bind(reinterpret_cast<X*>(pmem), func); |
| 130 | + } |
| 131 | + inline void bind(RET_TYPE(*func)()) |
| 132 | + { |
| 133 | + _binder.bind_static(this, &functor_zero::exec_static, func); |
| 134 | + } |
| 135 | + RET_TYPE operator() () const |
| 136 | + { |
| 137 | + return (_binder.exec()->*(_binder.get_mem_ptr()))(); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + template<typename PAR1, typename RET_TYPE=void> |
| 142 | + class functor_one |
| 143 | + { |
| 144 | + private: |
| 145 | + typedef RET_TYPE (*static_function)(PAR1 p1); |
| 146 | + typedef RET_TYPE (*void_static_function)(PAR1 p1); |
| 147 | + typedef RET_TYPE (generic_class::*generic_mem)(PAR1 p1); |
| 148 | + typedef binder<generic_mem, static_function, void_static_function> binder_type; |
| 149 | + binder_type _binder; |
| 150 | + |
| 151 | + RET_TYPE exec_static(PAR1 p1) const |
| 152 | + { |
| 153 | + return (*(_binder.get_static_func()))(p1); |
| 154 | + } |
| 155 | + public: |
| 156 | + typedef functor_one type; |
| 157 | + functor_one() { } |
| 158 | + |
| 159 | + template <typename X, typename Y> |
| 160 | + functor_one(Y* pmem, RET_TYPE(X::*func)(PAR1 p1) ) |
| 161 | + { |
| 162 | + _binder.bind(reinterpret_cast<X*>(pmem), func); |
| 163 | + } |
| 164 | + functor_one(RET_TYPE(*func)(PAR1 p1) ) |
| 165 | + { |
| 166 | + bind(func); |
| 167 | + } |
| 168 | + template < class X, class Y > |
| 169 | + inline void bind(Y* pmem, RET_TYPE(X::*func)(PAR1 p1)) |
| 170 | + { |
| 171 | + _binder.bind(reinterpret_cast<X*>(pmem), func); |
| 172 | + } |
| 173 | + inline void bind(RET_TYPE(*func)(PAR1 P1)) |
| 174 | + { |
| 175 | + _binder.bind_static(this, &functor_one::exec_static, func); |
| 176 | + } |
| 177 | + RET_TYPE operator() (PAR1 p1) const |
| 178 | + { |
| 179 | + return (_binder.exec()->*(_binder.get_mem_ptr()))(p1); |
| 180 | + } |
| 181 | + }; |
| 182 | + |
| 183 | + template<typename PAR1, typename PAR2, typename RET_TYPE=void> |
| 184 | + class functor_two |
| 185 | + { |
| 186 | + private: |
| 187 | + typedef RET_TYPE (*static_function)(PAR1 p1, PAR2 p2); |
| 188 | + typedef RET_TYPE (*void_static_function)(PAR1 p1, PAR2 p2); |
| 189 | + typedef RET_TYPE (generic_class::*generic_mem)(PAR1 p1, PAR2 p2); |
| 190 | + typedef binder<generic_mem, static_function, void_static_function> binder_type; |
| 191 | + binder_type _binder; |
| 192 | + |
| 193 | + RET_TYPE exec_static(PAR1 p1, PAR2 p2) const |
| 194 | + { |
| 195 | + return (*(_binder.get_static_func()))(p1, p2); |
| 196 | + } |
| 197 | + public: |
| 198 | + typedef functor_two type; |
| 199 | + functor_two() { } |
| 200 | + |
| 201 | + template <typename X, typename Y> |
| 202 | + functor_two(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2) ) |
| 203 | + { |
| 204 | + _binder.bind(reinterpret_cast<X*>(pmem), func); |
| 205 | + } |
| 206 | + functor_two(RET_TYPE(*func)(PAR1 p1, PAR2 p2) ) |
| 207 | + { |
| 208 | + bind(func); |
| 209 | + } |
| 210 | + template < class X, class Y > |
| 211 | + inline void bind(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2)) |
| 212 | + { |
| 213 | + _binder.bind(reinterpret_cast<X*>(pmem), func); |
| 214 | + } |
| 215 | + inline void bind(RET_TYPE(*func)(PAR1 P1, PAR2 p2)) |
| 216 | + { |
| 217 | + _binder.bind_static(this, &functor_two::exec_static, func); |
| 218 | + } |
| 219 | + RET_TYPE operator() (PAR1 p1, PAR2 p2) const |
| 220 | + { |
| 221 | + return (_binder.exec()->*(_binder.get_mem_ptr()))(p1, p2); |
| 222 | + } |
| 223 | + }; |
| 224 | + |
| 225 | + template<typename PAR1, typename PAR2, typename PAR3, typename RET_TYPE=void> |
| 226 | + class functor_three |
| 227 | + { |
| 228 | + private: |
| 229 | + typedef RET_TYPE (*static_function)(PAR1 p1, PAR2 p2, PAR3 p3); |
| 230 | + typedef RET_TYPE (*void_static_function)(PAR1 p1, PAR2 p2, PAR3 p3); |
| 231 | + typedef RET_TYPE (generic_class::*generic_mem)(PAR1 p1, PAR2 p2, PAR3 p3); |
| 232 | + typedef binder<generic_mem, static_function, void_static_function> binder_type; |
| 233 | + binder_type _binder; |
| 234 | + |
| 235 | + RET_TYPE exec_static(PAR1 p1, PAR2 p2, PAR3 p3) const |
| 236 | + { |
| 237 | + return (*(_binder.get_static_func()))(p1, p2, p3); |
| 238 | + } |
| 239 | + public: |
| 240 | + typedef functor_three type; |
| 241 | + functor_three() { } |
| 242 | + |
| 243 | + template <typename X, typename Y> |
| 244 | + functor_three(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3) ) |
| 245 | + { |
| 246 | + _binder.bind(reinterpret_cast<X*>(pmem), func); |
| 247 | + } |
| 248 | + functor_three(RET_TYPE(*func)(PAR1 p1, PAR2 p2, PAR3 p3) ) |
| 249 | + { |
| 250 | + bind(func); |
| 251 | + } |
| 252 | + template < class X, class Y > |
| 253 | + inline void bind(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3)) |
| 254 | + { |
| 255 | + _binder.bind(reinterpret_cast<X*>(pmem), func); |
| 256 | + } |
| 257 | + inline void bind(RET_TYPE(*func)(PAR1 P1, PAR2 p2, PAR3 p3)) |
| 258 | + { |
| 259 | + _binder.bind_static(this, &functor_three::exec_static, func); |
| 260 | + } |
| 261 | + RET_TYPE operator() (PAR1 p1, PAR2 p2, PAR3 p3) const |
| 262 | + { |
| 263 | + return (_binder.exec()->*(_binder.get_mem_ptr()))(p1, p2, p3); |
| 264 | + } |
| 265 | + }; |
| 266 | + |
| 267 | + template<typename PAR1, typename PAR2, typename PAR3, typename PAR4, typename RET_TYPE=void> |
| 268 | + class functor_four |
| 269 | + { |
| 270 | + private: |
| 271 | + typedef RET_TYPE (*static_function)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4); |
| 272 | + typedef RET_TYPE (*void_static_function)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4); |
| 273 | + typedef RET_TYPE (generic_class::*generic_mem)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4); |
| 274 | + typedef binder<generic_mem, static_function, void_static_function> binder_type; |
| 275 | + binder_type _binder; |
| 276 | + |
| 277 | + RET_TYPE exec_static(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) const |
| 278 | + { |
| 279 | + return (*(_binder.get_static_func()))(p1, p2, p3, p4); |
| 280 | + } |
| 281 | + public: |
| 282 | + typedef functor_four type; |
| 283 | + functor_four() { } |
| 284 | + |
| 285 | + template <typename X, typename Y> |
| 286 | + functor_four(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) ) |
| 287 | + { |
| 288 | + _binder.bind(reinterpret_cast<X*>(pmem), func); |
| 289 | + } |
| 290 | + functor_four(RET_TYPE(*func)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) ) |
| 291 | + { |
| 292 | + bind(func); |
| 293 | + } |
| 294 | + template < class X, class Y > |
| 295 | + inline void bind(Y* pmem, RET_TYPE(X::*func)(PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4)) |
| 296 | + { |
| 297 | + _binder.bind(reinterpret_cast<X*>(pmem), func); |
| 298 | + } |
| 299 | + inline void bind(RET_TYPE(*func)(PAR1 P1, PAR2 p2, PAR3 p3, PAR4 p4)) |
| 300 | + { |
| 301 | + _binder.bind_static(this, &functor_four::exec_static, func); |
| 302 | + } |
| 303 | + RET_TYPE operator() (PAR1 p1, PAR2 p2, PAR3 p3, PAR4 p4) const |
| 304 | + { |
| 305 | + return (_binder.exec()->*(_binder.get_mem_ptr()))(p1, p2, p3, p4); |
| 306 | + } |
| 307 | + }; |
| 308 | + } |
| 309 | +}} |
| 310 | + |
| 311 | +#endif |
0 commit comments