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