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