1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H 11 #define _LIBCPP___FUNCTIONAL_FUNCTION_H 12 13 #include <__assert> 14 #include <__config> 15 #include <__exception/exception.h> 16 #include <__functional/binary_function.h> 17 #include <__functional/invoke.h> 18 #include <__functional/unary_function.h> 19 #include <__iterator/iterator_traits.h> 20 #include <__memory/addressof.h> 21 #include <__memory/allocator.h> 22 #include <__memory/allocator_destructor.h> 23 #include <__memory/allocator_traits.h> 24 #include <__memory/builtin_new_allocator.h> 25 #include <__memory/compressed_pair.h> 26 #include <__memory/unique_ptr.h> 27 #include <__type_traits/aligned_storage.h> 28 #include <__type_traits/decay.h> 29 #include <__type_traits/is_core_convertible.h> 30 #include <__type_traits/is_scalar.h> 31 #include <__type_traits/is_trivially_copy_constructible.h> 32 #include <__type_traits/is_trivially_destructible.h> 33 #include <__type_traits/is_void.h> 34 #include <__type_traits/strip_signature.h> 35 #include <__utility/forward.h> 36 #include <__utility/move.h> 37 #include <__utility/piecewise_construct.h> 38 #include <__utility/swap.h> 39 #include <__verbose_abort> 40 #include <new> 41 #include <tuple> 42 #include <typeinfo> 43 44 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 45 # pragma GCC system_header 46 #endif 47 48 #ifndef _LIBCPP_CXX03_LANG 49 50 _LIBCPP_BEGIN_NAMESPACE_STD 51 52 // bad_function_call 53 54 _LIBCPP_DIAGNOSTIC_PUSH 55 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables") 56 class _LIBCPP_EXPORTED_FROM_ABI bad_function_call 57 : public exception 58 { 59 public: 60 // Note that when a key function is not used, every translation unit that uses 61 // bad_function_call will end up containing a weak definition of the vtable and 62 // typeinfo. 63 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION 64 ~bad_function_call() _NOEXCEPT override; 65 #else 66 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_function_call() _NOEXCEPT override {} 67 #endif 68 69 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE 70 const char* what() const _NOEXCEPT override; 71 #endif 72 }; 73 _LIBCPP_DIAGNOSTIC_POP 74 75 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY 76 void __throw_bad_function_call() 77 { 78 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 79 throw bad_function_call(); 80 #else 81 _LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode"); 82 #endif 83 } 84 85 template<class _Fp> class _LIBCPP_TEMPLATE_VIS function; // undefined 86 87 namespace __function 88 { 89 90 template<class _Rp> 91 struct __maybe_derive_from_unary_function 92 { 93 }; 94 95 template<class _Rp, class _A1> 96 struct __maybe_derive_from_unary_function<_Rp(_A1)> 97 : public __unary_function<_A1, _Rp> 98 { 99 }; 100 101 template<class _Rp> 102 struct __maybe_derive_from_binary_function 103 { 104 }; 105 106 template<class _Rp, class _A1, class _A2> 107 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)> 108 : public __binary_function<_A1, _A2, _Rp> 109 { 110 }; 111 112 template <class _Fp> 113 _LIBCPP_INLINE_VISIBILITY 114 bool __not_null(_Fp const&) { return true; } 115 116 template <class _Fp> 117 _LIBCPP_INLINE_VISIBILITY 118 bool __not_null(_Fp* __ptr) { return __ptr; } 119 120 template <class _Ret, class _Class> 121 _LIBCPP_INLINE_VISIBILITY 122 bool __not_null(_Ret _Class::*__ptr) { return __ptr; } 123 124 template <class _Fp> 125 _LIBCPP_INLINE_VISIBILITY 126 bool __not_null(function<_Fp> const& __f) { return !!__f; } 127 128 #ifdef _LIBCPP_HAS_EXTENSION_BLOCKS 129 template <class _Rp, class ..._Args> 130 _LIBCPP_INLINE_VISIBILITY 131 bool __not_null(_Rp (^__p)(_Args...)) { return __p; } 132 #endif 133 134 } // namespace __function 135 136 namespace __function { 137 138 // __alloc_func holds a functor and an allocator. 139 140 template <class _Fp, class _Ap, class _FB> class __alloc_func; 141 template <class _Fp, class _FB> 142 class __default_alloc_func; 143 144 template <class _Fp, class _Ap, class _Rp, class... _ArgTypes> 145 class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)> 146 { 147 __compressed_pair<_Fp, _Ap> __f_; 148 149 public: 150 typedef _LIBCPP_NODEBUG _Fp _Target; 151 typedef _LIBCPP_NODEBUG _Ap _Alloc; 152 153 _LIBCPP_INLINE_VISIBILITY 154 const _Target& __target() const { return __f_.first(); } 155 156 // WIN32 APIs may define __allocator, so use __get_allocator instead. 157 _LIBCPP_INLINE_VISIBILITY 158 const _Alloc& __get_allocator() const { return __f_.second(); } 159 160 _LIBCPP_INLINE_VISIBILITY 161 explicit __alloc_func(_Target&& __f) 162 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)), 163 _VSTD::forward_as_tuple()) 164 { 165 } 166 167 _LIBCPP_INLINE_VISIBILITY 168 explicit __alloc_func(const _Target& __f, const _Alloc& __a) 169 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f), 170 _VSTD::forward_as_tuple(__a)) 171 { 172 } 173 174 _LIBCPP_INLINE_VISIBILITY 175 explicit __alloc_func(const _Target& __f, _Alloc&& __a) 176 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f), 177 _VSTD::forward_as_tuple(_VSTD::move(__a))) 178 { 179 } 180 181 _LIBCPP_INLINE_VISIBILITY 182 explicit __alloc_func(_Target&& __f, _Alloc&& __a) 183 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)), 184 _VSTD::forward_as_tuple(_VSTD::move(__a))) 185 { 186 } 187 188 _LIBCPP_INLINE_VISIBILITY 189 _Rp operator()(_ArgTypes&&... __arg) 190 { 191 typedef __invoke_void_return_wrapper<_Rp> _Invoker; 192 return _Invoker::__call(__f_.first(), 193 _VSTD::forward<_ArgTypes>(__arg)...); 194 } 195 196 _LIBCPP_INLINE_VISIBILITY 197 __alloc_func* __clone() const 198 { 199 typedef allocator_traits<_Alloc> __alloc_traits; 200 typedef __rebind_alloc<__alloc_traits, __alloc_func> _AA; 201 _AA __a(__f_.second()); 202 typedef __allocator_destructor<_AA> _Dp; 203 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 204 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a)); 205 return __hold.release(); 206 } 207 208 _LIBCPP_INLINE_VISIBILITY 209 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); } 210 211 _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__alloc_func* __f) { 212 typedef allocator_traits<_Alloc> __alloc_traits; 213 typedef __rebind_alloc<__alloc_traits, __alloc_func> _FunAlloc; 214 _FunAlloc __a(__f->__get_allocator()); 215 __f->destroy(); 216 __a.deallocate(__f, 1); 217 } 218 }; 219 220 template <class _Fp, class _Rp, class... _ArgTypes> 221 class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> { 222 _Fp __f_; 223 224 public: 225 typedef _LIBCPP_NODEBUG _Fp _Target; 226 227 _LIBCPP_INLINE_VISIBILITY 228 const _Target& __target() const { return __f_; } 229 230 _LIBCPP_INLINE_VISIBILITY 231 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {} 232 233 _LIBCPP_INLINE_VISIBILITY 234 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {} 235 236 _LIBCPP_INLINE_VISIBILITY 237 _Rp operator()(_ArgTypes&&... __arg) { 238 typedef __invoke_void_return_wrapper<_Rp> _Invoker; 239 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...); 240 } 241 242 _LIBCPP_INLINE_VISIBILITY 243 __default_alloc_func* __clone() const { 244 __builtin_new_allocator::__holder_t __hold = 245 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1); 246 __default_alloc_func* __res = 247 ::new ((void*)__hold.get()) __default_alloc_func(__f_); 248 (void)__hold.release(); 249 return __res; 250 } 251 252 _LIBCPP_INLINE_VISIBILITY 253 void destroy() _NOEXCEPT { __f_.~_Target(); } 254 255 _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__default_alloc_func* __f) { 256 __f->destroy(); 257 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1); 258 } 259 }; 260 261 // __base provides an abstract interface for copyable functors. 262 263 template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base; 264 265 template<class _Rp, class ..._ArgTypes> 266 class __base<_Rp(_ArgTypes...)> 267 { 268 __base(const __base&); 269 __base& operator=(const __base&); 270 public: 271 _LIBCPP_INLINE_VISIBILITY __base() {} 272 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__base() {} 273 virtual __base* __clone() const = 0; 274 virtual void __clone(__base*) const = 0; 275 virtual void destroy() _NOEXCEPT = 0; 276 virtual void destroy_deallocate() _NOEXCEPT = 0; 277 virtual _Rp operator()(_ArgTypes&& ...) = 0; 278 #ifndef _LIBCPP_HAS_NO_RTTI 279 virtual const void* target(const type_info&) const _NOEXCEPT = 0; 280 virtual const std::type_info& target_type() const _NOEXCEPT = 0; 281 #endif // _LIBCPP_HAS_NO_RTTI 282 }; 283 284 // __func implements __base for a given functor type. 285 286 template<class _FD, class _Alloc, class _FB> class __func; 287 288 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 289 class __func<_Fp, _Alloc, _Rp(_ArgTypes...)> 290 : public __base<_Rp(_ArgTypes...)> 291 { 292 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_; 293 public: 294 _LIBCPP_INLINE_VISIBILITY 295 explicit __func(_Fp&& __f) 296 : __f_(_VSTD::move(__f)) {} 297 298 _LIBCPP_INLINE_VISIBILITY 299 explicit __func(const _Fp& __f, const _Alloc& __a) 300 : __f_(__f, __a) {} 301 302 _LIBCPP_INLINE_VISIBILITY 303 explicit __func(const _Fp& __f, _Alloc&& __a) 304 : __f_(__f, _VSTD::move(__a)) {} 305 306 _LIBCPP_INLINE_VISIBILITY 307 explicit __func(_Fp&& __f, _Alloc&& __a) 308 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {} 309 310 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const; 311 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>*) const; 312 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT; 313 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT; 314 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg); 315 #ifndef _LIBCPP_HAS_NO_RTTI 316 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(const type_info&) const _NOEXCEPT; 317 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT; 318 #endif // _LIBCPP_HAS_NO_RTTI 319 }; 320 321 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 322 __base<_Rp(_ArgTypes...)>* 323 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const 324 { 325 typedef allocator_traits<_Alloc> __alloc_traits; 326 typedef __rebind_alloc<__alloc_traits, __func> _Ap; 327 _Ap __a(__f_.__get_allocator()); 328 typedef __allocator_destructor<_Ap> _Dp; 329 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); 330 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a)); 331 return __hold.release(); 332 } 333 334 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 335 void 336 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const 337 { 338 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator()); 339 } 340 341 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 342 void 343 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT 344 { 345 __f_.destroy(); 346 } 347 348 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 349 void 350 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT 351 { 352 typedef allocator_traits<_Alloc> __alloc_traits; 353 typedef __rebind_alloc<__alloc_traits, __func> _Ap; 354 _Ap __a(__f_.__get_allocator()); 355 __f_.destroy(); 356 __a.deallocate(this, 1); 357 } 358 359 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 360 _Rp 361 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg) 362 { 363 return __f_(_VSTD::forward<_ArgTypes>(__arg)...); 364 } 365 366 #ifndef _LIBCPP_HAS_NO_RTTI 367 368 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 369 const void* 370 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT 371 { 372 if (__ti == typeid(_Fp)) 373 return _VSTD::addressof(__f_.__target()); 374 return nullptr; 375 } 376 377 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes> 378 const std::type_info& 379 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT 380 { 381 return typeid(_Fp); 382 } 383 384 #endif // _LIBCPP_HAS_NO_RTTI 385 386 // __value_func creates a value-type from a __func. 387 388 template <class _Fp> class __value_func; 389 390 template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)> 391 { 392 _LIBCPP_SUPPRESS_DEPRECATED_PUSH 393 typename aligned_storage<3 * sizeof(void*)>::type __buf_; 394 _LIBCPP_SUPPRESS_DEPRECATED_POP 395 396 typedef __base<_Rp(_ArgTypes...)> __func; 397 __func* __f_; 398 399 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI static __func* __as_base(void* __p) 400 { 401 return reinterpret_cast<__func*>(__p); 402 } 403 404 public: 405 _LIBCPP_INLINE_VISIBILITY 406 __value_func() _NOEXCEPT : __f_(nullptr) {} 407 408 template <class _Fp, class _Alloc> 409 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a) 410 : __f_(nullptr) 411 { 412 typedef allocator_traits<_Alloc> __alloc_traits; 413 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun; 414 typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc; 415 416 if (__function::__not_null(__f)) 417 { 418 _FunAlloc __af(__a); 419 if (sizeof(_Fun) <= sizeof(__buf_) && 420 is_nothrow_copy_constructible<_Fp>::value && 421 is_nothrow_copy_constructible<_FunAlloc>::value) 422 { 423 __f_ = 424 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af)); 425 } 426 else 427 { 428 typedef __allocator_destructor<_FunAlloc> _Dp; 429 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1)); 430 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a)); 431 __f_ = __hold.release(); 432 } 433 } 434 } 435 436 template <class _Fp, 437 class = typename enable_if<!is_same<__decay_t<_Fp>, __value_func>::value>::type> 438 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f) 439 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {} 440 441 _LIBCPP_INLINE_VISIBILITY 442 __value_func(const __value_func& __f) 443 { 444 if (__f.__f_ == nullptr) 445 __f_ = nullptr; 446 else if ((void*)__f.__f_ == &__f.__buf_) 447 { 448 __f_ = __as_base(&__buf_); 449 __f.__f_->__clone(__f_); 450 } 451 else 452 __f_ = __f.__f_->__clone(); 453 } 454 455 _LIBCPP_INLINE_VISIBILITY 456 __value_func(__value_func&& __f) _NOEXCEPT 457 { 458 if (__f.__f_ == nullptr) 459 __f_ = nullptr; 460 else if ((void*)__f.__f_ == &__f.__buf_) 461 { 462 __f_ = __as_base(&__buf_); 463 __f.__f_->__clone(__f_); 464 } 465 else 466 { 467 __f_ = __f.__f_; 468 __f.__f_ = nullptr; 469 } 470 } 471 472 _LIBCPP_INLINE_VISIBILITY 473 ~__value_func() 474 { 475 if ((void*)__f_ == &__buf_) 476 __f_->destroy(); 477 else if (__f_) 478 __f_->destroy_deallocate(); 479 } 480 481 _LIBCPP_INLINE_VISIBILITY 482 __value_func& operator=(__value_func&& __f) 483 { 484 *this = nullptr; 485 if (__f.__f_ == nullptr) 486 __f_ = nullptr; 487 else if ((void*)__f.__f_ == &__f.__buf_) 488 { 489 __f_ = __as_base(&__buf_); 490 __f.__f_->__clone(__f_); 491 } 492 else 493 { 494 __f_ = __f.__f_; 495 __f.__f_ = nullptr; 496 } 497 return *this; 498 } 499 500 _LIBCPP_INLINE_VISIBILITY 501 __value_func& operator=(nullptr_t) 502 { 503 __func* __f = __f_; 504 __f_ = nullptr; 505 if ((void*)__f == &__buf_) 506 __f->destroy(); 507 else if (__f) 508 __f->destroy_deallocate(); 509 return *this; 510 } 511 512 _LIBCPP_INLINE_VISIBILITY 513 _Rp operator()(_ArgTypes&&... __args) const 514 { 515 if (__f_ == nullptr) 516 __throw_bad_function_call(); 517 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...); 518 } 519 520 _LIBCPP_INLINE_VISIBILITY 521 void swap(__value_func& __f) _NOEXCEPT 522 { 523 if (&__f == this) 524 return; 525 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_) 526 { 527 _LIBCPP_SUPPRESS_DEPRECATED_PUSH 528 typename aligned_storage<sizeof(__buf_)>::type __tempbuf; 529 _LIBCPP_SUPPRESS_DEPRECATED_POP 530 __func* __t = __as_base(&__tempbuf); 531 __f_->__clone(__t); 532 __f_->destroy(); 533 __f_ = nullptr; 534 __f.__f_->__clone(__as_base(&__buf_)); 535 __f.__f_->destroy(); 536 __f.__f_ = nullptr; 537 __f_ = __as_base(&__buf_); 538 __t->__clone(__as_base(&__f.__buf_)); 539 __t->destroy(); 540 __f.__f_ = __as_base(&__f.__buf_); 541 } 542 else if ((void*)__f_ == &__buf_) 543 { 544 __f_->__clone(__as_base(&__f.__buf_)); 545 __f_->destroy(); 546 __f_ = __f.__f_; 547 __f.__f_ = __as_base(&__f.__buf_); 548 } 549 else if ((void*)__f.__f_ == &__f.__buf_) 550 { 551 __f.__f_->__clone(__as_base(&__buf_)); 552 __f.__f_->destroy(); 553 __f.__f_ = __f_; 554 __f_ = __as_base(&__buf_); 555 } 556 else 557 _VSTD::swap(__f_, __f.__f_); 558 } 559 560 _LIBCPP_INLINE_VISIBILITY 561 explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; } 562 563 #ifndef _LIBCPP_HAS_NO_RTTI 564 _LIBCPP_INLINE_VISIBILITY 565 const std::type_info& target_type() const _NOEXCEPT 566 { 567 if (__f_ == nullptr) 568 return typeid(void); 569 return __f_->target_type(); 570 } 571 572 template <typename _Tp> 573 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT 574 { 575 if (__f_ == nullptr) 576 return nullptr; 577 return (const _Tp*)__f_->target(typeid(_Tp)); 578 } 579 #endif // _LIBCPP_HAS_NO_RTTI 580 }; 581 582 // Storage for a functor object, to be used with __policy to manage copy and 583 // destruction. 584 union __policy_storage 585 { 586 mutable char __small[sizeof(void*) * 2]; 587 void* __large; 588 }; 589 590 // True if _Fun can safely be held in __policy_storage.__small. 591 template <typename _Fun> 592 struct __use_small_storage 593 : public integral_constant< 594 bool, sizeof(_Fun) <= sizeof(__policy_storage) && 595 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) && 596 is_trivially_copy_constructible<_Fun>::value && 597 is_trivially_destructible<_Fun>::value> {}; 598 599 // Policy contains information about how to copy, destroy, and move the 600 // underlying functor. You can think of it as a vtable of sorts. 601 struct __policy 602 { 603 // Used to copy or destroy __large values. null for trivial objects. 604 void* (*const __clone)(const void*); 605 void (*const __destroy)(void*); 606 607 // True if this is the null policy (no value). 608 const bool __is_null; 609 610 // The target type. May be null if RTTI is disabled. 611 const std::type_info* const __type_info; 612 613 // Returns a pointer to a static policy object suitable for the functor 614 // type. 615 template <typename _Fun> 616 _LIBCPP_INLINE_VISIBILITY static const __policy* __create() 617 { 618 return __choose_policy<_Fun>(__use_small_storage<_Fun>()); 619 } 620 621 _LIBCPP_INLINE_VISIBILITY 622 static const __policy* __create_empty() 623 { 624 static const _LIBCPP_CONSTEXPR __policy __policy = {nullptr, nullptr, 625 true, 626 #ifndef _LIBCPP_HAS_NO_RTTI 627 &typeid(void) 628 #else 629 nullptr 630 #endif 631 }; 632 return &__policy; 633 } 634 635 private: 636 template <typename _Fun> 637 _LIBCPP_HIDE_FROM_ABI static void* __large_clone(const void* __s) 638 { 639 const _Fun* __f = static_cast<const _Fun*>(__s); 640 return __f->__clone(); 641 } 642 643 template <typename _Fun> 644 _LIBCPP_HIDE_FROM_ABI static void __large_destroy(void* __s) { 645 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s)); 646 } 647 648 template <typename _Fun> 649 _LIBCPP_INLINE_VISIBILITY static const __policy* 650 __choose_policy(/* is_small = */ false_type) { 651 static const _LIBCPP_CONSTEXPR __policy __policy = { 652 &__large_clone<_Fun>, &__large_destroy<_Fun>, false, 653 #ifndef _LIBCPP_HAS_NO_RTTI 654 &typeid(typename _Fun::_Target) 655 #else 656 nullptr 657 #endif 658 }; 659 return &__policy; 660 } 661 662 template <typename _Fun> 663 _LIBCPP_INLINE_VISIBILITY static const __policy* 664 __choose_policy(/* is_small = */ true_type) 665 { 666 static const _LIBCPP_CONSTEXPR __policy __policy = { 667 nullptr, nullptr, false, 668 #ifndef _LIBCPP_HAS_NO_RTTI 669 &typeid(typename _Fun::_Target) 670 #else 671 nullptr 672 #endif 673 }; 674 return &__policy; 675 } 676 }; 677 678 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is 679 // faster for types that can be passed in registers. 680 template <typename _Tp> 681 using __fast_forward = __conditional_t<is_scalar<_Tp>::value, _Tp, _Tp&&>; 682 683 // __policy_invoker calls an instance of __alloc_func held in __policy_storage. 684 685 template <class _Fp> struct __policy_invoker; 686 687 template <class _Rp, class... _ArgTypes> 688 struct __policy_invoker<_Rp(_ArgTypes...)> 689 { 690 typedef _Rp (*__Call)(const __policy_storage*, 691 __fast_forward<_ArgTypes>...); 692 693 __Call __call_; 694 695 // Creates an invoker that throws bad_function_call. 696 _LIBCPP_INLINE_VISIBILITY 697 __policy_invoker() : __call_(&__call_empty) {} 698 699 // Creates an invoker that calls the given instance of __func. 700 template <typename _Fun> 701 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create() 702 { 703 return __policy_invoker(&__call_impl<_Fun>); 704 } 705 706 private: 707 _LIBCPP_INLINE_VISIBILITY 708 explicit __policy_invoker(__Call __c) : __call_(__c) {} 709 710 _LIBCPP_HIDE_FROM_ABI static _Rp __call_empty(const __policy_storage*, 711 __fast_forward<_ArgTypes>...) 712 { 713 __throw_bad_function_call(); 714 } 715 716 template <typename _Fun> 717 _LIBCPP_HIDE_FROM_ABI static _Rp __call_impl(const __policy_storage* __buf, 718 __fast_forward<_ArgTypes>... __args) 719 { 720 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value 721 ? &__buf->__small 722 : __buf->__large); 723 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...); 724 } 725 }; 726 727 // __policy_func uses a __policy and __policy_invoker to create a type-erased, 728 // copyable functor. 729 730 template <class _Fp> class __policy_func; 731 732 template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)> 733 { 734 // Inline storage for small objects. 735 __policy_storage __buf_; 736 737 // Calls the value stored in __buf_. This could technically be part of 738 // policy, but storing it here eliminates a level of indirection inside 739 // operator(). 740 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker; 741 __invoker __invoker_; 742 743 // The policy that describes how to move / copy / destroy __buf_. Never 744 // null, even if the function is empty. 745 const __policy* __policy_; 746 747 public: 748 _LIBCPP_INLINE_VISIBILITY 749 __policy_func() : __policy_(__policy::__create_empty()) {} 750 751 template <class _Fp, class _Alloc> 752 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a) 753 : __policy_(__policy::__create_empty()) 754 { 755 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun; 756 typedef allocator_traits<_Alloc> __alloc_traits; 757 typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc; 758 759 if (__function::__not_null(__f)) 760 { 761 __invoker_ = __invoker::template __create<_Fun>(); 762 __policy_ = __policy::__create<_Fun>(); 763 764 _FunAlloc __af(__a); 765 if (__use_small_storage<_Fun>()) 766 { 767 ::new ((void*)&__buf_.__small) 768 _Fun(_VSTD::move(__f), _Alloc(__af)); 769 } 770 else 771 { 772 typedef __allocator_destructor<_FunAlloc> _Dp; 773 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1)); 774 ::new ((void*)__hold.get()) 775 _Fun(_VSTD::move(__f), _Alloc(__af)); 776 __buf_.__large = __hold.release(); 777 } 778 } 779 } 780 781 template <class _Fp, class = typename enable_if<!is_same<__decay_t<_Fp>, __policy_func>::value>::type> 782 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f) 783 : __policy_(__policy::__create_empty()) { 784 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun; 785 786 if (__function::__not_null(__f)) { 787 __invoker_ = __invoker::template __create<_Fun>(); 788 __policy_ = __policy::__create<_Fun>(); 789 if (__use_small_storage<_Fun>()) { 790 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f)); 791 } else { 792 __builtin_new_allocator::__holder_t __hold = 793 __builtin_new_allocator::__allocate_type<_Fun>(1); 794 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f)); 795 (void)__hold.release(); 796 } 797 } 798 } 799 800 _LIBCPP_INLINE_VISIBILITY 801 __policy_func(const __policy_func& __f) 802 : __buf_(__f.__buf_), __invoker_(__f.__invoker_), 803 __policy_(__f.__policy_) 804 { 805 if (__policy_->__clone) 806 __buf_.__large = __policy_->__clone(__f.__buf_.__large); 807 } 808 809 _LIBCPP_INLINE_VISIBILITY 810 __policy_func(__policy_func&& __f) 811 : __buf_(__f.__buf_), __invoker_(__f.__invoker_), 812 __policy_(__f.__policy_) 813 { 814 if (__policy_->__destroy) 815 { 816 __f.__policy_ = __policy::__create_empty(); 817 __f.__invoker_ = __invoker(); 818 } 819 } 820 821 _LIBCPP_INLINE_VISIBILITY 822 ~__policy_func() 823 { 824 if (__policy_->__destroy) 825 __policy_->__destroy(__buf_.__large); 826 } 827 828 _LIBCPP_INLINE_VISIBILITY 829 __policy_func& operator=(__policy_func&& __f) 830 { 831 *this = nullptr; 832 __buf_ = __f.__buf_; 833 __invoker_ = __f.__invoker_; 834 __policy_ = __f.__policy_; 835 __f.__policy_ = __policy::__create_empty(); 836 __f.__invoker_ = __invoker(); 837 return *this; 838 } 839 840 _LIBCPP_INLINE_VISIBILITY 841 __policy_func& operator=(nullptr_t) 842 { 843 const __policy* __p = __policy_; 844 __policy_ = __policy::__create_empty(); 845 __invoker_ = __invoker(); 846 if (__p->__destroy) 847 __p->__destroy(__buf_.__large); 848 return *this; 849 } 850 851 _LIBCPP_INLINE_VISIBILITY 852 _Rp operator()(_ArgTypes&&... __args) const 853 { 854 return __invoker_.__call_(_VSTD::addressof(__buf_), 855 _VSTD::forward<_ArgTypes>(__args)...); 856 } 857 858 _LIBCPP_INLINE_VISIBILITY 859 void swap(__policy_func& __f) 860 { 861 _VSTD::swap(__invoker_, __f.__invoker_); 862 _VSTD::swap(__policy_, __f.__policy_); 863 _VSTD::swap(__buf_, __f.__buf_); 864 } 865 866 _LIBCPP_INLINE_VISIBILITY 867 explicit operator bool() const _NOEXCEPT 868 { 869 return !__policy_->__is_null; 870 } 871 872 #ifndef _LIBCPP_HAS_NO_RTTI 873 _LIBCPP_INLINE_VISIBILITY 874 const std::type_info& target_type() const _NOEXCEPT 875 { 876 return *__policy_->__type_info; 877 } 878 879 template <typename _Tp> 880 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT 881 { 882 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info) 883 return nullptr; 884 if (__policy_->__clone) // Out of line storage. 885 return reinterpret_cast<const _Tp*>(__buf_.__large); 886 else 887 return reinterpret_cast<const _Tp*>(&__buf_.__small); 888 } 889 #endif // _LIBCPP_HAS_NO_RTTI 890 }; 891 892 #if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) 893 894 extern "C" void *_Block_copy(const void *); 895 extern "C" void _Block_release(const void *); 896 897 template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes> 898 class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> 899 : public __base<_Rp(_ArgTypes...)> 900 { 901 typedef _Rp1(^__block_type)(_ArgTypes1...); 902 __block_type __f_; 903 904 public: 905 _LIBCPP_INLINE_VISIBILITY 906 explicit __func(__block_type const& __f) 907 #ifdef _LIBCPP_HAS_OBJC_ARC 908 : __f_(__f) 909 #else 910 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr)) 911 #endif 912 { } 913 914 // [TODO] add && to save on a retain 915 916 _LIBCPP_INLINE_VISIBILITY 917 explicit __func(__block_type __f, const _Alloc& /* unused */) 918 #ifdef _LIBCPP_HAS_OBJC_ARC 919 : __f_(__f) 920 #else 921 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr)) 922 #endif 923 { } 924 925 virtual __base<_Rp(_ArgTypes...)>* __clone() const { 926 _LIBCPP_ASSERT_INTERNAL(false, 927 "Block pointers are just pointers, so they should always fit into " 928 "std::function's small buffer optimization. This function should " 929 "never be invoked."); 930 return nullptr; 931 } 932 933 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const { 934 ::new ((void*)__p) __func(__f_); 935 } 936 937 virtual void destroy() _NOEXCEPT { 938 #ifndef _LIBCPP_HAS_OBJC_ARC 939 if (__f_) 940 _Block_release(__f_); 941 #endif 942 __f_ = 0; 943 } 944 945 virtual void destroy_deallocate() _NOEXCEPT { 946 _LIBCPP_ASSERT_INTERNAL(false, 947 "Block pointers are just pointers, so they should always fit into " 948 "std::function's small buffer optimization. This function should " 949 "never be invoked."); 950 } 951 952 virtual _Rp operator()(_ArgTypes&& ... __arg) { 953 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...); 954 } 955 956 #ifndef _LIBCPP_HAS_NO_RTTI 957 virtual const void* target(type_info const& __ti) const _NOEXCEPT { 958 if (__ti == typeid(__func::__block_type)) 959 return &__f_; 960 return (const void*)nullptr; 961 } 962 963 virtual const std::type_info& target_type() const _NOEXCEPT { 964 return typeid(__func::__block_type); 965 } 966 #endif // _LIBCPP_HAS_NO_RTTI 967 }; 968 969 #endif // _LIBCPP_HAS_EXTENSION_BLOCKS 970 971 } // namespace __function 972 973 template<class _Rp, class ..._ArgTypes> 974 class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)> 975 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>, 976 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)> 977 { 978 #ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION 979 typedef __function::__value_func<_Rp(_ArgTypes...)> __func; 980 #else 981 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func; 982 #endif 983 984 __func __f_; 985 986 template <class _Fp, bool = _And< 987 _IsNotSame<__remove_cvref_t<_Fp>, function>, 988 __invokable<_Fp, _ArgTypes...> 989 >::value> 990 struct __callable; 991 template <class _Fp> 992 struct __callable<_Fp, true> 993 { 994 static const bool value = is_void<_Rp>::value || 995 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type, 996 _Rp>::value; 997 }; 998 template <class _Fp> 999 struct __callable<_Fp, false> 1000 { 1001 static const bool value = false; 1002 }; 1003 1004 template <class _Fp> 1005 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type; 1006 public: 1007 typedef _Rp result_type; 1008 1009 // construct/copy/destroy: 1010 _LIBCPP_INLINE_VISIBILITY 1011 function() _NOEXCEPT { } 1012 _LIBCPP_INLINE_VISIBILITY 1013 _LIBCPP_HIDE_FROM_ABI function(nullptr_t) _NOEXCEPT {} 1014 _LIBCPP_HIDE_FROM_ABI function(const function&); 1015 _LIBCPP_HIDE_FROM_ABI function(function&&) _NOEXCEPT; 1016 template<class _Fp, class = _EnableIfLValueCallable<_Fp>> 1017 _LIBCPP_HIDE_FROM_ABI function(_Fp); 1018 1019 #if _LIBCPP_STD_VER <= 14 1020 template<class _Alloc> 1021 _LIBCPP_INLINE_VISIBILITY 1022 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {} 1023 template<class _Alloc> 1024 _LIBCPP_INLINE_VISIBILITY 1025 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {} 1026 template<class _Alloc> 1027 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, const function&); 1028 template<class _Alloc> 1029 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, function&&); 1030 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>> 1031 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc& __a, _Fp __f); 1032 #endif 1033 1034 _LIBCPP_HIDE_FROM_ABI function& operator=(const function&); 1035 _LIBCPP_HIDE_FROM_ABI function& operator=(function&&) _NOEXCEPT; 1036 _LIBCPP_HIDE_FROM_ABI function& operator=(nullptr_t) _NOEXCEPT; 1037 template<class _Fp, class = _EnableIfLValueCallable<__decay_t<_Fp>>> 1038 _LIBCPP_HIDE_FROM_ABI function& operator=(_Fp&&); 1039 1040 _LIBCPP_HIDE_FROM_ABI ~function(); 1041 1042 // function modifiers: 1043 _LIBCPP_HIDE_FROM_ABI void swap(function&) _NOEXCEPT; 1044 1045 #if _LIBCPP_STD_VER <= 14 1046 template<class _Fp, class _Alloc> 1047 _LIBCPP_INLINE_VISIBILITY 1048 void assign(_Fp&& __f, const _Alloc& __a) 1049 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);} 1050 #endif 1051 1052 // function capacity: 1053 _LIBCPP_INLINE_VISIBILITY 1054 explicit operator bool() const _NOEXCEPT { 1055 return static_cast<bool>(__f_); 1056 } 1057 1058 // deleted overloads close possible hole in the type system 1059 template<class _R2, class... _ArgTypes2> 1060 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete; 1061 #if _LIBCPP_STD_VER <= 17 1062 template<class _R2, class... _ArgTypes2> 1063 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete; 1064 #endif 1065 public: 1066 // function invocation: 1067 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const; 1068 1069 #ifndef _LIBCPP_HAS_NO_RTTI 1070 // function target access: 1071 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT; 1072 template <typename _Tp> 1073 _LIBCPP_HIDE_FROM_ABI _Tp* target() _NOEXCEPT; 1074 template <typename _Tp> 1075 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT; 1076 #endif // _LIBCPP_HAS_NO_RTTI 1077 }; 1078 1079 #if _LIBCPP_STD_VER >= 17 1080 template<class _Rp, class ..._Ap> 1081 function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>; 1082 1083 template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type> 1084 function(_Fp) -> function<_Stripped>; 1085 #endif // _LIBCPP_STD_VER >= 17 1086 1087 template<class _Rp, class ..._ArgTypes> 1088 function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {} 1089 1090 #if _LIBCPP_STD_VER <= 14 1091 template<class _Rp, class ..._ArgTypes> 1092 template <class _Alloc> 1093 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, 1094 const function& __f) : __f_(__f.__f_) {} 1095 #endif 1096 1097 template <class _Rp, class... _ArgTypes> 1098 function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT 1099 : __f_(_VSTD::move(__f.__f_)) {} 1100 1101 #if _LIBCPP_STD_VER <= 14 1102 template<class _Rp, class ..._ArgTypes> 1103 template <class _Alloc> 1104 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, 1105 function&& __f) 1106 : __f_(_VSTD::move(__f.__f_)) {} 1107 #endif 1108 1109 template <class _Rp, class... _ArgTypes> 1110 template <class _Fp, class> 1111 function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {} 1112 1113 #if _LIBCPP_STD_VER <= 14 1114 template <class _Rp, class... _ArgTypes> 1115 template <class _Fp, class _Alloc, class> 1116 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a, 1117 _Fp __f) 1118 : __f_(_VSTD::move(__f), __a) {} 1119 #endif 1120 1121 template<class _Rp, class ..._ArgTypes> 1122 function<_Rp(_ArgTypes...)>& 1123 function<_Rp(_ArgTypes...)>::operator=(const function& __f) 1124 { 1125 function(__f).swap(*this); 1126 return *this; 1127 } 1128 1129 template<class _Rp, class ..._ArgTypes> 1130 function<_Rp(_ArgTypes...)>& 1131 function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT 1132 { 1133 __f_ = _VSTD::move(__f.__f_); 1134 return *this; 1135 } 1136 1137 template<class _Rp, class ..._ArgTypes> 1138 function<_Rp(_ArgTypes...)>& 1139 function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT 1140 { 1141 __f_ = nullptr; 1142 return *this; 1143 } 1144 1145 template<class _Rp, class ..._ArgTypes> 1146 template <class _Fp, class> 1147 function<_Rp(_ArgTypes...)>& 1148 function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f) 1149 { 1150 function(_VSTD::forward<_Fp>(__f)).swap(*this); 1151 return *this; 1152 } 1153 1154 template<class _Rp, class ..._ArgTypes> 1155 function<_Rp(_ArgTypes...)>::~function() {} 1156 1157 template<class _Rp, class ..._ArgTypes> 1158 void 1159 function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT 1160 { 1161 __f_.swap(__f.__f_); 1162 } 1163 1164 template<class _Rp, class ..._ArgTypes> 1165 _Rp 1166 function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const 1167 { 1168 return __f_(_VSTD::forward<_ArgTypes>(__arg)...); 1169 } 1170 1171 #ifndef _LIBCPP_HAS_NO_RTTI 1172 1173 template<class _Rp, class ..._ArgTypes> 1174 const std::type_info& 1175 function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT 1176 { 1177 return __f_.target_type(); 1178 } 1179 1180 template<class _Rp, class ..._ArgTypes> 1181 template <typename _Tp> 1182 _Tp* 1183 function<_Rp(_ArgTypes...)>::target() _NOEXCEPT 1184 { 1185 return (_Tp*)(__f_.template target<_Tp>()); 1186 } 1187 1188 template<class _Rp, class ..._ArgTypes> 1189 template <typename _Tp> 1190 const _Tp* 1191 function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT 1192 { 1193 return __f_.template target<_Tp>(); 1194 } 1195 1196 #endif // _LIBCPP_HAS_NO_RTTI 1197 1198 template <class _Rp, class... _ArgTypes> 1199 inline _LIBCPP_INLINE_VISIBILITY 1200 bool 1201 operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;} 1202 1203 #if _LIBCPP_STD_VER <= 17 1204 1205 template <class _Rp, class... _ArgTypes> 1206 inline _LIBCPP_INLINE_VISIBILITY 1207 bool 1208 operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;} 1209 1210 template <class _Rp, class... _ArgTypes> 1211 inline _LIBCPP_INLINE_VISIBILITY 1212 bool 1213 operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;} 1214 1215 template <class _Rp, class... _ArgTypes> 1216 inline _LIBCPP_INLINE_VISIBILITY 1217 bool 1218 operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;} 1219 1220 #endif // _LIBCPP_STD_VER <= 17 1221 1222 template <class _Rp, class... _ArgTypes> 1223 inline _LIBCPP_INLINE_VISIBILITY 1224 void 1225 swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT 1226 {return __x.swap(__y);} 1227 1228 _LIBCPP_END_NAMESPACE_STD 1229 1230 #endif // _LIBCPP_CXX03_LANG 1231 1232 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H 1233