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