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