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