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