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