annotate DEPENDENCIES/generic/include/boost/function/function_template.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 // Boost.Function library
Chris@16 2
Chris@16 3 // Copyright Douglas Gregor 2001-2006
Chris@16 4 // Copyright Emil Dotchevski 2007
Chris@16 5 // Use, modification and distribution is subject to the Boost Software License, Version 1.0.
Chris@16 6 // (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8
Chris@16 9 // For more information, see http://www.boost.org
Chris@16 10
Chris@16 11 // Note: this header is a header template and must NOT have multiple-inclusion
Chris@16 12 // protection.
Chris@16 13 #include <boost/function/detail/prologue.hpp>
Chris@16 14 #include <boost/detail/no_exceptions_support.hpp>
Chris@16 15
Chris@16 16 #if defined(BOOST_MSVC)
Chris@16 17 # pragma warning( push )
Chris@16 18 # pragma warning( disable : 4127 ) // "conditional expression is constant"
Chris@16 19 #endif
Chris@16 20
Chris@16 21 #define BOOST_FUNCTION_TEMPLATE_PARMS BOOST_PP_ENUM_PARAMS(BOOST_FUNCTION_NUM_ARGS, typename T)
Chris@16 22
Chris@16 23 #define BOOST_FUNCTION_TEMPLATE_ARGS BOOST_PP_ENUM_PARAMS(BOOST_FUNCTION_NUM_ARGS, T)
Chris@16 24
Chris@16 25 #define BOOST_FUNCTION_PARM(J,I,D) BOOST_PP_CAT(T,I) BOOST_PP_CAT(a,I)
Chris@16 26
Chris@16 27 #define BOOST_FUNCTION_PARMS BOOST_PP_ENUM(BOOST_FUNCTION_NUM_ARGS,BOOST_FUNCTION_PARM,BOOST_PP_EMPTY)
Chris@16 28
Chris@16 29 #define BOOST_FUNCTION_ARGS BOOST_PP_ENUM_PARAMS(BOOST_FUNCTION_NUM_ARGS, a)
Chris@16 30
Chris@16 31 #define BOOST_FUNCTION_ARG_TYPE(J,I,D) \
Chris@16 32 typedef BOOST_PP_CAT(T,I) BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(I)),_type);
Chris@16 33
Chris@16 34 #define BOOST_FUNCTION_ARG_TYPES BOOST_PP_REPEAT(BOOST_FUNCTION_NUM_ARGS,BOOST_FUNCTION_ARG_TYPE,BOOST_PP_EMPTY)
Chris@16 35
Chris@16 36 // Comma if nonzero number of arguments
Chris@16 37 #if BOOST_FUNCTION_NUM_ARGS == 0
Chris@16 38 # define BOOST_FUNCTION_COMMA
Chris@16 39 #else
Chris@16 40 # define BOOST_FUNCTION_COMMA ,
Chris@16 41 #endif // BOOST_FUNCTION_NUM_ARGS > 0
Chris@16 42
Chris@16 43 // Class names used in this version of the code
Chris@16 44 #define BOOST_FUNCTION_FUNCTION BOOST_JOIN(function,BOOST_FUNCTION_NUM_ARGS)
Chris@16 45 #define BOOST_FUNCTION_FUNCTION_INVOKER \
Chris@16 46 BOOST_JOIN(function_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 47 #define BOOST_FUNCTION_VOID_FUNCTION_INVOKER \
Chris@16 48 BOOST_JOIN(void_function_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 49 #define BOOST_FUNCTION_FUNCTION_OBJ_INVOKER \
Chris@16 50 BOOST_JOIN(function_obj_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 51 #define BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER \
Chris@16 52 BOOST_JOIN(void_function_obj_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 53 #define BOOST_FUNCTION_FUNCTION_REF_INVOKER \
Chris@16 54 BOOST_JOIN(function_ref_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 55 #define BOOST_FUNCTION_VOID_FUNCTION_REF_INVOKER \
Chris@16 56 BOOST_JOIN(void_function_ref_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 57 #define BOOST_FUNCTION_MEMBER_INVOKER \
Chris@16 58 BOOST_JOIN(function_mem_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 59 #define BOOST_FUNCTION_VOID_MEMBER_INVOKER \
Chris@16 60 BOOST_JOIN(function_void_mem_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 61 #define BOOST_FUNCTION_GET_FUNCTION_INVOKER \
Chris@16 62 BOOST_JOIN(get_function_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 63 #define BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER \
Chris@16 64 BOOST_JOIN(get_function_obj_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 65 #define BOOST_FUNCTION_GET_FUNCTION_REF_INVOKER \
Chris@16 66 BOOST_JOIN(get_function_ref_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 67 #define BOOST_FUNCTION_GET_MEMBER_INVOKER \
Chris@16 68 BOOST_JOIN(get_member_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 69 #define BOOST_FUNCTION_GET_INVOKER \
Chris@16 70 BOOST_JOIN(get_invoker,BOOST_FUNCTION_NUM_ARGS)
Chris@16 71 #define BOOST_FUNCTION_VTABLE BOOST_JOIN(basic_vtable,BOOST_FUNCTION_NUM_ARGS)
Chris@16 72
Chris@16 73 #ifndef BOOST_NO_VOID_RETURNS
Chris@16 74 # define BOOST_FUNCTION_VOID_RETURN_TYPE void
Chris@16 75 # define BOOST_FUNCTION_RETURN(X) X
Chris@16 76 #else
Chris@16 77 # define BOOST_FUNCTION_VOID_RETURN_TYPE boost::detail::function::unusable
Chris@16 78 # define BOOST_FUNCTION_RETURN(X) X; return BOOST_FUNCTION_VOID_RETURN_TYPE ()
Chris@16 79 #endif
Chris@16 80
Chris@16 81 namespace boost {
Chris@16 82 namespace detail {
Chris@16 83 namespace function {
Chris@16 84 template<
Chris@16 85 typename FunctionPtr,
Chris@16 86 typename R BOOST_FUNCTION_COMMA
Chris@16 87 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 88 >
Chris@16 89 struct BOOST_FUNCTION_FUNCTION_INVOKER
Chris@16 90 {
Chris@16 91 static R invoke(function_buffer& function_ptr BOOST_FUNCTION_COMMA
Chris@16 92 BOOST_FUNCTION_PARMS)
Chris@16 93 {
Chris@16 94 FunctionPtr f = reinterpret_cast<FunctionPtr>(function_ptr.func_ptr);
Chris@16 95 return f(BOOST_FUNCTION_ARGS);
Chris@16 96 }
Chris@16 97 };
Chris@16 98
Chris@16 99 template<
Chris@16 100 typename FunctionPtr,
Chris@16 101 typename R BOOST_FUNCTION_COMMA
Chris@16 102 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 103 >
Chris@16 104 struct BOOST_FUNCTION_VOID_FUNCTION_INVOKER
Chris@16 105 {
Chris@16 106 static BOOST_FUNCTION_VOID_RETURN_TYPE
Chris@16 107 invoke(function_buffer& function_ptr BOOST_FUNCTION_COMMA
Chris@16 108 BOOST_FUNCTION_PARMS)
Chris@16 109
Chris@16 110 {
Chris@16 111 FunctionPtr f = reinterpret_cast<FunctionPtr>(function_ptr.func_ptr);
Chris@16 112 BOOST_FUNCTION_RETURN(f(BOOST_FUNCTION_ARGS));
Chris@16 113 }
Chris@16 114 };
Chris@16 115
Chris@16 116 template<
Chris@16 117 typename FunctionObj,
Chris@16 118 typename R BOOST_FUNCTION_COMMA
Chris@16 119 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 120 >
Chris@16 121 struct BOOST_FUNCTION_FUNCTION_OBJ_INVOKER
Chris@16 122 {
Chris@16 123 static R invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
Chris@16 124 BOOST_FUNCTION_PARMS)
Chris@16 125
Chris@16 126 {
Chris@16 127 FunctionObj* f;
Chris@16 128 if (function_allows_small_object_optimization<FunctionObj>::value)
Chris@16 129 f = reinterpret_cast<FunctionObj*>(&function_obj_ptr.data);
Chris@16 130 else
Chris@16 131 f = reinterpret_cast<FunctionObj*>(function_obj_ptr.obj_ptr);
Chris@16 132 return (*f)(BOOST_FUNCTION_ARGS);
Chris@16 133 }
Chris@16 134 };
Chris@16 135
Chris@16 136 template<
Chris@16 137 typename FunctionObj,
Chris@16 138 typename R BOOST_FUNCTION_COMMA
Chris@16 139 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 140 >
Chris@16 141 struct BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER
Chris@16 142 {
Chris@16 143 static BOOST_FUNCTION_VOID_RETURN_TYPE
Chris@16 144 invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
Chris@16 145 BOOST_FUNCTION_PARMS)
Chris@16 146
Chris@16 147 {
Chris@16 148 FunctionObj* f;
Chris@16 149 if (function_allows_small_object_optimization<FunctionObj>::value)
Chris@16 150 f = reinterpret_cast<FunctionObj*>(&function_obj_ptr.data);
Chris@16 151 else
Chris@16 152 f = reinterpret_cast<FunctionObj*>(function_obj_ptr.obj_ptr);
Chris@16 153 BOOST_FUNCTION_RETURN((*f)(BOOST_FUNCTION_ARGS));
Chris@16 154 }
Chris@16 155 };
Chris@16 156
Chris@16 157 template<
Chris@16 158 typename FunctionObj,
Chris@16 159 typename R BOOST_FUNCTION_COMMA
Chris@16 160 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 161 >
Chris@16 162 struct BOOST_FUNCTION_FUNCTION_REF_INVOKER
Chris@16 163 {
Chris@16 164 static R invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
Chris@16 165 BOOST_FUNCTION_PARMS)
Chris@16 166
Chris@16 167 {
Chris@16 168 FunctionObj* f =
Chris@16 169 reinterpret_cast<FunctionObj*>(function_obj_ptr.obj_ptr);
Chris@16 170 return (*f)(BOOST_FUNCTION_ARGS);
Chris@16 171 }
Chris@16 172 };
Chris@16 173
Chris@16 174 template<
Chris@16 175 typename FunctionObj,
Chris@16 176 typename R BOOST_FUNCTION_COMMA
Chris@16 177 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 178 >
Chris@16 179 struct BOOST_FUNCTION_VOID_FUNCTION_REF_INVOKER
Chris@16 180 {
Chris@16 181 static BOOST_FUNCTION_VOID_RETURN_TYPE
Chris@16 182 invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
Chris@16 183 BOOST_FUNCTION_PARMS)
Chris@16 184
Chris@16 185 {
Chris@16 186 FunctionObj* f =
Chris@16 187 reinterpret_cast<FunctionObj*>(function_obj_ptr.obj_ptr);
Chris@16 188 BOOST_FUNCTION_RETURN((*f)(BOOST_FUNCTION_ARGS));
Chris@16 189 }
Chris@16 190 };
Chris@16 191
Chris@16 192 #if BOOST_FUNCTION_NUM_ARGS > 0
Chris@16 193 /* Handle invocation of member pointers. */
Chris@16 194 template<
Chris@16 195 typename MemberPtr,
Chris@16 196 typename R BOOST_FUNCTION_COMMA
Chris@16 197 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 198 >
Chris@16 199 struct BOOST_FUNCTION_MEMBER_INVOKER
Chris@16 200 {
Chris@16 201 static R invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
Chris@16 202 BOOST_FUNCTION_PARMS)
Chris@16 203
Chris@16 204 {
Chris@16 205 MemberPtr* f =
Chris@16 206 reinterpret_cast<MemberPtr*>(&function_obj_ptr.data);
Chris@16 207 return boost::mem_fn(*f)(BOOST_FUNCTION_ARGS);
Chris@16 208 }
Chris@16 209 };
Chris@16 210
Chris@16 211 template<
Chris@16 212 typename MemberPtr,
Chris@16 213 typename R BOOST_FUNCTION_COMMA
Chris@16 214 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 215 >
Chris@16 216 struct BOOST_FUNCTION_VOID_MEMBER_INVOKER
Chris@16 217 {
Chris@16 218 static BOOST_FUNCTION_VOID_RETURN_TYPE
Chris@16 219 invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
Chris@16 220 BOOST_FUNCTION_PARMS)
Chris@16 221
Chris@16 222 {
Chris@16 223 MemberPtr* f =
Chris@16 224 reinterpret_cast<MemberPtr*>(&function_obj_ptr.data);
Chris@16 225 BOOST_FUNCTION_RETURN(boost::mem_fn(*f)(BOOST_FUNCTION_ARGS));
Chris@16 226 }
Chris@16 227 };
Chris@16 228 #endif
Chris@16 229
Chris@16 230 template<
Chris@16 231 typename FunctionPtr,
Chris@16 232 typename R BOOST_FUNCTION_COMMA
Chris@16 233 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 234 >
Chris@16 235 struct BOOST_FUNCTION_GET_FUNCTION_INVOKER
Chris@16 236 {
Chris@16 237 typedef typename mpl::if_c<(is_void<R>::value),
Chris@16 238 BOOST_FUNCTION_VOID_FUNCTION_INVOKER<
Chris@16 239 FunctionPtr,
Chris@16 240 R BOOST_FUNCTION_COMMA
Chris@16 241 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 242 >,
Chris@16 243 BOOST_FUNCTION_FUNCTION_INVOKER<
Chris@16 244 FunctionPtr,
Chris@16 245 R BOOST_FUNCTION_COMMA
Chris@16 246 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 247 >
Chris@16 248 >::type type;
Chris@16 249 };
Chris@16 250
Chris@16 251 template<
Chris@16 252 typename FunctionObj,
Chris@16 253 typename R BOOST_FUNCTION_COMMA
Chris@16 254 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 255 >
Chris@16 256 struct BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER
Chris@16 257 {
Chris@16 258 typedef typename mpl::if_c<(is_void<R>::value),
Chris@16 259 BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER<
Chris@16 260 FunctionObj,
Chris@16 261 R BOOST_FUNCTION_COMMA
Chris@16 262 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 263 >,
Chris@16 264 BOOST_FUNCTION_FUNCTION_OBJ_INVOKER<
Chris@16 265 FunctionObj,
Chris@16 266 R BOOST_FUNCTION_COMMA
Chris@16 267 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 268 >
Chris@16 269 >::type type;
Chris@16 270 };
Chris@16 271
Chris@16 272 template<
Chris@16 273 typename FunctionObj,
Chris@16 274 typename R BOOST_FUNCTION_COMMA
Chris@16 275 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 276 >
Chris@16 277 struct BOOST_FUNCTION_GET_FUNCTION_REF_INVOKER
Chris@16 278 {
Chris@16 279 typedef typename mpl::if_c<(is_void<R>::value),
Chris@16 280 BOOST_FUNCTION_VOID_FUNCTION_REF_INVOKER<
Chris@16 281 FunctionObj,
Chris@16 282 R BOOST_FUNCTION_COMMA
Chris@16 283 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 284 >,
Chris@16 285 BOOST_FUNCTION_FUNCTION_REF_INVOKER<
Chris@16 286 FunctionObj,
Chris@16 287 R BOOST_FUNCTION_COMMA
Chris@16 288 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 289 >
Chris@16 290 >::type type;
Chris@16 291 };
Chris@16 292
Chris@16 293 #if BOOST_FUNCTION_NUM_ARGS > 0
Chris@16 294 /* Retrieve the appropriate invoker for a member pointer. */
Chris@16 295 template<
Chris@16 296 typename MemberPtr,
Chris@16 297 typename R BOOST_FUNCTION_COMMA
Chris@16 298 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 299 >
Chris@16 300 struct BOOST_FUNCTION_GET_MEMBER_INVOKER
Chris@16 301 {
Chris@16 302 typedef typename mpl::if_c<(is_void<R>::value),
Chris@16 303 BOOST_FUNCTION_VOID_MEMBER_INVOKER<
Chris@16 304 MemberPtr,
Chris@16 305 R BOOST_FUNCTION_COMMA
Chris@16 306 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 307 >,
Chris@16 308 BOOST_FUNCTION_MEMBER_INVOKER<
Chris@16 309 MemberPtr,
Chris@16 310 R BOOST_FUNCTION_COMMA
Chris@16 311 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 312 >
Chris@16 313 >::type type;
Chris@16 314 };
Chris@16 315 #endif
Chris@16 316
Chris@16 317 /* Given the tag returned by get_function_tag, retrieve the
Chris@16 318 actual invoker that will be used for the given function
Chris@16 319 object.
Chris@16 320
Chris@16 321 Each specialization contains an "apply" nested class template
Chris@16 322 that accepts the function object, return type, function
Chris@16 323 argument types, and allocator. The resulting "apply" class
Chris@16 324 contains two typedefs, "invoker_type" and "manager_type",
Chris@16 325 which correspond to the invoker and manager types. */
Chris@16 326 template<typename Tag>
Chris@16 327 struct BOOST_FUNCTION_GET_INVOKER { };
Chris@16 328
Chris@16 329 /* Retrieve the invoker for a function pointer. */
Chris@16 330 template<>
Chris@16 331 struct BOOST_FUNCTION_GET_INVOKER<function_ptr_tag>
Chris@16 332 {
Chris@16 333 template<typename FunctionPtr,
Chris@16 334 typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS>
Chris@16 335 struct apply
Chris@16 336 {
Chris@16 337 typedef typename BOOST_FUNCTION_GET_FUNCTION_INVOKER<
Chris@16 338 FunctionPtr,
Chris@16 339 R BOOST_FUNCTION_COMMA
Chris@16 340 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 341 >::type
Chris@16 342 invoker_type;
Chris@16 343
Chris@16 344 typedef functor_manager<FunctionPtr> manager_type;
Chris@16 345 };
Chris@16 346
Chris@16 347 template<typename FunctionPtr,
Chris@16 348 typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS,
Chris@16 349 typename Allocator>
Chris@16 350 struct apply_a
Chris@16 351 {
Chris@16 352 typedef typename BOOST_FUNCTION_GET_FUNCTION_INVOKER<
Chris@16 353 FunctionPtr,
Chris@16 354 R BOOST_FUNCTION_COMMA
Chris@16 355 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 356 >::type
Chris@16 357 invoker_type;
Chris@16 358
Chris@16 359 typedef functor_manager<FunctionPtr> manager_type;
Chris@16 360 };
Chris@16 361 };
Chris@16 362
Chris@16 363 #if BOOST_FUNCTION_NUM_ARGS > 0
Chris@16 364 /* Retrieve the invoker for a member pointer. */
Chris@16 365 template<>
Chris@16 366 struct BOOST_FUNCTION_GET_INVOKER<member_ptr_tag>
Chris@16 367 {
Chris@16 368 template<typename MemberPtr,
Chris@16 369 typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS>
Chris@16 370 struct apply
Chris@16 371 {
Chris@16 372 typedef typename BOOST_FUNCTION_GET_MEMBER_INVOKER<
Chris@16 373 MemberPtr,
Chris@16 374 R BOOST_FUNCTION_COMMA
Chris@16 375 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 376 >::type
Chris@16 377 invoker_type;
Chris@16 378
Chris@16 379 typedef functor_manager<MemberPtr> manager_type;
Chris@16 380 };
Chris@16 381
Chris@16 382 template<typename MemberPtr,
Chris@16 383 typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS,
Chris@16 384 typename Allocator>
Chris@16 385 struct apply_a
Chris@16 386 {
Chris@16 387 typedef typename BOOST_FUNCTION_GET_MEMBER_INVOKER<
Chris@16 388 MemberPtr,
Chris@16 389 R BOOST_FUNCTION_COMMA
Chris@16 390 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 391 >::type
Chris@16 392 invoker_type;
Chris@16 393
Chris@16 394 typedef functor_manager<MemberPtr> manager_type;
Chris@16 395 };
Chris@16 396 };
Chris@16 397 #endif
Chris@16 398
Chris@16 399 /* Retrieve the invoker for a function object. */
Chris@16 400 template<>
Chris@16 401 struct BOOST_FUNCTION_GET_INVOKER<function_obj_tag>
Chris@16 402 {
Chris@16 403 template<typename FunctionObj,
Chris@16 404 typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS>
Chris@16 405 struct apply
Chris@16 406 {
Chris@16 407 typedef typename BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER<
Chris@16 408 FunctionObj,
Chris@16 409 R BOOST_FUNCTION_COMMA
Chris@16 410 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 411 >::type
Chris@16 412 invoker_type;
Chris@16 413
Chris@16 414 typedef functor_manager<FunctionObj> manager_type;
Chris@16 415 };
Chris@16 416
Chris@16 417 template<typename FunctionObj,
Chris@16 418 typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS,
Chris@16 419 typename Allocator>
Chris@16 420 struct apply_a
Chris@16 421 {
Chris@16 422 typedef typename BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER<
Chris@16 423 FunctionObj,
Chris@16 424 R BOOST_FUNCTION_COMMA
Chris@16 425 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 426 >::type
Chris@16 427 invoker_type;
Chris@16 428
Chris@16 429 typedef functor_manager_a<FunctionObj, Allocator> manager_type;
Chris@16 430 };
Chris@16 431 };
Chris@16 432
Chris@16 433 /* Retrieve the invoker for a reference to a function object. */
Chris@16 434 template<>
Chris@16 435 struct BOOST_FUNCTION_GET_INVOKER<function_obj_ref_tag>
Chris@16 436 {
Chris@16 437 template<typename RefWrapper,
Chris@16 438 typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS>
Chris@16 439 struct apply
Chris@16 440 {
Chris@16 441 typedef typename BOOST_FUNCTION_GET_FUNCTION_REF_INVOKER<
Chris@16 442 typename RefWrapper::type,
Chris@16 443 R BOOST_FUNCTION_COMMA
Chris@16 444 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 445 >::type
Chris@16 446 invoker_type;
Chris@16 447
Chris@16 448 typedef reference_manager<typename RefWrapper::type> manager_type;
Chris@16 449 };
Chris@16 450
Chris@16 451 template<typename RefWrapper,
Chris@16 452 typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS,
Chris@16 453 typename Allocator>
Chris@16 454 struct apply_a
Chris@16 455 {
Chris@16 456 typedef typename BOOST_FUNCTION_GET_FUNCTION_REF_INVOKER<
Chris@16 457 typename RefWrapper::type,
Chris@16 458 R BOOST_FUNCTION_COMMA
Chris@16 459 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 460 >::type
Chris@16 461 invoker_type;
Chris@16 462
Chris@16 463 typedef reference_manager<typename RefWrapper::type> manager_type;
Chris@16 464 };
Chris@16 465 };
Chris@16 466
Chris@16 467
Chris@16 468 /**
Chris@16 469 * vtable for a specific boost::function instance. This
Chris@16 470 * structure must be an aggregate so that we can use static
Chris@16 471 * initialization in boost::function's assign_to and assign_to_a
Chris@16 472 * members. It therefore cannot have any constructors,
Chris@16 473 * destructors, base classes, etc.
Chris@16 474 */
Chris@16 475 template<typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS>
Chris@16 476 struct BOOST_FUNCTION_VTABLE
Chris@16 477 {
Chris@16 478 #ifndef BOOST_NO_VOID_RETURNS
Chris@16 479 typedef R result_type;
Chris@16 480 #else
Chris@16 481 typedef typename function_return_type<R>::type result_type;
Chris@16 482 #endif // BOOST_NO_VOID_RETURNS
Chris@16 483
Chris@16 484 typedef result_type (*invoker_type)(function_buffer&
Chris@16 485 BOOST_FUNCTION_COMMA
Chris@16 486 BOOST_FUNCTION_TEMPLATE_ARGS);
Chris@16 487
Chris@16 488 template<typename F>
Chris@16 489 bool assign_to(F f, function_buffer& functor) const
Chris@16 490 {
Chris@16 491 typedef typename get_function_tag<F>::type tag;
Chris@16 492 return assign_to(f, functor, tag());
Chris@16 493 }
Chris@16 494 template<typename F,typename Allocator>
Chris@16 495 bool assign_to_a(F f, function_buffer& functor, Allocator a) const
Chris@16 496 {
Chris@16 497 typedef typename get_function_tag<F>::type tag;
Chris@16 498 return assign_to_a(f, functor, a, tag());
Chris@16 499 }
Chris@16 500
Chris@16 501 void clear(function_buffer& functor) const
Chris@16 502 {
Chris@16 503 if (base.manager)
Chris@16 504 base.manager(functor, functor, destroy_functor_tag);
Chris@16 505 }
Chris@16 506
Chris@16 507 private:
Chris@16 508 // Function pointers
Chris@16 509 template<typename FunctionPtr>
Chris@16 510 bool
Chris@16 511 assign_to(FunctionPtr f, function_buffer& functor, function_ptr_tag) const
Chris@16 512 {
Chris@16 513 this->clear(functor);
Chris@16 514 if (f) {
Chris@16 515 // should be a reinterpret cast, but some compilers insist
Chris@16 516 // on giving cv-qualifiers to free functions
Chris@16 517 functor.func_ptr = reinterpret_cast<void (*)()>(f);
Chris@16 518 return true;
Chris@16 519 } else {
Chris@16 520 return false;
Chris@16 521 }
Chris@16 522 }
Chris@16 523 template<typename FunctionPtr,typename Allocator>
Chris@16 524 bool
Chris@16 525 assign_to_a(FunctionPtr f, function_buffer& functor, Allocator, function_ptr_tag) const
Chris@16 526 {
Chris@16 527 return assign_to(f,functor,function_ptr_tag());
Chris@16 528 }
Chris@16 529
Chris@16 530 // Member pointers
Chris@16 531 #if BOOST_FUNCTION_NUM_ARGS > 0
Chris@16 532 template<typename MemberPtr>
Chris@16 533 bool assign_to(MemberPtr f, function_buffer& functor, member_ptr_tag) const
Chris@16 534 {
Chris@16 535 // DPG TBD: Add explicit support for member function
Chris@16 536 // objects, so we invoke through mem_fn() but we retain the
Chris@16 537 // right target_type() values.
Chris@16 538 if (f) {
Chris@16 539 this->assign_to(boost::mem_fn(f), functor);
Chris@16 540 return true;
Chris@16 541 } else {
Chris@16 542 return false;
Chris@16 543 }
Chris@16 544 }
Chris@16 545 template<typename MemberPtr,typename Allocator>
Chris@16 546 bool assign_to_a(MemberPtr f, function_buffer& functor, Allocator a, member_ptr_tag) const
Chris@16 547 {
Chris@16 548 // DPG TBD: Add explicit support for member function
Chris@16 549 // objects, so we invoke through mem_fn() but we retain the
Chris@16 550 // right target_type() values.
Chris@16 551 if (f) {
Chris@16 552 this->assign_to_a(boost::mem_fn(f), functor, a);
Chris@16 553 return true;
Chris@16 554 } else {
Chris@16 555 return false;
Chris@16 556 }
Chris@16 557 }
Chris@16 558 #endif // BOOST_FUNCTION_NUM_ARGS > 0
Chris@16 559
Chris@16 560 // Function objects
Chris@16 561 // Assign to a function object using the small object optimization
Chris@16 562 template<typename FunctionObj>
Chris@16 563 void
Chris@16 564 assign_functor(FunctionObj f, function_buffer& functor, mpl::true_) const
Chris@16 565 {
Chris@16 566 new (reinterpret_cast<void*>(&functor.data)) FunctionObj(f);
Chris@16 567 }
Chris@16 568 template<typename FunctionObj,typename Allocator>
Chris@16 569 void
Chris@16 570 assign_functor_a(FunctionObj f, function_buffer& functor, Allocator, mpl::true_) const
Chris@16 571 {
Chris@16 572 assign_functor(f,functor,mpl::true_());
Chris@16 573 }
Chris@16 574
Chris@16 575 // Assign to a function object allocated on the heap.
Chris@16 576 template<typename FunctionObj>
Chris@16 577 void
Chris@16 578 assign_functor(FunctionObj f, function_buffer& functor, mpl::false_) const
Chris@16 579 {
Chris@16 580 functor.obj_ptr = new FunctionObj(f);
Chris@16 581 }
Chris@16 582 template<typename FunctionObj,typename Allocator>
Chris@16 583 void
Chris@16 584 assign_functor_a(FunctionObj f, function_buffer& functor, Allocator a, mpl::false_) const
Chris@16 585 {
Chris@16 586 typedef functor_wrapper<FunctionObj,Allocator> functor_wrapper_type;
Chris@16 587 typedef typename Allocator::template rebind<functor_wrapper_type>::other
Chris@16 588 wrapper_allocator_type;
Chris@16 589 typedef typename wrapper_allocator_type::pointer wrapper_allocator_pointer_type;
Chris@16 590 wrapper_allocator_type wrapper_allocator(a);
Chris@16 591 wrapper_allocator_pointer_type copy = wrapper_allocator.allocate(1);
Chris@16 592 wrapper_allocator.construct(copy, functor_wrapper_type(f,a));
Chris@16 593 functor_wrapper_type* new_f = static_cast<functor_wrapper_type*>(copy);
Chris@16 594 functor.obj_ptr = new_f;
Chris@16 595 }
Chris@16 596
Chris@16 597 template<typename FunctionObj>
Chris@16 598 bool
Chris@16 599 assign_to(FunctionObj f, function_buffer& functor, function_obj_tag) const
Chris@16 600 {
Chris@16 601 if (!boost::detail::function::has_empty_target(boost::addressof(f))) {
Chris@16 602 assign_functor(f, functor,
Chris@16 603 mpl::bool_<(function_allows_small_object_optimization<FunctionObj>::value)>());
Chris@16 604 return true;
Chris@16 605 } else {
Chris@16 606 return false;
Chris@16 607 }
Chris@16 608 }
Chris@16 609 template<typename FunctionObj,typename Allocator>
Chris@16 610 bool
Chris@16 611 assign_to_a(FunctionObj f, function_buffer& functor, Allocator a, function_obj_tag) const
Chris@16 612 {
Chris@16 613 if (!boost::detail::function::has_empty_target(boost::addressof(f))) {
Chris@16 614 assign_functor_a(f, functor, a,
Chris@16 615 mpl::bool_<(function_allows_small_object_optimization<FunctionObj>::value)>());
Chris@16 616 return true;
Chris@16 617 } else {
Chris@16 618 return false;
Chris@16 619 }
Chris@16 620 }
Chris@16 621
Chris@16 622 // Reference to a function object
Chris@16 623 template<typename FunctionObj>
Chris@16 624 bool
Chris@16 625 assign_to(const reference_wrapper<FunctionObj>& f,
Chris@16 626 function_buffer& functor, function_obj_ref_tag) const
Chris@16 627 {
Chris@16 628 functor.obj_ref.obj_ptr = (void *)(f.get_pointer());
Chris@16 629 functor.obj_ref.is_const_qualified = is_const<FunctionObj>::value;
Chris@16 630 functor.obj_ref.is_volatile_qualified = is_volatile<FunctionObj>::value;
Chris@16 631 return true;
Chris@16 632 }
Chris@16 633 template<typename FunctionObj,typename Allocator>
Chris@16 634 bool
Chris@16 635 assign_to_a(const reference_wrapper<FunctionObj>& f,
Chris@16 636 function_buffer& functor, Allocator, function_obj_ref_tag) const
Chris@16 637 {
Chris@16 638 return assign_to(f,functor,function_obj_ref_tag());
Chris@16 639 }
Chris@16 640
Chris@16 641 public:
Chris@16 642 vtable_base base;
Chris@16 643 invoker_type invoker;
Chris@16 644 };
Chris@16 645 } // end namespace function
Chris@16 646 } // end namespace detail
Chris@16 647
Chris@16 648 template<
Chris@16 649 typename R BOOST_FUNCTION_COMMA
Chris@16 650 BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 651 >
Chris@16 652 class BOOST_FUNCTION_FUNCTION : public function_base
Chris@16 653
Chris@16 654 #if BOOST_FUNCTION_NUM_ARGS == 1
Chris@16 655
Chris@16 656 , public std::unary_function<T0,R>
Chris@16 657
Chris@16 658 #elif BOOST_FUNCTION_NUM_ARGS == 2
Chris@16 659
Chris@16 660 , public std::binary_function<T0,T1,R>
Chris@16 661
Chris@16 662 #endif
Chris@16 663
Chris@16 664 {
Chris@16 665 public:
Chris@16 666 #ifndef BOOST_NO_VOID_RETURNS
Chris@16 667 typedef R result_type;
Chris@16 668 #else
Chris@16 669 typedef typename boost::detail::function::function_return_type<R>::type
Chris@16 670 result_type;
Chris@16 671 #endif // BOOST_NO_VOID_RETURNS
Chris@16 672
Chris@16 673 private:
Chris@16 674 typedef boost::detail::function::BOOST_FUNCTION_VTABLE<
Chris@16 675 R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_ARGS>
Chris@16 676 vtable_type;
Chris@16 677
Chris@16 678 vtable_type* get_vtable() const {
Chris@16 679 return reinterpret_cast<vtable_type*>(
Chris@16 680 reinterpret_cast<std::size_t>(vtable) & ~static_cast<std::size_t>(0x01));
Chris@16 681 }
Chris@16 682
Chris@16 683 struct clear_type {};
Chris@16 684
Chris@16 685 public:
Chris@16 686 BOOST_STATIC_CONSTANT(int, args = BOOST_FUNCTION_NUM_ARGS);
Chris@16 687
Chris@16 688 // add signature for boost::lambda
Chris@16 689 template<typename Args>
Chris@16 690 struct sig
Chris@16 691 {
Chris@16 692 typedef result_type type;
Chris@16 693 };
Chris@16 694
Chris@16 695 #if BOOST_FUNCTION_NUM_ARGS == 1
Chris@16 696 typedef T0 argument_type;
Chris@16 697 #elif BOOST_FUNCTION_NUM_ARGS == 2
Chris@16 698 typedef T0 first_argument_type;
Chris@16 699 typedef T1 second_argument_type;
Chris@16 700 #endif
Chris@16 701
Chris@16 702 BOOST_STATIC_CONSTANT(int, arity = BOOST_FUNCTION_NUM_ARGS);
Chris@16 703 BOOST_FUNCTION_ARG_TYPES
Chris@16 704
Chris@16 705 typedef BOOST_FUNCTION_FUNCTION self_type;
Chris@16 706
Chris@16 707 BOOST_FUNCTION_FUNCTION() : function_base() { }
Chris@16 708
Chris@16 709 // MSVC chokes if the following two constructors are collapsed into
Chris@16 710 // one with a default parameter.
Chris@16 711 template<typename Functor>
Chris@16 712 BOOST_FUNCTION_FUNCTION(Functor BOOST_FUNCTION_TARGET_FIX(const &) f
Chris@16 713 #ifndef BOOST_NO_SFINAE
Chris@16 714 ,typename enable_if_c<
Chris@16 715 (boost::type_traits::ice_not<
Chris@16 716 (is_integral<Functor>::value)>::value),
Chris@16 717 int>::type = 0
Chris@16 718 #endif // BOOST_NO_SFINAE
Chris@16 719 ) :
Chris@16 720 function_base()
Chris@16 721 {
Chris@16 722 this->assign_to(f);
Chris@16 723 }
Chris@16 724 template<typename Functor,typename Allocator>
Chris@16 725 BOOST_FUNCTION_FUNCTION(Functor BOOST_FUNCTION_TARGET_FIX(const &) f, Allocator a
Chris@16 726 #ifndef BOOST_NO_SFINAE
Chris@16 727 ,typename enable_if_c<
Chris@16 728 (boost::type_traits::ice_not<
Chris@16 729 (is_integral<Functor>::value)>::value),
Chris@16 730 int>::type = 0
Chris@16 731 #endif // BOOST_NO_SFINAE
Chris@16 732 ) :
Chris@16 733 function_base()
Chris@16 734 {
Chris@16 735 this->assign_to_a(f,a);
Chris@16 736 }
Chris@16 737
Chris@16 738 #ifndef BOOST_NO_SFINAE
Chris@16 739 BOOST_FUNCTION_FUNCTION(clear_type*) : function_base() { }
Chris@16 740 #else
Chris@16 741 BOOST_FUNCTION_FUNCTION(int zero) : function_base()
Chris@16 742 {
Chris@16 743 BOOST_ASSERT(zero == 0);
Chris@16 744 }
Chris@16 745 #endif
Chris@16 746
Chris@16 747 BOOST_FUNCTION_FUNCTION(const BOOST_FUNCTION_FUNCTION& f) : function_base()
Chris@16 748 {
Chris@16 749 this->assign_to_own(f);
Chris@16 750 }
Chris@16 751
Chris@16 752 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 753 BOOST_FUNCTION_FUNCTION(BOOST_FUNCTION_FUNCTION&& f) : function_base()
Chris@16 754 {
Chris@16 755 this->move_assign(f);
Chris@16 756 }
Chris@16 757 #endif
Chris@16 758
Chris@16 759 ~BOOST_FUNCTION_FUNCTION() { clear(); }
Chris@16 760
Chris@16 761 result_type operator()(BOOST_FUNCTION_PARMS) const
Chris@16 762 {
Chris@16 763 if (this->empty())
Chris@16 764 boost::throw_exception(bad_function_call());
Chris@16 765
Chris@16 766 return get_vtable()->invoker
Chris@16 767 (this->functor BOOST_FUNCTION_COMMA BOOST_FUNCTION_ARGS);
Chris@16 768 }
Chris@16 769
Chris@16 770 // The distinction between when to use BOOST_FUNCTION_FUNCTION and
Chris@16 771 // when to use self_type is obnoxious. MSVC cannot handle self_type as
Chris@16 772 // the return type of these assignment operators, but Borland C++ cannot
Chris@16 773 // handle BOOST_FUNCTION_FUNCTION as the type of the temporary to
Chris@16 774 // construct.
Chris@16 775 template<typename Functor>
Chris@16 776 #ifndef BOOST_NO_SFINAE
Chris@16 777 typename enable_if_c<
Chris@16 778 (boost::type_traits::ice_not<
Chris@16 779 (is_integral<Functor>::value)>::value),
Chris@16 780 BOOST_FUNCTION_FUNCTION&>::type
Chris@16 781 #else
Chris@16 782 BOOST_FUNCTION_FUNCTION&
Chris@16 783 #endif
Chris@16 784 operator=(Functor BOOST_FUNCTION_TARGET_FIX(const &) f)
Chris@16 785 {
Chris@16 786 this->clear();
Chris@16 787 BOOST_TRY {
Chris@16 788 this->assign_to(f);
Chris@16 789 } BOOST_CATCH (...) {
Chris@16 790 vtable = 0;
Chris@16 791 BOOST_RETHROW;
Chris@16 792 }
Chris@16 793 BOOST_CATCH_END
Chris@16 794 return *this;
Chris@16 795 }
Chris@16 796 template<typename Functor,typename Allocator>
Chris@16 797 void assign(Functor BOOST_FUNCTION_TARGET_FIX(const &) f, Allocator a)
Chris@16 798 {
Chris@16 799 this->clear();
Chris@16 800 BOOST_TRY{
Chris@16 801 this->assign_to_a(f,a);
Chris@16 802 } BOOST_CATCH (...) {
Chris@16 803 vtable = 0;
Chris@16 804 BOOST_RETHROW;
Chris@16 805 }
Chris@16 806 BOOST_CATCH_END
Chris@16 807 }
Chris@16 808
Chris@16 809 #ifndef BOOST_NO_SFINAE
Chris@16 810 BOOST_FUNCTION_FUNCTION& operator=(clear_type*)
Chris@16 811 {
Chris@16 812 this->clear();
Chris@16 813 return *this;
Chris@16 814 }
Chris@16 815 #else
Chris@16 816 BOOST_FUNCTION_FUNCTION& operator=(int zero)
Chris@16 817 {
Chris@16 818 BOOST_ASSERT(zero == 0);
Chris@16 819 this->clear();
Chris@16 820 return *this;
Chris@16 821 }
Chris@16 822 #endif
Chris@16 823
Chris@16 824 // Assignment from another BOOST_FUNCTION_FUNCTION
Chris@16 825 BOOST_FUNCTION_FUNCTION& operator=(const BOOST_FUNCTION_FUNCTION& f)
Chris@16 826 {
Chris@16 827 if (&f == this)
Chris@16 828 return *this;
Chris@16 829
Chris@16 830 this->clear();
Chris@16 831 BOOST_TRY {
Chris@16 832 this->assign_to_own(f);
Chris@16 833 } BOOST_CATCH (...) {
Chris@16 834 vtable = 0;
Chris@16 835 BOOST_RETHROW;
Chris@16 836 }
Chris@16 837 BOOST_CATCH_END
Chris@16 838 return *this;
Chris@16 839 }
Chris@16 840
Chris@16 841 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 842 // Move assignment from another BOOST_FUNCTION_FUNCTION
Chris@16 843 BOOST_FUNCTION_FUNCTION& operator=(BOOST_FUNCTION_FUNCTION&& f)
Chris@16 844 {
Chris@16 845
Chris@16 846 if (&f == this)
Chris@16 847 return *this;
Chris@16 848
Chris@16 849 this->clear();
Chris@16 850 BOOST_TRY {
Chris@16 851 this->move_assign(f);
Chris@16 852 } BOOST_CATCH (...) {
Chris@16 853 vtable = 0;
Chris@16 854 BOOST_RETHROW;
Chris@16 855 }
Chris@16 856 BOOST_CATCH_END
Chris@16 857 return *this;
Chris@16 858 }
Chris@16 859 #endif
Chris@16 860
Chris@16 861 void swap(BOOST_FUNCTION_FUNCTION& other)
Chris@16 862 {
Chris@16 863 if (&other == this)
Chris@16 864 return;
Chris@16 865
Chris@16 866 BOOST_FUNCTION_FUNCTION tmp;
Chris@16 867 tmp.move_assign(*this);
Chris@16 868 this->move_assign(other);
Chris@16 869 other.move_assign(tmp);
Chris@16 870 }
Chris@16 871
Chris@16 872 // Clear out a target, if there is one
Chris@16 873 void clear()
Chris@16 874 {
Chris@16 875 if (vtable) {
Chris@16 876 if (!this->has_trivial_copy_and_destroy())
Chris@16 877 get_vtable()->clear(this->functor);
Chris@16 878 vtable = 0;
Chris@16 879 }
Chris@16 880 }
Chris@16 881
Chris@16 882 #if (defined __SUNPRO_CC) && (__SUNPRO_CC <= 0x530) && !(defined BOOST_NO_COMPILER_CONFIG)
Chris@16 883 // Sun C++ 5.3 can't handle the safe_bool idiom, so don't use it
Chris@16 884 operator bool () const { return !this->empty(); }
Chris@16 885 #else
Chris@16 886 private:
Chris@16 887 struct dummy {
Chris@16 888 void nonnull() {}
Chris@16 889 };
Chris@16 890
Chris@16 891 typedef void (dummy::*safe_bool)();
Chris@16 892
Chris@16 893 public:
Chris@16 894 operator safe_bool () const
Chris@16 895 { return (this->empty())? 0 : &dummy::nonnull; }
Chris@16 896
Chris@16 897 bool operator!() const
Chris@16 898 { return this->empty(); }
Chris@16 899 #endif
Chris@16 900
Chris@16 901 private:
Chris@16 902 void assign_to_own(const BOOST_FUNCTION_FUNCTION& f)
Chris@16 903 {
Chris@16 904 if (!f.empty()) {
Chris@16 905 this->vtable = f.vtable;
Chris@16 906 if (this->has_trivial_copy_and_destroy())
Chris@16 907 this->functor = f.functor;
Chris@16 908 else
Chris@16 909 get_vtable()->base.manager(f.functor, this->functor,
Chris@16 910 boost::detail::function::clone_functor_tag);
Chris@16 911 }
Chris@16 912 }
Chris@16 913
Chris@16 914 template<typename Functor>
Chris@16 915 void assign_to(Functor f)
Chris@16 916 {
Chris@16 917 using detail::function::vtable_base;
Chris@16 918
Chris@16 919 typedef typename detail::function::get_function_tag<Functor>::type tag;
Chris@16 920 typedef detail::function::BOOST_FUNCTION_GET_INVOKER<tag> get_invoker;
Chris@16 921 typedef typename get_invoker::
Chris@16 922 template apply<Functor, R BOOST_FUNCTION_COMMA
Chris@16 923 BOOST_FUNCTION_TEMPLATE_ARGS>
Chris@16 924 handler_type;
Chris@16 925
Chris@16 926 typedef typename handler_type::invoker_type invoker_type;
Chris@16 927 typedef typename handler_type::manager_type manager_type;
Chris@16 928
Chris@16 929 // Note: it is extremely important that this initialization use
Chris@16 930 // static initialization. Otherwise, we will have a race
Chris@16 931 // condition here in multi-threaded code. See
Chris@16 932 // http://thread.gmane.org/gmane.comp.lib.boost.devel/164902/.
Chris@16 933 static const vtable_type stored_vtable =
Chris@16 934 { { &manager_type::manage }, &invoker_type::invoke };
Chris@16 935
Chris@16 936 if (stored_vtable.assign_to(f, functor)) {
Chris@16 937 std::size_t value = reinterpret_cast<std::size_t>(&stored_vtable.base);
Chris@16 938 if (boost::has_trivial_copy_constructor<Functor>::value &&
Chris@16 939 boost::has_trivial_destructor<Functor>::value &&
Chris@16 940 detail::function::function_allows_small_object_optimization<Functor>::value)
Chris@16 941 value |= static_cast<size_t>(0x01);
Chris@16 942 vtable = reinterpret_cast<detail::function::vtable_base *>(value);
Chris@16 943 } else
Chris@16 944 vtable = 0;
Chris@16 945 }
Chris@16 946
Chris@16 947 template<typename Functor,typename Allocator>
Chris@16 948 void assign_to_a(Functor f,Allocator a)
Chris@16 949 {
Chris@16 950 using detail::function::vtable_base;
Chris@16 951
Chris@16 952 typedef typename detail::function::get_function_tag<Functor>::type tag;
Chris@16 953 typedef detail::function::BOOST_FUNCTION_GET_INVOKER<tag> get_invoker;
Chris@16 954 typedef typename get_invoker::
Chris@16 955 template apply_a<Functor, R BOOST_FUNCTION_COMMA
Chris@16 956 BOOST_FUNCTION_TEMPLATE_ARGS,
Chris@16 957 Allocator>
Chris@16 958 handler_type;
Chris@16 959
Chris@16 960 typedef typename handler_type::invoker_type invoker_type;
Chris@16 961 typedef typename handler_type::manager_type manager_type;
Chris@16 962
Chris@16 963 // Note: it is extremely important that this initialization use
Chris@16 964 // static initialization. Otherwise, we will have a race
Chris@16 965 // condition here in multi-threaded code. See
Chris@16 966 // http://thread.gmane.org/gmane.comp.lib.boost.devel/164902/.
Chris@16 967 static const vtable_type stored_vtable =
Chris@16 968 { { &manager_type::manage }, &invoker_type::invoke };
Chris@16 969
Chris@16 970 if (stored_vtable.assign_to_a(f, functor, a)) {
Chris@16 971 std::size_t value = reinterpret_cast<std::size_t>(&stored_vtable.base);
Chris@16 972 if (boost::has_trivial_copy_constructor<Functor>::value &&
Chris@16 973 boost::has_trivial_destructor<Functor>::value &&
Chris@16 974 detail::function::function_allows_small_object_optimization<Functor>::value)
Chris@16 975 value |= static_cast<std::size_t>(0x01);
Chris@16 976 vtable = reinterpret_cast<detail::function::vtable_base *>(value);
Chris@16 977 } else
Chris@16 978 vtable = 0;
Chris@16 979 }
Chris@16 980
Chris@16 981 // Moves the value from the specified argument to *this. If the argument
Chris@16 982 // has its function object allocated on the heap, move_assign will pass
Chris@16 983 // its buffer to *this, and set the argument's buffer pointer to NULL.
Chris@16 984 void move_assign(BOOST_FUNCTION_FUNCTION& f)
Chris@16 985 {
Chris@16 986 if (&f == this)
Chris@16 987 return;
Chris@16 988
Chris@16 989 BOOST_TRY {
Chris@16 990 if (!f.empty()) {
Chris@16 991 this->vtable = f.vtable;
Chris@16 992 if (this->has_trivial_copy_and_destroy())
Chris@16 993 this->functor = f.functor;
Chris@16 994 else
Chris@16 995 get_vtable()->base.manager(f.functor, this->functor,
Chris@16 996 boost::detail::function::move_functor_tag);
Chris@16 997 f.vtable = 0;
Chris@16 998 } else {
Chris@16 999 clear();
Chris@16 1000 }
Chris@16 1001 } BOOST_CATCH (...) {
Chris@16 1002 vtable = 0;
Chris@16 1003 BOOST_RETHROW;
Chris@16 1004 }
Chris@16 1005 BOOST_CATCH_END
Chris@16 1006 }
Chris@16 1007 };
Chris@16 1008
Chris@16 1009 template<typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS>
Chris@16 1010 inline void swap(BOOST_FUNCTION_FUNCTION<
Chris@16 1011 R BOOST_FUNCTION_COMMA
Chris@16 1012 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 1013 >& f1,
Chris@16 1014 BOOST_FUNCTION_FUNCTION<
Chris@16 1015 R BOOST_FUNCTION_COMMA
Chris@16 1016 BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 1017 >& f2)
Chris@16 1018 {
Chris@16 1019 f1.swap(f2);
Chris@16 1020 }
Chris@16 1021
Chris@16 1022 // Poison comparisons between boost::function objects of the same type.
Chris@16 1023 template<typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS>
Chris@16 1024 void operator==(const BOOST_FUNCTION_FUNCTION<
Chris@16 1025 R BOOST_FUNCTION_COMMA
Chris@16 1026 BOOST_FUNCTION_TEMPLATE_ARGS>&,
Chris@16 1027 const BOOST_FUNCTION_FUNCTION<
Chris@16 1028 R BOOST_FUNCTION_COMMA
Chris@16 1029 BOOST_FUNCTION_TEMPLATE_ARGS>&);
Chris@16 1030 template<typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS>
Chris@16 1031 void operator!=(const BOOST_FUNCTION_FUNCTION<
Chris@16 1032 R BOOST_FUNCTION_COMMA
Chris@16 1033 BOOST_FUNCTION_TEMPLATE_ARGS>&,
Chris@16 1034 const BOOST_FUNCTION_FUNCTION<
Chris@16 1035 R BOOST_FUNCTION_COMMA
Chris@16 1036 BOOST_FUNCTION_TEMPLATE_ARGS>& );
Chris@16 1037
Chris@16 1038 #if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX)
Chris@16 1039
Chris@16 1040 #if BOOST_FUNCTION_NUM_ARGS == 0
Chris@16 1041 #define BOOST_FUNCTION_PARTIAL_SPEC R (void)
Chris@16 1042 #else
Chris@16 1043 #define BOOST_FUNCTION_PARTIAL_SPEC R (BOOST_PP_ENUM_PARAMS(BOOST_FUNCTION_NUM_ARGS,T))
Chris@16 1044 #endif
Chris@16 1045
Chris@16 1046 template<typename R BOOST_FUNCTION_COMMA
Chris@16 1047 BOOST_FUNCTION_TEMPLATE_PARMS>
Chris@16 1048 class function<BOOST_FUNCTION_PARTIAL_SPEC>
Chris@16 1049 : public BOOST_FUNCTION_FUNCTION<R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_ARGS>
Chris@16 1050 {
Chris@16 1051 typedef BOOST_FUNCTION_FUNCTION<R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_ARGS> base_type;
Chris@16 1052 typedef function self_type;
Chris@16 1053
Chris@16 1054 struct clear_type {};
Chris@16 1055
Chris@16 1056 public:
Chris@16 1057
Chris@16 1058 function() : base_type() {}
Chris@16 1059
Chris@16 1060 template<typename Functor>
Chris@16 1061 function(Functor f
Chris@16 1062 #ifndef BOOST_NO_SFINAE
Chris@16 1063 ,typename enable_if_c<
Chris@16 1064 (boost::type_traits::ice_not<
Chris@16 1065 (is_integral<Functor>::value)>::value),
Chris@16 1066 int>::type = 0
Chris@16 1067 #endif
Chris@16 1068 ) :
Chris@16 1069 base_type(f)
Chris@16 1070 {
Chris@16 1071 }
Chris@16 1072 template<typename Functor,typename Allocator>
Chris@16 1073 function(Functor f, Allocator a
Chris@16 1074 #ifndef BOOST_NO_SFINAE
Chris@16 1075 ,typename enable_if_c<
Chris@16 1076 (boost::type_traits::ice_not<
Chris@16 1077 (is_integral<Functor>::value)>::value),
Chris@16 1078 int>::type = 0
Chris@16 1079 #endif
Chris@16 1080 ) :
Chris@16 1081 base_type(f,a)
Chris@16 1082 {
Chris@16 1083 }
Chris@16 1084
Chris@16 1085 #ifndef BOOST_NO_SFINAE
Chris@16 1086 function(clear_type*) : base_type() {}
Chris@16 1087 #endif
Chris@16 1088
Chris@16 1089 function(const self_type& f) : base_type(static_cast<const base_type&>(f)){}
Chris@16 1090
Chris@16 1091 function(const base_type& f) : base_type(static_cast<const base_type&>(f)){}
Chris@16 1092
Chris@16 1093 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 1094 // Move constructors
Chris@16 1095 function(self_type&& f): base_type(static_cast<base_type&&>(f)){}
Chris@16 1096 function(base_type&& f): base_type(static_cast<base_type&&>(f)){}
Chris@16 1097 #endif
Chris@16 1098
Chris@16 1099 self_type& operator=(const self_type& f)
Chris@16 1100 {
Chris@16 1101 self_type(f).swap(*this);
Chris@16 1102 return *this;
Chris@16 1103 }
Chris@16 1104
Chris@16 1105 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 1106 self_type& operator=(self_type&& f)
Chris@16 1107 {
Chris@16 1108 self_type(static_cast<self_type&&>(f)).swap(*this);
Chris@16 1109 return *this;
Chris@16 1110 }
Chris@16 1111 #endif
Chris@16 1112
Chris@16 1113 template<typename Functor>
Chris@16 1114 #ifndef BOOST_NO_SFINAE
Chris@16 1115 typename enable_if_c<
Chris@16 1116 (boost::type_traits::ice_not<
Chris@16 1117 (is_integral<Functor>::value)>::value),
Chris@16 1118 self_type&>::type
Chris@16 1119 #else
Chris@16 1120 self_type&
Chris@16 1121 #endif
Chris@16 1122 operator=(Functor f)
Chris@16 1123 {
Chris@16 1124 self_type(f).swap(*this);
Chris@16 1125 return *this;
Chris@16 1126 }
Chris@16 1127
Chris@16 1128 #ifndef BOOST_NO_SFINAE
Chris@16 1129 self_type& operator=(clear_type*)
Chris@16 1130 {
Chris@16 1131 this->clear();
Chris@16 1132 return *this;
Chris@16 1133 }
Chris@16 1134 #endif
Chris@16 1135
Chris@16 1136 self_type& operator=(const base_type& f)
Chris@16 1137 {
Chris@16 1138 self_type(f).swap(*this);
Chris@16 1139 return *this;
Chris@16 1140 }
Chris@16 1141
Chris@16 1142 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 1143 self_type& operator=(base_type&& f)
Chris@16 1144 {
Chris@16 1145 self_type(static_cast<base_type&&>(f)).swap(*this);
Chris@16 1146 return *this;
Chris@16 1147 }
Chris@16 1148 #endif
Chris@16 1149 };
Chris@16 1150
Chris@16 1151 #undef BOOST_FUNCTION_PARTIAL_SPEC
Chris@16 1152 #endif // have partial specialization
Chris@16 1153
Chris@16 1154 } // end namespace boost
Chris@16 1155
Chris@16 1156 // Cleanup after ourselves...
Chris@16 1157 #undef BOOST_FUNCTION_VTABLE
Chris@16 1158 #undef BOOST_FUNCTION_COMMA
Chris@16 1159 #undef BOOST_FUNCTION_FUNCTION
Chris@16 1160 #undef BOOST_FUNCTION_FUNCTION_INVOKER
Chris@16 1161 #undef BOOST_FUNCTION_VOID_FUNCTION_INVOKER
Chris@16 1162 #undef BOOST_FUNCTION_FUNCTION_OBJ_INVOKER
Chris@16 1163 #undef BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER
Chris@16 1164 #undef BOOST_FUNCTION_FUNCTION_REF_INVOKER
Chris@16 1165 #undef BOOST_FUNCTION_VOID_FUNCTION_REF_INVOKER
Chris@16 1166 #undef BOOST_FUNCTION_MEMBER_INVOKER
Chris@16 1167 #undef BOOST_FUNCTION_VOID_MEMBER_INVOKER
Chris@16 1168 #undef BOOST_FUNCTION_GET_FUNCTION_INVOKER
Chris@16 1169 #undef BOOST_FUNCTION_GET_FUNCTION_OBJ_INVOKER
Chris@16 1170 #undef BOOST_FUNCTION_GET_FUNCTION_REF_INVOKER
Chris@16 1171 #undef BOOST_FUNCTION_GET_MEM_FUNCTION_INVOKER
Chris@16 1172 #undef BOOST_FUNCTION_GET_INVOKER
Chris@16 1173 #undef BOOST_FUNCTION_TEMPLATE_PARMS
Chris@16 1174 #undef BOOST_FUNCTION_TEMPLATE_ARGS
Chris@16 1175 #undef BOOST_FUNCTION_PARMS
Chris@16 1176 #undef BOOST_FUNCTION_PARM
Chris@16 1177 #undef BOOST_FUNCTION_ARGS
Chris@16 1178 #undef BOOST_FUNCTION_ARG_TYPE
Chris@16 1179 #undef BOOST_FUNCTION_ARG_TYPES
Chris@16 1180 #undef BOOST_FUNCTION_VOID_RETURN_TYPE
Chris@16 1181 #undef BOOST_FUNCTION_RETURN
Chris@16 1182
Chris@16 1183 #if defined(BOOST_MSVC)
Chris@16 1184 # pragma warning( pop )
Chris@16 1185 #endif