| 1 | // rak - Rakshasa's toolbox |
|---|
| 2 | // Copyright (C) 2005-2007, Jari Sundell |
|---|
| 3 | // |
|---|
| 4 | // This program is free software; you can redistribute it and/or modify |
|---|
| 5 | // it under the terms of the GNU General Public License as published by |
|---|
| 6 | // the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | // (at your option) any later version. |
|---|
| 8 | // |
|---|
| 9 | // This program is distributed in the hope that it will be useful, |
|---|
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | // GNU General Public License for more details. |
|---|
| 13 | // |
|---|
| 14 | // You should have received a copy of the GNU General Public License |
|---|
| 15 | // along with this program; if not, write to the Free Software |
|---|
| 16 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 | // |
|---|
| 18 | // In addition, as a special exception, the copyright holders give |
|---|
| 19 | // permission to link the code of portions of this program with the |
|---|
| 20 | // OpenSSL library under certain conditions as described in each |
|---|
| 21 | // individual source file, and distribute linked combinations |
|---|
| 22 | // including the two. |
|---|
| 23 | // |
|---|
| 24 | // You must obey the GNU General Public License in all respects for |
|---|
| 25 | // all of the code used other than OpenSSL. If you modify file(s) |
|---|
| 26 | // with this exception, you may extend this exception to your version |
|---|
| 27 | // of the file(s), but you are not obligated to do so. If you do not |
|---|
| 28 | // wish to do so, delete this exception statement from your version. |
|---|
| 29 | // If you delete this exception statement from all source files in the |
|---|
| 30 | // program, then also delete it here. |
|---|
| 31 | // |
|---|
| 32 | // Contact: Jari Sundell <jaris@ifi.uio.no> |
|---|
| 33 | // |
|---|
| 34 | // Skomakerveien 33 |
|---|
| 35 | // 3185 Skoppum, NORWAY |
|---|
| 36 | |
|---|
| 37 | #ifndef RAK_FUNCTIONAL_H |
|---|
| 38 | #define RAK_FUNCTIONAL_H |
|---|
| 39 | |
|---|
| 40 | #include <functional> |
|---|
| 41 | |
|---|
| 42 | namespace rak { |
|---|
| 43 | |
|---|
| 44 | template <typename Type> |
|---|
| 45 | struct reference_fix { |
|---|
| 46 | typedef Type type; |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | template <typename Type> |
|---|
| 50 | struct reference_fix<Type&> { |
|---|
| 51 | typedef Type type; |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | template <typename Type> |
|---|
| 55 | struct value_t { |
|---|
| 56 | value_t(Type v) : m_v(v) {} |
|---|
| 57 | |
|---|
| 58 | Type operator () () const { return m_v; } |
|---|
| 59 | |
|---|
| 60 | Type m_v; |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | template <typename Type> |
|---|
| 64 | inline value_t<Type> |
|---|
| 65 | value(Type v) { |
|---|
| 66 | return value_t<Type>(v); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | template <typename Type, typename Ftor> |
|---|
| 70 | struct accumulate_t { |
|---|
| 71 | accumulate_t(Type t, Ftor f) : result(t), m_f(f) {} |
|---|
| 72 | |
|---|
| 73 | template <typename Arg> |
|---|
| 74 | void operator () (const Arg& a) { result += m_f(a); } |
|---|
| 75 | |
|---|
| 76 | Type result; |
|---|
| 77 | Ftor m_f; |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | template <typename Type, typename Ftor> |
|---|
| 81 | inline accumulate_t<Type, Ftor> |
|---|
| 82 | accumulate(Type t, Ftor f) { |
|---|
| 83 | return accumulate_t<Type, Ftor>(t, f); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | // Operators: |
|---|
| 87 | |
|---|
| 88 | template <typename Type, typename Ftor> |
|---|
| 89 | struct equal_t { |
|---|
| 90 | typedef bool result_type; |
|---|
| 91 | |
|---|
| 92 | equal_t(Type t, Ftor f) : m_t(t), m_f(f) {} |
|---|
| 93 | |
|---|
| 94 | template <typename Arg> |
|---|
| 95 | bool operator () (Arg& a) { |
|---|
| 96 | return m_t == m_f(a); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | Type m_t; |
|---|
| 100 | Ftor m_f; |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | template <typename Type, typename Ftor> |
|---|
| 104 | inline equal_t<Type, Ftor> |
|---|
| 105 | equal(Type t, Ftor f) { |
|---|
| 106 | return equal_t<Type, Ftor>(t, f); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | template <typename Type, typename Ftor> |
|---|
| 110 | struct equal_ptr_t { |
|---|
| 111 | typedef bool result_type; |
|---|
| 112 | |
|---|
| 113 | equal_ptr_t(Type* t, Ftor f) : m_t(t), m_f(f) {} |
|---|
| 114 | |
|---|
| 115 | template <typename Arg> |
|---|
| 116 | bool operator () (const Arg& a) { |
|---|
| 117 | return *m_t == *m_f(a); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | Type* m_t; |
|---|
| 121 | Ftor m_f; |
|---|
| 122 | }; |
|---|
| 123 | |
|---|
| 124 | template <typename Type, typename Ftor> |
|---|
| 125 | inline equal_ptr_t<Type, Ftor> |
|---|
| 126 | equal_ptr(Type* t, Ftor f) { |
|---|
| 127 | return equal_ptr_t<Type, Ftor>(t, f); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | template <typename Type, typename Ftor> |
|---|
| 131 | struct not_equal_t { |
|---|
| 132 | typedef bool result_type; |
|---|
| 133 | |
|---|
| 134 | not_equal_t(Type t, Ftor f) : m_t(t), m_f(f) {} |
|---|
| 135 | |
|---|
| 136 | template <typename Arg> |
|---|
| 137 | bool operator () (Arg& a) { |
|---|
| 138 | return m_t != m_f(a); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | Type m_t; |
|---|
| 142 | Ftor m_f; |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | template <typename Type, typename Ftor> |
|---|
| 146 | inline not_equal_t<Type, Ftor> |
|---|
| 147 | not_equal(Type t, Ftor f) { |
|---|
| 148 | return not_equal_t<Type, Ftor>(t, f); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | template <typename Type, typename Ftor> |
|---|
| 152 | struct less_t { |
|---|
| 153 | typedef bool result_type; |
|---|
| 154 | |
|---|
| 155 | less_t(Type t, Ftor f) : m_t(t), m_f(f) {} |
|---|
| 156 | |
|---|
| 157 | template <typename Arg> |
|---|
| 158 | bool operator () (Arg& a) { |
|---|
| 159 | return m_t < m_f(a); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | Type m_t; |
|---|
| 163 | Ftor m_f; |
|---|
| 164 | }; |
|---|
| 165 | |
|---|
| 166 | template <typename Type, typename Ftor> |
|---|
| 167 | inline less_t<Type, Ftor> |
|---|
| 168 | less(Type t, Ftor f) { |
|---|
| 169 | return less_t<Type, Ftor>(t, f); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | template <typename FtorA, typename FtorB> |
|---|
| 173 | struct less2_t : public std::binary_function<typename FtorA::argument_type, typename FtorB::argument_type, bool> { |
|---|
| 174 | less2_t(FtorA f_a, FtorB f_b) : m_f_a(f_a), m_f_b(f_b) {} |
|---|
| 175 | |
|---|
| 176 | bool operator () (typename FtorA::argument_type a, typename FtorB::argument_type b) { |
|---|
| 177 | return m_f_a(a) < m_f_b(b); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | FtorA m_f_a; |
|---|
| 181 | FtorB m_f_b; |
|---|
| 182 | }; |
|---|
| 183 | |
|---|
| 184 | template <typename FtorA, typename FtorB> |
|---|
| 185 | inline less2_t<FtorA, FtorB> |
|---|
| 186 | less2(FtorA f_a, FtorB f_b) { |
|---|
| 187 | return less2_t<FtorA,FtorB>(f_a,f_b); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | template <typename Type, typename Ftor> |
|---|
| 191 | struct _greater { |
|---|
| 192 | typedef bool result_type; |
|---|
| 193 | |
|---|
| 194 | _greater(Type t, Ftor f) : m_t(t), m_f(f) {} |
|---|
| 195 | |
|---|
| 196 | template <typename Arg> |
|---|
| 197 | bool operator () (Arg& a) { |
|---|
| 198 | return m_t > m_f(a); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | Type m_t; |
|---|
| 202 | Ftor m_f; |
|---|
| 203 | }; |
|---|
| 204 | |
|---|
| 205 | template <typename Type, typename Ftor> |
|---|
| 206 | inline _greater<Type, Ftor> |
|---|
| 207 | greater(Type t, Ftor f) { |
|---|
| 208 | return _greater<Type, Ftor>(t, f); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | template <typename FtorA, typename FtorB> |
|---|
| 212 | struct greater2_t : public std::binary_function<typename FtorA::argument_type, typename FtorB::argument_type, bool> { |
|---|
| 213 | greater2_t(FtorA f_a, FtorB f_b) : m_f_a(f_a), m_f_b(f_b) {} |
|---|
| 214 | |
|---|
| 215 | bool operator () (typename FtorA::argument_type a, typename FtorB::argument_type b) { |
|---|
| 216 | return m_f_a(a) > m_f_b(b); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | FtorA m_f_a; |
|---|
| 220 | FtorB m_f_b; |
|---|
| 221 | }; |
|---|
| 222 | |
|---|
| 223 | template <typename FtorA, typename FtorB> |
|---|
| 224 | inline greater2_t<FtorA, FtorB> |
|---|
| 225 | greater2(FtorA f_a, FtorB f_b) { |
|---|
| 226 | return greater2_t<FtorA,FtorB>(f_a,f_b); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | template <typename Type, typename Ftor> |
|---|
| 230 | struct less_equal_t { |
|---|
| 231 | typedef bool result_type; |
|---|
| 232 | |
|---|
| 233 | less_equal_t(Type t, Ftor f) : m_t(t), m_f(f) {} |
|---|
| 234 | |
|---|
| 235 | template <typename Arg> |
|---|
| 236 | bool operator () (Arg& a) { |
|---|
| 237 | return m_t <= m_f(a); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | Type m_t; |
|---|
| 241 | Ftor m_f; |
|---|
| 242 | }; |
|---|
| 243 | |
|---|
| 244 | template <typename Type, typename Ftor> |
|---|
| 245 | inline less_equal_t<Type, Ftor> |
|---|
| 246 | less_equal(Type t, Ftor f) { |
|---|
| 247 | return less_equal_t<Type, Ftor>(t, f); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | template <typename Type, typename Ftor> |
|---|
| 251 | struct greater_equal_t { |
|---|
| 252 | typedef bool result_type; |
|---|
| 253 | |
|---|
| 254 | greater_equal_t(Type t, Ftor f) : m_t(t), m_f(f) {} |
|---|
| 255 | |
|---|
| 256 | template <typename Arg> |
|---|
| 257 | bool operator () (Arg& a) { |
|---|
| 258 | return m_t >= m_f(a); |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | Type m_t; |
|---|
| 262 | Ftor m_f; |
|---|
| 263 | }; |
|---|
| 264 | |
|---|
| 265 | template <typename Type, typename Ftor> |
|---|
| 266 | inline greater_equal_t<Type, Ftor> |
|---|
| 267 | greater_equal(Type t, Ftor f) { |
|---|
| 268 | return greater_equal_t<Type, Ftor>(t, f); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | template<typename Tp> |
|---|
| 272 | struct invert : public std::unary_function<Tp, Tp> { |
|---|
| 273 | Tp |
|---|
| 274 | operator () (const Tp& x) const { return ~x; } |
|---|
| 275 | }; |
|---|
| 276 | |
|---|
| 277 | template <typename Src, typename Dest> |
|---|
| 278 | struct on_t : public std::unary_function<typename Src::argument_type, typename Dest::result_type> { |
|---|
| 279 | typedef typename Dest::result_type result_type; |
|---|
| 280 | |
|---|
| 281 | on_t(Src s, Dest d) : m_dest(d), m_src(s) {} |
|---|
| 282 | |
|---|
| 283 | result_type operator () (typename reference_fix<typename Src::argument_type>::type arg) { |
|---|
| 284 | return m_dest(m_src(arg)); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | Dest m_dest; |
|---|
| 288 | Src m_src; |
|---|
| 289 | }; |
|---|
| 290 | |
|---|
| 291 | template <typename Src, typename Dest> |
|---|
| 292 | inline on_t<Src, Dest> |
|---|
| 293 | on(Src s, Dest d) { |
|---|
| 294 | return on_t<Src, Dest>(s, d); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | template <typename Src, typename Dest> |
|---|
| 298 | struct on2_t : public std::binary_function<typename Src::argument_type, typename Dest::second_argument_type, typename Dest::result_type> { |
|---|
| 299 | typedef typename Dest::result_type result_type; |
|---|
| 300 | |
|---|
| 301 | on2_t(Src s, Dest d) : m_dest(d), m_src(s) {} |
|---|
| 302 | |
|---|
| 303 | result_type operator () (typename reference_fix<typename Src::argument_type>::type first, typename reference_fix<typename Dest::second_argument_type>::type second) { |
|---|
| 304 | return m_dest(m_src(first), second); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | Dest m_dest; |
|---|
| 308 | Src m_src; |
|---|
| 309 | }; |
|---|
| 310 | |
|---|
| 311 | template <typename Src, typename Dest> |
|---|
| 312 | inline on2_t<Src, Dest> |
|---|
| 313 | on2(Src s, Dest d) { |
|---|
| 314 | return on2_t<Src, Dest>(s, d); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | // Creates a functor for accessing a member. |
|---|
| 318 | template <typename Class, typename Member> |
|---|
| 319 | struct mem_ptr_t : public std::unary_function<Class*, Member&> { |
|---|
| 320 | mem_ptr_t(Member Class::*m) : m_member(m) {} |
|---|
| 321 | |
|---|
| 322 | Member& operator () (Class* c) { |
|---|
| 323 | return c->*m_member; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | const Member& operator () (const Class* c) { |
|---|
| 327 | return c->*m_member; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | Member Class::*m_member; |
|---|
| 331 | }; |
|---|
| 332 | |
|---|
| 333 | template <typename Class, typename Member> |
|---|
| 334 | inline mem_ptr_t<Class, Member> |
|---|
| 335 | mem_ptr(Member Class::*m) { |
|---|
| 336 | return mem_ptr_t<Class, Member>(m); |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | template <typename Class, typename Member> |
|---|
| 340 | struct mem_ref_t : public std::unary_function<Class&, Member&> { |
|---|
| 341 | mem_ref_t(Member Class::*m) : m_member(m) {} |
|---|
| 342 | |
|---|
| 343 | Member& operator () (Class& c) { |
|---|
| 344 | return c.*m_member; |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | Member Class::*m_member; |
|---|
| 348 | }; |
|---|
| 349 | |
|---|
| 350 | template <typename Class, typename Member> |
|---|
| 351 | struct const_mem_ref_t : public std::unary_function<const Class&, const Member&> { |
|---|
| 352 | const_mem_ref_t(const Member Class::*m) : m_member(m) {} |
|---|
| 353 | |
|---|
| 354 | const Member& operator () (const Class& c) { |
|---|
| 355 | return c.*m_member; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | const Member Class::*m_member; |
|---|
| 359 | }; |
|---|
| 360 | |
|---|
| 361 | template <typename Class, typename Member> |
|---|
| 362 | inline mem_ref_t<Class, Member> |
|---|
| 363 | mem_ref(Member Class::*m) { |
|---|
| 364 | return mem_ref_t<Class, Member>(m); |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | template <typename Class, typename Member> |
|---|
| 368 | inline const_mem_ref_t<Class, Member> |
|---|
| 369 | const_mem_ref(const Member Class::*m) { |
|---|
| 370 | return const_mem_ref_t<Class, Member>(m); |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | template <typename Cond, typename Then> |
|---|
| 374 | struct if_then_t { |
|---|
| 375 | if_then_t(Cond c, Then t) : m_cond(c), m_then(t) {} |
|---|
| 376 | |
|---|
| 377 | template <typename Arg> |
|---|
| 378 | void operator () (Arg& a) { |
|---|
| 379 | if (m_cond(a)) |
|---|
| 380 | m_then(a); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | Cond m_cond; |
|---|
| 384 | Then m_then; |
|---|
| 385 | }; |
|---|
| 386 | |
|---|
| 387 | template <typename Cond, typename Then> |
|---|
| 388 | inline if_then_t<Cond, Then> |
|---|
| 389 | if_then(Cond c, Then t) { |
|---|
| 390 | return if_then_t<Cond, Then>(c, t); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | template <typename T> |
|---|
| 394 | struct call_delete : public std::unary_function<T*, void> { |
|---|
| 395 | void operator () (T* t) { |
|---|
| 396 | delete t; |
|---|
| 397 | } |
|---|
| 398 | }; |
|---|
| 399 | |
|---|
| 400 | template <typename T> |
|---|
| 401 | inline void |
|---|
| 402 | call_delete_func(T* t) { |
|---|
| 403 | delete t; |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | template <typename Operation> |
|---|
| 407 | class bind1st_t : public std::unary_function<typename Operation::second_argument_type, typename Operation::result_type> { |
|---|
| 408 | public: |
|---|
| 409 | typedef typename reference_fix<typename Operation::first_argument_type>::type value_type; |
|---|
| 410 | typedef typename reference_fix<typename Operation::second_argument_type>::type argument_type; |
|---|
| 411 | |
|---|
| 412 | bind1st_t(const Operation& op, const value_type v) : |
|---|
| 413 | m_op(op), m_value(v) {} |
|---|
| 414 | |
|---|
| 415 | typename Operation::result_type |
|---|
| 416 | operator () (const argument_type arg) { |
|---|
| 417 | return m_op(m_value, arg); |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | protected: |
|---|
| 421 | Operation m_op; |
|---|
| 422 | value_type m_value; |
|---|
| 423 | }; |
|---|
| 424 | |
|---|
| 425 | template <typename Operation, typename Type> |
|---|
| 426 | inline bind1st_t<Operation> |
|---|
| 427 | bind1st(const Operation& op, const Type& val) { |
|---|
| 428 | return bind1st_t<Operation>(op, val); |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | template <typename Operation> |
|---|
| 432 | class bind2nd_t : public std::unary_function<typename Operation::first_argument_type, typename Operation::result_type> { |
|---|
| 433 | public: |
|---|
| 434 | typedef typename reference_fix<typename Operation::first_argument_type>::type argument_type; |
|---|
| 435 | typedef typename reference_fix<typename Operation::second_argument_type>::type value_type; |
|---|
| 436 | |
|---|
| 437 | bind2nd_t(const Operation& op, const value_type v) : |
|---|
| 438 | m_op(op), m_value(v) {} |
|---|
| 439 | |
|---|
| 440 | typename Operation::result_type |
|---|
| 441 | operator () (argument_type arg) { |
|---|
| 442 | return m_op(arg, m_value); |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | protected: |
|---|
| 446 | Operation m_op; |
|---|
| 447 | value_type m_value; |
|---|
| 448 | }; |
|---|
| 449 | |
|---|
| 450 | template <typename Operation, typename Type> |
|---|
| 451 | inline bind2nd_t<Operation> |
|---|
| 452 | bind2nd(const Operation& op, const Type& val) { |
|---|
| 453 | return bind2nd_t<Operation>(op, val); |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | // Lightweight callback function including pointer to object. Should |
|---|
| 457 | // be replaced by TR1 stuff later. Requires an object to bind, instead |
|---|
| 458 | // of using a seperate functor for that. |
|---|
| 459 | |
|---|
| 460 | template <typename Ret> |
|---|
| 461 | class ptr_fun0 { |
|---|
| 462 | public: |
|---|
| 463 | typedef Ret result_type; |
|---|
| 464 | typedef Ret (*Function)(); |
|---|
| 465 | |
|---|
| 466 | ptr_fun0() {} |
|---|
| 467 | ptr_fun0(Function f) : m_function(f) {} |
|---|
| 468 | |
|---|
| 469 | bool is_valid() const { return m_function; } |
|---|
| 470 | |
|---|
| 471 | Ret operator () () { return m_function(); } |
|---|
| 472 | |
|---|
| 473 | private: |
|---|
| 474 | Function m_function; |
|---|
| 475 | }; |
|---|
| 476 | |
|---|
| 477 | template <typename Object, typename Ret> |
|---|
| 478 | class mem_fun0 { |
|---|
| 479 | public: |
|---|
| 480 | typedef Ret result_type; |
|---|
| 481 | typedef Ret (Object::*Function)(); |
|---|
| 482 | |
|---|
| 483 | mem_fun0() : m_object(NULL) {} |
|---|
| 484 | mem_fun0(Object* o, Function f) : m_object(o), m_function(f) {} |
|---|
| 485 | |
|---|
| 486 | bool is_valid() const { return m_object; } |
|---|
| 487 | |
|---|
| 488 | Ret operator () () { return (m_object->*m_function)(); } |
|---|
| 489 | |
|---|
| 490 | private: |
|---|
| 491 | Object* m_object; |
|---|
| 492 | Function m_function; |
|---|
| 493 | }; |
|---|
| 494 | |
|---|
| 495 | template <typename Object, typename Ret> |
|---|
| 496 | class const_mem_fun0 { |
|---|
| 497 | public: |
|---|
| 498 | typedef Ret result_type; |
|---|
| 499 | typedef Ret (Object::*Function)() const; |
|---|
| 500 | |
|---|
| 501 | const_mem_fun0() : m_object(NULL) {} |
|---|
| 502 | const_mem_fun0(const Object* o, Function f) : m_object(o), m_function(f) {} |
|---|
| 503 | |
|---|
| 504 | bool is_valid() const { return m_object; } |
|---|
| 505 | |
|---|
| 506 | Ret operator () () const { return (m_object->*m_function)(); } |
|---|
| 507 | |
|---|
| 508 | private: |
|---|
| 509 | const Object* m_object; |
|---|
| 510 | Function m_function; |
|---|
| 511 | }; |
|---|
| 512 | |
|---|
| 513 | template <typename Object, typename Ret, typename Arg1> |
|---|
| 514 | class mem_fun1 { |
|---|
| 515 | public: |
|---|
| 516 | typedef Ret result_type; |
|---|
| 517 | typedef Ret (Object::*Function)(Arg1); |
|---|
| 518 | |
|---|
| 519 | mem_fun1() : m_object(NULL) {} |
|---|
| 520 | mem_fun1(Object* o, Function f) : m_object(o), m_function(f) {} |
|---|
| 521 | |
|---|
| 522 | bool is_valid() const { return m_object; } |
|---|
| 523 | |
|---|
| 524 | Ret operator () (Arg1 a1) { return (m_object->*m_function)(a1); } |
|---|
| 525 | |
|---|
| 526 | private: |
|---|
| 527 | Object* m_object; |
|---|
| 528 | Function m_function; |
|---|
| 529 | }; |
|---|
| 530 | |
|---|
| 531 | template <typename Object, typename Ret, typename Arg1> |
|---|
| 532 | class const_mem_fun1 { |
|---|
| 533 | public: |
|---|
| 534 | typedef Ret result_type; |
|---|
| 535 | typedef Ret (Object::*Function)(Arg1) const; |
|---|
| 536 | |
|---|
| 537 | const_mem_fun1() : m_object(NULL) {} |
|---|
| 538 | const_mem_fun1(const Object* o, Function f) : m_object(o), m_function(f) {} |
|---|
| 539 | |
|---|
| 540 | bool is_valid() const { return m_object; } |
|---|
| 541 | |
|---|
| 542 | Ret operator () (Arg1 a1) const { return (m_object->*m_function)(a1); } |
|---|
| 543 | |
|---|
| 544 | private: |
|---|
| 545 | const Object* m_object; |
|---|
| 546 | Function m_function; |
|---|
| 547 | }; |
|---|
| 548 | |
|---|
| 549 | template <typename Object, typename Ret, typename Arg1, typename Arg2> |
|---|
| 550 | class mem_fun2 : public std::binary_function<Arg1, Arg2, Ret> { |
|---|
| 551 | public: |
|---|
| 552 | typedef Ret result_type; |
|---|
| 553 | typedef Ret (Object::*Function)(Arg1, Arg2); |
|---|
| 554 | typedef Object object_type; |
|---|
| 555 | |
|---|
| 556 | mem_fun2() : m_object(NULL) {} |
|---|
| 557 | mem_fun2(Object* o, Function f) : m_object(o), m_function(f) {} |
|---|
| 558 | |
|---|
| 559 | bool is_valid() const { return m_object; } |
|---|
| 560 | |
|---|
| 561 | object_type* object() { return m_object; } |
|---|
| 562 | const object_type* object() const { return m_object; } |
|---|
| 563 | |
|---|
| 564 | Ret operator () (Arg1 a1, Arg2 a2) { return (m_object->*m_function)(a1, a2); } |
|---|
| 565 | |
|---|
| 566 | private: |
|---|
| 567 | Object* m_object; |
|---|
| 568 | Function m_function; |
|---|
| 569 | }; |
|---|
| 570 | |
|---|
| 571 | template <typename Object, typename Ret, typename Arg1, typename Arg2, typename Arg3> |
|---|
| 572 | class mem_fun3 { |
|---|
| 573 | public: |
|---|
| 574 | typedef Ret result_type; |
|---|
| 575 | typedef Ret (Object::*Function)(Arg1, Arg2, Arg3); |
|---|
| 576 | |
|---|
| 577 | mem_fun3() : m_object(NULL) {} |
|---|
| 578 | mem_fun3(Object* o, Function f) : m_object(o), m_function(f) {} |
|---|
| 579 | |
|---|
| 580 | bool is_valid() const { return m_object; } |
|---|
| 581 | |
|---|
| 582 | Ret operator () (Arg1 a1, Arg2 a2, Arg3 a3) { return (m_object->*m_function)(a1, a2, a3); } |
|---|
| 583 | |
|---|
| 584 | private: |
|---|
| 585 | Object* m_object; |
|---|
| 586 | Function m_function; |
|---|
| 587 | }; |
|---|
| 588 | |
|---|
| 589 | template <typename Ret> |
|---|
| 590 | inline ptr_fun0<Ret> |
|---|
| 591 | ptr_fun(Ret (*f)()) { return ptr_fun0<Ret>(f); } |
|---|
| 592 | |
|---|
| 593 | template <typename Object, typename Ret> |
|---|
| 594 | inline mem_fun0<Object, Ret> |
|---|
| 595 | make_mem_fun(Object* o, Ret (Object::*f)()) { |
|---|
| 596 | return mem_fun0<Object, Ret>(o, f); |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | template <typename Object, typename Ret> |
|---|
| 600 | inline const_mem_fun0<Object, Ret> |
|---|
| 601 | make_mem_fun(const Object* o, Ret (Object::*f)() const) { |
|---|
| 602 | return const_mem_fun0<Object, Ret>(o, f); |
|---|
| 603 | } |
|---|
| 604 | |
|---|
| 605 | template <typename Object, typename Ret, typename Arg1> |
|---|
| 606 | inline mem_fun1<Object, Ret, Arg1> |
|---|
| 607 | make_mem_fun(Object* o, Ret (Object::*f)(Arg1)) { |
|---|
| 608 | return mem_fun1<Object, Ret, Arg1>(o, f); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | template <typename Object, typename Ret, typename Arg1> |
|---|
| 612 | inline const_mem_fun1<Object, Ret, Arg1> |
|---|
| 613 | make_mem_fun(const Object* o, Ret (Object::*f)(Arg1) const) { |
|---|
| 614 | return const_mem_fun1<Object, Ret, Arg1>(o, f); |
|---|
| 615 | } |
|---|
| 616 | |
|---|
| 617 | template <typename Object, typename Ret, typename Arg1, typename Arg2> |
|---|
| 618 | inline mem_fun2<Object, Ret, Arg1, Arg2> |
|---|
| 619 | make_mem_fun(Object* o, Ret (Object::*f)(Arg1, Arg2)) { |
|---|
| 620 | return mem_fun2<Object, Ret, Arg1, Arg2>(o, f); |
|---|
| 621 | } |
|---|
| 622 | |
|---|
| 623 | template <typename Object, typename Ret, typename Arg1, typename Arg2, typename Arg3> |
|---|
| 624 | inline mem_fun3<Object, Ret, Arg1, Arg2, Arg3> |
|---|
| 625 | make_mem_fun(Object* o, Ret (Object::*f)(Arg1, Arg2, Arg3)) { |
|---|
| 626 | return mem_fun3<Object, Ret, Arg1, Arg2, Arg3>(o, f); |
|---|
| 627 | } |
|---|
| 628 | |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | #endif |
|---|