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 } // namespace __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 template<class _R2> 1721 bool operator==(const function<_R2()>&) const = delete; 1722 template<class _R2> 1723 bool operator!=(const function<_R2()>&) const = delete; 1724 1725 // 20.7.16.2.4, function invocation: 1726 _Rp operator()() const; 1727 1728 #ifndef _LIBCPP_NO_RTTI 1729 // 20.7.16.2.5, function target access: 1730 const std::type_info& target_type() const; 1731 template <typename _Tp> _Tp* target(); 1732 template <typename _Tp> const _Tp* target() const; 1733 #endif // _LIBCPP_NO_RTTI 1734 }; 1735 1736 template<class _Rp> 1737 function<_Rp()>::function(const function& __f) 1738 { 1739 if (__f.__f_ == 0) 1740 __f_ = 0; 1741 else if (__f.__f_ == (const __base*)&__f.__buf_) 1742 { 1743 __f_ = (__base*)&__buf_; 1744 __f.__f_->__clone(__f_); 1745 } 1746 else 1747 __f_ = __f.__f_->__clone(); 1748 } 1749 1750 template<class _Rp> 1751 template<class _Alloc> 1752 function<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f) 1753 { 1754 if (__f.__f_ == 0) 1755 __f_ = 0; 1756 else if (__f.__f_ == (const __base*)&__f.__buf_) 1757 { 1758 __f_ = (__base*)&__buf_; 1759 __f.__f_->__clone(__f_); 1760 } 1761 else 1762 __f_ = __f.__f_->__clone(); 1763 } 1764 1765 template<class _Rp> 1766 template <class _Fp> 1767 function<_Rp()>::function(_Fp __f, 1768 typename enable_if<!is_integral<_Fp>::value>::type*) 1769 : __f_(0) 1770 { 1771 if (__function::__not_null(__f)) 1772 { 1773 typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF; 1774 if (sizeof(_FF) <= sizeof(__buf_)) 1775 { 1776 __f_ = (__base*)&__buf_; 1777 ::new ((void*)__f_) _FF(__f); 1778 } 1779 else 1780 { 1781 typedef allocator<_FF> _Ap; 1782 _Ap __a; 1783 typedef __allocator_destructor<_Ap> _Dp; 1784 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 1785 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a)); 1786 __f_ = __hold.release(); 1787 } 1788 } 1789 } 1790 1791 template<class _Rp> 1792 template <class _Fp, class _Alloc> 1793 function<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f, 1794 typename enable_if<!is_integral<_Fp>::value>::type*) 1795 : __f_(0) 1796 { 1797 typedef allocator_traits<_Alloc> __alloc_traits; 1798 if (__function::__not_null(__f)) 1799 { 1800 typedef __function::__func<_Fp, _Alloc, _Rp()> _FF; 1801 if (sizeof(_FF) <= sizeof(__buf_)) 1802 { 1803 __f_ = (__base*)&__buf_; 1804 ::new ((void*)__f_) _FF(__f, __a0); 1805 } 1806 else 1807 { 1808 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap; 1809 _Ap __a(__a0); 1810 typedef __allocator_destructor<_Ap> _Dp; 1811 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 1812 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a)); 1813 __f_ = __hold.release(); 1814 } 1815 } 1816 } 1817 1818 template<class _Rp> 1819 function<_Rp()>& 1820 function<_Rp()>::operator=(const function& __f) 1821 { 1822 if (__f) 1823 function(__f).swap(*this); 1824 else 1825 *this = nullptr; 1826 return *this; 1827 } 1828 1829 template<class _Rp> 1830 function<_Rp()>& 1831 function<_Rp()>::operator=(nullptr_t) 1832 { 1833 __base* __t = __f_; 1834 __f_ = 0; 1835 if (__t == (__base*)&__buf_) 1836 __t->destroy(); 1837 else if (__t) 1838 __t->destroy_deallocate(); 1839 return *this; 1840 } 1841 1842 template<class _Rp> 1843 template <class _Fp> 1844 typename enable_if 1845 < 1846 !is_integral<_Fp>::value, 1847 function<_Rp()>& 1848 >::type 1849 function<_Rp()>::operator=(_Fp __f) 1850 { 1851 function(_VSTD::move(__f)).swap(*this); 1852 return *this; 1853 } 1854 1855 template<class _Rp> 1856 function<_Rp()>::~function() 1857 { 1858 if (__f_ == (__base*)&__buf_) 1859 __f_->destroy(); 1860 else if (__f_) 1861 __f_->destroy_deallocate(); 1862 } 1863 1864 template<class _Rp> 1865 void 1866 function<_Rp()>::swap(function& __f) 1867 { 1868 if (_VSTD::addressof(__f) == this) 1869 return; 1870 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) 1871 { 1872 typename aligned_storage<sizeof(__buf_)>::type __tempbuf; 1873 __base* __t = (__base*)&__tempbuf; 1874 __f_->__clone(__t); 1875 __f_->destroy(); 1876 __f_ = 0; 1877 __f.__f_->__clone((__base*)&__buf_); 1878 __f.__f_->destroy(); 1879 __f.__f_ = 0; 1880 __f_ = (__base*)&__buf_; 1881 __t->__clone((__base*)&__f.__buf_); 1882 __t->destroy(); 1883 __f.__f_ = (__base*)&__f.__buf_; 1884 } 1885 else if (__f_ == (__base*)&__buf_) 1886 { 1887 __f_->__clone((__base*)&__f.__buf_); 1888 __f_->destroy(); 1889 __f_ = __f.__f_; 1890 __f.__f_ = (__base*)&__f.__buf_; 1891 } 1892 else if (__f.__f_ == (__base*)&__f.__buf_) 1893 { 1894 __f.__f_->__clone((__base*)&__buf_); 1895 __f.__f_->destroy(); 1896 __f.__f_ = __f_; 1897 __f_ = (__base*)&__buf_; 1898 } 1899 else 1900 _VSTD::swap(__f_, __f.__f_); 1901 } 1902 1903 template<class _Rp> 1904 _Rp 1905 function<_Rp()>::operator()() const 1906 { 1907 if (__f_ == 0) 1908 __throw_bad_function_call(); 1909 return (*__f_)(); 1910 } 1911 1912 #ifndef _LIBCPP_NO_RTTI 1913 1914 template<class _Rp> 1915 const std::type_info& 1916 function<_Rp()>::target_type() const 1917 { 1918 if (__f_ == 0) 1919 return typeid(void); 1920 return __f_->target_type(); 1921 } 1922 1923 template<class _Rp> 1924 template <typename _Tp> 1925 _Tp* 1926 function<_Rp()>::target() 1927 { 1928 if (__f_ == 0) 1929 return (_Tp*)0; 1930 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp))); 1931 } 1932 1933 template<class _Rp> 1934 template <typename _Tp> 1935 const _Tp* 1936 function<_Rp()>::target() const 1937 { 1938 if (__f_ == 0) 1939 return (const _Tp*)0; 1940 return (const _Tp*)__f_->target(typeid(_Tp)); 1941 } 1942 1943 #endif // _LIBCPP_NO_RTTI 1944 1945 template<class _Rp, class _A0> 1946 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0)> 1947 : public unary_function<_A0, _Rp> 1948 { 1949 typedef __function::__base<_Rp(_A0)> __base; 1950 aligned_storage<3*sizeof(void*)>::type __buf_; 1951 __base* __f_; 1952 1953 public: 1954 typedef _Rp result_type; 1955 1956 // 20.7.16.2.1, construct/copy/destroy: 1957 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} 1958 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} 1959 function(const function&); 1960 template<class _Fp> 1961 function(_Fp, 1962 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 1963 1964 template<class _Alloc> 1965 _LIBCPP_INLINE_VISIBILITY 1966 function(allocator_arg_t, const _Alloc&) : __f_(0) {} 1967 template<class _Alloc> 1968 _LIBCPP_INLINE_VISIBILITY 1969 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} 1970 template<class _Alloc> 1971 function(allocator_arg_t, const _Alloc&, const function&); 1972 template<class _Fp, class _Alloc> 1973 function(allocator_arg_t, const _Alloc& __a, _Fp __f, 1974 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 1975 1976 function& operator=(const function&); 1977 function& operator=(nullptr_t); 1978 template<class _Fp> 1979 typename enable_if 1980 < 1981 !is_integral<_Fp>::value, 1982 function& 1983 >::type 1984 operator=(_Fp); 1985 1986 ~function(); 1987 1988 // 20.7.16.2.2, function modifiers: 1989 void swap(function&); 1990 template<class _Fp, class _Alloc> 1991 _LIBCPP_INLINE_VISIBILITY 1992 void assign(_Fp __f, const _Alloc& __a) 1993 {function(allocator_arg, __a, __f).swap(*this);} 1994 1995 // 20.7.16.2.3, function capacity: 1996 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;} 1997 1998 template<class _R2, class _B0> 1999 bool operator==(const function<_R2(_B0)>&) const = delete; 2000 template<class _R2, class _B0> 2001 bool operator!=(const function<_R2(_B0)>&) const = delete; 2002 2003 // 20.7.16.2.4, function invocation: 2004 _Rp operator()(_A0) const; 2005 2006 #ifndef _LIBCPP_NO_RTTI 2007 // 20.7.16.2.5, function target access: 2008 const std::type_info& target_type() const; 2009 template <typename _Tp> _Tp* target(); 2010 template <typename _Tp> const _Tp* target() const; 2011 #endif // _LIBCPP_NO_RTTI 2012 }; 2013 2014 template<class _Rp, class _A0> 2015 function<_Rp(_A0)>::function(const function& __f) 2016 { 2017 if (__f.__f_ == 0) 2018 __f_ = 0; 2019 else if (__f.__f_ == (const __base*)&__f.__buf_) 2020 { 2021 __f_ = (__base*)&__buf_; 2022 __f.__f_->__clone(__f_); 2023 } 2024 else 2025 __f_ = __f.__f_->__clone(); 2026 } 2027 2028 template<class _Rp, class _A0> 2029 template<class _Alloc> 2030 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f) 2031 { 2032 if (__f.__f_ == 0) 2033 __f_ = 0; 2034 else if (__f.__f_ == (const __base*)&__f.__buf_) 2035 { 2036 __f_ = (__base*)&__buf_; 2037 __f.__f_->__clone(__f_); 2038 } 2039 else 2040 __f_ = __f.__f_->__clone(); 2041 } 2042 2043 template<class _Rp, class _A0> 2044 template <class _Fp> 2045 function<_Rp(_A0)>::function(_Fp __f, 2046 typename enable_if<!is_integral<_Fp>::value>::type*) 2047 : __f_(0) 2048 { 2049 if (__function::__not_null(__f)) 2050 { 2051 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF; 2052 if (sizeof(_FF) <= sizeof(__buf_)) 2053 { 2054 __f_ = (__base*)&__buf_; 2055 ::new ((void*)__f_) _FF(__f); 2056 } 2057 else 2058 { 2059 typedef allocator<_FF> _Ap; 2060 _Ap __a; 2061 typedef __allocator_destructor<_Ap> _Dp; 2062 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2063 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a)); 2064 __f_ = __hold.release(); 2065 } 2066 } 2067 } 2068 2069 template<class _Rp, class _A0> 2070 template <class _Fp, class _Alloc> 2071 function<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f, 2072 typename enable_if<!is_integral<_Fp>::value>::type*) 2073 : __f_(0) 2074 { 2075 typedef allocator_traits<_Alloc> __alloc_traits; 2076 if (__function::__not_null(__f)) 2077 { 2078 typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF; 2079 if (sizeof(_FF) <= sizeof(__buf_)) 2080 { 2081 __f_ = (__base*)&__buf_; 2082 ::new ((void*)__f_) _FF(__f, __a0); 2083 } 2084 else 2085 { 2086 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap; 2087 _Ap __a(__a0); 2088 typedef __allocator_destructor<_Ap> _Dp; 2089 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2090 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a)); 2091 __f_ = __hold.release(); 2092 } 2093 } 2094 } 2095 2096 template<class _Rp, class _A0> 2097 function<_Rp(_A0)>& 2098 function<_Rp(_A0)>::operator=(const function& __f) 2099 { 2100 if (__f) 2101 function(__f).swap(*this); 2102 else 2103 *this = nullptr; 2104 return *this; 2105 } 2106 2107 template<class _Rp, class _A0> 2108 function<_Rp(_A0)>& 2109 function<_Rp(_A0)>::operator=(nullptr_t) 2110 { 2111 __base* __t = __f_; 2112 __f_ = 0; 2113 if (__t == (__base*)&__buf_) 2114 __t->destroy(); 2115 else if (__t) 2116 __t->destroy_deallocate(); 2117 return *this; 2118 } 2119 2120 template<class _Rp, class _A0> 2121 template <class _Fp> 2122 typename enable_if 2123 < 2124 !is_integral<_Fp>::value, 2125 function<_Rp(_A0)>& 2126 >::type 2127 function<_Rp(_A0)>::operator=(_Fp __f) 2128 { 2129 function(_VSTD::move(__f)).swap(*this); 2130 return *this; 2131 } 2132 2133 template<class _Rp, class _A0> 2134 function<_Rp(_A0)>::~function() 2135 { 2136 if (__f_ == (__base*)&__buf_) 2137 __f_->destroy(); 2138 else if (__f_) 2139 __f_->destroy_deallocate(); 2140 } 2141 2142 template<class _Rp, class _A0> 2143 void 2144 function<_Rp(_A0)>::swap(function& __f) 2145 { 2146 if (_VSTD::addressof(__f) == this) 2147 return; 2148 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) 2149 { 2150 typename aligned_storage<sizeof(__buf_)>::type __tempbuf; 2151 __base* __t = (__base*)&__tempbuf; 2152 __f_->__clone(__t); 2153 __f_->destroy(); 2154 __f_ = 0; 2155 __f.__f_->__clone((__base*)&__buf_); 2156 __f.__f_->destroy(); 2157 __f.__f_ = 0; 2158 __f_ = (__base*)&__buf_; 2159 __t->__clone((__base*)&__f.__buf_); 2160 __t->destroy(); 2161 __f.__f_ = (__base*)&__f.__buf_; 2162 } 2163 else if (__f_ == (__base*)&__buf_) 2164 { 2165 __f_->__clone((__base*)&__f.__buf_); 2166 __f_->destroy(); 2167 __f_ = __f.__f_; 2168 __f.__f_ = (__base*)&__f.__buf_; 2169 } 2170 else if (__f.__f_ == (__base*)&__f.__buf_) 2171 { 2172 __f.__f_->__clone((__base*)&__buf_); 2173 __f.__f_->destroy(); 2174 __f.__f_ = __f_; 2175 __f_ = (__base*)&__buf_; 2176 } 2177 else 2178 _VSTD::swap(__f_, __f.__f_); 2179 } 2180 2181 template<class _Rp, class _A0> 2182 _Rp 2183 function<_Rp(_A0)>::operator()(_A0 __a0) const 2184 { 2185 if (__f_ == 0) 2186 __throw_bad_function_call(); 2187 return (*__f_)(__a0); 2188 } 2189 2190 #ifndef _LIBCPP_NO_RTTI 2191 2192 template<class _Rp, class _A0> 2193 const std::type_info& 2194 function<_Rp(_A0)>::target_type() const 2195 { 2196 if (__f_ == 0) 2197 return typeid(void); 2198 return __f_->target_type(); 2199 } 2200 2201 template<class _Rp, class _A0> 2202 template <typename _Tp> 2203 _Tp* 2204 function<_Rp(_A0)>::target() 2205 { 2206 if (__f_ == 0) 2207 return (_Tp*)0; 2208 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp))); 2209 } 2210 2211 template<class _Rp, class _A0> 2212 template <typename _Tp> 2213 const _Tp* 2214 function<_Rp(_A0)>::target() const 2215 { 2216 if (__f_ == 0) 2217 return (const _Tp*)0; 2218 return (const _Tp*)__f_->target(typeid(_Tp)); 2219 } 2220 2221 #endif // _LIBCPP_NO_RTTI 2222 2223 template<class _Rp, class _A0, class _A1> 2224 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1)> 2225 : public binary_function<_A0, _A1, _Rp> 2226 { 2227 typedef __function::__base<_Rp(_A0, _A1)> __base; 2228 aligned_storage<3*sizeof(void*)>::type __buf_; 2229 __base* __f_; 2230 2231 public: 2232 typedef _Rp result_type; 2233 2234 // 20.7.16.2.1, construct/copy/destroy: 2235 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} 2236 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} 2237 function(const function&); 2238 template<class _Fp> 2239 function(_Fp, 2240 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 2241 2242 template<class _Alloc> 2243 _LIBCPP_INLINE_VISIBILITY 2244 function(allocator_arg_t, const _Alloc&) : __f_(0) {} 2245 template<class _Alloc> 2246 _LIBCPP_INLINE_VISIBILITY 2247 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} 2248 template<class _Alloc> 2249 function(allocator_arg_t, const _Alloc&, const function&); 2250 template<class _Fp, class _Alloc> 2251 function(allocator_arg_t, const _Alloc& __a, _Fp __f, 2252 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 2253 2254 function& operator=(const function&); 2255 function& operator=(nullptr_t); 2256 template<class _Fp> 2257 typename enable_if 2258 < 2259 !is_integral<_Fp>::value, 2260 function& 2261 >::type 2262 operator=(_Fp); 2263 2264 ~function(); 2265 2266 // 20.7.16.2.2, function modifiers: 2267 void swap(function&); 2268 template<class _Fp, class _Alloc> 2269 _LIBCPP_INLINE_VISIBILITY 2270 void assign(_Fp __f, const _Alloc& __a) 2271 {function(allocator_arg, __a, __f).swap(*this);} 2272 2273 // 20.7.16.2.3, function capacity: 2274 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;} 2275 2276 template<class _R2, class _B0, class _B1> 2277 bool operator==(const function<_R2(_B0, _B1)>&) const = delete; 2278 template<class _R2, class _B0, class _B1> 2279 bool operator!=(const function<_R2(_B0, _B1)>&) const = delete; 2280 2281 // 20.7.16.2.4, function invocation: 2282 _Rp operator()(_A0, _A1) const; 2283 2284 #ifndef _LIBCPP_NO_RTTI 2285 // 20.7.16.2.5, function target access: 2286 const std::type_info& target_type() const; 2287 template <typename _Tp> _Tp* target(); 2288 template <typename _Tp> const _Tp* target() const; 2289 #endif // _LIBCPP_NO_RTTI 2290 }; 2291 2292 template<class _Rp, class _A0, class _A1> 2293 function<_Rp(_A0, _A1)>::function(const function& __f) 2294 { 2295 if (__f.__f_ == 0) 2296 __f_ = 0; 2297 else if (__f.__f_ == (const __base*)&__f.__buf_) 2298 { 2299 __f_ = (__base*)&__buf_; 2300 __f.__f_->__clone(__f_); 2301 } 2302 else 2303 __f_ = __f.__f_->__clone(); 2304 } 2305 2306 template<class _Rp, class _A0, class _A1> 2307 template<class _Alloc> 2308 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f) 2309 { 2310 if (__f.__f_ == 0) 2311 __f_ = 0; 2312 else if (__f.__f_ == (const __base*)&__f.__buf_) 2313 { 2314 __f_ = (__base*)&__buf_; 2315 __f.__f_->__clone(__f_); 2316 } 2317 else 2318 __f_ = __f.__f_->__clone(); 2319 } 2320 2321 template<class _Rp, class _A0, class _A1> 2322 template <class _Fp> 2323 function<_Rp(_A0, _A1)>::function(_Fp __f, 2324 typename enable_if<!is_integral<_Fp>::value>::type*) 2325 : __f_(0) 2326 { 2327 if (__function::__not_null(__f)) 2328 { 2329 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF; 2330 if (sizeof(_FF) <= sizeof(__buf_)) 2331 { 2332 __f_ = (__base*)&__buf_; 2333 ::new ((void*)__f_) _FF(__f); 2334 } 2335 else 2336 { 2337 typedef allocator<_FF> _Ap; 2338 _Ap __a; 2339 typedef __allocator_destructor<_Ap> _Dp; 2340 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2341 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a)); 2342 __f_ = __hold.release(); 2343 } 2344 } 2345 } 2346 2347 template<class _Rp, class _A0, class _A1> 2348 template <class _Fp, class _Alloc> 2349 function<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f, 2350 typename enable_if<!is_integral<_Fp>::value>::type*) 2351 : __f_(0) 2352 { 2353 typedef allocator_traits<_Alloc> __alloc_traits; 2354 if (__function::__not_null(__f)) 2355 { 2356 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF; 2357 if (sizeof(_FF) <= sizeof(__buf_)) 2358 { 2359 __f_ = (__base*)&__buf_; 2360 ::new ((void*)__f_) _FF(__f, __a0); 2361 } 2362 else 2363 { 2364 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap; 2365 _Ap __a(__a0); 2366 typedef __allocator_destructor<_Ap> _Dp; 2367 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2368 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a)); 2369 __f_ = __hold.release(); 2370 } 2371 } 2372 } 2373 2374 template<class _Rp, class _A0, class _A1> 2375 function<_Rp(_A0, _A1)>& 2376 function<_Rp(_A0, _A1)>::operator=(const function& __f) 2377 { 2378 if (__f) 2379 function(__f).swap(*this); 2380 else 2381 *this = nullptr; 2382 return *this; 2383 } 2384 2385 template<class _Rp, class _A0, class _A1> 2386 function<_Rp(_A0, _A1)>& 2387 function<_Rp(_A0, _A1)>::operator=(nullptr_t) 2388 { 2389 __base* __t = __f_; 2390 __f_ = 0; 2391 if (__t == (__base*)&__buf_) 2392 __t->destroy(); 2393 else if (__t) 2394 __t->destroy_deallocate(); 2395 return *this; 2396 } 2397 2398 template<class _Rp, class _A0, class _A1> 2399 template <class _Fp> 2400 typename enable_if 2401 < 2402 !is_integral<_Fp>::value, 2403 function<_Rp(_A0, _A1)>& 2404 >::type 2405 function<_Rp(_A0, _A1)>::operator=(_Fp __f) 2406 { 2407 function(_VSTD::move(__f)).swap(*this); 2408 return *this; 2409 } 2410 2411 template<class _Rp, class _A0, class _A1> 2412 function<_Rp(_A0, _A1)>::~function() 2413 { 2414 if (__f_ == (__base*)&__buf_) 2415 __f_->destroy(); 2416 else if (__f_) 2417 __f_->destroy_deallocate(); 2418 } 2419 2420 template<class _Rp, class _A0, class _A1> 2421 void 2422 function<_Rp(_A0, _A1)>::swap(function& __f) 2423 { 2424 if (_VSTD::addressof(__f) == this) 2425 return; 2426 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) 2427 { 2428 typename aligned_storage<sizeof(__buf_)>::type __tempbuf; 2429 __base* __t = (__base*)&__tempbuf; 2430 __f_->__clone(__t); 2431 __f_->destroy(); 2432 __f_ = 0; 2433 __f.__f_->__clone((__base*)&__buf_); 2434 __f.__f_->destroy(); 2435 __f.__f_ = 0; 2436 __f_ = (__base*)&__buf_; 2437 __t->__clone((__base*)&__f.__buf_); 2438 __t->destroy(); 2439 __f.__f_ = (__base*)&__f.__buf_; 2440 } 2441 else if (__f_ == (__base*)&__buf_) 2442 { 2443 __f_->__clone((__base*)&__f.__buf_); 2444 __f_->destroy(); 2445 __f_ = __f.__f_; 2446 __f.__f_ = (__base*)&__f.__buf_; 2447 } 2448 else if (__f.__f_ == (__base*)&__f.__buf_) 2449 { 2450 __f.__f_->__clone((__base*)&__buf_); 2451 __f.__f_->destroy(); 2452 __f.__f_ = __f_; 2453 __f_ = (__base*)&__buf_; 2454 } 2455 else 2456 _VSTD::swap(__f_, __f.__f_); 2457 } 2458 2459 template<class _Rp, class _A0, class _A1> 2460 _Rp 2461 function<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const 2462 { 2463 if (__f_ == 0) 2464 __throw_bad_function_call(); 2465 return (*__f_)(__a0, __a1); 2466 } 2467 2468 #ifndef _LIBCPP_NO_RTTI 2469 2470 template<class _Rp, class _A0, class _A1> 2471 const std::type_info& 2472 function<_Rp(_A0, _A1)>::target_type() const 2473 { 2474 if (__f_ == 0) 2475 return typeid(void); 2476 return __f_->target_type(); 2477 } 2478 2479 template<class _Rp, class _A0, class _A1> 2480 template <typename _Tp> 2481 _Tp* 2482 function<_Rp(_A0, _A1)>::target() 2483 { 2484 if (__f_ == 0) 2485 return (_Tp*)0; 2486 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp))); 2487 } 2488 2489 template<class _Rp, class _A0, class _A1> 2490 template <typename _Tp> 2491 const _Tp* 2492 function<_Rp(_A0, _A1)>::target() const 2493 { 2494 if (__f_ == 0) 2495 return (const _Tp*)0; 2496 return (const _Tp*)__f_->target(typeid(_Tp)); 2497 } 2498 2499 #endif // _LIBCPP_NO_RTTI 2500 2501 template<class _Rp, class _A0, class _A1, class _A2> 2502 class _LIBCPP_TEMPLATE_VIS function<_Rp(_A0, _A1, _A2)> 2503 { 2504 typedef __function::__base<_Rp(_A0, _A1, _A2)> __base; 2505 aligned_storage<3*sizeof(void*)>::type __buf_; 2506 __base* __f_; 2507 2508 public: 2509 typedef _Rp result_type; 2510 2511 // 20.7.16.2.1, construct/copy/destroy: 2512 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} 2513 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} 2514 function(const function&); 2515 template<class _Fp> 2516 function(_Fp, 2517 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 2518 2519 template<class _Alloc> 2520 _LIBCPP_INLINE_VISIBILITY 2521 function(allocator_arg_t, const _Alloc&) : __f_(0) {} 2522 template<class _Alloc> 2523 _LIBCPP_INLINE_VISIBILITY 2524 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} 2525 template<class _Alloc> 2526 function(allocator_arg_t, const _Alloc&, const function&); 2527 template<class _Fp, class _Alloc> 2528 function(allocator_arg_t, const _Alloc& __a, _Fp __f, 2529 typename enable_if<!is_integral<_Fp>::value>::type* = 0); 2530 2531 function& operator=(const function&); 2532 function& operator=(nullptr_t); 2533 template<class _Fp> 2534 typename enable_if 2535 < 2536 !is_integral<_Fp>::value, 2537 function& 2538 >::type 2539 operator=(_Fp); 2540 2541 ~function(); 2542 2543 // 20.7.16.2.2, function modifiers: 2544 void swap(function&); 2545 template<class _Fp, class _Alloc> 2546 _LIBCPP_INLINE_VISIBILITY 2547 void assign(_Fp __f, const _Alloc& __a) 2548 {function(allocator_arg, __a, __f).swap(*this);} 2549 2550 // 20.7.16.2.3, function capacity: 2551 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;} 2552 2553 template<class _R2, class _B0, class _B1, class _B2> 2554 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const = delete; 2555 template<class _R2, class _B0, class _B1, class _B2> 2556 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const = delete; 2557 2558 // 20.7.16.2.4, function invocation: 2559 _Rp operator()(_A0, _A1, _A2) const; 2560 2561 #ifndef _LIBCPP_NO_RTTI 2562 // 20.7.16.2.5, function target access: 2563 const std::type_info& target_type() const; 2564 template <typename _Tp> _Tp* target(); 2565 template <typename _Tp> const _Tp* target() const; 2566 #endif // _LIBCPP_NO_RTTI 2567 }; 2568 2569 template<class _Rp, class _A0, class _A1, class _A2> 2570 function<_Rp(_A0, _A1, _A2)>::function(const function& __f) 2571 { 2572 if (__f.__f_ == 0) 2573 __f_ = 0; 2574 else if (__f.__f_ == (const __base*)&__f.__buf_) 2575 { 2576 __f_ = (__base*)&__buf_; 2577 __f.__f_->__clone(__f_); 2578 } 2579 else 2580 __f_ = __f.__f_->__clone(); 2581 } 2582 2583 template<class _Rp, class _A0, class _A1, class _A2> 2584 template<class _Alloc> 2585 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&, 2586 const function& __f) 2587 { 2588 if (__f.__f_ == 0) 2589 __f_ = 0; 2590 else if (__f.__f_ == (const __base*)&__f.__buf_) 2591 { 2592 __f_ = (__base*)&__buf_; 2593 __f.__f_->__clone(__f_); 2594 } 2595 else 2596 __f_ = __f.__f_->__clone(); 2597 } 2598 2599 template<class _Rp, class _A0, class _A1, class _A2> 2600 template <class _Fp> 2601 function<_Rp(_A0, _A1, _A2)>::function(_Fp __f, 2602 typename enable_if<!is_integral<_Fp>::value>::type*) 2603 : __f_(0) 2604 { 2605 if (__function::__not_null(__f)) 2606 { 2607 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF; 2608 if (sizeof(_FF) <= sizeof(__buf_)) 2609 { 2610 __f_ = (__base*)&__buf_; 2611 ::new ((void*)__f_) _FF(__f); 2612 } 2613 else 2614 { 2615 typedef allocator<_FF> _Ap; 2616 _Ap __a; 2617 typedef __allocator_destructor<_Ap> _Dp; 2618 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2619 ::new ((void*)__hold.get()) _FF(__f, allocator<_Fp>(__a)); 2620 __f_ = __hold.release(); 2621 } 2622 } 2623 } 2624 2625 template<class _Rp, class _A0, class _A1, class _A2> 2626 template <class _Fp, class _Alloc> 2627 function<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f, 2628 typename enable_if<!is_integral<_Fp>::value>::type*) 2629 : __f_(0) 2630 { 2631 typedef allocator_traits<_Alloc> __alloc_traits; 2632 if (__function::__not_null(__f)) 2633 { 2634 typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF; 2635 if (sizeof(_FF) <= sizeof(__buf_)) 2636 { 2637 __f_ = (__base*)&__buf_; 2638 ::new ((void*)__f_) _FF(__f, __a0); 2639 } 2640 else 2641 { 2642 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap; 2643 _Ap __a(__a0); 2644 typedef __allocator_destructor<_Ap> _Dp; 2645 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 2646 ::new ((void*)__hold.get()) _FF(__f, _Alloc(__a)); 2647 __f_ = __hold.release(); 2648 } 2649 } 2650 } 2651 2652 template<class _Rp, class _A0, class _A1, class _A2> 2653 function<_Rp(_A0, _A1, _A2)>& 2654 function<_Rp(_A0, _A1, _A2)>::operator=(const function& __f) 2655 { 2656 if (__f) 2657 function(__f).swap(*this); 2658 else 2659 *this = nullptr; 2660 return *this; 2661 } 2662 2663 template<class _Rp, class _A0, class _A1, class _A2> 2664 function<_Rp(_A0, _A1, _A2)>& 2665 function<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t) 2666 { 2667 __base* __t = __f_; 2668 __f_ = 0; 2669 if (__t == (__base*)&__buf_) 2670 __t->destroy(); 2671 else if (__t) 2672 __t->destroy_deallocate(); 2673 return *this; 2674 } 2675 2676 template<class _Rp, class _A0, class _A1, class _A2> 2677 template <class _Fp> 2678 typename enable_if 2679 < 2680 !is_integral<_Fp>::value, 2681 function<_Rp(_A0, _A1, _A2)>& 2682 >::type 2683 function<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f) 2684 { 2685 function(_VSTD::move(__f)).swap(*this); 2686 return *this; 2687 } 2688 2689 template<class _Rp, class _A0, class _A1, class _A2> 2690 function<_Rp(_A0, _A1, _A2)>::~function() 2691 { 2692 if (__f_ == (__base*)&__buf_) 2693 __f_->destroy(); 2694 else if (__f_) 2695 __f_->destroy_deallocate(); 2696 } 2697 2698 template<class _Rp, class _A0, class _A1, class _A2> 2699 void 2700 function<_Rp(_A0, _A1, _A2)>::swap(function& __f) 2701 { 2702 if (_VSTD::addressof(__f) == this) 2703 return; 2704 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) 2705 { 2706 typename aligned_storage<sizeof(__buf_)>::type __tempbuf; 2707 __base* __t = (__base*)&__tempbuf; 2708 __f_->__clone(__t); 2709 __f_->destroy(); 2710 __f_ = 0; 2711 __f.__f_->__clone((__base*)&__buf_); 2712 __f.__f_->destroy(); 2713 __f.__f_ = 0; 2714 __f_ = (__base*)&__buf_; 2715 __t->__clone((__base*)&__f.__buf_); 2716 __t->destroy(); 2717 __f.__f_ = (__base*)&__f.__buf_; 2718 } 2719 else if (__f_ == (__base*)&__buf_) 2720 { 2721 __f_->__clone((__base*)&__f.__buf_); 2722 __f_->destroy(); 2723 __f_ = __f.__f_; 2724 __f.__f_ = (__base*)&__f.__buf_; 2725 } 2726 else if (__f.__f_ == (__base*)&__f.__buf_) 2727 { 2728 __f.__f_->__clone((__base*)&__buf_); 2729 __f.__f_->destroy(); 2730 __f.__f_ = __f_; 2731 __f_ = (__base*)&__buf_; 2732 } 2733 else 2734 _VSTD::swap(__f_, __f.__f_); 2735 } 2736 2737 template<class _Rp, class _A0, class _A1, class _A2> 2738 _Rp 2739 function<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const 2740 { 2741 if (__f_ == 0) 2742 __throw_bad_function_call(); 2743 return (*__f_)(__a0, __a1, __a2); 2744 } 2745 2746 #ifndef _LIBCPP_NO_RTTI 2747 2748 template<class _Rp, class _A0, class _A1, class _A2> 2749 const std::type_info& 2750 function<_Rp(_A0, _A1, _A2)>::target_type() const 2751 { 2752 if (__f_ == 0) 2753 return typeid(void); 2754 return __f_->target_type(); 2755 } 2756 2757 template<class _Rp, class _A0, class _A1, class _A2> 2758 template <typename _Tp> 2759 _Tp* 2760 function<_Rp(_A0, _A1, _A2)>::target() 2761 { 2762 if (__f_ == 0) 2763 return (_Tp*)0; 2764 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp))); 2765 } 2766 2767 template<class _Rp, class _A0, class _A1, class _A2> 2768 template <typename _Tp> 2769 const _Tp* 2770 function<_Rp(_A0, _A1, _A2)>::target() const 2771 { 2772 if (__f_ == 0) 2773 return (const _Tp*)0; 2774 return (const _Tp*)__f_->target(typeid(_Tp)); 2775 } 2776 2777 #endif // _LIBCPP_NO_RTTI 2778 2779 template <class _Fp> 2780 inline _LIBCPP_INLINE_VISIBILITY 2781 bool 2782 operator==(const function<_Fp>& __f, nullptr_t) {return !__f;} 2783 2784 template <class _Fp> 2785 inline _LIBCPP_INLINE_VISIBILITY 2786 bool 2787 operator==(nullptr_t, const function<_Fp>& __f) {return !__f;} 2788 2789 template <class _Fp> 2790 inline _LIBCPP_INLINE_VISIBILITY 2791 bool 2792 operator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;} 2793 2794 template <class _Fp> 2795 inline _LIBCPP_INLINE_VISIBILITY 2796 bool 2797 operator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;} 2798 2799 template <class _Fp> 2800 inline _LIBCPP_INLINE_VISIBILITY 2801 void 2802 swap(function<_Fp>& __x, function<_Fp>& __y) 2803 {return __x.swap(__y);} 2804 2805 #endif 2806 2807 _LIBCPP_END_NAMESPACE_STD 2808 2809 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H 2810