File indexing completed on 2026-05-03 08:13:51
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H
0011 #define _LIBCPP___FUNCTIONAL_FUNCTION_H
0012
0013 #include <__assert>
0014 #include <__config>
0015 #include <__cstddef/nullptr_t.h>
0016 #include <__exception/exception.h>
0017 #include <__functional/binary_function.h>
0018 #include <__functional/invoke.h>
0019 #include <__functional/unary_function.h>
0020 #include <__iterator/iterator_traits.h>
0021 #include <__memory/addressof.h>
0022 #include <__memory/allocator.h>
0023 #include <__memory/allocator_destructor.h>
0024 #include <__memory/allocator_traits.h>
0025 #include <__memory/compressed_pair.h>
0026 #include <__memory/unique_ptr.h>
0027 #include <__type_traits/aligned_storage.h>
0028 #include <__type_traits/decay.h>
0029 #include <__type_traits/is_core_convertible.h>
0030 #include <__type_traits/is_scalar.h>
0031 #include <__type_traits/is_trivially_constructible.h>
0032 #include <__type_traits/is_trivially_destructible.h>
0033 #include <__type_traits/is_void.h>
0034 #include <__type_traits/strip_signature.h>
0035 #include <__utility/forward.h>
0036 #include <__utility/move.h>
0037 #include <__utility/piecewise_construct.h>
0038 #include <__utility/swap.h>
0039 #include <__verbose_abort>
0040 #include <tuple>
0041 #include <typeinfo>
0042
0043 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0044 # pragma GCC system_header
0045 #endif
0046
0047 _LIBCPP_PUSH_MACROS
0048 #include <__undef_macros>
0049
0050 #ifndef _LIBCPP_CXX03_LANG
0051
0052 _LIBCPP_BEGIN_NAMESPACE_STD
0053
0054
0055
0056 _LIBCPP_DIAGNOSTIC_PUSH
0057 # if !_LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
0058 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
0059 # endif
0060 class _LIBCPP_EXPORTED_FROM_ABI bad_function_call : public exception {
0061 public:
0062 _LIBCPP_HIDE_FROM_ABI bad_function_call() _NOEXCEPT = default;
0063 _LIBCPP_HIDE_FROM_ABI bad_function_call(const bad_function_call&) _NOEXCEPT = default;
0064 _LIBCPP_HIDE_FROM_ABI bad_function_call& operator=(const bad_function_call&) _NOEXCEPT = default;
0065
0066
0067
0068 # if _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
0069 ~bad_function_call() _NOEXCEPT override;
0070 # else
0071 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_function_call() _NOEXCEPT override {}
0072 # endif
0073
0074 # ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
0075 const char* what() const _NOEXCEPT override;
0076 # endif
0077 };
0078 _LIBCPP_DIAGNOSTIC_POP
0079
0080 [[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_function_call() {
0081 # if _LIBCPP_HAS_EXCEPTIONS
0082 throw bad_function_call();
0083 # else
0084 _LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode");
0085 # endif
0086 }
0087
0088 template <class _Fp>
0089 class _LIBCPP_TEMPLATE_VIS function;
0090
0091 namespace __function {
0092
0093 template <class _Rp>
0094 struct __maybe_derive_from_unary_function {};
0095
0096 template <class _Rp, class _A1>
0097 struct __maybe_derive_from_unary_function<_Rp(_A1)> : public __unary_function<_A1, _Rp> {};
0098
0099 template <class _Rp>
0100 struct __maybe_derive_from_binary_function {};
0101
0102 template <class _Rp, class _A1, class _A2>
0103 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)> : public __binary_function<_A1, _A2, _Rp> {};
0104
0105 template <class _Fp>
0106 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Fp const&) {
0107 return true;
0108 }
0109
0110 template <class _Fp>
0111 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Fp* __ptr) {
0112 return __ptr;
0113 }
0114
0115 template <class _Ret, class _Class>
0116 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Ret _Class::*__ptr) {
0117 return __ptr;
0118 }
0119
0120 template <class _Fp>
0121 _LIBCPP_HIDE_FROM_ABI bool __not_null(function<_Fp> const& __f) {
0122 return !!__f;
0123 }
0124
0125 # if _LIBCPP_HAS_EXTENSION_BLOCKS
0126 template <class _Rp, class... _Args>
0127 _LIBCPP_HIDE_FROM_ABI bool __not_null(_Rp (^__p)(_Args...)) {
0128 return __p;
0129 }
0130 # endif
0131
0132 }
0133
0134 namespace __function {
0135
0136
0137
0138 template <class _Fp, class _Ap, class _FB>
0139 class __alloc_func;
0140 template <class _Fp, class _FB>
0141 class __default_alloc_func;
0142
0143 template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
0144 class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)> {
0145 _LIBCPP_COMPRESSED_PAIR(_Fp, __func_, _Ap, __alloc_);
0146
0147 public:
0148 using _Target _LIBCPP_NODEBUG = _Fp;
0149 using _Alloc _LIBCPP_NODEBUG = _Ap;
0150
0151 _LIBCPP_HIDE_FROM_ABI const _Target& __target() const { return __func_; }
0152
0153
0154 _LIBCPP_HIDE_FROM_ABI const _Alloc& __get_allocator() const { return __alloc_; }
0155
0156 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(_Target&& __f) : __func_(std::move(__f)), __alloc_() {}
0157
0158 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(const _Target& __f, const _Alloc& __a) : __func_(__f), __alloc_(__a) {}
0159
0160 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(const _Target& __f, _Alloc&& __a)
0161 : __func_(__f), __alloc_(std::move(__a)) {}
0162
0163 _LIBCPP_HIDE_FROM_ABI explicit __alloc_func(_Target&& __f, _Alloc&& __a)
0164 : __func_(std::move(__f)), __alloc_(std::move(__a)) {}
0165
0166 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __arg) {
0167 return std::__invoke_r<_Rp>(__func_, std::forward<_ArgTypes>(__arg)...);
0168 }
0169
0170 _LIBCPP_HIDE_FROM_ABI __alloc_func* __clone() const {
0171 typedef allocator_traits<_Alloc> __alloc_traits;
0172 typedef __rebind_alloc<__alloc_traits, __alloc_func> _AA;
0173 _AA __a(__alloc_);
0174 typedef __allocator_destructor<_AA> _Dp;
0175 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
0176 ::new ((void*)__hold.get()) __alloc_func(__func_, _Alloc(__a));
0177 return __hold.release();
0178 }
0179
0180 _LIBCPP_HIDE_FROM_ABI void destroy() _NOEXCEPT {
0181 __func_.~_Fp();
0182 __alloc_.~_Alloc();
0183 }
0184
0185 _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__alloc_func* __f) {
0186 typedef allocator_traits<_Alloc> __alloc_traits;
0187 typedef __rebind_alloc<__alloc_traits, __alloc_func> _FunAlloc;
0188 _FunAlloc __a(__f->__get_allocator());
0189 __f->destroy();
0190 __a.deallocate(__f, 1);
0191 }
0192 };
0193
0194 template <class _Tp>
0195 struct __deallocating_deleter {
0196 _LIBCPP_HIDE_FROM_ABI void operator()(void* __p) const {
0197 std::__libcpp_deallocate<_Tp>(static_cast<_Tp*>(__p), __element_count(1));
0198 }
0199 };
0200
0201 template <class _Fp, class _Rp, class... _ArgTypes>
0202 class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
0203 _Fp __f_;
0204
0205 public:
0206 using _Target _LIBCPP_NODEBUG = _Fp;
0207
0208 _LIBCPP_HIDE_FROM_ABI const _Target& __target() const { return __f_; }
0209
0210 _LIBCPP_HIDE_FROM_ABI explicit __default_alloc_func(_Target&& __f) : __f_(std::move(__f)) {}
0211
0212 _LIBCPP_HIDE_FROM_ABI explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
0213
0214 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __arg) {
0215 return std::__invoke_r<_Rp>(__f_, std::forward<_ArgTypes>(__arg)...);
0216 }
0217
0218 _LIBCPP_HIDE_FROM_ABI __default_alloc_func* __clone() const {
0219 using _Self = __default_alloc_func;
0220 unique_ptr<_Self, __deallocating_deleter<_Self>> __hold(std::__libcpp_allocate<_Self>(__element_count(1)));
0221 _Self* __res = ::new ((void*)__hold.get()) _Self(__f_);
0222 (void)__hold.release();
0223 return __res;
0224 }
0225
0226 _LIBCPP_HIDE_FROM_ABI void destroy() _NOEXCEPT { __f_.~_Target(); }
0227
0228 _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__default_alloc_func* __f) {
0229 __f->destroy();
0230 std::__libcpp_deallocate<__default_alloc_func>(__f, __element_count(1));
0231 }
0232 };
0233
0234
0235
0236 template <class _Fp>
0237 class _LIBCPP_TEMPLATE_VIS __base;
0238
0239 template <class _Rp, class... _ArgTypes>
0240 class __base<_Rp(_ArgTypes...)> {
0241 public:
0242 __base(const __base&) = delete;
0243 __base& operator=(const __base&) = delete;
0244
0245 _LIBCPP_HIDE_FROM_ABI __base() {}
0246 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__base() {}
0247 virtual __base* __clone() const = 0;
0248 virtual void __clone(__base*) const = 0;
0249 virtual void destroy() _NOEXCEPT = 0;
0250 virtual void destroy_deallocate() _NOEXCEPT = 0;
0251 virtual _Rp operator()(_ArgTypes&&...) = 0;
0252 # if _LIBCPP_HAS_RTTI
0253 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
0254 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
0255 # endif
0256 };
0257
0258
0259
0260 template <class _FD, class _Alloc, class _FB>
0261 class __func;
0262
0263 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
0264 class __func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __base<_Rp(_ArgTypes...)> {
0265 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
0266
0267 public:
0268 _LIBCPP_HIDE_FROM_ABI explicit __func(_Fp&& __f) : __f_(std::move(__f)) {}
0269
0270 _LIBCPP_HIDE_FROM_ABI explicit __func(const _Fp& __f, const _Alloc& __a) : __f_(__f, __a) {}
0271
0272 _LIBCPP_HIDE_FROM_ABI explicit __func(const _Fp& __f, _Alloc&& __a) : __f_(__f, std::move(__a)) {}
0273
0274 _LIBCPP_HIDE_FROM_ABI explicit __func(_Fp&& __f, _Alloc&& __a) : __f_(std::move(__f), std::move(__a)) {}
0275
0276 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const;
0277 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
0278 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT;
0279 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT;
0280 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg);
0281 # if _LIBCPP_HAS_RTTI
0282 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(const type_info&) const _NOEXCEPT;
0283 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT;
0284 # endif
0285 };
0286
0287 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
0288 __base<_Rp(_ArgTypes...)>* __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const {
0289 typedef allocator_traits<_Alloc> __alloc_traits;
0290 typedef __rebind_alloc<__alloc_traits, __func> _Ap;
0291 _Ap __a(__f_.__get_allocator());
0292 typedef __allocator_destructor<_Ap> _Dp;
0293 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
0294 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
0295 return __hold.release();
0296 }
0297
0298 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
0299 void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const {
0300 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
0301 }
0302
0303 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
0304 void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT {
0305 __f_.destroy();
0306 }
0307
0308 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
0309 void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT {
0310 typedef allocator_traits<_Alloc> __alloc_traits;
0311 typedef __rebind_alloc<__alloc_traits, __func> _Ap;
0312 _Ap __a(__f_.__get_allocator());
0313 __f_.destroy();
0314 __a.deallocate(this, 1);
0315 }
0316
0317 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
0318 _Rp __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
0319 return __f_(std::forward<_ArgTypes>(__arg)...);
0320 }
0321
0322 # if _LIBCPP_HAS_RTTI
0323
0324 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
0325 const void* __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT {
0326 if (__ti == typeid(_Fp))
0327 return std::addressof(__f_.__target());
0328 return nullptr;
0329 }
0330
0331 template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
0332 const std::type_info& __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT {
0333 return typeid(_Fp);
0334 }
0335
0336 # endif
0337
0338
0339
0340 template <class _Fp>
0341 class __value_func;
0342
0343 template <class _Rp, class... _ArgTypes>
0344 class __value_func<_Rp(_ArgTypes...)> {
0345 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0346 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
0347 _LIBCPP_SUPPRESS_DEPRECATED_POP
0348
0349 typedef __base<_Rp(_ArgTypes...)> __func;
0350 __func* __f_;
0351
0352 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI static __func* __as_base(void* __p) { return reinterpret_cast<__func*>(__p); }
0353
0354 public:
0355 _LIBCPP_HIDE_FROM_ABI __value_func() _NOEXCEPT : __f_(nullptr) {}
0356
0357 template <class _Fp, class _Alloc>
0358 _LIBCPP_HIDE_FROM_ABI __value_func(_Fp&& __f, const _Alloc& __a) : __f_(nullptr) {
0359 typedef allocator_traits<_Alloc> __alloc_traits;
0360 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
0361 typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
0362
0363 if (__function::__not_null(__f)) {
0364 _FunAlloc __af(__a);
0365 if (sizeof(_Fun) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value &&
0366 is_nothrow_copy_constructible<_FunAlloc>::value) {
0367 __f_ = ::new ((void*)&__buf_) _Fun(std::move(__f), _Alloc(__af));
0368 } else {
0369 typedef __allocator_destructor<_FunAlloc> _Dp;
0370 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
0371 ::new ((void*)__hold.get()) _Fun(std::move(__f), _Alloc(__a));
0372 __f_ = __hold.release();
0373 }
0374 }
0375 }
0376
0377 template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __value_func>::value, int> = 0>
0378 _LIBCPP_HIDE_FROM_ABI explicit __value_func(_Fp&& __f) : __value_func(std::forward<_Fp>(__f), allocator<_Fp>()) {}
0379
0380 _LIBCPP_HIDE_FROM_ABI __value_func(const __value_func& __f) {
0381 if (__f.__f_ == nullptr)
0382 __f_ = nullptr;
0383 else if ((void*)__f.__f_ == &__f.__buf_) {
0384 __f_ = __as_base(&__buf_);
0385 __f.__f_->__clone(__f_);
0386 } else
0387 __f_ = __f.__f_->__clone();
0388 }
0389
0390 _LIBCPP_HIDE_FROM_ABI __value_func(__value_func&& __f) _NOEXCEPT {
0391 if (__f.__f_ == nullptr)
0392 __f_ = nullptr;
0393 else if ((void*)__f.__f_ == &__f.__buf_) {
0394 __f_ = __as_base(&__buf_);
0395 __f.__f_->__clone(__f_);
0396 } else {
0397 __f_ = __f.__f_;
0398 __f.__f_ = nullptr;
0399 }
0400 }
0401
0402 _LIBCPP_HIDE_FROM_ABI ~__value_func() {
0403 if ((void*)__f_ == &__buf_)
0404 __f_->destroy();
0405 else if (__f_)
0406 __f_->destroy_deallocate();
0407 }
0408
0409 _LIBCPP_HIDE_FROM_ABI __value_func& operator=(__value_func&& __f) {
0410 *this = nullptr;
0411 if (__f.__f_ == nullptr)
0412 __f_ = nullptr;
0413 else if ((void*)__f.__f_ == &__f.__buf_) {
0414 __f_ = __as_base(&__buf_);
0415 __f.__f_->__clone(__f_);
0416 } else {
0417 __f_ = __f.__f_;
0418 __f.__f_ = nullptr;
0419 }
0420 return *this;
0421 }
0422
0423 _LIBCPP_HIDE_FROM_ABI __value_func& operator=(nullptr_t) {
0424 __func* __f = __f_;
0425 __f_ = nullptr;
0426 if ((void*)__f == &__buf_)
0427 __f->destroy();
0428 else if (__f)
0429 __f->destroy_deallocate();
0430 return *this;
0431 }
0432
0433 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __args) const {
0434 if (__f_ == nullptr)
0435 __throw_bad_function_call();
0436 return (*__f_)(std::forward<_ArgTypes>(__args)...);
0437 }
0438
0439 _LIBCPP_HIDE_FROM_ABI void swap(__value_func& __f) _NOEXCEPT {
0440 if (&__f == this)
0441 return;
0442 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_) {
0443 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0444 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
0445 _LIBCPP_SUPPRESS_DEPRECATED_POP
0446 __func* __t = __as_base(&__tempbuf);
0447 __f_->__clone(__t);
0448 __f_->destroy();
0449 __f_ = nullptr;
0450 __f.__f_->__clone(__as_base(&__buf_));
0451 __f.__f_->destroy();
0452 __f.__f_ = nullptr;
0453 __f_ = __as_base(&__buf_);
0454 __t->__clone(__as_base(&__f.__buf_));
0455 __t->destroy();
0456 __f.__f_ = __as_base(&__f.__buf_);
0457 } else if ((void*)__f_ == &__buf_) {
0458 __f_->__clone(__as_base(&__f.__buf_));
0459 __f_->destroy();
0460 __f_ = __f.__f_;
0461 __f.__f_ = __as_base(&__f.__buf_);
0462 } else if ((void*)__f.__f_ == &__f.__buf_) {
0463 __f.__f_->__clone(__as_base(&__buf_));
0464 __f.__f_->destroy();
0465 __f.__f_ = __f_;
0466 __f_ = __as_base(&__buf_);
0467 } else
0468 std::swap(__f_, __f.__f_);
0469 }
0470
0471 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
0472
0473 # if _LIBCPP_HAS_RTTI
0474 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT {
0475 if (__f_ == nullptr)
0476 return typeid(void);
0477 return __f_->target_type();
0478 }
0479
0480 template <typename _Tp>
0481 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {
0482 if (__f_ == nullptr)
0483 return nullptr;
0484 return (const _Tp*)__f_->target(typeid(_Tp));
0485 }
0486 # endif
0487 };
0488
0489
0490
0491 union __policy_storage {
0492 mutable char __small[sizeof(void*) * 2];
0493 void* __large;
0494 };
0495
0496
0497 template <typename _Fun>
0498 struct __use_small_storage
0499 : public integral_constant<
0500 bool,
0501 sizeof(_Fun) <= sizeof(__policy_storage)&& _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
0502 is_trivially_copy_constructible<_Fun>::value && is_trivially_destructible<_Fun>::value> {};
0503
0504
0505
0506 struct __policy {
0507
0508 void* (*const __clone)(const void*);
0509 void (*const __destroy)(void*);
0510
0511
0512 const bool __is_null;
0513
0514
0515 const std::type_info* const __type_info;
0516
0517
0518
0519 template <typename _Fun>
0520 _LIBCPP_HIDE_FROM_ABI static const __policy* __create() {
0521 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
0522 }
0523
0524 _LIBCPP_HIDE_FROM_ABI static const __policy* __create_empty() {
0525 static constexpr __policy __policy = {
0526 nullptr,
0527 nullptr,
0528 true,
0529 # if _LIBCPP_HAS_RTTI
0530 &typeid(void)
0531 # else
0532 nullptr
0533 # endif
0534 };
0535 return &__policy;
0536 }
0537
0538 private:
0539 template <typename _Fun>
0540 _LIBCPP_HIDE_FROM_ABI static void* __large_clone(const void* __s) {
0541 const _Fun* __f = static_cast<const _Fun*>(__s);
0542 return __f->__clone();
0543 }
0544
0545 template <typename _Fun>
0546 _LIBCPP_HIDE_FROM_ABI static void __large_destroy(void* __s) {
0547 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
0548 }
0549
0550 template <typename _Fun>
0551 _LIBCPP_HIDE_FROM_ABI static const __policy* __choose_policy( false_type) {
0552 static constexpr __policy __policy = {
0553 &__large_clone<_Fun>,
0554 &__large_destroy<_Fun>,
0555 false,
0556 # if _LIBCPP_HAS_RTTI
0557 &typeid(typename _Fun::_Target)
0558 # else
0559 nullptr
0560 # endif
0561 };
0562 return &__policy;
0563 }
0564
0565 template <typename _Fun>
0566 _LIBCPP_HIDE_FROM_ABI static const __policy* __choose_policy( true_type) {
0567 static constexpr __policy __policy = {
0568 nullptr,
0569 nullptr,
0570 false,
0571 # if _LIBCPP_HAS_RTTI
0572 &typeid(typename _Fun::_Target)
0573 # else
0574 nullptr
0575 # endif
0576 };
0577 return &__policy;
0578 }
0579 };
0580
0581
0582
0583 template <typename _Tp>
0584 using __fast_forward _LIBCPP_NODEBUG = __conditional_t<is_scalar<_Tp>::value, _Tp, _Tp&&>;
0585
0586
0587
0588 template <class _Fp>
0589 struct __policy_invoker;
0590
0591 template <class _Rp, class... _ArgTypes>
0592 struct __policy_invoker<_Rp(_ArgTypes...)> {
0593 typedef _Rp (*__Call)(const __policy_storage*, __fast_forward<_ArgTypes>...);
0594
0595 __Call __call_;
0596
0597
0598 _LIBCPP_HIDE_FROM_ABI __policy_invoker() : __call_(&__call_empty) {}
0599
0600
0601 template <typename _Fun>
0602 _LIBCPP_HIDE_FROM_ABI static __policy_invoker __create() {
0603 return __policy_invoker(&__call_impl<_Fun>);
0604 }
0605
0606 private:
0607 _LIBCPP_HIDE_FROM_ABI explicit __policy_invoker(__Call __c) : __call_(__c) {}
0608
0609 _LIBCPP_HIDE_FROM_ABI static _Rp __call_empty(const __policy_storage*, __fast_forward<_ArgTypes>...) {
0610 __throw_bad_function_call();
0611 }
0612
0613 template <typename _Fun>
0614 _LIBCPP_HIDE_FROM_ABI static _Rp __call_impl(const __policy_storage* __buf, __fast_forward<_ArgTypes>... __args) {
0615 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value ? &__buf->__small : __buf->__large);
0616 return (*__f)(std::forward<_ArgTypes>(__args)...);
0617 }
0618 };
0619
0620
0621
0622
0623 template <class _Fp>
0624 class __policy_func;
0625
0626 template <class _Rp, class... _ArgTypes>
0627 class __policy_func<_Rp(_ArgTypes...)> {
0628
0629 __policy_storage __buf_;
0630
0631
0632
0633
0634 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
0635 __invoker __invoker_;
0636
0637
0638
0639 const __policy* __policy_;
0640
0641 public:
0642 _LIBCPP_HIDE_FROM_ABI __policy_func() : __policy_(__policy::__create_empty()) {}
0643
0644 template <class _Fp, class _Alloc>
0645 _LIBCPP_HIDE_FROM_ABI __policy_func(_Fp&& __f, const _Alloc& __a) : __policy_(__policy::__create_empty()) {
0646 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
0647 typedef allocator_traits<_Alloc> __alloc_traits;
0648 typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
0649
0650 if (__function::__not_null(__f)) {
0651 __invoker_ = __invoker::template __create<_Fun>();
0652 __policy_ = __policy::__create<_Fun>();
0653
0654 _FunAlloc __af(__a);
0655 if (__use_small_storage<_Fun>()) {
0656 ::new ((void*)&__buf_.__small) _Fun(std::move(__f), _Alloc(__af));
0657 } else {
0658 typedef __allocator_destructor<_FunAlloc> _Dp;
0659 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
0660 ::new ((void*)__hold.get()) _Fun(std::move(__f), _Alloc(__af));
0661 __buf_.__large = __hold.release();
0662 }
0663 }
0664 }
0665
0666 template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __policy_func>::value, int> = 0>
0667 _LIBCPP_HIDE_FROM_ABI explicit __policy_func(_Fp&& __f) : __policy_(__policy::__create_empty()) {
0668 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
0669
0670 if (__function::__not_null(__f)) {
0671 __invoker_ = __invoker::template __create<_Fun>();
0672 __policy_ = __policy::__create<_Fun>();
0673 if (__use_small_storage<_Fun>()) {
0674 ::new ((void*)&__buf_.__small) _Fun(std::move(__f));
0675 } else {
0676 unique_ptr<_Fun, __deallocating_deleter<_Fun>> __hold(std::__libcpp_allocate<_Fun>(__element_count(1)));
0677 __buf_.__large = ::new ((void*)__hold.get()) _Fun(std::move(__f));
0678 (void)__hold.release();
0679 }
0680 }
0681 }
0682
0683 _LIBCPP_HIDE_FROM_ABI __policy_func(const __policy_func& __f)
0684 : __buf_(__f.__buf_), __invoker_(__f.__invoker_), __policy_(__f.__policy_) {
0685 if (__policy_->__clone)
0686 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
0687 }
0688
0689 _LIBCPP_HIDE_FROM_ABI __policy_func(__policy_func&& __f)
0690 : __buf_(__f.__buf_), __invoker_(__f.__invoker_), __policy_(__f.__policy_) {
0691 if (__policy_->__destroy) {
0692 __f.__policy_ = __policy::__create_empty();
0693 __f.__invoker_ = __invoker();
0694 }
0695 }
0696
0697 _LIBCPP_HIDE_FROM_ABI ~__policy_func() {
0698 if (__policy_->__destroy)
0699 __policy_->__destroy(__buf_.__large);
0700 }
0701
0702 _LIBCPP_HIDE_FROM_ABI __policy_func& operator=(__policy_func&& __f) {
0703 *this = nullptr;
0704 __buf_ = __f.__buf_;
0705 __invoker_ = __f.__invoker_;
0706 __policy_ = __f.__policy_;
0707 __f.__policy_ = __policy::__create_empty();
0708 __f.__invoker_ = __invoker();
0709 return *this;
0710 }
0711
0712 _LIBCPP_HIDE_FROM_ABI __policy_func& operator=(nullptr_t) {
0713 const __policy* __p = __policy_;
0714 __policy_ = __policy::__create_empty();
0715 __invoker_ = __invoker();
0716 if (__p->__destroy)
0717 __p->__destroy(__buf_.__large);
0718 return *this;
0719 }
0720
0721 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __args) const {
0722 return __invoker_.__call_(std::addressof(__buf_), std::forward<_ArgTypes>(__args)...);
0723 }
0724
0725 _LIBCPP_HIDE_FROM_ABI void swap(__policy_func& __f) {
0726 std::swap(__invoker_, __f.__invoker_);
0727 std::swap(__policy_, __f.__policy_);
0728 std::swap(__buf_, __f.__buf_);
0729 }
0730
0731 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return !__policy_->__is_null; }
0732
0733 # if _LIBCPP_HAS_RTTI
0734 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT { return *__policy_->__type_info; }
0735
0736 template <typename _Tp>
0737 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {
0738 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
0739 return nullptr;
0740 if (__policy_->__clone)
0741 return reinterpret_cast<const _Tp*>(__buf_.__large);
0742 else
0743 return reinterpret_cast<const _Tp*>(&__buf_.__small);
0744 }
0745 # endif
0746 };
0747
0748 # if _LIBCPP_HAS_BLOCKS_RUNTIME
0749
0750 extern "C" void* _Block_copy(const void*);
0751 extern "C" void _Block_release(const void*);
0752
0753 template <class _Rp1, class... _ArgTypes1, class _Alloc, class _Rp, class... _ArgTypes>
0754 class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base<_Rp(_ArgTypes...)> {
0755 typedef _Rp1 (^__block_type)(_ArgTypes1...);
0756 __block_type __f_;
0757
0758 public:
0759 _LIBCPP_HIDE_FROM_ABI explicit __func(__block_type const& __f)
0760 # if _LIBCPP_HAS_OBJC_ARC
0761 : __f_(__f)
0762 # else
0763 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
0764 # endif
0765 {
0766 }
0767
0768
0769
0770 _LIBCPP_HIDE_FROM_ABI explicit __func(__block_type __f, const _Alloc& )
0771 # if _LIBCPP_HAS_OBJC_ARC
0772 : __f_(__f)
0773 # else
0774 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
0775 # endif
0776 {
0777 }
0778
0779 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const {
0780 _LIBCPP_ASSERT_INTERNAL(
0781 false,
0782 "Block pointers are just pointers, so they should always fit into "
0783 "std::function's small buffer optimization. This function should "
0784 "never be invoked.");
0785 return nullptr;
0786 }
0787
0788 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
0789 ::new ((void*)__p) __func(__f_);
0790 }
0791
0792 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT {
0793 # if !_LIBCPP_HAS_OBJC_ARC
0794 if (__f_)
0795 _Block_release(__f_);
0796 # endif
0797 __f_ = 0;
0798 }
0799
0800 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT {
0801 _LIBCPP_ASSERT_INTERNAL(
0802 false,
0803 "Block pointers are just pointers, so they should always fit into "
0804 "std::function's small buffer optimization. This function should "
0805 "never be invoked.");
0806 }
0807
0808 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg) {
0809 return std::__invoke(__f_, std::forward<_ArgTypes>(__arg)...);
0810 }
0811
0812 # if _LIBCPP_HAS_RTTI
0813 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(type_info const& __ti) const _NOEXCEPT {
0814 if (__ti == typeid(__func::__block_type))
0815 return &__f_;
0816 return (const void*)nullptr;
0817 }
0818
0819 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT {
0820 return typeid(__func::__block_type);
0821 }
0822 # endif
0823 };
0824
0825 # endif
0826
0827 }
0828
0829 template <class _Rp, class... _ArgTypes>
0830 class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
0831 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
0832 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)> {
0833 # ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
0834 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
0835 # else
0836 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
0837 # endif
0838
0839 __func __f_;
0840
0841 template <class _Fp,
0842 bool = _And<_IsNotSame<__remove_cvref_t<_Fp>, function>, __is_invocable<_Fp, _ArgTypes...> >::value>
0843 struct __callable;
0844 template <class _Fp>
0845 struct __callable<_Fp, true> {
0846 static const bool value =
0847 is_void<_Rp>::value || __is_core_convertible<__invoke_result_t<_Fp, _ArgTypes...>, _Rp>::value;
0848 };
0849 template <class _Fp>
0850 struct __callable<_Fp, false> {
0851 static const bool value = false;
0852 };
0853
0854 template <class _Fp>
0855 using _EnableIfLValueCallable _LIBCPP_NODEBUG = __enable_if_t<__callable<_Fp&>::value>;
0856
0857 public:
0858 typedef _Rp result_type;
0859
0860
0861 _LIBCPP_HIDE_FROM_ABI function() _NOEXCEPT {}
0862 _LIBCPP_HIDE_FROM_ABI function(nullptr_t) _NOEXCEPT {}
0863 _LIBCPP_HIDE_FROM_ABI function(const function&);
0864 _LIBCPP_HIDE_FROM_ABI function(function&&) _NOEXCEPT;
0865 template <class _Fp, class = _EnableIfLValueCallable<_Fp>>
0866 _LIBCPP_HIDE_FROM_ABI function(_Fp);
0867
0868 # if _LIBCPP_STD_VER <= 14
0869 template <class _Alloc>
0870 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
0871 template <class _Alloc>
0872 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
0873 template <class _Alloc>
0874 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, const function&);
0875 template <class _Alloc>
0876 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, function&&);
0877 template <class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
0878 _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc& __a, _Fp __f);
0879 # endif
0880
0881 _LIBCPP_HIDE_FROM_ABI function& operator=(const function&);
0882 _LIBCPP_HIDE_FROM_ABI function& operator=(function&&) _NOEXCEPT;
0883 _LIBCPP_HIDE_FROM_ABI function& operator=(nullptr_t) _NOEXCEPT;
0884 template <class _Fp, class = _EnableIfLValueCallable<__decay_t<_Fp>>>
0885 _LIBCPP_HIDE_FROM_ABI function& operator=(_Fp&&);
0886
0887 _LIBCPP_HIDE_FROM_ABI ~function();
0888
0889
0890 _LIBCPP_HIDE_FROM_ABI void swap(function&) _NOEXCEPT;
0891
0892 # if _LIBCPP_STD_VER <= 14
0893 template <class _Fp, class _Alloc>
0894 _LIBCPP_HIDE_FROM_ABI void assign(_Fp&& __f, const _Alloc& __a) {
0895 function(allocator_arg, __a, std::forward<_Fp>(__f)).swap(*this);
0896 }
0897 # endif
0898
0899
0900 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return static_cast<bool>(__f_); }
0901
0902
0903 template <class _R2, class... _ArgTypes2>
0904 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
0905 # if _LIBCPP_STD_VER <= 17
0906 template <class _R2, class... _ArgTypes2>
0907 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
0908 # endif
0909
0910 public:
0911
0912 _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
0913
0914 # if _LIBCPP_HAS_RTTI
0915
0916 _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT;
0917 template <typename _Tp>
0918 _LIBCPP_HIDE_FROM_ABI _Tp* target() _NOEXCEPT;
0919 template <typename _Tp>
0920 _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT;
0921 # endif
0922 };
0923
0924 # if _LIBCPP_STD_VER >= 17
0925 template <class _Rp, class... _Ap>
0926 function(_Rp (*)(_Ap...)) -> function<_Rp(_Ap...)>;
0927
0928 template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
0929 function(_Fp) -> function<_Stripped>;
0930 # endif
0931
0932 template <class _Rp, class... _ArgTypes>
0933 function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
0934
0935 # if _LIBCPP_STD_VER <= 14
0936 template <class _Rp, class... _ArgTypes>
0937 template <class _Alloc>
0938 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, const function& __f) : __f_(__f.__f_) {}
0939 # endif
0940
0941 template <class _Rp, class... _ArgTypes>
0942 function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT : __f_(std::move(__f.__f_)) {}
0943
0944 # if _LIBCPP_STD_VER <= 14
0945 template <class _Rp, class... _ArgTypes>
0946 template <class _Alloc>
0947 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, function&& __f) : __f_(std::move(__f.__f_)) {}
0948 # endif
0949
0950 template <class _Rp, class... _ArgTypes>
0951 template <class _Fp, class>
0952 function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(std::move(__f)) {}
0953
0954 # if _LIBCPP_STD_VER <= 14
0955 template <class _Rp, class... _ArgTypes>
0956 template <class _Fp, class _Alloc, class>
0957 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a, _Fp __f) : __f_(std::move(__f), __a) {}
0958 # endif
0959
0960 template <class _Rp, class... _ArgTypes>
0961 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(const function& __f) {
0962 function(__f).swap(*this);
0963 return *this;
0964 }
0965
0966 template <class _Rp, class... _ArgTypes>
0967 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT {
0968 __f_ = std::move(__f.__f_);
0969 return *this;
0970 }
0971
0972 template <class _Rp, class... _ArgTypes>
0973 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT {
0974 __f_ = nullptr;
0975 return *this;
0976 }
0977
0978 template <class _Rp, class... _ArgTypes>
0979 template <class _Fp, class>
0980 function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f) {
0981 function(std::forward<_Fp>(__f)).swap(*this);
0982 return *this;
0983 }
0984
0985 template <class _Rp, class... _ArgTypes>
0986 function<_Rp(_ArgTypes...)>::~function() {}
0987
0988 template <class _Rp, class... _ArgTypes>
0989 void function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT {
0990 __f_.swap(__f.__f_);
0991 }
0992
0993 template <class _Rp, class... _ArgTypes>
0994 _Rp function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
0995 return __f_(std::forward<_ArgTypes>(__arg)...);
0996 }
0997
0998 # if _LIBCPP_HAS_RTTI
0999
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
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
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
1051
1052 _LIBCPP_POP_MACROS
1053
1054 #endif