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