Brian Silverman | e458090 | 2018-08-04 23:36:31 -0700 | [diff] [blame^] | 1 | // Boost.Function library |
| 2 | |
| 3 | // Copyright Douglas Gregor 2001-2006 |
| 4 | // Copyright Emil Dotchevski 2007 |
| 5 | // Use, modification and distribution is subject to the Boost Software License, Version 1.0. |
| 6 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
| 8 | |
| 9 | // For more information, see http://www.boost.org |
| 10 | |
| 11 | #ifndef BOOST_FUNCTION_BASE_HEADER |
| 12 | #define BOOST_FUNCTION_BASE_HEADER |
| 13 | |
| 14 | #include <stdexcept> |
| 15 | #include <string> |
| 16 | #include <memory> |
| 17 | #include <new> |
| 18 | #include <boost/config.hpp> |
| 19 | #include <boost/assert.hpp> |
| 20 | #include <boost/integer.hpp> |
| 21 | #include <boost/type_index.hpp> |
| 22 | #include <boost/type_traits/has_trivial_copy.hpp> |
| 23 | #include <boost/type_traits/has_trivial_destructor.hpp> |
| 24 | #include <boost/type_traits/is_const.hpp> |
| 25 | #include <boost/type_traits/is_integral.hpp> |
| 26 | #include <boost/type_traits/is_volatile.hpp> |
| 27 | #include <boost/type_traits/composite_traits.hpp> |
| 28 | #include <boost/ref.hpp> |
| 29 | #include <boost/mpl/if.hpp> |
| 30 | #include <boost/detail/workaround.hpp> |
| 31 | #include <boost/type_traits/alignment_of.hpp> |
| 32 | #ifndef BOOST_NO_SFINAE |
| 33 | # include "boost/utility/enable_if.hpp" |
| 34 | #else |
| 35 | # include "boost/mpl/bool.hpp" |
| 36 | #endif |
| 37 | #include <boost/function_equal.hpp> |
| 38 | #include <boost/function/function_fwd.hpp> |
| 39 | |
| 40 | #if defined(BOOST_MSVC) |
| 41 | # pragma warning( push ) |
| 42 | # pragma warning( disable : 4793 ) // complaint about native code generation |
| 43 | # pragma warning( disable : 4127 ) // "conditional expression is constant" |
| 44 | #endif |
| 45 | |
| 46 | #if defined(__ICL) && __ICL <= 600 || defined(__MWERKS__) && __MWERKS__ < 0x2406 && !defined(BOOST_STRICT_CONFIG) |
| 47 | # define BOOST_FUNCTION_TARGET_FIX(x) x |
| 48 | #else |
| 49 | # define BOOST_FUNCTION_TARGET_FIX(x) |
| 50 | #endif // __ICL etc |
| 51 | |
| 52 | # define BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor,Type) \ |
| 53 | typename ::boost::enable_if_c< \ |
| 54 | !(::boost::is_integral<Functor>::value), \ |
| 55 | Type>::type |
| 56 | |
| 57 | namespace boost { |
| 58 | namespace detail { |
| 59 | namespace function { |
| 60 | class X; |
| 61 | |
| 62 | /** |
| 63 | * A buffer used to store small function objects in |
| 64 | * boost::function. It is a union containing function pointers, |
| 65 | * object pointers, and a structure that resembles a bound |
| 66 | * member function pointer. |
| 67 | */ |
| 68 | union function_buffer_members |
| 69 | { |
| 70 | // For pointers to function objects |
| 71 | typedef void* obj_ptr_t; |
| 72 | mutable obj_ptr_t obj_ptr; |
| 73 | |
| 74 | // For pointers to std::type_info objects |
| 75 | struct type_t { |
| 76 | // (get_functor_type_tag, check_functor_type_tag). |
| 77 | const boost::typeindex::type_info* type; |
| 78 | |
| 79 | // Whether the type is const-qualified. |
| 80 | bool const_qualified; |
| 81 | // Whether the type is volatile-qualified. |
| 82 | bool volatile_qualified; |
| 83 | } type; |
| 84 | |
| 85 | // For function pointers of all kinds |
| 86 | typedef void (*func_ptr_t)(); |
| 87 | mutable func_ptr_t func_ptr; |
| 88 | |
| 89 | // For bound member pointers |
| 90 | struct bound_memfunc_ptr_t { |
| 91 | void (X::*memfunc_ptr)(int); |
| 92 | void* obj_ptr; |
| 93 | } bound_memfunc_ptr; |
| 94 | |
| 95 | // For references to function objects. We explicitly keep |
| 96 | // track of the cv-qualifiers on the object referenced. |
| 97 | struct obj_ref_t { |
| 98 | mutable void* obj_ptr; |
| 99 | bool is_const_qualified; |
| 100 | bool is_volatile_qualified; |
| 101 | } obj_ref; |
| 102 | }; |
| 103 | |
| 104 | union function_buffer |
| 105 | { |
| 106 | // Type-specific union members |
| 107 | mutable function_buffer_members members; |
| 108 | |
| 109 | // To relax aliasing constraints |
| 110 | mutable char data[sizeof(function_buffer_members)]; |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * The unusable class is a placeholder for unused function arguments |
| 115 | * It is also completely unusable except that it constructable from |
| 116 | * anything. This helps compilers without partial specialization to |
| 117 | * handle Boost.Function objects returning void. |
| 118 | */ |
| 119 | struct unusable |
| 120 | { |
| 121 | unusable() {} |
| 122 | template<typename T> unusable(const T&) {} |
| 123 | }; |
| 124 | |
| 125 | /* Determine the return type. This supports compilers that do not support |
| 126 | * void returns or partial specialization by silently changing the return |
| 127 | * type to "unusable". |
| 128 | */ |
| 129 | template<typename T> struct function_return_type { typedef T type; }; |
| 130 | |
| 131 | template<> |
| 132 | struct function_return_type<void> |
| 133 | { |
| 134 | typedef unusable type; |
| 135 | }; |
| 136 | |
| 137 | // The operation type to perform on the given functor/function pointer |
| 138 | enum functor_manager_operation_type { |
| 139 | clone_functor_tag, |
| 140 | move_functor_tag, |
| 141 | destroy_functor_tag, |
| 142 | check_functor_type_tag, |
| 143 | get_functor_type_tag |
| 144 | }; |
| 145 | |
| 146 | // Tags used to decide between different types of functions |
| 147 | struct function_ptr_tag {}; |
| 148 | struct function_obj_tag {}; |
| 149 | struct member_ptr_tag {}; |
| 150 | struct function_obj_ref_tag {}; |
| 151 | |
| 152 | template<typename F> |
| 153 | class get_function_tag |
| 154 | { |
| 155 | typedef typename mpl::if_c<(is_pointer<F>::value), |
| 156 | function_ptr_tag, |
| 157 | function_obj_tag>::type ptr_or_obj_tag; |
| 158 | |
| 159 | typedef typename mpl::if_c<(is_member_pointer<F>::value), |
| 160 | member_ptr_tag, |
| 161 | ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag; |
| 162 | |
| 163 | typedef typename mpl::if_c<(is_reference_wrapper<F>::value), |
| 164 | function_obj_ref_tag, |
| 165 | ptr_or_obj_or_mem_tag>::type or_ref_tag; |
| 166 | |
| 167 | public: |
| 168 | typedef or_ref_tag type; |
| 169 | }; |
| 170 | |
| 171 | // The trivial manager does nothing but return the same pointer (if we |
| 172 | // are cloning) or return the null pointer (if we are deleting). |
| 173 | template<typename F> |
| 174 | struct reference_manager |
| 175 | { |
| 176 | static inline void |
| 177 | manage(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 178 | functor_manager_operation_type op) |
| 179 | { |
| 180 | switch (op) { |
| 181 | case clone_functor_tag: |
| 182 | out_buffer.members.obj_ref = in_buffer.members.obj_ref; |
| 183 | return; |
| 184 | |
| 185 | case move_functor_tag: |
| 186 | out_buffer.members.obj_ref = in_buffer.members.obj_ref; |
| 187 | in_buffer.members.obj_ref.obj_ptr = 0; |
| 188 | return; |
| 189 | |
| 190 | case destroy_functor_tag: |
| 191 | out_buffer.members.obj_ref.obj_ptr = 0; |
| 192 | return; |
| 193 | |
| 194 | case check_functor_type_tag: |
| 195 | { |
| 196 | // Check whether we have the same type. We can add |
| 197 | // cv-qualifiers, but we can't take them away. |
| 198 | if (*out_buffer.members.type.type == boost::typeindex::type_id<F>() |
| 199 | && (!in_buffer.members.obj_ref.is_const_qualified |
| 200 | || out_buffer.members.type.const_qualified) |
| 201 | && (!in_buffer.members.obj_ref.is_volatile_qualified |
| 202 | || out_buffer.members.type.volatile_qualified)) |
| 203 | out_buffer.members.obj_ptr = in_buffer.members.obj_ref.obj_ptr; |
| 204 | else |
| 205 | out_buffer.members.obj_ptr = 0; |
| 206 | } |
| 207 | return; |
| 208 | |
| 209 | case get_functor_type_tag: |
| 210 | out_buffer.members.type.type = &boost::typeindex::type_id<F>().type_info(); |
| 211 | out_buffer.members.type.const_qualified = in_buffer.members.obj_ref.is_const_qualified; |
| 212 | out_buffer.members.type.volatile_qualified = in_buffer.members.obj_ref.is_volatile_qualified; |
| 213 | return; |
| 214 | } |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | /** |
| 219 | * Determine if boost::function can use the small-object |
| 220 | * optimization with the function object type F. |
| 221 | */ |
| 222 | template<typename F> |
| 223 | struct function_allows_small_object_optimization |
| 224 | { |
| 225 | BOOST_STATIC_CONSTANT |
| 226 | (bool, |
| 227 | value = ((sizeof(F) <= sizeof(function_buffer) && |
| 228 | (alignment_of<function_buffer>::value |
| 229 | % alignment_of<F>::value == 0)))); |
| 230 | }; |
| 231 | |
| 232 | template <typename F,typename A> |
| 233 | struct functor_wrapper: public F, public A |
| 234 | { |
| 235 | functor_wrapper( F f, A a ): |
| 236 | F(f), |
| 237 | A(a) |
| 238 | { |
| 239 | } |
| 240 | |
| 241 | functor_wrapper(const functor_wrapper& f) : |
| 242 | F(static_cast<const F&>(f)), |
| 243 | A(static_cast<const A&>(f)) |
| 244 | { |
| 245 | } |
| 246 | }; |
| 247 | |
| 248 | /** |
| 249 | * The functor_manager class contains a static function "manage" which |
| 250 | * can clone or destroy the given function/function object pointer. |
| 251 | */ |
| 252 | template<typename Functor> |
| 253 | struct functor_manager_common |
| 254 | { |
| 255 | typedef Functor functor_type; |
| 256 | |
| 257 | // Function pointers |
| 258 | static inline void |
| 259 | manage_ptr(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 260 | functor_manager_operation_type op) |
| 261 | { |
| 262 | if (op == clone_functor_tag) |
| 263 | out_buffer.members.func_ptr = in_buffer.members.func_ptr; |
| 264 | else if (op == move_functor_tag) { |
| 265 | out_buffer.members.func_ptr = in_buffer.members.func_ptr; |
| 266 | in_buffer.members.func_ptr = 0; |
| 267 | } else if (op == destroy_functor_tag) |
| 268 | out_buffer.members.func_ptr = 0; |
| 269 | else if (op == check_functor_type_tag) { |
| 270 | if (*out_buffer.members.type.type == boost::typeindex::type_id<Functor>()) |
| 271 | out_buffer.members.obj_ptr = &in_buffer.members.func_ptr; |
| 272 | else |
| 273 | out_buffer.members.obj_ptr = 0; |
| 274 | } else /* op == get_functor_type_tag */ { |
| 275 | out_buffer.members.type.type = &boost::typeindex::type_id<Functor>().type_info(); |
| 276 | out_buffer.members.type.const_qualified = false; |
| 277 | out_buffer.members.type.volatile_qualified = false; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // Function objects that fit in the small-object buffer. |
| 282 | static inline void |
| 283 | manage_small(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 284 | functor_manager_operation_type op) |
| 285 | { |
| 286 | if (op == clone_functor_tag || op == move_functor_tag) { |
| 287 | const functor_type* in_functor = |
| 288 | reinterpret_cast<const functor_type*>(in_buffer.data); |
| 289 | new (reinterpret_cast<void*>(out_buffer.data)) functor_type(*in_functor); |
| 290 | |
| 291 | if (op == move_functor_tag) { |
| 292 | functor_type* f = reinterpret_cast<functor_type*>(in_buffer.data); |
| 293 | (void)f; // suppress warning about the value of f not being used (MSVC) |
| 294 | f->~Functor(); |
| 295 | } |
| 296 | } else if (op == destroy_functor_tag) { |
| 297 | // Some compilers (Borland, vc6, ...) are unhappy with ~functor_type. |
| 298 | functor_type* f = reinterpret_cast<functor_type*>(out_buffer.data); |
| 299 | (void)f; // suppress warning about the value of f not being used (MSVC) |
| 300 | f->~Functor(); |
| 301 | } else if (op == check_functor_type_tag) { |
| 302 | if (*out_buffer.members.type.type == boost::typeindex::type_id<Functor>()) |
| 303 | out_buffer.members.obj_ptr = in_buffer.data; |
| 304 | else |
| 305 | out_buffer.members.obj_ptr = 0; |
| 306 | } else /* op == get_functor_type_tag */ { |
| 307 | out_buffer.members.type.type = &boost::typeindex::type_id<Functor>().type_info(); |
| 308 | out_buffer.members.type.const_qualified = false; |
| 309 | out_buffer.members.type.volatile_qualified = false; |
| 310 | } |
| 311 | } |
| 312 | }; |
| 313 | |
| 314 | template<typename Functor> |
| 315 | struct functor_manager |
| 316 | { |
| 317 | private: |
| 318 | typedef Functor functor_type; |
| 319 | |
| 320 | // Function pointers |
| 321 | static inline void |
| 322 | manager(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 323 | functor_manager_operation_type op, function_ptr_tag) |
| 324 | { |
| 325 | functor_manager_common<Functor>::manage_ptr(in_buffer,out_buffer,op); |
| 326 | } |
| 327 | |
| 328 | // Function objects that fit in the small-object buffer. |
| 329 | static inline void |
| 330 | manager(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 331 | functor_manager_operation_type op, mpl::true_) |
| 332 | { |
| 333 | functor_manager_common<Functor>::manage_small(in_buffer,out_buffer,op); |
| 334 | } |
| 335 | |
| 336 | // Function objects that require heap allocation |
| 337 | static inline void |
| 338 | manager(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 339 | functor_manager_operation_type op, mpl::false_) |
| 340 | { |
| 341 | if (op == clone_functor_tag) { |
| 342 | // Clone the functor |
| 343 | // GCC 2.95.3 gets the CV qualifiers wrong here, so we |
| 344 | // can't do the static_cast that we should do. |
| 345 | // jewillco: Changing this to static_cast because GCC 2.95.3 is |
| 346 | // obsolete. |
| 347 | const functor_type* f = |
| 348 | static_cast<const functor_type*>(in_buffer.members.obj_ptr); |
| 349 | functor_type* new_f = new functor_type(*f); |
| 350 | out_buffer.members.obj_ptr = new_f; |
| 351 | } else if (op == move_functor_tag) { |
| 352 | out_buffer.members.obj_ptr = in_buffer.members.obj_ptr; |
| 353 | in_buffer.members.obj_ptr = 0; |
| 354 | } else if (op == destroy_functor_tag) { |
| 355 | /* Cast from the void pointer to the functor pointer type */ |
| 356 | functor_type* f = |
| 357 | static_cast<functor_type*>(out_buffer.members.obj_ptr); |
| 358 | delete f; |
| 359 | out_buffer.members.obj_ptr = 0; |
| 360 | } else if (op == check_functor_type_tag) { |
| 361 | if (*out_buffer.members.type.type == boost::typeindex::type_id<Functor>()) |
| 362 | out_buffer.members.obj_ptr = in_buffer.members.obj_ptr; |
| 363 | else |
| 364 | out_buffer.members.obj_ptr = 0; |
| 365 | } else /* op == get_functor_type_tag */ { |
| 366 | out_buffer.members.type.type = &boost::typeindex::type_id<Functor>().type_info(); |
| 367 | out_buffer.members.type.const_qualified = false; |
| 368 | out_buffer.members.type.volatile_qualified = false; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // For function objects, we determine whether the function |
| 373 | // object can use the small-object optimization buffer or |
| 374 | // whether we need to allocate it on the heap. |
| 375 | static inline void |
| 376 | manager(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 377 | functor_manager_operation_type op, function_obj_tag) |
| 378 | { |
| 379 | manager(in_buffer, out_buffer, op, |
| 380 | mpl::bool_<(function_allows_small_object_optimization<functor_type>::value)>()); |
| 381 | } |
| 382 | |
| 383 | // For member pointers, we use the small-object optimization buffer. |
| 384 | static inline void |
| 385 | manager(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 386 | functor_manager_operation_type op, member_ptr_tag) |
| 387 | { |
| 388 | manager(in_buffer, out_buffer, op, mpl::true_()); |
| 389 | } |
| 390 | |
| 391 | public: |
| 392 | /* Dispatch to an appropriate manager based on whether we have a |
| 393 | function pointer or a function object pointer. */ |
| 394 | static inline void |
| 395 | manage(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 396 | functor_manager_operation_type op) |
| 397 | { |
| 398 | typedef typename get_function_tag<functor_type>::type tag_type; |
| 399 | switch (op) { |
| 400 | case get_functor_type_tag: |
| 401 | out_buffer.members.type.type = &boost::typeindex::type_id<functor_type>().type_info(); |
| 402 | out_buffer.members.type.const_qualified = false; |
| 403 | out_buffer.members.type.volatile_qualified = false; |
| 404 | return; |
| 405 | |
| 406 | default: |
| 407 | manager(in_buffer, out_buffer, op, tag_type()); |
| 408 | return; |
| 409 | } |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | template<typename Functor, typename Allocator> |
| 414 | struct functor_manager_a |
| 415 | { |
| 416 | private: |
| 417 | typedef Functor functor_type; |
| 418 | |
| 419 | // Function pointers |
| 420 | static inline void |
| 421 | manager(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 422 | functor_manager_operation_type op, function_ptr_tag) |
| 423 | { |
| 424 | functor_manager_common<Functor>::manage_ptr(in_buffer,out_buffer,op); |
| 425 | } |
| 426 | |
| 427 | // Function objects that fit in the small-object buffer. |
| 428 | static inline void |
| 429 | manager(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 430 | functor_manager_operation_type op, mpl::true_) |
| 431 | { |
| 432 | functor_manager_common<Functor>::manage_small(in_buffer,out_buffer,op); |
| 433 | } |
| 434 | |
| 435 | // Function objects that require heap allocation |
| 436 | static inline void |
| 437 | manager(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 438 | functor_manager_operation_type op, mpl::false_) |
| 439 | { |
| 440 | typedef functor_wrapper<Functor,Allocator> functor_wrapper_type; |
| 441 | #if defined(BOOST_NO_CXX11_ALLOCATOR) |
| 442 | typedef typename Allocator::template rebind<functor_wrapper_type>::other |
| 443 | wrapper_allocator_type; |
| 444 | typedef typename wrapper_allocator_type::pointer wrapper_allocator_pointer_type; |
| 445 | #else |
| 446 | using wrapper_allocator_type = typename std::allocator_traits<Allocator>::template rebind_alloc<functor_wrapper_type>; |
| 447 | using wrapper_allocator_pointer_type = typename std::allocator_traits<wrapper_allocator_type>::pointer; |
| 448 | #endif |
| 449 | |
| 450 | if (op == clone_functor_tag) { |
| 451 | // Clone the functor |
| 452 | // GCC 2.95.3 gets the CV qualifiers wrong here, so we |
| 453 | // can't do the static_cast that we should do. |
| 454 | const functor_wrapper_type* f = |
| 455 | static_cast<const functor_wrapper_type*>(in_buffer.members.obj_ptr); |
| 456 | wrapper_allocator_type wrapper_allocator(static_cast<Allocator const &>(*f)); |
| 457 | wrapper_allocator_pointer_type copy = wrapper_allocator.allocate(1); |
| 458 | #if defined(BOOST_NO_CXX11_ALLOCATOR) |
| 459 | wrapper_allocator.construct(copy, *f); |
| 460 | #else |
| 461 | std::allocator_traits<wrapper_allocator_type>::construct(wrapper_allocator, copy, *f); |
| 462 | #endif |
| 463 | |
| 464 | // Get back to the original pointer type |
| 465 | functor_wrapper_type* new_f = static_cast<functor_wrapper_type*>(copy); |
| 466 | out_buffer.members.obj_ptr = new_f; |
| 467 | } else if (op == move_functor_tag) { |
| 468 | out_buffer.members.obj_ptr = in_buffer.members.obj_ptr; |
| 469 | in_buffer.members.obj_ptr = 0; |
| 470 | } else if (op == destroy_functor_tag) { |
| 471 | /* Cast from the void pointer to the functor_wrapper_type */ |
| 472 | functor_wrapper_type* victim = |
| 473 | static_cast<functor_wrapper_type*>(in_buffer.members.obj_ptr); |
| 474 | wrapper_allocator_type wrapper_allocator(static_cast<Allocator const &>(*victim)); |
| 475 | #if defined(BOOST_NO_CXX11_ALLOCATOR) |
| 476 | wrapper_allocator.destroy(victim); |
| 477 | #else |
| 478 | std::allocator_traits<wrapper_allocator_type>::destroy(wrapper_allocator, victim); |
| 479 | #endif |
| 480 | wrapper_allocator.deallocate(victim,1); |
| 481 | out_buffer.members.obj_ptr = 0; |
| 482 | } else if (op == check_functor_type_tag) { |
| 483 | if (*out_buffer.members.type.type == boost::typeindex::type_id<Functor>()) |
| 484 | out_buffer.members.obj_ptr = in_buffer.members.obj_ptr; |
| 485 | else |
| 486 | out_buffer.members.obj_ptr = 0; |
| 487 | } else /* op == get_functor_type_tag */ { |
| 488 | out_buffer.members.type.type = &boost::typeindex::type_id<Functor>().type_info(); |
| 489 | out_buffer.members.type.const_qualified = false; |
| 490 | out_buffer.members.type.volatile_qualified = false; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | // For function objects, we determine whether the function |
| 495 | // object can use the small-object optimization buffer or |
| 496 | // whether we need to allocate it on the heap. |
| 497 | static inline void |
| 498 | manager(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 499 | functor_manager_operation_type op, function_obj_tag) |
| 500 | { |
| 501 | manager(in_buffer, out_buffer, op, |
| 502 | mpl::bool_<(function_allows_small_object_optimization<functor_type>::value)>()); |
| 503 | } |
| 504 | |
| 505 | public: |
| 506 | /* Dispatch to an appropriate manager based on whether we have a |
| 507 | function pointer or a function object pointer. */ |
| 508 | static inline void |
| 509 | manage(const function_buffer& in_buffer, function_buffer& out_buffer, |
| 510 | functor_manager_operation_type op) |
| 511 | { |
| 512 | typedef typename get_function_tag<functor_type>::type tag_type; |
| 513 | switch (op) { |
| 514 | case get_functor_type_tag: |
| 515 | out_buffer.members.type.type = &boost::typeindex::type_id<functor_type>().type_info(); |
| 516 | out_buffer.members.type.const_qualified = false; |
| 517 | out_buffer.members.type.volatile_qualified = false; |
| 518 | return; |
| 519 | |
| 520 | default: |
| 521 | manager(in_buffer, out_buffer, op, tag_type()); |
| 522 | return; |
| 523 | } |
| 524 | } |
| 525 | }; |
| 526 | |
| 527 | // A type that is only used for comparisons against zero |
| 528 | struct useless_clear_type {}; |
| 529 | |
| 530 | #ifdef BOOST_NO_SFINAE |
| 531 | // These routines perform comparisons between a Boost.Function |
| 532 | // object and an arbitrary function object (when the last |
| 533 | // parameter is mpl::bool_<false>) or against zero (when the |
| 534 | // last parameter is mpl::bool_<true>). They are only necessary |
| 535 | // for compilers that don't support SFINAE. |
| 536 | template<typename Function, typename Functor> |
| 537 | bool |
| 538 | compare_equal(const Function& f, const Functor&, int, mpl::bool_<true>) |
| 539 | { return f.empty(); } |
| 540 | |
| 541 | template<typename Function, typename Functor> |
| 542 | bool |
| 543 | compare_not_equal(const Function& f, const Functor&, int, |
| 544 | mpl::bool_<true>) |
| 545 | { return !f.empty(); } |
| 546 | |
| 547 | template<typename Function, typename Functor> |
| 548 | bool |
| 549 | compare_equal(const Function& f, const Functor& g, long, |
| 550 | mpl::bool_<false>) |
| 551 | { |
| 552 | if (const Functor* fp = f.template target<Functor>()) |
| 553 | return function_equal(*fp, g); |
| 554 | else return false; |
| 555 | } |
| 556 | |
| 557 | template<typename Function, typename Functor> |
| 558 | bool |
| 559 | compare_equal(const Function& f, const reference_wrapper<Functor>& g, |
| 560 | int, mpl::bool_<false>) |
| 561 | { |
| 562 | if (const Functor* fp = f.template target<Functor>()) |
| 563 | return fp == g.get_pointer(); |
| 564 | else return false; |
| 565 | } |
| 566 | |
| 567 | template<typename Function, typename Functor> |
| 568 | bool |
| 569 | compare_not_equal(const Function& f, const Functor& g, long, |
| 570 | mpl::bool_<false>) |
| 571 | { |
| 572 | if (const Functor* fp = f.template target<Functor>()) |
| 573 | return !function_equal(*fp, g); |
| 574 | else return true; |
| 575 | } |
| 576 | |
| 577 | template<typename Function, typename Functor> |
| 578 | bool |
| 579 | compare_not_equal(const Function& f, |
| 580 | const reference_wrapper<Functor>& g, int, |
| 581 | mpl::bool_<false>) |
| 582 | { |
| 583 | if (const Functor* fp = f.template target<Functor>()) |
| 584 | return fp != g.get_pointer(); |
| 585 | else return true; |
| 586 | } |
| 587 | #endif // BOOST_NO_SFINAE |
| 588 | |
| 589 | /** |
| 590 | * Stores the "manager" portion of the vtable for a |
| 591 | * boost::function object. |
| 592 | */ |
| 593 | struct vtable_base |
| 594 | { |
| 595 | void (*manager)(const function_buffer& in_buffer, |
| 596 | function_buffer& out_buffer, |
| 597 | functor_manager_operation_type op); |
| 598 | }; |
| 599 | } // end namespace function |
| 600 | } // end namespace detail |
| 601 | |
| 602 | /** |
| 603 | * The function_base class contains the basic elements needed for the |
| 604 | * function1, function2, function3, etc. classes. It is common to all |
| 605 | * functions (and as such can be used to tell if we have one of the |
| 606 | * functionN objects). |
| 607 | */ |
| 608 | class function_base |
| 609 | { |
| 610 | public: |
| 611 | function_base() : vtable(0) { } |
| 612 | |
| 613 | /** Determine if the function is empty (i.e., has no target). */ |
| 614 | bool empty() const { return !vtable; } |
| 615 | |
| 616 | /** Retrieve the type of the stored function object, or type_id<void>() |
| 617 | if this is empty. */ |
| 618 | const boost::typeindex::type_info& target_type() const |
| 619 | { |
| 620 | if (!vtable) return boost::typeindex::type_id<void>().type_info(); |
| 621 | |
| 622 | detail::function::function_buffer type; |
| 623 | get_vtable()->manager(functor, type, detail::function::get_functor_type_tag); |
| 624 | return *type.members.type.type; |
| 625 | } |
| 626 | |
| 627 | template<typename Functor> |
| 628 | Functor* target() |
| 629 | { |
| 630 | if (!vtable) return 0; |
| 631 | |
| 632 | detail::function::function_buffer type_result; |
| 633 | type_result.members.type.type = &boost::typeindex::type_id<Functor>().type_info(); |
| 634 | type_result.members.type.const_qualified = is_const<Functor>::value; |
| 635 | type_result.members.type.volatile_qualified = is_volatile<Functor>::value; |
| 636 | get_vtable()->manager(functor, type_result, |
| 637 | detail::function::check_functor_type_tag); |
| 638 | return static_cast<Functor*>(type_result.members.obj_ptr); |
| 639 | } |
| 640 | |
| 641 | template<typename Functor> |
| 642 | const Functor* target() const |
| 643 | { |
| 644 | if (!vtable) return 0; |
| 645 | |
| 646 | detail::function::function_buffer type_result; |
| 647 | type_result.members.type.type = &boost::typeindex::type_id<Functor>().type_info(); |
| 648 | type_result.members.type.const_qualified = true; |
| 649 | type_result.members.type.volatile_qualified = is_volatile<Functor>::value; |
| 650 | get_vtable()->manager(functor, type_result, |
| 651 | detail::function::check_functor_type_tag); |
| 652 | // GCC 2.95.3 gets the CV qualifiers wrong here, so we |
| 653 | // can't do the static_cast that we should do. |
| 654 | return static_cast<const Functor*>(type_result.members.obj_ptr); |
| 655 | } |
| 656 | |
| 657 | template<typename F> |
| 658 | bool contains(const F& f) const |
| 659 | { |
| 660 | if (const F* fp = this->template target<F>()) |
| 661 | { |
| 662 | return function_equal(*fp, f); |
| 663 | } else { |
| 664 | return false; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | #if defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3 |
| 669 | // GCC 3.3 and newer cannot copy with the global operator==, due to |
| 670 | // problems with instantiation of function return types before it |
| 671 | // has been verified that the argument types match up. |
| 672 | template<typename Functor> |
| 673 | BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool) |
| 674 | operator==(Functor g) const |
| 675 | { |
| 676 | if (const Functor* fp = target<Functor>()) |
| 677 | return function_equal(*fp, g); |
| 678 | else return false; |
| 679 | } |
| 680 | |
| 681 | template<typename Functor> |
| 682 | BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool) |
| 683 | operator!=(Functor g) const |
| 684 | { |
| 685 | if (const Functor* fp = target<Functor>()) |
| 686 | return !function_equal(*fp, g); |
| 687 | else return true; |
| 688 | } |
| 689 | #endif |
| 690 | |
| 691 | public: // should be protected, but GCC 2.95.3 will fail to allow access |
| 692 | detail::function::vtable_base* get_vtable() const { |
| 693 | return reinterpret_cast<detail::function::vtable_base*>( |
| 694 | reinterpret_cast<std::size_t>(vtable) & ~static_cast<std::size_t>(0x01)); |
| 695 | } |
| 696 | |
| 697 | bool has_trivial_copy_and_destroy() const { |
| 698 | return reinterpret_cast<std::size_t>(vtable) & 0x01; |
| 699 | } |
| 700 | |
| 701 | detail::function::vtable_base* vtable; |
| 702 | mutable detail::function::function_buffer functor; |
| 703 | }; |
| 704 | |
| 705 | #if defined(BOOST_CLANG) |
| 706 | # pragma clang diagnostic push |
| 707 | # pragma clang diagnostic ignored "-Wweak-vtables" |
| 708 | #endif |
| 709 | /** |
| 710 | * The bad_function_call exception class is thrown when a boost::function |
| 711 | * object is invoked |
| 712 | */ |
| 713 | class bad_function_call : public std::runtime_error |
| 714 | { |
| 715 | public: |
| 716 | bad_function_call() : std::runtime_error("call to empty boost::function") {} |
| 717 | }; |
| 718 | #if defined(BOOST_CLANG) |
| 719 | # pragma clang diagnostic pop |
| 720 | #endif |
| 721 | |
| 722 | #ifndef BOOST_NO_SFINAE |
| 723 | inline bool operator==(const function_base& f, |
| 724 | detail::function::useless_clear_type*) |
| 725 | { |
| 726 | return f.empty(); |
| 727 | } |
| 728 | |
| 729 | inline bool operator!=(const function_base& f, |
| 730 | detail::function::useless_clear_type*) |
| 731 | { |
| 732 | return !f.empty(); |
| 733 | } |
| 734 | |
| 735 | inline bool operator==(detail::function::useless_clear_type*, |
| 736 | const function_base& f) |
| 737 | { |
| 738 | return f.empty(); |
| 739 | } |
| 740 | |
| 741 | inline bool operator!=(detail::function::useless_clear_type*, |
| 742 | const function_base& f) |
| 743 | { |
| 744 | return !f.empty(); |
| 745 | } |
| 746 | #endif |
| 747 | |
| 748 | #ifdef BOOST_NO_SFINAE |
| 749 | // Comparisons between boost::function objects and arbitrary function objects |
| 750 | template<typename Functor> |
| 751 | inline bool operator==(const function_base& f, Functor g) |
| 752 | { |
| 753 | typedef mpl::bool_<(is_integral<Functor>::value)> integral; |
| 754 | return detail::function::compare_equal(f, g, 0, integral()); |
| 755 | } |
| 756 | |
| 757 | template<typename Functor> |
| 758 | inline bool operator==(Functor g, const function_base& f) |
| 759 | { |
| 760 | typedef mpl::bool_<(is_integral<Functor>::value)> integral; |
| 761 | return detail::function::compare_equal(f, g, 0, integral()); |
| 762 | } |
| 763 | |
| 764 | template<typename Functor> |
| 765 | inline bool operator!=(const function_base& f, Functor g) |
| 766 | { |
| 767 | typedef mpl::bool_<(is_integral<Functor>::value)> integral; |
| 768 | return detail::function::compare_not_equal(f, g, 0, integral()); |
| 769 | } |
| 770 | |
| 771 | template<typename Functor> |
| 772 | inline bool operator!=(Functor g, const function_base& f) |
| 773 | { |
| 774 | typedef mpl::bool_<(is_integral<Functor>::value)> integral; |
| 775 | return detail::function::compare_not_equal(f, g, 0, integral()); |
| 776 | } |
| 777 | #else |
| 778 | |
| 779 | # if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3) |
| 780 | // Comparisons between boost::function objects and arbitrary function |
| 781 | // objects. GCC 3.3 and before has an obnoxious bug that prevents this |
| 782 | // from working. |
| 783 | template<typename Functor> |
| 784 | BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool) |
| 785 | operator==(const function_base& f, Functor g) |
| 786 | { |
| 787 | if (const Functor* fp = f.template target<Functor>()) |
| 788 | return function_equal(*fp, g); |
| 789 | else return false; |
| 790 | } |
| 791 | |
| 792 | template<typename Functor> |
| 793 | BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool) |
| 794 | operator==(Functor g, const function_base& f) |
| 795 | { |
| 796 | if (const Functor* fp = f.template target<Functor>()) |
| 797 | return function_equal(g, *fp); |
| 798 | else return false; |
| 799 | } |
| 800 | |
| 801 | template<typename Functor> |
| 802 | BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool) |
| 803 | operator!=(const function_base& f, Functor g) |
| 804 | { |
| 805 | if (const Functor* fp = f.template target<Functor>()) |
| 806 | return !function_equal(*fp, g); |
| 807 | else return true; |
| 808 | } |
| 809 | |
| 810 | template<typename Functor> |
| 811 | BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool) |
| 812 | operator!=(Functor g, const function_base& f) |
| 813 | { |
| 814 | if (const Functor* fp = f.template target<Functor>()) |
| 815 | return !function_equal(g, *fp); |
| 816 | else return true; |
| 817 | } |
| 818 | # endif |
| 819 | |
| 820 | template<typename Functor> |
| 821 | BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool) |
| 822 | operator==(const function_base& f, reference_wrapper<Functor> g) |
| 823 | { |
| 824 | if (const Functor* fp = f.template target<Functor>()) |
| 825 | return fp == g.get_pointer(); |
| 826 | else return false; |
| 827 | } |
| 828 | |
| 829 | template<typename Functor> |
| 830 | BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool) |
| 831 | operator==(reference_wrapper<Functor> g, const function_base& f) |
| 832 | { |
| 833 | if (const Functor* fp = f.template target<Functor>()) |
| 834 | return g.get_pointer() == fp; |
| 835 | else return false; |
| 836 | } |
| 837 | |
| 838 | template<typename Functor> |
| 839 | BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool) |
| 840 | operator!=(const function_base& f, reference_wrapper<Functor> g) |
| 841 | { |
| 842 | if (const Functor* fp = f.template target<Functor>()) |
| 843 | return fp != g.get_pointer(); |
| 844 | else return true; |
| 845 | } |
| 846 | |
| 847 | template<typename Functor> |
| 848 | BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool) |
| 849 | operator!=(reference_wrapper<Functor> g, const function_base& f) |
| 850 | { |
| 851 | if (const Functor* fp = f.template target<Functor>()) |
| 852 | return g.get_pointer() != fp; |
| 853 | else return true; |
| 854 | } |
| 855 | |
| 856 | #endif // Compiler supporting SFINAE |
| 857 | |
| 858 | namespace detail { |
| 859 | namespace function { |
| 860 | inline bool has_empty_target(const function_base* f) |
| 861 | { |
| 862 | return f->empty(); |
| 863 | } |
| 864 | |
| 865 | #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310) |
| 866 | inline bool has_empty_target(const void*) |
| 867 | { |
| 868 | return false; |
| 869 | } |
| 870 | #else |
| 871 | inline bool has_empty_target(...) |
| 872 | { |
| 873 | return false; |
| 874 | } |
| 875 | #endif |
| 876 | } // end namespace function |
| 877 | } // end namespace detail |
| 878 | } // end namespace boost |
| 879 | |
| 880 | #undef BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL |
| 881 | |
| 882 | #if defined(BOOST_MSVC) |
| 883 | # pragma warning( pop ) |
| 884 | #endif |
| 885 | |
| 886 | #endif // BOOST_FUNCTION_BASE_HEADER |