1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H 11 #define _LIBCPP___FUNCTIONAL_FUNCTION_H 12 13 #include <__assert> 14 #include <__config> 15 #include <__functional/binary_function.h> 16 #include <__functional/invoke.h> 17 #include <__functional/unary_function.h> 18 #include <__iterator/iterator_traits.h> 19 #include <__memory/addressof.h> 20 #include <__memory/allocator_traits.h> 21 #include <__memory/compressed_pair.h> 22 #include <__memory/shared_ptr.h> 23 #include <__utility/forward.h> 24 #include <__utility/move.h> 25 #include <__utility/swap.h> 26 #include <exception> 27 #include <memory> // TODO: replace with <__memory/__builtin_new_allocator.h> 28 #include <type_traits> 29 30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 31 # pragma GCC system_header 32 #endif 33 34 _LIBCPP_BEGIN_NAMESPACE_STD 35 36 // bad_function_call 37 38 class _LIBCPP_EXCEPTION_ABI bad_function_call 39 : public exception 40 { 41 public: 42 // Note that when a key function is not used, every translation unit that uses 43 // bad_function_call will end up containing a weak definition of the vtable and 44 // typeinfo. 45 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION 46 virtual ~bad_function_call() _NOEXCEPT; 47 #else 48 virtual ~bad_function_call() _NOEXCEPT {} 49 #endif 50 51 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE 52 virtual const char* what() const _NOEXCEPT; 53 #endif 54 }; 55 56 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY 57 void __throw_bad_function_call() 58 { 59 #ifndef _LIBCPP_NO_EXCEPTIONS 60 throw bad_function_call(); 61 #else 62 _VSTD::abort(); 63 #endif 64 } 65 66 #if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated) 67 # define _LIBCPP_DEPRECATED_CXX03_FUNCTION \ 68 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type"))) 69 #else 70 # define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */ 71 #endif 72 73 template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined 74 75 namespace __function 76 { 77 78 template<class _Rp> 79 struct __maybe_derive_from_unary_function 80 { 81 }; 82 83 template<class _Rp, class _A1> 84 struct __maybe_derive_from_unary_function<_Rp(_A1)> 85 : public unary_function<_A1, _Rp> 86 { 87 }; 88 89 template<class _Rp> 90 struct __maybe_derive_from_binary_function 91 { 92 }; 93 94 template<class _Rp, class _A1, class _A2> 95 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)> 96 : public binary_function<_A1, _A2, _Rp> 97 { 98 }; 99 100 template <class _Fp> 101 _LIBCPP_INLINE_VISIBILITY 102 bool __not_null(_Fp const&) { return true; } 103 104 template <class _Fp> 105 _LIBCPP_INLINE_VISIBILITY 106 bool __not_null(_Fp* __ptr) { return __ptr; } 107 108 template <class _Ret, class _Class> 109 _LIBCPP_INLINE_VISIBILITY 110 bool __not_null(_Ret _Class::*__ptr) { return __ptr; } 111 112 template <class _Fp> 113 _LIBCPP_INLINE_VISIBILITY 114 bool __not_null(function<_Fp> const& __f) { return !!__f; } 115 116 #ifdef _LIBCPP_HAS_EXTENSION_BLOCKS 117 template <class _Rp, class ..._Args> 118 _LIBCPP_INLINE_VISIBILITY 119 bool __not_null(_Rp (^__p)(_Args...)) { return __p; } 120 #endif 121 122 } // namespace __function 123 124 #ifndef _LIBCPP_CXX03_LANG 125 126 namespace __function { 127 128 // __alloc_func holds a functor and an allocator. 129 130 template <class _Fp, class _Ap, class _FB> class __alloc_func; 131 template <class _Fp, class _FB> 132 class __default_alloc_func; 133 134 template <class _Fp, class _Ap, class _Rp, class... _ArgTypes> 135 class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)> 136 { 137 __compressed_pair<_Fp, _Ap> __f_; 138 139 public: 140 typedef _LIBCPP_NODEBUG _Fp _Target; 141 typedef _LIBCPP_NODEBUG _Ap _Alloc; 142 143 _LIBCPP_INLINE_VISIBILITY 144 const _Target& __target() const { return __f_.first(); } 145 146 // WIN32 APIs may define __allocator, so use __get_allocator instead. 147 _LIBCPP_INLINE_VISIBILITY 148 const _Alloc& __get_allocator() const { return __f_.second(); } 149 150 _LIBCPP_INLINE_VISIBILITY 151 explicit __alloc_func(_Target&& __f) 152 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)), 153 _VSTD::forward_as_tuple()) 154 { 155 } 156 157 _LIBCPP_INLINE_VISIBILITY 158 explicit __alloc_func(const _Target& __f, const _Alloc& __a) 159 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f), 160 _VSTD::forward_as_tuple(__a)) 161 { 162 } 163 164 _LIBCPP_INLINE_VISIBILITY 165 explicit __alloc_func(const _Target& __f, _Alloc&& __a) 166 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f), 167 _VSTD::forward_as_tuple(_VSTD::move(__a))) 168 { 169 } 170 171 _LIBCPP_INLINE_VISIBILITY 172 explicit __alloc_func(_Target&& __f, _Alloc&& __a) 173 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)), 174 _VSTD::forward_as_tuple(_VSTD::move(__a))) 175 { 176 } 177 178 _LIBCPP_INLINE_VISIBILITY 179 _Rp operator()(_ArgTypes&&... __arg) 180 { 181 typedef __invoke_void_return_wrapper<_Rp> _Invoker; 182 return _Invoker::__call(__f_.first(), 183 _VSTD::forward<_ArgTypes>(__arg)...); 184 } 185 186 _LIBCPP_INLINE_VISIBILITY 187 __alloc_func* __clone() const 188 { 189 typedef allocator_traits<_Alloc> __alloc_traits; 190 typedef 191 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type 192 _AA; 193 _AA __a(__f_.second()); 194 typedef __allocator_destructor<_AA> _Dp; 195 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 196 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a)); 197 return __hold.release(); 198 } 199 200 _LIBCPP_INLINE_VISIBILITY 201 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); } 202 203 static void __destroy_and_delete(__alloc_func* __f) { 204 typedef allocator_traits<_Alloc> __alloc_traits; 205 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type 206 _FunAlloc; 207 _FunAlloc __a(__f->__get_allocator()); 208 __f->destroy(); 209 __a.deallocate(__f, 1); 210 } 211 }; 212 213 template <class _Fp, class _Rp, class... _ArgTypes> 214 class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> { 215 _Fp __f_; 216 217 public: 218 typedef _LIBCPP_NODEBUG _Fp _Target; 219 220 _LIBCPP_INLINE_VISIBILITY 221 const _Target& __target() const { return __f_; } 222 223 _LIBCPP_INLINE_VISIBILITY 224 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {} 225 226 _LIBCPP_INLINE_VISIBILITY 227 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {} 228 229 _LIBCPP_INLINE_VISIBILITY 230 _Rp operator()(_ArgTypes&&... __arg) { 231 typedef __invoke_void_return_wrapper<_Rp> _Invoker; 232 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...); 233 } 234 235 _LIBCPP_INLINE_VISIBILITY 236 __default_alloc_func* __clone() const { 237 __builtin_new_allocator::__holder_t __hold = 238 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1); 239 __default_alloc_func* __res = 240 ::new ((void*)__hold.get()) __default_alloc_func(__f_); 241 (void)__hold.release(); 242 return __res; 243 } 244 245 _LIBCPP_INLINE_VISIBILITY 246 void destroy() _NOEXCEPT { __f_.~_Target(); } 247 248 static void __destroy_and_delete(__default_alloc_func* __f) { 249 __f->destroy(); 250 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1); 251 } 252 }; 253 254 // __base provides an abstract interface for copyable functors. 255 256 template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base; 257 258 template<class _Rp, class ..._ArgTypes> 259 class __base<_Rp(_ArgTypes...)> 260 { 261 __base(const __base&); 262 __base& operator=(const __base&); 263 public: 264 _LIBCPP_INLINE_VISIBILITY __base() {} 265 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {} 266 virtual __base* __clone() const = 0; 267 virtual void __clone(__base*) const = 0; 268 virtual void destroy() _NOEXCEPT = 0; 269 virtual void destroy_deallocate() _NOEXCEPT = 0; 270 virtual _Rp operator()(_ArgTypes&& ...) = 0; 271 #ifndef _LIBCPP_NO_RTTI 272 virtual const void* target(const type_info&) const _NOEXCEPT = 0; 273 virtual const std::type_info& target_type() const _NOEXCEPT = 0; 274 #endif // _LIBCPP_NO_RTTI 275 }; 276 277 // __func implements __base for a given functor type. 278 279 template<class _FD, class _Alloc, class _FB> class __func; 280 281 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 282 class __func<_Fp, _Alloc, _Rp(_ArgTypes...)> 283 : public __base<_Rp(_ArgTypes...)> 284 { 285 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_; 286 public: 287 _LIBCPP_INLINE_VISIBILITY 288 explicit __func(_Fp&& __f) 289 : __f_(_VSTD::move(__f)) {} 290 291 _LIBCPP_INLINE_VISIBILITY 292 explicit __func(const _Fp& __f, const _Alloc& __a) 293 : __f_(__f, __a) {} 294 295 _LIBCPP_INLINE_VISIBILITY 296 explicit __func(const _Fp& __f, _Alloc&& __a) 297 : __f_(__f, _VSTD::move(__a)) {} 298 299 _LIBCPP_INLINE_VISIBILITY 300 explicit __func(_Fp&& __f, _Alloc&& __a) 301 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {} 302 303 virtual __base<_Rp(_ArgTypes...)>* __clone() const; 304 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const; 305 virtual void destroy() _NOEXCEPT; 306 virtual void destroy_deallocate() _NOEXCEPT; 307 virtual _Rp operator()(_ArgTypes&&... __arg); 308 #ifndef _LIBCPP_NO_RTTI 309 virtual const void* target(const type_info&) const _NOEXCEPT; 310 virtual const std::type_info& target_type() const _NOEXCEPT; 311 #endif // _LIBCPP_NO_RTTI 312 }; 313 314 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 315 __base<_Rp(_ArgTypes...)>* 316 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const 317 { 318 typedef allocator_traits<_Alloc> __alloc_traits; 319 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap; 320 _Ap __a(__f_.__get_allocator()); 321 typedef __allocator_destructor<_Ap> _Dp; 322 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 323 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a)); 324 return __hold.release(); 325 } 326 327 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 328 void 329 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const 330 { 331 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator()); 332 } 333 334 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 335 void 336 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT 337 { 338 __f_.destroy(); 339 } 340 341 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 342 void 343 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT 344 { 345 typedef allocator_traits<_Alloc> __alloc_traits; 346 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap; 347 _Ap __a(__f_.__get_allocator()); 348 __f_.destroy(); 349 __a.deallocate(this, 1); 350 } 351 352 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 353 _Rp 354 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg) 355 { 356 return __f_(_VSTD::forward<_ArgTypes>(__arg)...); 357 } 358 359 #ifndef _LIBCPP_NO_RTTI 360 361 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 362 const void* 363 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT 364 { 365 if (__ti == typeid(_Fp)) 366 return _VSTD::addressof(__f_.__target()); 367 return nullptr; 368 } 369 370 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 371 const std::type_info& 372 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT 373 { 374 return typeid(_Fp); 375 } 376 377 #endif // _LIBCPP_NO_RTTI 378 379 // __value_func creates a value-type from a __func. 380 381 template <class _Fp> class __value_func; 382 383 template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)> 384 { 385 typename aligned_storage<3 * sizeof(void*)>::type __buf_; 386 387 typedef __base<_Rp(_ArgTypes...)> __func; 388 __func* __f_; 389 390 _LIBCPP_NO_CFI static __func* __as_base(void* p) 391 { 392 return reinterpret_cast<__func*>(p); 393 } 394 395 public: 396 _LIBCPP_INLINE_VISIBILITY 397 __value_func() _NOEXCEPT : __f_(nullptr) {} 398 399 template <class _Fp, class _Alloc> 400 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a) 401 : __f_(nullptr) 402 { 403 typedef allocator_traits<_Alloc> __alloc_traits; 404 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun; 405 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type 406 _FunAlloc; 407 408 if (__function::__not_null(__f)) 409 { 410 _FunAlloc __af(__a); 411 if (sizeof(_Fun) <= sizeof(__buf_) && 412 is_nothrow_copy_constructible<_Fp>::value && 413 is_nothrow_copy_constructible<_FunAlloc>::value) 414 { 415 __f_ = 416 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af)); 417 } 418 else 419 { 420 typedef __allocator_destructor<_FunAlloc> _Dp; 421 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1)); 422 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a)); 423 __f_ = __hold.release(); 424 } 425 } 426 } 427 428 template <class _Fp, 429 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type> 430 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f) 431 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {} 432 433 _LIBCPP_INLINE_VISIBILITY 434 __value_func(const __value_func& __f) 435 { 436 if (__f.__f_ == nullptr) 437 __f_ = nullptr; 438 else if ((void*)__f.__f_ == &__f.__buf_) 439 { 440 __f_ = __as_base(&__buf_); 441 __f.__f_->__clone(__f_); 442 } 443 else 444 __f_ = __f.__f_->__clone(); 445 } 446 447 _LIBCPP_INLINE_VISIBILITY 448 __value_func(__value_func&& __f) _NOEXCEPT 449 { 450 if (__f.__f_ == nullptr) 451 __f_ = nullptr; 452 else if ((void*)__f.__f_ == &__f.__buf_) 453 { 454 __f_ = __as_base(&__buf_); 455 __f.__f_->__clone(__f_); 456 } 457 else 458 { 459 __f_ = __f.__f_; 460 __f.__f_ = nullptr; 461 } 462 } 463 464 _LIBCPP_INLINE_VISIBILITY 465 ~__value_func() 466 { 467 if ((void*)__f_ == &__buf_) 468 __f_->destroy(); 469 else if (__f_) 470 __f_->destroy_deallocate(); 471 } 472 473 _LIBCPP_INLINE_VISIBILITY 474 __value_func& operator=(__value_func&& __f) 475 { 476 *this = nullptr; 477 if (__f.__f_ == nullptr) 478 __f_ = nullptr; 479 else if ((void*)__f.__f_ == &__f.__buf_) 480 { 481 __f_ = __as_base(&__buf_); 482 __f.__f_->__clone(__f_); 483 } 484 else 485 { 486 __f_ = __f.__f_; 487 __f.__f_ = nullptr; 488 } 489 return *this; 490 } 491 492 _LIBCPP_INLINE_VISIBILITY 493 __value_func& operator=(nullptr_t) 494 { 495 __func* __f = __f_; 496 __f_ = nullptr; 497 if ((void*)__f == &__buf_) 498 __f->destroy(); 499 else if (__f) 500 __f->destroy_deallocate(); 501 return *this; 502 } 503 504 _LIBCPP_INLINE_VISIBILITY 505 _Rp operator()(_ArgTypes&&... __args) const 506 { 507 if (__f_ == nullptr) 508 __throw_bad_function_call(); 509 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...); 510 } 511 512 _LIBCPP_INLINE_VISIBILITY 513 void swap(__value_func& __f) _NOEXCEPT 514 { 515 if (&__f == this) 516 return; 517 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_) 518 { 519 typename aligned_storage<sizeof(__buf_)>::type __tempbuf; 520 __func* __t = __as_base(&__tempbuf); 521 __f_->__clone(__t); 522 __f_->destroy(); 523 __f_ = nullptr; 524 __f.__f_->__clone(__as_base(&__buf_)); 525 __f.__f_->destroy(); 526 __f.__f_ = nullptr; 527 __f_ = __as_base(&__buf_); 528 __t->__clone(__as_base(&__f.__buf_)); 529 __t->destroy(); 530 __f.__f_ = __as_base(&__f.__buf_); 531 } 532 else if ((void*)__f_ == &__buf_) 533 { 534 __f_->__clone(__as_base(&__f.__buf_)); 535 __f_->destroy(); 536 __f_ = __f.__f_; 537 __f.__f_ = __as_base(&__f.__buf_); 538 } 539 else if ((void*)__f.__f_ == &__f.__buf_) 540 { 541 __f.__f_->__clone(__as_base(&__buf_)); 542 __f.__f_->destroy(); 543 __f.__f_ = __f_; 544 __f_ = __as_base(&__buf_); 545 } 546 else 547 _VSTD::swap(__f_, __f.__f_); 548 } 549 550 _LIBCPP_INLINE_VISIBILITY 551 explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; } 552 553 #ifndef _LIBCPP_NO_RTTI 554 _LIBCPP_INLINE_VISIBILITY 555 const std::type_info& target_type() const _NOEXCEPT 556 { 557 if (__f_ == nullptr) 558 return typeid(void); 559 return __f_->target_type(); 560 } 561 562 template <typename _Tp> 563 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT 564 { 565 if (__f_ == nullptr) 566 return nullptr; 567 return (const _Tp*)__f_->target(typeid(_Tp)); 568 } 569 #endif // _LIBCPP_NO_RTTI 570 }; 571 572 // Storage for a functor object, to be used with __policy to manage copy and 573 // destruction. 574 union __policy_storage 575 { 576 mutable char __small[sizeof(void*) * 2]; 577 void* __large; 578 }; 579 580 // True if _Fun can safely be held in __policy_storage.__small. 581 template <typename _Fun> 582 struct __use_small_storage 583 : public integral_constant< 584 bool, sizeof(_Fun) <= sizeof(__policy_storage) && 585 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) && 586 is_trivially_copy_constructible<_Fun>::value && 587 is_trivially_destructible<_Fun>::value> {}; 588 589 // Policy contains information about how to copy, destroy, and move the 590 // underlying functor. You can think of it as a vtable of sorts. 591 struct __policy 592 { 593 // Used to copy or destroy __large values. null for trivial objects. 594 void* (*const __clone)(const void*); 595 void (*const __destroy)(void*); 596 597 // True if this is the null policy (no value). 598 const bool __is_null; 599 600 // The target type. May be null if RTTI is disabled. 601 const std::type_info* const __type_info; 602 603 // Returns a pointer to a static policy object suitable for the functor 604 // type. 605 template <typename _Fun> 606 _LIBCPP_INLINE_VISIBILITY static const __policy* __create() 607 { 608 return __choose_policy<_Fun>(__use_small_storage<_Fun>()); 609 } 610 611 _LIBCPP_INLINE_VISIBILITY 612 static const __policy* __create_empty() 613 { 614 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr, 615 true, 616 #ifndef _LIBCPP_NO_RTTI 617 &typeid(void) 618 #else 619 nullptr 620 #endif 621 }; 622 return &__policy_; 623 } 624 625 private: 626 template <typename _Fun> static void* __large_clone(const void* __s) 627 { 628 const _Fun* __f = static_cast<const _Fun*>(__s); 629 return __f->__clone(); 630 } 631 632 template <typename _Fun> 633 static void __large_destroy(void* __s) { 634 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s)); 635 } 636 637 template <typename _Fun> 638 _LIBCPP_INLINE_VISIBILITY static const __policy* 639 __choose_policy(/* is_small = */ false_type) { 640 static const _LIBCPP_CONSTEXPR __policy __policy_ = { 641 &__large_clone<_Fun>, &__large_destroy<_Fun>, false, 642 #ifndef _LIBCPP_NO_RTTI 643 &typeid(typename _Fun::_Target) 644 #else 645 nullptr 646 #endif 647 }; 648 return &__policy_; 649 } 650 651 template <typename _Fun> 652 _LIBCPP_INLINE_VISIBILITY static const __policy* 653 __choose_policy(/* is_small = */ true_type) 654 { 655 static const _LIBCPP_CONSTEXPR __policy __policy_ = { 656 nullptr, nullptr, false, 657 #ifndef _LIBCPP_NO_RTTI 658 &typeid(typename _Fun::_Target) 659 #else 660 nullptr 661 #endif 662 }; 663 return &__policy_; 664 } 665 }; 666 667 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is 668 // faster for types that can be passed in registers. 669 template <typename _Tp> 670 using __fast_forward = 671 typename conditional<is_scalar<_Tp>::value, _Tp, _Tp&&>::type; 672 673 // __policy_invoker calls an instance of __alloc_func held in __policy_storage. 674 675 template <class _Fp> struct __policy_invoker; 676 677 template <class _Rp, class... _ArgTypes> 678 struct __policy_invoker<_Rp(_ArgTypes...)> 679 { 680 typedef _Rp (*__Call)(const __policy_storage*, 681 __fast_forward<_ArgTypes>...); 682 683 __Call __call_; 684 685 // Creates an invoker that throws bad_function_call. 686 _LIBCPP_INLINE_VISIBILITY 687 __policy_invoker() : __call_(&__call_empty) {} 688 689 // Creates an invoker that calls the given instance of __func. 690 template <typename _Fun> 691 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create() 692 { 693 return __policy_invoker(&__call_impl<_Fun>); 694 } 695 696 private: 697 _LIBCPP_INLINE_VISIBILITY 698 explicit __policy_invoker(__Call __c) : __call_(__c) {} 699 700 static _Rp __call_empty(const __policy_storage*, 701 __fast_forward<_ArgTypes>...) 702 { 703 __throw_bad_function_call(); 704 } 705 706 template <typename _Fun> 707 static _Rp __call_impl(const __policy_storage* __buf, 708 __fast_forward<_ArgTypes>... __args) 709 { 710 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value 711 ? &__buf->__small 712 : __buf->__large); 713 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...); 714 } 715 }; 716 717 // __policy_func uses a __policy and __policy_invoker to create a type-erased, 718 // copyable functor. 719 720 template <class _Fp> class __policy_func; 721 722 template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)> 723 { 724 // Inline storage for small objects. 725 __policy_storage __buf_; 726 727 // Calls the value stored in __buf_. This could technically be part of 728 // policy, but storing it here eliminates a level of indirection inside 729 // operator(). 730 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker; 731 __invoker __invoker_; 732 733 // The policy that describes how to move / copy / destroy __buf_. Never 734 // null, even if the function is empty. 735 const __policy* __policy_; 736 737 public: 738 _LIBCPP_INLINE_VISIBILITY 739 __policy_func() : __policy_(__policy::__create_empty()) {} 740 741 template <class _Fp, class _Alloc> 742 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a) 743 : __policy_(__policy::__create_empty()) 744 { 745 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun; 746 typedef allocator_traits<_Alloc> __alloc_traits; 747 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type 748 _FunAlloc; 749 750 if (__function::__not_null(__f)) 751 { 752 __invoker_ = __invoker::template __create<_Fun>(); 753 __policy_ = __policy::__create<_Fun>(); 754 755 _FunAlloc __af(__a); 756 if (__use_small_storage<_Fun>()) 757 { 758 ::new ((void*)&__buf_.__small) 759 _Fun(_VSTD::move(__f), _Alloc(__af)); 760 } 761 else 762 { 763 typedef __allocator_destructor<_FunAlloc> _Dp; 764 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1)); 765 ::new ((void*)__hold.get()) 766 _Fun(_VSTD::move(__f), _Alloc(__af)); 767 __buf_.__large = __hold.release(); 768 } 769 } 770 } 771 772 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type> 773 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f) 774 : __policy_(__policy::__create_empty()) { 775 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun; 776 777 if (__function::__not_null(__f)) { 778 __invoker_ = __invoker::template __create<_Fun>(); 779 __policy_ = __policy::__create<_Fun>(); 780 if (__use_small_storage<_Fun>()) { 781 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f)); 782 } else { 783 __builtin_new_allocator::__holder_t __hold = 784 __builtin_new_allocator::__allocate_type<_Fun>(1); 785 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f)); 786 (void)__hold.release(); 787 } 788 } 789 } 790 791 _LIBCPP_INLINE_VISIBILITY 792 __policy_func(const __policy_func& __f) 793 : __buf_(__f.__buf_), __invoker_(__f.__invoker_), 794 __policy_(__f.__policy_) 795 { 796 if (__policy_->__clone) 797 __buf_.__large = __policy_->__clone(__f.__buf_.__large); 798 } 799 800 _LIBCPP_INLINE_VISIBILITY 801 __policy_func(__policy_func&& __f) 802 : __buf_(__f.__buf_), __invoker_(__f.__invoker_), 803 __policy_(__f.__policy_) 804 { 805 if (__policy_->__destroy) 806 { 807 __f.__policy_ = __policy::__create_empty(); 808 __f.__invoker_ = __invoker(); 809 } 810 } 811 812 _LIBCPP_INLINE_VISIBILITY 813 ~__policy_func() 814 { 815 if (__policy_->__destroy) 816 __policy_->__destroy(__buf_.__large); 817 } 818 819 _LIBCPP_INLINE_VISIBILITY 820 __policy_func& operator=(__policy_func&& __f) 821 { 822 *this = nullptr; 823 __buf_ = __f.__buf_; 824 __invoker_ = __f.__invoker_; 825 __policy_ = __f.__policy_; 826 __f.__policy_ = __policy::__create_empty(); 827 __f.__invoker_ = __invoker(); 828 return *this; 829 } 830 831 _LIBCPP_INLINE_VISIBILITY 832 __policy_func& operator=(nullptr_t) 833 { 834 const __policy* __p = __policy_; 835 __policy_ = __policy::__create_empty(); 836 __invoker_ = __invoker(); 837 if (__p->__destroy) 838 __p->__destroy(__buf_.__large); 839 return *this; 840 } 841 842 _LIBCPP_INLINE_VISIBILITY 843 _Rp operator()(_ArgTypes&&... __args) const 844 { 845 return __invoker_.__call_(_VSTD::addressof(__buf_), 846 _VSTD::forward<_ArgTypes>(__args)...); 847 } 848 849 _LIBCPP_INLINE_VISIBILITY 850 void swap(__policy_func& __f) 851 { 852 _VSTD::swap(__invoker_, __f.__invoker_); 853 _VSTD::swap(__policy_, __f.__policy_); 854 _VSTD::swap(__buf_, __f.__buf_); 855 } 856 857 _LIBCPP_INLINE_VISIBILITY 858 explicit operator bool() const _NOEXCEPT 859 { 860 return !__policy_->__is_null; 861 } 862 863 #ifndef _LIBCPP_NO_RTTI 864 _LIBCPP_INLINE_VISIBILITY 865 const std::type_info& target_type() const _NOEXCEPT 866 { 867 return *__policy_->__type_info; 868 } 869 870 template <typename _Tp> 871 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT 872 { 873 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info) 874 return nullptr; 875 if (__policy_->__clone) // Out of line storage. 876 return reinterpret_cast<const _Tp*>(__buf_.__large); 877 else 878 return reinterpret_cast<const _Tp*>(&__buf_.__small); 879 } 880 #endif // _LIBCPP_NO_RTTI 881 }; 882 883 #if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC) 884 885 extern "C" void *_Block_copy(const void *); 886 extern "C" void _Block_release(const void *); 887 888 template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes> 889 class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> 890 : public __base<_Rp(_ArgTypes...)> 891 { 892 typedef _Rp1(^__block_type)(_ArgTypes1...); 893 __block_type __f_; 894 895 public: 896 _LIBCPP_INLINE_VISIBILITY 897 explicit __func(__block_type const& __f) 898 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr)) 899 { } 900 901 // [TODO] add && to save on a retain 902 903 _LIBCPP_INLINE_VISIBILITY 904 explicit __func(__block_type __f, const _Alloc& /* unused */) 905 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr)) 906 { } 907 908 virtual __base<_Rp(_ArgTypes...)>* __clone() const { 909 _LIBCPP_ASSERT(false, 910 "Block pointers are just pointers, so they should always fit into " 911 "std::function's small buffer optimization. This function should " 912 "never be invoked."); 913 return nullptr; 914 } 915 916 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const { 917 ::new ((void*)__p) __func(__f_); 918 } 919 920 virtual void destroy() _NOEXCEPT { 921 if (__f_) 922 _Block_release(__f_); 923 __f_ = 0; 924 } 925 926 virtual void destroy_deallocate() _NOEXCEPT { 927 _LIBCPP_ASSERT(false, 928 "Block pointers are just pointers, so they should always fit into " 929 "std::function's small buffer optimization. This function should " 930 "never be invoked."); 931 } 932 933 virtual _Rp operator()(_ArgTypes&& ... __arg) { 934 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...); 935 } 936 937 #ifndef _LIBCPP_NO_RTTI 938 virtual const void* target(type_info const& __ti) const _NOEXCEPT { 939 if (__ti == typeid(__func::__block_type)) 940 return &__f_; 941 return (const void*)nullptr; 942 } 943 944 virtual const std::type_info& target_type() const _NOEXCEPT { 945 return typeid(__func::__block_type); 946 } 947 #endif // _LIBCPP_NO_RTTI 948 }; 949 950 #endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC 951 952 } // namespace __function 953 954 template<class _Rp, class ..._ArgTypes> 955 class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)> 956 #if _LIBCPP_STD_VER <= 17 || !defined(_LIBCPP_ABI_NO_BINDER_BASES) 957 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>, 958 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)> 959 #endif 960 { 961 #ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION 962 typedef __function::__value_func<_Rp(_ArgTypes...)> __func; 963 #else 964 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func; 965 #endif 966 967 __func __f_; 968 969 template <class _Fp, bool = _And< 970 _IsNotSame<__uncvref_t<_Fp>, function>, 971 __invokable<_Fp, _ArgTypes...> 972 >::value> 973 struct __callable; 974 template <class _Fp> 975 struct __callable<_Fp, true> 976 { 977 static const bool value = is_void<_Rp>::value || 978 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type, 979 _Rp>::value; 980 }; 981 template <class _Fp> 982 struct __callable<_Fp, false> 983 { 984 static const bool value = false; 985 }; 986 987 template <class _Fp> 988 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type; 989 public: 990 typedef _Rp result_type; 991 992 // construct/copy/destroy: 993 _LIBCPP_INLINE_VISIBILITY 994 function() _NOEXCEPT { } 995 _LIBCPP_INLINE_VISIBILITY 996 function(nullptr_t) _NOEXCEPT {} 997 function(const function&); 998 function(function&&) _NOEXCEPT; 999 template<class _Fp, class = _EnableIfLValueCallable<_Fp>> 1000 function(_Fp); 1001 1002 #if _LIBCPP_STD_VER <= 14 1003 template<class _Alloc> 1004 _LIBCPP_INLINE_VISIBILITY 1005 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {} 1006 template<class _Alloc> 1007 _LIBCPP_INLINE_VISIBILITY 1008 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {} 1009 template<class _Alloc> 1010 function(allocator_arg_t, const _Alloc&, const function&); 1011 template<class _Alloc> 1012 function(allocator_arg_t, const _Alloc&, function&&); 1013 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>> 1014 function(allocator_arg_t, const _Alloc& __a, _Fp __f); 1015 #endif 1016 1017 function& operator=(const function&); 1018 function& operator=(function&&) _NOEXCEPT; 1019 function& operator=(nullptr_t) _NOEXCEPT; 1020 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>> 1021 function& operator=(_Fp&&); 1022 1023 ~function(); 1024 1025 // function modifiers: 1026 void swap(function&) _NOEXCEPT; 1027 1028 #if _LIBCPP_STD_VER <= 14 1029 template<class _Fp, class _Alloc> 1030 _LIBCPP_INLINE_VISIBILITY 1031 void assign(_Fp&& __f, const _Alloc& __a) 1032 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);} 1033 #endif 1034 1035 // function capacity: 1036 _LIBCPP_INLINE_VISIBILITY 1037 explicit operator bool() const _NOEXCEPT { 1038 return static_cast<bool>(__f_); 1039 } 1040 1041 // deleted overloads close possible hole in the type system 1042 template<class _R2, class... _ArgTypes2> 1043 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete; 1044 template<class _R2, class... _ArgTypes2> 1045 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete; 1046 public: 1047 // function invocation: 1048 _Rp operator()(_ArgTypes...) const; 1049 1050 #ifndef _LIBCPP_NO_RTTI 1051 // function target access: 1052 const std::type_info& target_type() const _NOEXCEPT; 1053 template <typename _Tp> _Tp* target() _NOEXCEPT; 1054 template <typename _Tp> const _Tp* target() const _NOEXCEPT; 1055 #endif // _LIBCPP_NO_RTTI 1056 }; 1057 1058 #if _LIBCPP_STD_VER >= 17 1059 template<class _Rp, class ..._Ap> 1060 function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>; 1061 1062 template<class _Fp> 1063 struct __strip_signature; 1064 1065 template<class _Rp, class _Gp, class ..._Ap> 1066 struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); }; 1067 template<class _Rp, class _Gp, class ..._Ap> 1068 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); }; 1069 template<class _Rp, class _Gp, class ..._Ap> 1070 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); }; 1071 template<class _Rp, class _Gp, class ..._Ap> 1072 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); }; 1073 1074 template<class _Rp, class _Gp, class ..._Ap> 1075 struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); }; 1076 template<class _Rp, class _Gp, class ..._Ap> 1077 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); }; 1078 template<class _Rp, class _Gp, class ..._Ap> 1079 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); }; 1080 template<class _Rp, class _Gp, class ..._Ap> 1081 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); }; 1082 1083 template<class _Rp, class _Gp, class ..._Ap> 1084 struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); }; 1085 template<class _Rp, class _Gp, class ..._Ap> 1086 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); }; 1087 template<class _Rp, class _Gp, class ..._Ap> 1088 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); }; 1089 template<class _Rp, class _Gp, class ..._Ap> 1090 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); }; 1091 1092 template<class _Rp, class _Gp, class ..._Ap> 1093 struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); }; 1094 template<class _Rp, class _Gp, class ..._Ap> 1095 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); }; 1096 template<class _Rp, class _Gp, class ..._Ap> 1097 struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); }; 1098 template<class _Rp, class _Gp, class ..._Ap> 1099 struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); }; 1100 1101 template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type> 1102 function(_Fp) -> function<_Stripped>; 1103 #endif // _LIBCPP_STD_VER >= 17 1104 1105 template<class _Rp, class ..._ArgTypes> 1106 function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {} 1107 1108 #if _LIBCPP_STD_VER <= 14 1109 template<class _Rp, class ..._ArgTypes> 1110 template <class _Alloc> 1111 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, 1112 const function& __f) : __f_(__f.__f_) {} 1113 #endif 1114 1115 template <class _Rp, class... _ArgTypes> 1116 function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT 1117 : __f_(_VSTD::move(__f.__f_)) {} 1118 1119 #if _LIBCPP_STD_VER <= 14 1120 template<class _Rp, class ..._ArgTypes> 1121 template <class _Alloc> 1122 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, 1123 function&& __f) 1124 : __f_(_VSTD::move(__f.__f_)) {} 1125 #endif 1126 1127 template <class _Rp, class... _ArgTypes> 1128 template <class _Fp, class> 1129 function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {} 1130 1131 #if _LIBCPP_STD_VER <= 14 1132 template <class _Rp, class... _ArgTypes> 1133 template <class _Fp, class _Alloc, class> 1134 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a, 1135 _Fp __f) 1136 : __f_(_VSTD::move(__f), __a) {} 1137 #endif 1138 1139 template<class _Rp, class ..._ArgTypes> 1140 function<_Rp(_ArgTypes...)>& 1141 function<_Rp(_ArgTypes...)>::operator=(const function& __f) 1142 { 1143 function(__f).swap(*this); 1144 return *this; 1145 } 1146 1147 template<class _Rp, class ..._ArgTypes> 1148 function<_Rp(_ArgTypes...)>& 1149 function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT 1150 { 1151 __f_ = _VSTD::move(__f.__f_); 1152 return *this; 1153 } 1154 1155 template<class _Rp, class ..._ArgTypes> 1156 function<_Rp(_ArgTypes...)>& 1157 function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT 1158 { 1159 __f_ = nullptr; 1160 return *this; 1161 } 1162 1163 template<class _Rp, class ..._ArgTypes> 1164 template <class _Fp, class> 1165 function<_Rp(_ArgTypes...)>& 1166 function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f) 1167 { 1168 function(_VSTD::forward<_Fp>(__f)).swap(*this); 1169 return *this; 1170 } 1171 1172 template<class _Rp, class ..._ArgTypes> 1173 function<_Rp(_ArgTypes...)>::~function() {} 1174 1175 template<class _Rp, class ..._ArgTypes> 1176 void 1177 function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT 1178 { 1179 __f_.swap(__f.__f_); 1180 } 1181 1182 template<class _Rp, class ..._ArgTypes> 1183 _Rp 1184 function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const 1185 { 1186 return __f_(_VSTD::forward<_ArgTypes>(__arg)...); 1187 } 1188 1189 #ifndef _LIBCPP_NO_RTTI 1190 1191 template<class _Rp, class ..._ArgTypes> 1192 const std::type_info& 1193 function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT 1194 { 1195 return __f_.target_type(); 1196 } 1197 1198 template<class _Rp, class ..._ArgTypes> 1199 template <typename _Tp> 1200 _Tp* 1201 function<_Rp(_ArgTypes...)>::target() _NOEXCEPT 1202 { 1203 return (_Tp*)(__f_.template target<_Tp>()); 1204 } 1205 1206 template<class _Rp, class ..._ArgTypes> 1207 template <typename _Tp> 1208 const _Tp* 1209 function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT 1210 { 1211 return __f_.template target<_Tp>(); 1212 } 1213 1214 #endif // _LIBCPP_NO_RTTI 1215 1216 template <class _Rp, class... _ArgTypes> 1217 inline _LIBCPP_INLINE_VISIBILITY 1218 bool 1219 operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;} 1220 1221 template <class _Rp, class... _ArgTypes> 1222 inline _LIBCPP_INLINE_VISIBILITY 1223 bool 1224 operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;} 1225 1226 template <class _Rp, class... _ArgTypes> 1227 inline _LIBCPP_INLINE_VISIBILITY 1228 bool 1229 operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;} 1230 1231 template <class _Rp, class... _ArgTypes> 1232 inline _LIBCPP_INLINE_VISIBILITY 1233 bool 1234 operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;} 1235 1236 template <class _Rp, class... _ArgTypes> 1237 inline _LIBCPP_INLINE_VISIBILITY 1238 void 1239 swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT 1240 {return __x.swap(__y);} 1241 1242 #else // _LIBCPP_CXX03_LANG 1243 1244 namespace __function { 1245 1246 template<class _Fp> class __base; 1247 1248 template<class _Rp> 1249 class __base<_Rp()> 1250 { 1251 __base(const __base&); 1252 __base& operator=(const __base&); 1253 public: 1254 __base() {} 1255 virtual ~__base() {} 1256 virtual __base* __clone() const = 0; 1257 virtual void __clone(__base*) const = 0; 1258 virtual void destroy() = 0; 1259 virtual void destroy_deallocate() = 0; 1260 virtual _Rp operator()() = 0; 1261 #ifndef _LIBCPP_NO_RTTI 1262 virtual const void* target(const type_info&) const = 0; 1263 virtual const std::type_info& target_type() const = 0; 1264 #endif // _LIBCPP_NO_RTTI 1265 }; 1266 1267 template<class _Rp, class _A0> 1268 class __base<_Rp(_A0)> 1269 { 1270 __base(const __base&); 1271 __base& operator=(const __base&); 1272 public: 1273 __base() {} 1274 virtual ~__base() {} 1275 virtual __base* __clone() const = 0; 1276 virtual void __clone(__base*) const = 0; 1277 virtual void destroy() = 0; 1278 virtual void destroy_deallocate() = 0; 1279 virtual _Rp operator()(_A0) = 0; 1280 #ifndef _LIBCPP_NO_RTTI 1281 virtual const void* target(const type_info&) const = 0; 1282 virtual const std::type_info& target_type() const = 0; 1283 #endif // _LIBCPP_NO_RTTI 1284 }; 1285 1286 template<class _Rp, class _A0, class _A1> 1287 class __base<_Rp(_A0, _A1)> 1288 { 1289 __base(const __base&); 1290 __base& operator=(const __base&); 1291 public: 1292 __base() {} 1293 virtual ~__base() {} 1294 virtual __base* __clone() const = 0; 1295 virtual void __clone(__base*) const = 0; 1296 virtual void destroy() = 0; 1297 virtual void destroy_deallocate() = 0; 1298 virtual _Rp operator()(_A0, _A1) = 0; 1299 #ifndef _LIBCPP_NO_RTTI 1300 virtual const void* target(const type_info&) const = 0; 1301 virtual const std::type_info& target_type() const = 0; 1302 #endif // _LIBCPP_NO_RTTI 1303 }; 1304 1305 template<class _Rp, class _A0, class _A1, class _A2> 1306 class __base<_Rp(_A0, _A1, _A2)> 1307 { 1308 __base(const __base&); 1309 __base& operator=(const __base&); 1310 public: 1311 __base() {} 1312 virtual ~__base() {} 1313 virtual __base* __clone() const = 0; 1314 virtual void __clone(__base*) const = 0; 1315 virtual void destroy() = 0; 1316 virtual void destroy_deallocate() = 0; 1317 virtual _Rp operator()(_A0, _A1, _A2) = 0; 1318 #ifndef _LIBCPP_NO_RTTI 1319 virtual const void* target(const type_info&) const = 0; 1320 virtual const std::type_info& target_type() const = 0; 1321 #endif // _LIBCPP_NO_RTTI 1322 }; 1323 1324 template<class _FD, class _Alloc, class _FB> class __func; 1325 1326 template<class _Fp, class _Alloc, class _Rp> 1327 class __func<_Fp, _Alloc, _Rp()> 1328 : public __base<_Rp()> 1329 { 1330 __compressed_pair<_Fp, _Alloc> __f_; 1331 public: 1332 explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {} 1333 explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {} 1334 virtual __base<_Rp()>* __clone() const; 1335 virtual void __clone(__base<_Rp()>*) const; 1336 virtual void destroy(); 1337 virtual void destroy_deallocate(); 1338 virtual _Rp operator()(); 1339 #ifndef _LIBCPP_NO_RTTI 1340 virtual const void* target(const type_info&) const; 1341 virtual const std::type_info& target_type() const; 1342 #endif // _LIBCPP_NO_RTTI 1343 }; 1344 1345 template<class _Fp, class _Alloc, class _Rp> 1346 __base<_Rp()>* 1347 __func<_Fp, _Alloc, _Rp()>::__clone() const 1348 { 1349 typedef allocator_traits<_Alloc> __alloc_traits; 1350 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap; 1351 _Ap __a(__f_.second()); 1352 typedef __allocator_destructor<_Ap> _Dp; 1353 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 1354 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a)); 1355 return __hold.release(); 1356 } 1357 1358 template<class _Fp, class _Alloc, class _Rp> 1359 void 1360 __func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const 1361 { 1362 ::new ((void*)__p) __func(__f_.first(), __f_.second()); 1363 } 1364 1365 template<class _Fp, class _Alloc, class _Rp> 1366 void 1367 __func<_Fp, _Alloc, _Rp()>::destroy() 1368 { 1369 __f_.~__compressed_pair<_Fp, _Alloc>(); 1370 } 1371 1372 template<class _Fp, class _Alloc, class _Rp> 1373 void 1374 __func<_Fp, _Alloc, _Rp()>::destroy_deallocate() 1375 { 1376 typedef allocator_traits<_Alloc> __alloc_traits; 1377 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap; 1378 _Ap __a(__f_.second()); 1379 __f_.~__compressed_pair<_Fp, _Alloc>(); 1380 __a.deallocate(this, 1); 1381 } 1382 1383 template<class _Fp, class _Alloc, class _Rp> 1384 _Rp 1385 __func<_Fp, _Alloc, _Rp()>::operator()() 1386 { 1387 typedef __invoke_void_return_wrapper<_Rp> _Invoker; 1388 return _Invoker::__call(__f_.first()); 1389 } 1390 1391 #ifndef _LIBCPP_NO_RTTI 1392 1393 template<class _Fp, class _Alloc, class _Rp> 1394 const void* 1395 __func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const 1396 { 1397 if (__ti == typeid(_Fp)) 1398 return _VSTD::addressof(__f_.first()); 1399 return (const void*)0; 1400 } 1401 1402 template<class _Fp, class _Alloc, class _Rp> 1403 const std::type_info& 1404 __func<_Fp, _Alloc, _Rp()>::target_type() const 1405 { 1406 return typeid(_Fp); 1407 } 1408 1409 #endif // _LIBCPP_NO_RTTI 1410 1411 template<class _Fp, class _Alloc, class _Rp, class _A0> 1412 class __func<_Fp, _Alloc, _Rp(_A0)> 1413 : public __base<_Rp(_A0)> 1414 { 1415 __compressed_pair<_Fp, _Alloc> __f_; 1416 public: 1417 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {} 1418 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a) 1419 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {} 1420 virtual __base<_Rp(_A0)>* __clone() const; 1421 virtual void __clone(__base<_Rp(_A0)>*) const; 1422 virtual void destroy(); 1423 virtual void destroy_deallocate(); 1424 virtual _Rp operator()(_A0); 1425 #ifndef _LIBCPP_NO_RTTI 1426 virtual const void* target(const type_info&) const; 1427 virtual const std::type_info& target_type() const; 1428 #endif // _LIBCPP_NO_RTTI 1429 }; 1430 1431 template<class _Fp, class _Alloc, class _Rp, class _A0> 1432 __base<_Rp(_A0)>* 1433 __func<_Fp, _Alloc, _Rp(_A0)>::__clone() const 1434 { 1435 typedef allocator_traits<_Alloc> __alloc_traits; 1436 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap; 1437 _Ap __a(__f_.second()); 1438 typedef __allocator_destructor<_Ap> _Dp; 1439 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 1440 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a)); 1441 return __hold.release(); 1442 } 1443 1444 template<class _Fp, class _Alloc, class _Rp, class _A0> 1445 void 1446 __func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const 1447 { 1448 ::new ((void*)__p) __func(__f_.first(), __f_.second()); 1449 } 1450 1451 template<class _Fp, class _Alloc, class _Rp, class _A0> 1452 void 1453 __func<_Fp, _Alloc, _Rp(_A0)>::destroy() 1454 { 1455 __f_.~__compressed_pair<_Fp, _Alloc>(); 1456 } 1457 1458 template<class _Fp, class _Alloc, class _Rp, class _A0> 1459 void 1460 __func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate() 1461 { 1462 typedef allocator_traits<_Alloc> __alloc_traits; 1463 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap; 1464 _Ap __a(__f_.second()); 1465 __f_.~__compressed_pair<_Fp, _Alloc>(); 1466 __a.deallocate(this, 1); 1467 } 1468 1469 template<class _Fp, class _Alloc, class _Rp, class _A0> 1470 _Rp 1471 __func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0) 1472 { 1473 typedef __invoke_void_return_wrapper<_Rp> _Invoker; 1474 return _Invoker::__call(__f_.first(), __a0); 1475 } 1476 1477 #ifndef _LIBCPP_NO_RTTI 1478 1479 template<class _Fp, class _Alloc, class _Rp, class _A0> 1480 const void* 1481 __func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const 1482 { 1483 if (__ti == typeid(_Fp)) 1484 return &__f_.first(); 1485 return (const void*)0; 1486 } 1487 1488 template<class _Fp, class _Alloc, class _Rp, class _A0> 1489 const std::type_info& 1490 __func<_Fp, _Alloc, _Rp(_A0)>::target_type() const 1491 { 1492 return typeid(_Fp); 1493 } 1494 1495 #endif // _LIBCPP_NO_RTTI 1496 1497 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1> 1498 class __func<_Fp, _Alloc, _Rp(_A0, _A1)> 1499 : public __base<_Rp(_A0, _A1)> 1500 { 1501 __compressed_pair<_Fp, _Alloc> __f_; 1502 public: 1503 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {} 1504 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a) 1505 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {} 1506 virtual __base<_Rp(_A0, _A1)>* __clone() const; 1507 virtual void __clone(__base<_Rp(_A0, _A1)>*) const; 1508 virtual void destroy(); 1509 virtual void destroy_deallocate(); 1510 virtual _Rp operator()(_A0, _A1); 1511 #ifndef _LIBCPP_NO_RTTI 1512 virtual const void* target(const type_info&) const; 1513 virtual const std::type_info& target_type() const; 1514 #endif // _LIBCPP_NO_RTTI 1515 }; 1516 1517 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1> 1518 __base<_Rp(_A0, _A1)>* 1519 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const 1520 { 1521 typedef allocator_traits<_Alloc> __alloc_traits; 1522 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap; 1523 _Ap __a(__f_.second()); 1524 typedef __allocator_destructor<_Ap> _Dp; 1525 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 1526 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a)); 1527 return __hold.release(); 1528 } 1529 1530 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1> 1531 void 1532 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const 1533 { 1534 ::new ((void*)__p) __func(__f_.first(), __f_.second()); 1535 } 1536 1537 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1> 1538 void 1539 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy() 1540 { 1541 __f_.~__compressed_pair<_Fp, _Alloc>(); 1542 } 1543 1544 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1> 1545 void 1546 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate() 1547 { 1548 typedef allocator_traits<_Alloc> __alloc_traits; 1549 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap; 1550 _Ap __a(__f_.second()); 1551 __f_.~__compressed_pair<_Fp, _Alloc>(); 1552 __a.deallocate(this, 1); 1553 } 1554 1555 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1> 1556 _Rp 1557 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) 1558 { 1559 typedef __invoke_void_return_wrapper<_Rp> _Invoker; 1560 return _Invoker::__call(__f_.first(), __a0, __a1); 1561 } 1562 1563 #ifndef _LIBCPP_NO_RTTI 1564 1565 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1> 1566 const void* 1567 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const 1568 { 1569 if (__ti == typeid(_Fp)) 1570 return &__f_.first(); 1571 return (const void*)0; 1572 } 1573 1574 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1> 1575 const std::type_info& 1576 __func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const 1577 { 1578 return typeid(_Fp); 1579 } 1580 1581 #endif // _LIBCPP_NO_RTTI 1582 1583 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2> 1584 class __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> 1585 : public __base<_Rp(_A0, _A1, _A2)> 1586 { 1587 __compressed_pair<_Fp, _Alloc> __f_; 1588 public: 1589 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f), __default_init_tag()) {} 1590 _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a) 1591 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {} 1592 virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const; 1593 virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const; 1594 virtual void destroy(); 1595 virtual void destroy_deallocate(); 1596 virtual _Rp operator()(_A0, _A1, _A2); 1597 #ifndef _LIBCPP_NO_RTTI 1598 virtual const void* target(const type_info&) const; 1599 virtual const std::type_info& target_type() const; 1600 #endif // _LIBCPP_NO_RTTI 1601 }; 1602 1603 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2> 1604 __base<_Rp(_A0, _A1, _A2)>* 1605 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const 1606 { 1607 typedef allocator_traits<_Alloc> __alloc_traits; 1608 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap; 1609 _Ap __a(__f_.second()); 1610 typedef __allocator_destructor<_Ap> _Dp; 1611 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 1612 ::new ((void*)__hold.get()) __func(__f_.first(), _Alloc(__a)); 1613 return __hold.release(); 1614 } 1615 1616 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2> 1617 void 1618 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const 1619 { 1620 ::new ((void*)__p) __func(__f_.first(), __f_.second()); 1621 } 1622 1623 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2> 1624 void 1625 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy() 1626 { 1627 __f_.~__compressed_pair<_Fp, _Alloc>(); 1628 } 1629 1630 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2> 1631 void 1632 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate() 1633 { 1634 typedef allocator_traits<_Alloc> __alloc_traits; 1635 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap; 1636 _Ap __a(__f_.second()); 1637 __f_.~__compressed_pair<_Fp, _Alloc>(); 1638 __a.deallocate(this, 1); 1639 } 1640 1641 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2> 1642 _Rp 1643 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) 1644 { 1645 typedef __invoke_void_return_wrapper<_Rp> _Invoker; 1646 return _Invoker::__call(__f_.first(), __a0, __a1, __a2); 1647 } 1648 1649 #ifndef _LIBCPP_NO_RTTI 1650 1651 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2> 1652 const void* 1653 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const 1654 { 1655 if (__ti == typeid(_Fp)) 1656 return &__f_.first(); 1657 return (const void*)0; 1658 } 1659 1660 template<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2> 1661 const std::type_info& 1662 __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const 1663 { 1664 return typeid(_Fp); 1665 } 1666 1667 #endif // _LIBCPP_NO_RTTI 1668 1669 } // namespace __function 1670 1671 template<class _Rp> 1672 class _LIBCPP_TEMPLATE_VIS function<_Rp()> 1673 { 1674 typedef __function::__base<_Rp()> __base; 1675 aligned_storage<3*sizeof(void*)>::type __buf_; 1676 __base* __f_; 1677 1678 public: 1679 typedef _Rp result_type; 1680 1681 // 20.7.16.2.1, construct/copy/destroy: 1682 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} 1683 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} 1684 function(const function&); 1685 template<class _Fp> 1686 function(_Fp, 1687 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 1688 1689 template<class _Alloc> 1690 _LIBCPP_INLINE_VISIBILITY 1691 function(allocator_arg_t, const _Alloc&) : __f_(0) {} 1692 template<class _Alloc> 1693 _LIBCPP_INLINE_VISIBILITY 1694 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} 1695 template<class _Alloc> 1696 function(allocator_arg_t, const _Alloc&, const function&); 1697 template<class _Fp, class _Alloc> 1698 function(allocator_arg_t, const _Alloc& __a, _Fp __f, 1699 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 1700 1701 function& operator=(const function&); 1702 function& operator=(nullptr_t); 1703 template<class _Fp> 1704 typename enable_if 1705 < 1706 !is_integral<_Fp>::value, 1707 function& 1708 >::type 1709 operator=(_Fp); 1710 1711 ~function(); 1712 1713 // 20.7.16.2.2, function modifiers: 1714 void swap(function&); 1715 template<class _Fp, class _Alloc> 1716 _LIBCPP_INLINE_VISIBILITY 1717 void assign(_Fp __f, const _Alloc& __a) 1718 {function(allocator_arg, __a, __f).swap(*this);} 1719 1720 // 20.7.16.2.3, function capacity: 1721 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;} 1722 1723 template<class _R2> 1724 bool operator==(const function<_R2()>&) const = delete; 1725 template<class _R2> 1726 bool operator!=(const function<_R2()>&) const = delete; 1727 1728 // 20.7.16.2.4, function invocation: 1729 _Rp operator()() const; 1730 1731 #ifndef _LIBCPP_NO_RTTI 1732 // 20.7.16.2.5, function target access: 1733 const std::type_info& target_type() const; 1734 template <typename _Tp> _Tp* target(); 1735 template <typename _Tp> const _Tp* target() const; 1736 #endif // _LIBCPP_NO_RTTI 1737 }; 1738 1739 template<class _Rp> 1740 function<_Rp()>::function(const function& __f) 1741 { 1742 if (__f.__f_ == 0) 1743 __f_ = 0; 1744 else if (__f.__f_ == (const __base*)&__f.__buf_) 1745 { 1746 __f_ = (__base*)&__buf_; 1747 __f.__f_->__clone(__f_); 1748 } 1749 else 1750 __f_ = __f.__f_->__clone(); 1751 } 1752 1753 template<class _Rp> 1754 template<class _Alloc> 1755 function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f) 1756 { 1757 if (__f.__f_ == 0) 1758 __f_ = 0; 1759 else if (__f.__f_ == (const __base*)&__f.__buf_) 1760 { 1761 __f_ = (__base*)&__buf_; 1762 __f.__f_->__clone(__f_); 1763 } 1764 else 1765 __f_ = __f.__f_->__clone(); 1766 } 1767 1768 template<class _Rp> 1769 template <class _Fp> 1770 function<_Rp()>::function(_Fp __f, 1771 typename enable_if<!is_integral<_Fp>::value>::type*) 1772 : __f_(0) 1773 { 1774 if (__function::__not_null(__f)) 1775 { 1776 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF; 1777 if (sizeof(_FF) <= sizeof(__buf_)) 1778 { 1779 __f_ = (__base*)&__buf_; 1780 ::new ((void*)__f_) _FF(__f); 1781 } 1782 else 1783 { 1784 typedef allocator<_FF> _Ap; 1785 _Ap __a; 1786 typedef __allocator_destructor<_Ap> _Dp; 1787 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 1788 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a)); 1789 __f_ = __hold.release(); 1790 } 1791 } 1792 } 1793 1794 template<class _Rp> 1795 template <class _Fp, class _Alloc> 1796 function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f, 1797 typename enable_if<!is_integral<_Fp>::value>::type*) 1798 : __f_(0) 1799 { 1800 typedef allocator_traits<_Alloc> __alloc_traits; 1801 if (__function::__not_null(__f)) 1802 { 1803 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF; 1804 if (sizeof(_FF) <= sizeof(__buf_)) 1805 { 1806 __f_ = (__base*)&__buf_; 1807 ::new ((void*)__f_) _FF(__f, __a0); 1808 } 1809 else 1810 { 1811 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap; 1812 _Ap __a(__a0); 1813 typedef __allocator_destructor<_Ap> _Dp; 1814 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 1815 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a)); 1816 __f_ = __hold.release(); 1817 } 1818 } 1819 } 1820 1821 template<class _Rp> 1822 function<_Rp()>& 1823 function<_Rp()>::operator=(const function& __f) 1824 { 1825 if (__f) 1826 function(__f).swap(*this); 1827 else 1828 *this = nullptr; 1829 return *this; 1830 } 1831 1832 template<class _Rp> 1833 function<_Rp()>& 1834 function<_Rp()>::operator=(nullptr_t) 1835 { 1836 __base* __t = __f_; 1837 __f_ = 0; 1838 if (__t == (__base*)&__buf_) 1839 __t->destroy(); 1840 else if (__t) 1841 __t->destroy_deallocate(); 1842 return *this; 1843 } 1844 1845 template<class _Rp> 1846 template <class _Fp> 1847 typename enable_if 1848 < 1849 !is_integral<_Fp>::value, 1850 function<_Rp()>& 1851 >::type 1852 function<_Rp()>::operator=(_Fp __f) 1853 { 1854 function(_VSTD::move(__f)).swap(*this); 1855 return *this; 1856 } 1857 1858 template<class _Rp> 1859 function<_Rp()>::~function() 1860 { 1861 if (__f_ == (__base*)&__buf_) 1862 __f_->destroy(); 1863 else if (__f_) 1864 __f_->destroy_deallocate(); 1865 } 1866 1867 template<class _Rp> 1868 void 1869 function<_Rp()>::swap(function& __f) 1870 { 1871 if (_VSTD::addressof(__f) == this) 1872 return; 1873 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) 1874 { 1875 typename aligned_storage<sizeof(__buf_)>::type __tempbuf; 1876 __base* __t = (__base*)&__tempbuf; 1877 __f_->__clone(__t); 1878 __f_->destroy(); 1879 __f_ = 0; 1880 __f.__f_->__clone((__base*)&__buf_); 1881 __f.__f_->destroy(); 1882 __f.__f_ = 0; 1883 __f_ = (__base*)&__buf_; 1884 __t->__clone((__base*)&__f.__buf_); 1885 __t->destroy(); 1886 __f.__f_ = (__base*)&__f.__buf_; 1887 } 1888 else if (__f_ == (__base*)&__buf_) 1889 { 1890 __f_->__clone((__base*)&__f.__buf_); 1891 __f_->destroy(); 1892 __f_ = __f.__f_; 1893 __f.__f_ = (__base*)&__f.__buf_; 1894 } 1895 else if (__f.__f_ == (__base*)&__f.__buf_) 1896 { 1897 __f.__f_->__clone((__base*)&__buf_); 1898 __f.__f_->destroy(); 1899 __f.__f_ = __f_; 1900 __f_ = (__base*)&__buf_; 1901 } 1902 else 1903 _VSTD::swap(__f_, __f.__f_); 1904 } 1905 1906 template<class _Rp> 1907 _Rp 1908 function<_Rp()>::operator()() const 1909 { 1910 if (__f_ == 0) 1911 __throw_bad_function_call(); 1912 return (*__f_)(); 1913 } 1914 1915 #ifndef _LIBCPP_NO_RTTI 1916 1917 template<class _Rp> 1918 const std::type_info& 1919 function<_Rp()>::target_type() const 1920 { 1921 if (__f_ == 0) 1922 return typeid(void); 1923 return __f_->target_type(); 1924 } 1925 1926 template<class _Rp> 1927 template <typename _Tp> 1928 _Tp* 1929 function<_Rp()>::target() 1930 { 1931 if (__f_ == 0) 1932 return (_Tp*)0; 1933 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp))); 1934 } 1935 1936 template<class _Rp> 1937 template <typename _Tp> 1938 const _Tp* 1939 function<_Rp()>::target() const 1940 { 1941 if (__f_ == 0) 1942 return (const _Tp*)0; 1943 return (const _Tp*)__f_->target(typeid(_Tp)); 1944 } 1945 1946 #endif // _LIBCPP_NO_RTTI 1947 1948 template<class _Rp, class _A0> 1949 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0)> 1950 : public unary_function<_A0, _Rp> 1951 { 1952 typedef __function::__base<_Rp(_A0)> __base; 1953 aligned_storage<3*sizeof(void*)>::type __buf_; 1954 __base* __f_; 1955 1956 public: 1957 typedef _Rp result_type; 1958 1959 // 20.7.16.2.1, construct/copy/destroy: 1960 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} 1961 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} 1962 function(const function&); 1963 template<class _Fp> 1964 function(_Fp, 1965 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 1966 1967 template<class _Alloc> 1968 _LIBCPP_INLINE_VISIBILITY 1969 function(allocator_arg_t, const _Alloc&) : __f_(0) {} 1970 template<class _Alloc> 1971 _LIBCPP_INLINE_VISIBILITY 1972 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} 1973 template<class _Alloc> 1974 function(allocator_arg_t, const _Alloc&, const function&); 1975 template<class _Fp, class _Alloc> 1976 function(allocator_arg_t, const _Alloc& __a, _Fp __f, 1977 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 1978 1979 function& operator=(const function&); 1980 function& operator=(nullptr_t); 1981 template<class _Fp> 1982 typename enable_if 1983 < 1984 !is_integral<_Fp>::value, 1985 function& 1986 >::type 1987 operator=(_Fp); 1988 1989 ~function(); 1990 1991 // 20.7.16.2.2, function modifiers: 1992 void swap(function&); 1993 template<class _Fp, class _Alloc> 1994 _LIBCPP_INLINE_VISIBILITY 1995 void assign(_Fp __f, const _Alloc& __a) 1996 {function(allocator_arg, __a, __f).swap(*this);} 1997 1998 // 20.7.16.2.3, function capacity: 1999 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;} 2000 2001 template<class _R2, class _B0> 2002 bool operator==(const function<_R2(_B0)>&) const = delete; 2003 template<class _R2, class _B0> 2004 bool operator!=(const function<_R2(_B0)>&) const = delete; 2005 2006 // 20.7.16.2.4, function invocation: 2007 _Rp operator()(_A0) const; 2008 2009 #ifndef _LIBCPP_NO_RTTI 2010 // 20.7.16.2.5, function target access: 2011 const std::type_info& target_type() const; 2012 template <typename _Tp> _Tp* target(); 2013 template <typename _Tp> const _Tp* target() const; 2014 #endif // _LIBCPP_NO_RTTI 2015 }; 2016 2017 template<class _Rp, class _A0> 2018 function<_Rp(_A0)>::function(const function& __f) 2019 { 2020 if (__f.__f_ == 0) 2021 __f_ = 0; 2022 else if (__f.__f_ == (const __base*)&__f.__buf_) 2023 { 2024 __f_ = (__base*)&__buf_; 2025 __f.__f_->__clone(__f_); 2026 } 2027 else 2028 __f_ = __f.__f_->__clone(); 2029 } 2030 2031 template<class _Rp, class _A0> 2032 template<class _Alloc> 2033 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f) 2034 { 2035 if (__f.__f_ == 0) 2036 __f_ = 0; 2037 else if (__f.__f_ == (const __base*)&__f.__buf_) 2038 { 2039 __f_ = (__base*)&__buf_; 2040 __f.__f_->__clone(__f_); 2041 } 2042 else 2043 __f_ = __f.__f_->__clone(); 2044 } 2045 2046 template<class _Rp, class _A0> 2047 template <class _Fp> 2048 function<_Rp(_A0)>::function(_Fp __f, 2049 typename enable_if<!is_integral<_Fp>::value>::type*) 2050 : __f_(0) 2051 { 2052 if (__function::__not_null(__f)) 2053 { 2054 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF; 2055 if (sizeof(_FF) <= sizeof(__buf_)) 2056 { 2057 __f_ = (__base*)&__buf_; 2058 ::new ((void*)__f_) _FF(__f); 2059 } 2060 else 2061 { 2062 typedef allocator<_FF> _Ap; 2063 _Ap __a; 2064 typedef __allocator_destructor<_Ap> _Dp; 2065 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2066 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a)); 2067 __f_ = __hold.release(); 2068 } 2069 } 2070 } 2071 2072 template<class _Rp, class _A0> 2073 template <class _Fp, class _Alloc> 2074 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f, 2075 typename enable_if<!is_integral<_Fp>::value>::type*) 2076 : __f_(0) 2077 { 2078 typedef allocator_traits<_Alloc> __alloc_traits; 2079 if (__function::__not_null(__f)) 2080 { 2081 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF; 2082 if (sizeof(_FF) <= sizeof(__buf_)) 2083 { 2084 __f_ = (__base*)&__buf_; 2085 ::new ((void*)__f_) _FF(__f, __a0); 2086 } 2087 else 2088 { 2089 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap; 2090 _Ap __a(__a0); 2091 typedef __allocator_destructor<_Ap> _Dp; 2092 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2093 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a)); 2094 __f_ = __hold.release(); 2095 } 2096 } 2097 } 2098 2099 template<class _Rp, class _A0> 2100 function<_Rp(_A0)>& 2101 function<_Rp(_A0)>::operator=(const function& __f) 2102 { 2103 if (__f) 2104 function(__f).swap(*this); 2105 else 2106 *this = nullptr; 2107 return *this; 2108 } 2109 2110 template<class _Rp, class _A0> 2111 function<_Rp(_A0)>& 2112 function<_Rp(_A0)>::operator=(nullptr_t) 2113 { 2114 __base* __t = __f_; 2115 __f_ = 0; 2116 if (__t == (__base*)&__buf_) 2117 __t->destroy(); 2118 else if (__t) 2119 __t->destroy_deallocate(); 2120 return *this; 2121 } 2122 2123 template<class _Rp, class _A0> 2124 template <class _Fp> 2125 typename enable_if 2126 < 2127 !is_integral<_Fp>::value, 2128 function<_Rp(_A0)>& 2129 >::type 2130 function<_Rp(_A0)>::operator=(_Fp __f) 2131 { 2132 function(_VSTD::move(__f)).swap(*this); 2133 return *this; 2134 } 2135 2136 template<class _Rp, class _A0> 2137 function<_Rp(_A0)>::~function() 2138 { 2139 if (__f_ == (__base*)&__buf_) 2140 __f_->destroy(); 2141 else if (__f_) 2142 __f_->destroy_deallocate(); 2143 } 2144 2145 template<class _Rp, class _A0> 2146 void 2147 function<_Rp(_A0)>::swap(function& __f) 2148 { 2149 if (_VSTD::addressof(__f) == this) 2150 return; 2151 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) 2152 { 2153 typename aligned_storage<sizeof(__buf_)>::type __tempbuf; 2154 __base* __t = (__base*)&__tempbuf; 2155 __f_->__clone(__t); 2156 __f_->destroy(); 2157 __f_ = 0; 2158 __f.__f_->__clone((__base*)&__buf_); 2159 __f.__f_->destroy(); 2160 __f.__f_ = 0; 2161 __f_ = (__base*)&__buf_; 2162 __t->__clone((__base*)&__f.__buf_); 2163 __t->destroy(); 2164 __f.__f_ = (__base*)&__f.__buf_; 2165 } 2166 else if (__f_ == (__base*)&__buf_) 2167 { 2168 __f_->__clone((__base*)&__f.__buf_); 2169 __f_->destroy(); 2170 __f_ = __f.__f_; 2171 __f.__f_ = (__base*)&__f.__buf_; 2172 } 2173 else if (__f.__f_ == (__base*)&__f.__buf_) 2174 { 2175 __f.__f_->__clone((__base*)&__buf_); 2176 __f.__f_->destroy(); 2177 __f.__f_ = __f_; 2178 __f_ = (__base*)&__buf_; 2179 } 2180 else 2181 _VSTD::swap(__f_, __f.__f_); 2182 } 2183 2184 template<class _Rp, class _A0> 2185 _Rp 2186 function<_Rp(_A0)>::operator()(_A0 __a0) const 2187 { 2188 if (__f_ == 0) 2189 __throw_bad_function_call(); 2190 return (*__f_)(__a0); 2191 } 2192 2193 #ifndef _LIBCPP_NO_RTTI 2194 2195 template<class _Rp, class _A0> 2196 const std::type_info& 2197 function<_Rp(_A0)>::target_type() const 2198 { 2199 if (__f_ == 0) 2200 return typeid(void); 2201 return __f_->target_type(); 2202 } 2203 2204 template<class _Rp, class _A0> 2205 template <typename _Tp> 2206 _Tp* 2207 function<_Rp(_A0)>::target() 2208 { 2209 if (__f_ == 0) 2210 return (_Tp*)0; 2211 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp))); 2212 } 2213 2214 template<class _Rp, class _A0> 2215 template <typename _Tp> 2216 const _Tp* 2217 function<_Rp(_A0)>::target() const 2218 { 2219 if (__f_ == 0) 2220 return (const _Tp*)0; 2221 return (const _Tp*)__f_->target(typeid(_Tp)); 2222 } 2223 2224 #endif // _LIBCPP_NO_RTTI 2225 2226 template<class _Rp, class _A0, class _A1> 2227 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1)> 2228 : public binary_function<_A0, _A1, _Rp> 2229 { 2230 typedef __function::__base<_Rp(_A0, _A1)> __base; 2231 aligned_storage<3*sizeof(void*)>::type __buf_; 2232 __base* __f_; 2233 2234 public: 2235 typedef _Rp result_type; 2236 2237 // 20.7.16.2.1, construct/copy/destroy: 2238 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} 2239 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} 2240 function(const function&); 2241 template<class _Fp> 2242 function(_Fp, 2243 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 2244 2245 template<class _Alloc> 2246 _LIBCPP_INLINE_VISIBILITY 2247 function(allocator_arg_t, const _Alloc&) : __f_(0) {} 2248 template<class _Alloc> 2249 _LIBCPP_INLINE_VISIBILITY 2250 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} 2251 template<class _Alloc> 2252 function(allocator_arg_t, const _Alloc&, const function&); 2253 template<class _Fp, class _Alloc> 2254 function(allocator_arg_t, const _Alloc& __a, _Fp __f, 2255 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 2256 2257 function& operator=(const function&); 2258 function& operator=(nullptr_t); 2259 template<class _Fp> 2260 typename enable_if 2261 < 2262 !is_integral<_Fp>::value, 2263 function& 2264 >::type 2265 operator=(_Fp); 2266 2267 ~function(); 2268 2269 // 20.7.16.2.2, function modifiers: 2270 void swap(function&); 2271 template<class _Fp, class _Alloc> 2272 _LIBCPP_INLINE_VISIBILITY 2273 void assign(_Fp __f, const _Alloc& __a) 2274 {function(allocator_arg, __a, __f).swap(*this);} 2275 2276 // 20.7.16.2.3, function capacity: 2277 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;} 2278 2279 template<class _R2, class _B0, class _B1> 2280 bool operator==(const function<_R2(_B0, _B1)>&) const = delete; 2281 template<class _R2, class _B0, class _B1> 2282 bool operator!=(const function<_R2(_B0, _B1)>&) const = delete; 2283 2284 // 20.7.16.2.4, function invocation: 2285 _Rp operator()(_A0, _A1) const; 2286 2287 #ifndef _LIBCPP_NO_RTTI 2288 // 20.7.16.2.5, function target access: 2289 const std::type_info& target_type() const; 2290 template <typename _Tp> _Tp* target(); 2291 template <typename _Tp> const _Tp* target() const; 2292 #endif // _LIBCPP_NO_RTTI 2293 }; 2294 2295 template<class _Rp, class _A0, class _A1> 2296 function<_Rp(_A0, _A1)>::function(const function& __f) 2297 { 2298 if (__f.__f_ == 0) 2299 __f_ = 0; 2300 else if (__f.__f_ == (const __base*)&__f.__buf_) 2301 { 2302 __f_ = (__base*)&__buf_; 2303 __f.__f_->__clone(__f_); 2304 } 2305 else 2306 __f_ = __f.__f_->__clone(); 2307 } 2308 2309 template<class _Rp, class _A0, class _A1> 2310 template<class _Alloc> 2311 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f) 2312 { 2313 if (__f.__f_ == 0) 2314 __f_ = 0; 2315 else if (__f.__f_ == (const __base*)&__f.__buf_) 2316 { 2317 __f_ = (__base*)&__buf_; 2318 __f.__f_->__clone(__f_); 2319 } 2320 else 2321 __f_ = __f.__f_->__clone(); 2322 } 2323 2324 template<class _Rp, class _A0, class _A1> 2325 template <class _Fp> 2326 function<_Rp(_A0, _A1)>::function(_Fp __f, 2327 typename enable_if<!is_integral<_Fp>::value>::type*) 2328 : __f_(0) 2329 { 2330 if (__function::__not_null(__f)) 2331 { 2332 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF; 2333 if (sizeof(_FF) <= sizeof(__buf_)) 2334 { 2335 __f_ = (__base*)&__buf_; 2336 ::new ((void*)__f_) _FF(__f); 2337 } 2338 else 2339 { 2340 typedef allocator<_FF> _Ap; 2341 _Ap __a; 2342 typedef __allocator_destructor<_Ap> _Dp; 2343 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2344 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a)); 2345 __f_ = __hold.release(); 2346 } 2347 } 2348 } 2349 2350 template<class _Rp, class _A0, class _A1> 2351 template <class _Fp, class _Alloc> 2352 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f, 2353 typename enable_if<!is_integral<_Fp>::value>::type*) 2354 : __f_(0) 2355 { 2356 typedef allocator_traits<_Alloc> __alloc_traits; 2357 if (__function::__not_null(__f)) 2358 { 2359 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF; 2360 if (sizeof(_FF) <= sizeof(__buf_)) 2361 { 2362 __f_ = (__base*)&__buf_; 2363 ::new ((void*)__f_) _FF(__f, __a0); 2364 } 2365 else 2366 { 2367 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap; 2368 _Ap __a(__a0); 2369 typedef __allocator_destructor<_Ap> _Dp; 2370 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2371 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a)); 2372 __f_ = __hold.release(); 2373 } 2374 } 2375 } 2376 2377 template<class _Rp, class _A0, class _A1> 2378 function<_Rp(_A0, _A1)>& 2379 function<_Rp(_A0, _A1)>::operator=(const function& __f) 2380 { 2381 if (__f) 2382 function(__f).swap(*this); 2383 else 2384 *this = nullptr; 2385 return *this; 2386 } 2387 2388 template<class _Rp, class _A0, class _A1> 2389 function<_Rp(_A0, _A1)>& 2390 function<_Rp(_A0, _A1)>::operator=(nullptr_t) 2391 { 2392 __base* __t = __f_; 2393 __f_ = 0; 2394 if (__t == (__base*)&__buf_) 2395 __t->destroy(); 2396 else if (__t) 2397 __t->destroy_deallocate(); 2398 return *this; 2399 } 2400 2401 template<class _Rp, class _A0, class _A1> 2402 template <class _Fp> 2403 typename enable_if 2404 < 2405 !is_integral<_Fp>::value, 2406 function<_Rp(_A0, _A1)>& 2407 >::type 2408 function<_Rp(_A0, _A1)>::operator=(_Fp __f) 2409 { 2410 function(_VSTD::move(__f)).swap(*this); 2411 return *this; 2412 } 2413 2414 template<class _Rp, class _A0, class _A1> 2415 function<_Rp(_A0, _A1)>::~function() 2416 { 2417 if (__f_ == (__base*)&__buf_) 2418 __f_->destroy(); 2419 else if (__f_) 2420 __f_->destroy_deallocate(); 2421 } 2422 2423 template<class _Rp, class _A0, class _A1> 2424 void 2425 function<_Rp(_A0, _A1)>::swap(function& __f) 2426 { 2427 if (_VSTD::addressof(__f) == this) 2428 return; 2429 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) 2430 { 2431 typename aligned_storage<sizeof(__buf_)>::type __tempbuf; 2432 __base* __t = (__base*)&__tempbuf; 2433 __f_->__clone(__t); 2434 __f_->destroy(); 2435 __f_ = 0; 2436 __f.__f_->__clone((__base*)&__buf_); 2437 __f.__f_->destroy(); 2438 __f.__f_ = 0; 2439 __f_ = (__base*)&__buf_; 2440 __t->__clone((__base*)&__f.__buf_); 2441 __t->destroy(); 2442 __f.__f_ = (__base*)&__f.__buf_; 2443 } 2444 else if (__f_ == (__base*)&__buf_) 2445 { 2446 __f_->__clone((__base*)&__f.__buf_); 2447 __f_->destroy(); 2448 __f_ = __f.__f_; 2449 __f.__f_ = (__base*)&__f.__buf_; 2450 } 2451 else if (__f.__f_ == (__base*)&__f.__buf_) 2452 { 2453 __f.__f_->__clone((__base*)&__buf_); 2454 __f.__f_->destroy(); 2455 __f.__f_ = __f_; 2456 __f_ = (__base*)&__buf_; 2457 } 2458 else 2459 _VSTD::swap(__f_, __f.__f_); 2460 } 2461 2462 template<class _Rp, class _A0, class _A1> 2463 _Rp 2464 function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const 2465 { 2466 if (__f_ == 0) 2467 __throw_bad_function_call(); 2468 return (*__f_)(__a0, __a1); 2469 } 2470 2471 #ifndef _LIBCPP_NO_RTTI 2472 2473 template<class _Rp, class _A0, class _A1> 2474 const std::type_info& 2475 function<_Rp(_A0, _A1)>::target_type() const 2476 { 2477 if (__f_ == 0) 2478 return typeid(void); 2479 return __f_->target_type(); 2480 } 2481 2482 template<class _Rp, class _A0, class _A1> 2483 template <typename _Tp> 2484 _Tp* 2485 function<_Rp(_A0, _A1)>::target() 2486 { 2487 if (__f_ == 0) 2488 return (_Tp*)0; 2489 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp))); 2490 } 2491 2492 template<class _Rp, class _A0, class _A1> 2493 template <typename _Tp> 2494 const _Tp* 2495 function<_Rp(_A0, _A1)>::target() const 2496 { 2497 if (__f_ == 0) 2498 return (const _Tp*)0; 2499 return (const _Tp*)__f_->target(typeid(_Tp)); 2500 } 2501 2502 #endif // _LIBCPP_NO_RTTI 2503 2504 template<class _Rp, class _A0, class _A1, class _A2> 2505 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1, _A2)> 2506 { 2507 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base; 2508 aligned_storage<3*sizeof(void*)>::type __buf_; 2509 __base* __f_; 2510 2511 public: 2512 typedef _Rp result_type; 2513 2514 // 20.7.16.2.1, construct/copy/destroy: 2515 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} 2516 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} 2517 function(const function&); 2518 template<class _Fp> 2519 function(_Fp, 2520 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 2521 2522 template<class _Alloc> 2523 _LIBCPP_INLINE_VISIBILITY 2524 function(allocator_arg_t, const _Alloc&) : __f_(0) {} 2525 template<class _Alloc> 2526 _LIBCPP_INLINE_VISIBILITY 2527 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} 2528 template<class _Alloc> 2529 function(allocator_arg_t, const _Alloc&, const function&); 2530 template<class _Fp, class _Alloc> 2531 function(allocator_arg_t, const _Alloc& __a, _Fp __f, 2532 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 2533 2534 function& operator=(const function&); 2535 function& operator=(nullptr_t); 2536 template<class _Fp> 2537 typename enable_if 2538 < 2539 !is_integral<_Fp>::value, 2540 function& 2541 >::type 2542 operator=(_Fp); 2543 2544 ~function(); 2545 2546 // 20.7.16.2.2, function modifiers: 2547 void swap(function&); 2548 template<class _Fp, class _Alloc> 2549 _LIBCPP_INLINE_VISIBILITY 2550 void assign(_Fp __f, const _Alloc& __a) 2551 {function(allocator_arg, __a, __f).swap(*this);} 2552 2553 // 20.7.16.2.3, function capacity: 2554 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;} 2555 2556 template<class _R2, class _B0, class _B1, class _B2> 2557 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const = delete; 2558 template<class _R2, class _B0, class _B1, class _B2> 2559 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const = delete; 2560 2561 // 20.7.16.2.4, function invocation: 2562 _Rp operator()(_A0, _A1, _A2) const; 2563 2564 #ifndef _LIBCPP_NO_RTTI 2565 // 20.7.16.2.5, function target access: 2566 const std::type_info& target_type() const; 2567 template <typename _Tp> _Tp* target(); 2568 template <typename _Tp> const _Tp* target() const; 2569 #endif // _LIBCPP_NO_RTTI 2570 }; 2571 2572 template<class _Rp, class _A0, class _A1, class _A2> 2573 function<_Rp(_A0, _A1, _A2)>::function(const function& __f) 2574 { 2575 if (__f.__f_ == 0) 2576 __f_ = 0; 2577 else if (__f.__f_ == (const __base*)&__f.__buf_) 2578 { 2579 __f_ = (__base*)&__buf_; 2580 __f.__f_->__clone(__f_); 2581 } 2582 else 2583 __f_ = __f.__f_->__clone(); 2584 } 2585 2586 template<class _Rp, class _A0, class _A1, class _A2> 2587 template<class _Alloc> 2588 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&, 2589 const function& __f) 2590 { 2591 if (__f.__f_ == 0) 2592 __f_ = 0; 2593 else if (__f.__f_ == (const __base*)&__f.__buf_) 2594 { 2595 __f_ = (__base*)&__buf_; 2596 __f.__f_->__clone(__f_); 2597 } 2598 else 2599 __f_ = __f.__f_->__clone(); 2600 } 2601 2602 template<class _Rp, class _A0, class _A1, class _A2> 2603 template <class _Fp> 2604 function<_Rp(_A0, _A1, _A2)>::function(_Fp __f, 2605 typename enable_if<!is_integral<_Fp>::value>::type*) 2606 : __f_(0) 2607 { 2608 if (__function::__not_null(__f)) 2609 { 2610 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF; 2611 if (sizeof(_FF) <= sizeof(__buf_)) 2612 { 2613 __f_ = (__base*)&__buf_; 2614 ::new ((void*)__f_) _FF(__f); 2615 } 2616 else 2617 { 2618 typedef allocator<_FF> _Ap; 2619 _Ap __a; 2620 typedef __allocator_destructor<_Ap> _Dp; 2621 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2622 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a)); 2623 __f_ = __hold.release(); 2624 } 2625 } 2626 } 2627 2628 template<class _Rp, class _A0, class _A1, class _A2> 2629 template <class _Fp, class _Alloc> 2630 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f, 2631 typename enable_if<!is_integral<_Fp>::value>::type*) 2632 : __f_(0) 2633 { 2634 typedef allocator_traits<_Alloc> __alloc_traits; 2635 if (__function::__not_null(__f)) 2636 { 2637 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF; 2638 if (sizeof(_FF) <= sizeof(__buf_)) 2639 { 2640 __f_ = (__base*)&__buf_; 2641 ::new ((void*)__f_) _FF(__f, __a0); 2642 } 2643 else 2644 { 2645 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap; 2646 _Ap __a(__a0); 2647 typedef __allocator_destructor<_Ap> _Dp; 2648 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2649 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a)); 2650 __f_ = __hold.release(); 2651 } 2652 } 2653 } 2654 2655 template<class _Rp, class _A0, class _A1, class _A2> 2656 function<_Rp(_A0, _A1, _A2)>& 2657 function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f) 2658 { 2659 if (__f) 2660 function(__f).swap(*this); 2661 else 2662 *this = nullptr; 2663 return *this; 2664 } 2665 2666 template<class _Rp, class _A0, class _A1, class _A2> 2667 function<_Rp(_A0, _A1, _A2)>& 2668 function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t) 2669 { 2670 __base* __t = __f_; 2671 __f_ = 0; 2672 if (__t == (__base*)&__buf_) 2673 __t->destroy(); 2674 else if (__t) 2675 __t->destroy_deallocate(); 2676 return *this; 2677 } 2678 2679 template<class _Rp, class _A0, class _A1, class _A2> 2680 template <class _Fp> 2681 typename enable_if 2682 < 2683 !is_integral<_Fp>::value, 2684 function<_Rp(_A0, _A1, _A2)>& 2685 >::type 2686 function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f) 2687 { 2688 function(_VSTD::move(__f)).swap(*this); 2689 return *this; 2690 } 2691 2692 template<class _Rp, class _A0, class _A1, class _A2> 2693 function<_Rp(_A0, _A1, _A2)>::~function() 2694 { 2695 if (__f_ == (__base*)&__buf_) 2696 __f_->destroy(); 2697 else if (__f_) 2698 __f_->destroy_deallocate(); 2699 } 2700 2701 template<class _Rp, class _A0, class _A1, class _A2> 2702 void 2703 function<_Rp(_A0, _A1, _A2)>::swap(function& __f) 2704 { 2705 if (_VSTD::addressof(__f) == this) 2706 return; 2707 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) 2708 { 2709 typename aligned_storage<sizeof(__buf_)>::type __tempbuf; 2710 __base* __t = (__base*)&__tempbuf; 2711 __f_->__clone(__t); 2712 __f_->destroy(); 2713 __f_ = 0; 2714 __f.__f_->__clone((__base*)&__buf_); 2715 __f.__f_->destroy(); 2716 __f.__f_ = 0; 2717 __f_ = (__base*)&__buf_; 2718 __t->__clone((__base*)&__f.__buf_); 2719 __t->destroy(); 2720 __f.__f_ = (__base*)&__f.__buf_; 2721 } 2722 else if (__f_ == (__base*)&__buf_) 2723 { 2724 __f_->__clone((__base*)&__f.__buf_); 2725 __f_->destroy(); 2726 __f_ = __f.__f_; 2727 __f.__f_ = (__base*)&__f.__buf_; 2728 } 2729 else if (__f.__f_ == (__base*)&__f.__buf_) 2730 { 2731 __f.__f_->__clone((__base*)&__buf_); 2732 __f.__f_->destroy(); 2733 __f.__f_ = __f_; 2734 __f_ = (__base*)&__buf_; 2735 } 2736 else 2737 _VSTD::swap(__f_, __f.__f_); 2738 } 2739 2740 template<class _Rp, class _A0, class _A1, class _A2> 2741 _Rp 2742 function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const 2743 { 2744 if (__f_ == 0) 2745 __throw_bad_function_call(); 2746 return (*__f_)(__a0, __a1, __a2); 2747 } 2748 2749 #ifndef _LIBCPP_NO_RTTI 2750 2751 template<class _Rp, class _A0, class _A1, class _A2> 2752 const std::type_info& 2753 function<_Rp(_A0, _A1, _A2)>::target_type() const 2754 { 2755 if (__f_ == 0) 2756 return typeid(void); 2757 return __f_->target_type(); 2758 } 2759 2760 template<class _Rp, class _A0, class _A1, class _A2> 2761 template <typename _Tp> 2762 _Tp* 2763 function<_Rp(_A0, _A1, _A2)>::target() 2764 { 2765 if (__f_ == 0) 2766 return (_Tp*)0; 2767 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp))); 2768 } 2769 2770 template<class _Rp, class _A0, class _A1, class _A2> 2771 template <typename _Tp> 2772 const _Tp* 2773 function<_Rp(_A0, _A1, _A2)>::target() const 2774 { 2775 if (__f_ == 0) 2776 return (const _Tp*)0; 2777 return (const _Tp*)__f_->target(typeid(_Tp)); 2778 } 2779 2780 #endif // _LIBCPP_NO_RTTI 2781 2782 template <class _Fp> 2783 inline _LIBCPP_INLINE_VISIBILITY 2784 bool 2785 operator==(const function<_Fp>& __f, nullptr_t) {return !__f;} 2786 2787 template <class _Fp> 2788 inline _LIBCPP_INLINE_VISIBILITY 2789 bool 2790 operator==(nullptr_t, const function<_Fp>& __f) {return !__f;} 2791 2792 template <class _Fp> 2793 inline _LIBCPP_INLINE_VISIBILITY 2794 bool 2795 operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;} 2796 2797 template <class _Fp> 2798 inline _LIBCPP_INLINE_VISIBILITY 2799 bool 2800 operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;} 2801 2802 template <class _Fp> 2803 inline _LIBCPP_INLINE_VISIBILITY 2804 void 2805 swap(function<_Fp>& __x, function<_Fp>& __y) 2806 {return __x.swap(__y);} 2807 2808 #endif 2809 2810 _LIBCPP_END_NAMESPACE_STD 2811 2812 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H 2813