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