Path: blob/main/contrib/llvm-project/libcxx/include/__functional/function.h
35260 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H10#define _LIBCPP___FUNCTIONAL_FUNCTION_H1112#include <__assert>13#include <__config>14#include <__exception/exception.h>15#include <__functional/binary_function.h>16#include <__functional/invoke.h>17#include <__functional/unary_function.h>18#include <__iterator/iterator_traits.h>19#include <__memory/addressof.h>20#include <__memory/allocator.h>21#include <__memory/allocator_destructor.h>22#include <__memory/allocator_traits.h>23#include <__memory/builtin_new_allocator.h>24#include <__memory/compressed_pair.h>25#include <__memory/unique_ptr.h>26#include <__type_traits/aligned_storage.h>27#include <__type_traits/decay.h>28#include <__type_traits/is_core_convertible.h>29#include <__type_traits/is_scalar.h>30#include <__type_traits/is_trivially_constructible.h>31#include <__type_traits/is_trivially_destructible.h>32#include <__type_traits/is_void.h>33#include <__type_traits/strip_signature.h>34#include <__utility/forward.h>35#include <__utility/move.h>36#include <__utility/piecewise_construct.h>37#include <__utility/swap.h>38#include <__verbose_abort>39#include <new>40#include <tuple>41#include <typeinfo>4243#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)44# pragma GCC system_header45#endif4647_LIBCPP_PUSH_MACROS48#include <__undef_macros>4950#ifndef _LIBCPP_CXX03_LANG5152_LIBCPP_BEGIN_NAMESPACE_STD5354// bad_function_call5556_LIBCPP_DIAGNOSTIC_PUSH57# if !_LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION58_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")59# endif60class _LIBCPP_EXPORTED_FROM_ABI bad_function_call : public exception {61public:62_LIBCPP_HIDE_FROM_ABI bad_function_call() _NOEXCEPT = default;63_LIBCPP_HIDE_FROM_ABI bad_function_call(const bad_function_call&) _NOEXCEPT = default;64_LIBCPP_HIDE_FROM_ABI bad_function_call& operator=(const bad_function_call&) _NOEXCEPT = default;65// Note that when a key function is not used, every translation unit that uses66// bad_function_call will end up containing a weak definition of the vtable and67// typeinfo.68# if _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION69~bad_function_call() _NOEXCEPT override;70# else71_LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_function_call() _NOEXCEPT override {}72# endif7374# ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE75const char* what() const _NOEXCEPT override;76# endif77};78_LIBCPP_DIAGNOSTIC_POP7980_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_function_call() {81# ifndef _LIBCPP_HAS_NO_EXCEPTIONS82throw bad_function_call();83# else84_LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode");85# endif86}8788template <class _Fp>89class _LIBCPP_TEMPLATE_VIS function; // undefined9091namespace __function {9293template <class _Rp>94struct __maybe_derive_from_unary_function {};9596template <class _Rp, class _A1>97struct __maybe_derive_from_unary_function<_Rp(_A1)> : public __unary_function<_A1, _Rp> {};9899template <class _Rp>100struct __maybe_derive_from_binary_function {};101102template <class _Rp, class _A1, class _A2>103struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)> : public __binary_function<_A1, _A2, _Rp> {};104105template <class _Fp>106_LIBCPP_HIDE_FROM_ABI bool __not_null(_Fp const&) {107return true;108}109110template <class _Fp>111_LIBCPP_HIDE_FROM_ABI bool __not_null(_Fp* __ptr) {112return __ptr;113}114115template <class _Ret, class _Class>116_LIBCPP_HIDE_FROM_ABI bool __not_null(_Ret _Class::*__ptr) {117return __ptr;118}119120template <class _Fp>121_LIBCPP_HIDE_FROM_ABI bool __not_null(function<_Fp> const& __f) {122return !!__f;123}124125# ifdef _LIBCPP_HAS_EXTENSION_BLOCKS126template <class _Rp, class... _Args>127_LIBCPP_HIDE_FROM_ABI bool __not_null(_Rp (^__p)(_Args...)) {128return __p;129}130# endif131132} // namespace __function133134namespace __function {135136// __alloc_func holds a functor and an allocator.137138template <class _Fp, class _Ap, class _FB>139class __alloc_func;140template <class _Fp, class _FB>141class __default_alloc_func;142143template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>144class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)> {145__compressed_pair<_Fp, _Ap> __f_;146147public:148typedef _LIBCPP_NODEBUG _Fp _Target;149typedef _LIBCPP_NODEBUG _Ap _Alloc;150151_LIBCPP_HIDE_FROM_ABI const _Target& __target() const { return __f_.first(); }152153// WIN32 APIs may define __allocator, so use __get_allocator instead.154_LIBCPP_HIDE_FROM_ABI const _Alloc& __get_allocator() const { return __f_.second(); }155156_LIBCPP_HIDE_FROM_ABI explicit __alloc_func(_Target&& __f)157: __f_(piecewise_construct, std::forward_as_tuple(std::move(__f)), std::forward_as_tuple()) {}158159_LIBCPP_HIDE_FROM_ABI explicit __alloc_func(const _Target& __f, const _Alloc& __a)160: __f_(piecewise_construct, std::forward_as_tuple(__f), std::forward_as_tuple(__a)) {}161162_LIBCPP_HIDE_FROM_ABI explicit __alloc_func(const _Target& __f, _Alloc&& __a)163: __f_(piecewise_construct, std::forward_as_tuple(__f), std::forward_as_tuple(std::move(__a))) {}164165_LIBCPP_HIDE_FROM_ABI explicit __alloc_func(_Target&& __f, _Alloc&& __a)166: __f_(piecewise_construct, std::forward_as_tuple(std::move(__f)), std::forward_as_tuple(std::move(__a))) {}167168_LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __arg) {169typedef __invoke_void_return_wrapper<_Rp> _Invoker;170return _Invoker::__call(__f_.first(), std::forward<_ArgTypes>(__arg)...);171}172173_LIBCPP_HIDE_FROM_ABI __alloc_func* __clone() const {174typedef allocator_traits<_Alloc> __alloc_traits;175typedef __rebind_alloc<__alloc_traits, __alloc_func> _AA;176_AA __a(__f_.second());177typedef __allocator_destructor<_AA> _Dp;178unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));179::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));180return __hold.release();181}182183_LIBCPP_HIDE_FROM_ABI void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }184185_LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__alloc_func* __f) {186typedef allocator_traits<_Alloc> __alloc_traits;187typedef __rebind_alloc<__alloc_traits, __alloc_func> _FunAlloc;188_FunAlloc __a(__f->__get_allocator());189__f->destroy();190__a.deallocate(__f, 1);191}192};193194template <class _Fp, class _Rp, class... _ArgTypes>195class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {196_Fp __f_;197198public:199typedef _LIBCPP_NODEBUG _Fp _Target;200201_LIBCPP_HIDE_FROM_ABI const _Target& __target() const { return __f_; }202203_LIBCPP_HIDE_FROM_ABI explicit __default_alloc_func(_Target&& __f) : __f_(std::move(__f)) {}204205_LIBCPP_HIDE_FROM_ABI explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}206207_LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __arg) {208typedef __invoke_void_return_wrapper<_Rp> _Invoker;209return _Invoker::__call(__f_, std::forward<_ArgTypes>(__arg)...);210}211212_LIBCPP_HIDE_FROM_ABI __default_alloc_func* __clone() const {213__builtin_new_allocator::__holder_t __hold = __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);214__default_alloc_func* __res = ::new ((void*)__hold.get()) __default_alloc_func(__f_);215(void)__hold.release();216return __res;217}218219_LIBCPP_HIDE_FROM_ABI void destroy() _NOEXCEPT { __f_.~_Target(); }220221_LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__default_alloc_func* __f) {222__f->destroy();223__builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);224}225};226227// __base provides an abstract interface for copyable functors.228229template <class _Fp>230class _LIBCPP_TEMPLATE_VIS __base;231232template <class _Rp, class... _ArgTypes>233class __base<_Rp(_ArgTypes...)> {234public:235__base(const __base&) = delete;236__base& operator=(const __base&) = delete;237238_LIBCPP_HIDE_FROM_ABI __base() {}239_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__base() {}240virtual __base* __clone() const = 0;241virtual void __clone(__base*) const = 0;242virtual void destroy() _NOEXCEPT = 0;243virtual void destroy_deallocate() _NOEXCEPT = 0;244virtual _Rp operator()(_ArgTypes&&...) = 0;245# ifndef _LIBCPP_HAS_NO_RTTI246virtual const void* target(const type_info&) const _NOEXCEPT = 0;247virtual const std::type_info& target_type() const _NOEXCEPT = 0;248# endif // _LIBCPP_HAS_NO_RTTI249};250251// __func implements __base for a given functor type.252253template <class _FD, class _Alloc, class _FB>254class __func;255256template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>257class __func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __base<_Rp(_ArgTypes...)> {258__alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;259260public:261_LIBCPP_HIDE_FROM_ABI explicit __func(_Fp&& __f) : __f_(std::move(__f)) {}262263_LIBCPP_HIDE_FROM_ABI explicit __func(const _Fp& __f, const _Alloc& __a) : __f_(__f, __a) {}264265_LIBCPP_HIDE_FROM_ABI explicit __func(const _Fp& __f, _Alloc&& __a) : __f_(__f, std::move(__a)) {}266267_LIBCPP_HIDE_FROM_ABI explicit __func(_Fp&& __f, _Alloc&& __a) : __f_(std::move(__f), std::move(__a)) {}268269_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const;270_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;271_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT;272_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT;273_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg);274# ifndef _LIBCPP_HAS_NO_RTTI275_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(const type_info&) const _NOEXCEPT;276_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT;277# endif // _LIBCPP_HAS_NO_RTTI278};279280template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>281__base<_Rp(_ArgTypes...)>* __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const {282typedef allocator_traits<_Alloc> __alloc_traits;283typedef __rebind_alloc<__alloc_traits, __func> _Ap;284_Ap __a(__f_.__get_allocator());285typedef __allocator_destructor<_Ap> _Dp;286unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));287::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));288return __hold.release();289}290291template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>292void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const {293::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());294}295296template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>297void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT {298__f_.destroy();299}300301template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>302void __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT {303typedef allocator_traits<_Alloc> __alloc_traits;304typedef __rebind_alloc<__alloc_traits, __func> _Ap;305_Ap __a(__f_.__get_allocator());306__f_.destroy();307__a.deallocate(this, 1);308}309310template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>311_Rp __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {312return __f_(std::forward<_ArgTypes>(__arg)...);313}314315# ifndef _LIBCPP_HAS_NO_RTTI316317template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>318const void* __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT {319if (__ti == typeid(_Fp))320return std::addressof(__f_.__target());321return nullptr;322}323324template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>325const std::type_info& __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT {326return typeid(_Fp);327}328329# endif // _LIBCPP_HAS_NO_RTTI330331// __value_func creates a value-type from a __func.332333template <class _Fp>334class __value_func;335336template <class _Rp, class... _ArgTypes>337class __value_func<_Rp(_ArgTypes...)> {338_LIBCPP_SUPPRESS_DEPRECATED_PUSH339typename aligned_storage<3 * sizeof(void*)>::type __buf_;340_LIBCPP_SUPPRESS_DEPRECATED_POP341342typedef __base<_Rp(_ArgTypes...)> __func;343__func* __f_;344345_LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI static __func* __as_base(void* __p) { return reinterpret_cast<__func*>(__p); }346347public:348_LIBCPP_HIDE_FROM_ABI __value_func() _NOEXCEPT : __f_(nullptr) {}349350template <class _Fp, class _Alloc>351_LIBCPP_HIDE_FROM_ABI __value_func(_Fp&& __f, const _Alloc& __a) : __f_(nullptr) {352typedef allocator_traits<_Alloc> __alloc_traits;353typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;354typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;355356if (__function::__not_null(__f)) {357_FunAlloc __af(__a);358if (sizeof(_Fun) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value &&359is_nothrow_copy_constructible<_FunAlloc>::value) {360__f_ = ::new ((void*)&__buf_) _Fun(std::move(__f), _Alloc(__af));361} else {362typedef __allocator_destructor<_FunAlloc> _Dp;363unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));364::new ((void*)__hold.get()) _Fun(std::move(__f), _Alloc(__a));365__f_ = __hold.release();366}367}368}369370template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __value_func>::value, int> = 0>371_LIBCPP_HIDE_FROM_ABI explicit __value_func(_Fp&& __f) : __value_func(std::forward<_Fp>(__f), allocator<_Fp>()) {}372373_LIBCPP_HIDE_FROM_ABI __value_func(const __value_func& __f) {374if (__f.__f_ == nullptr)375__f_ = nullptr;376else if ((void*)__f.__f_ == &__f.__buf_) {377__f_ = __as_base(&__buf_);378__f.__f_->__clone(__f_);379} else380__f_ = __f.__f_->__clone();381}382383_LIBCPP_HIDE_FROM_ABI __value_func(__value_func&& __f) _NOEXCEPT {384if (__f.__f_ == nullptr)385__f_ = nullptr;386else if ((void*)__f.__f_ == &__f.__buf_) {387__f_ = __as_base(&__buf_);388__f.__f_->__clone(__f_);389} else {390__f_ = __f.__f_;391__f.__f_ = nullptr;392}393}394395_LIBCPP_HIDE_FROM_ABI ~__value_func() {396if ((void*)__f_ == &__buf_)397__f_->destroy();398else if (__f_)399__f_->destroy_deallocate();400}401402_LIBCPP_HIDE_FROM_ABI __value_func& operator=(__value_func&& __f) {403*this = nullptr;404if (__f.__f_ == nullptr)405__f_ = nullptr;406else if ((void*)__f.__f_ == &__f.__buf_) {407__f_ = __as_base(&__buf_);408__f.__f_->__clone(__f_);409} else {410__f_ = __f.__f_;411__f.__f_ = nullptr;412}413return *this;414}415416_LIBCPP_HIDE_FROM_ABI __value_func& operator=(nullptr_t) {417__func* __f = __f_;418__f_ = nullptr;419if ((void*)__f == &__buf_)420__f->destroy();421else if (__f)422__f->destroy_deallocate();423return *this;424}425426_LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __args) const {427if (__f_ == nullptr)428__throw_bad_function_call();429return (*__f_)(std::forward<_ArgTypes>(__args)...);430}431432_LIBCPP_HIDE_FROM_ABI void swap(__value_func& __f) _NOEXCEPT {433if (&__f == this)434return;435if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_) {436_LIBCPP_SUPPRESS_DEPRECATED_PUSH437typename aligned_storage<sizeof(__buf_)>::type __tempbuf;438_LIBCPP_SUPPRESS_DEPRECATED_POP439__func* __t = __as_base(&__tempbuf);440__f_->__clone(__t);441__f_->destroy();442__f_ = nullptr;443__f.__f_->__clone(__as_base(&__buf_));444__f.__f_->destroy();445__f.__f_ = nullptr;446__f_ = __as_base(&__buf_);447__t->__clone(__as_base(&__f.__buf_));448__t->destroy();449__f.__f_ = __as_base(&__f.__buf_);450} else if ((void*)__f_ == &__buf_) {451__f_->__clone(__as_base(&__f.__buf_));452__f_->destroy();453__f_ = __f.__f_;454__f.__f_ = __as_base(&__f.__buf_);455} else if ((void*)__f.__f_ == &__f.__buf_) {456__f.__f_->__clone(__as_base(&__buf_));457__f.__f_->destroy();458__f.__f_ = __f_;459__f_ = __as_base(&__buf_);460} else461std::swap(__f_, __f.__f_);462}463464_LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }465466# ifndef _LIBCPP_HAS_NO_RTTI467_LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT {468if (__f_ == nullptr)469return typeid(void);470return __f_->target_type();471}472473template <typename _Tp>474_LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {475if (__f_ == nullptr)476return nullptr;477return (const _Tp*)__f_->target(typeid(_Tp));478}479# endif // _LIBCPP_HAS_NO_RTTI480};481482// Storage for a functor object, to be used with __policy to manage copy and483// destruction.484union __policy_storage {485mutable char __small[sizeof(void*) * 2];486void* __large;487};488489// True if _Fun can safely be held in __policy_storage.__small.490template <typename _Fun>491struct __use_small_storage492: public integral_constant<493bool,494sizeof(_Fun) <= sizeof(__policy_storage)&& _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&495is_trivially_copy_constructible<_Fun>::value && is_trivially_destructible<_Fun>::value> {};496497// Policy contains information about how to copy, destroy, and move the498// underlying functor. You can think of it as a vtable of sorts.499struct __policy {500// Used to copy or destroy __large values. null for trivial objects.501void* (*const __clone)(const void*);502void (*const __destroy)(void*);503504// True if this is the null policy (no value).505const bool __is_null;506507// The target type. May be null if RTTI is disabled.508const std::type_info* const __type_info;509510// Returns a pointer to a static policy object suitable for the functor511// type.512template <typename _Fun>513_LIBCPP_HIDE_FROM_ABI static const __policy* __create() {514return __choose_policy<_Fun>(__use_small_storage<_Fun>());515}516517_LIBCPP_HIDE_FROM_ABI static const __policy* __create_empty() {518static constexpr __policy __policy = {519nullptr,520nullptr,521true,522# ifndef _LIBCPP_HAS_NO_RTTI523&typeid(void)524# else525nullptr526# endif527};528return &__policy;529}530531private:532template <typename _Fun>533_LIBCPP_HIDE_FROM_ABI static void* __large_clone(const void* __s) {534const _Fun* __f = static_cast<const _Fun*>(__s);535return __f->__clone();536}537538template <typename _Fun>539_LIBCPP_HIDE_FROM_ABI static void __large_destroy(void* __s) {540_Fun::__destroy_and_delete(static_cast<_Fun*>(__s));541}542543template <typename _Fun>544_LIBCPP_HIDE_FROM_ABI static const __policy* __choose_policy(/* is_small = */ false_type) {545static constexpr __policy __policy = {546&__large_clone<_Fun>,547&__large_destroy<_Fun>,548false,549# ifndef _LIBCPP_HAS_NO_RTTI550&typeid(typename _Fun::_Target)551# else552nullptr553# endif554};555return &__policy;556}557558template <typename _Fun>559_LIBCPP_HIDE_FROM_ABI static const __policy* __choose_policy(/* is_small = */ true_type) {560static constexpr __policy __policy = {561nullptr,562nullptr,563false,564# ifndef _LIBCPP_HAS_NO_RTTI565&typeid(typename _Fun::_Target)566# else567nullptr568# endif569};570return &__policy;571}572};573574// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is575// faster for types that can be passed in registers.576template <typename _Tp>577using __fast_forward = __conditional_t<is_scalar<_Tp>::value, _Tp, _Tp&&>;578579// __policy_invoker calls an instance of __alloc_func held in __policy_storage.580581template <class _Fp>582struct __policy_invoker;583584template <class _Rp, class... _ArgTypes>585struct __policy_invoker<_Rp(_ArgTypes...)> {586typedef _Rp (*__Call)(const __policy_storage*, __fast_forward<_ArgTypes>...);587588__Call __call_;589590// Creates an invoker that throws bad_function_call.591_LIBCPP_HIDE_FROM_ABI __policy_invoker() : __call_(&__call_empty) {}592593// Creates an invoker that calls the given instance of __func.594template <typename _Fun>595_LIBCPP_HIDE_FROM_ABI static __policy_invoker __create() {596return __policy_invoker(&__call_impl<_Fun>);597}598599private:600_LIBCPP_HIDE_FROM_ABI explicit __policy_invoker(__Call __c) : __call_(__c) {}601602_LIBCPP_HIDE_FROM_ABI static _Rp __call_empty(const __policy_storage*, __fast_forward<_ArgTypes>...) {603__throw_bad_function_call();604}605606template <typename _Fun>607_LIBCPP_HIDE_FROM_ABI static _Rp __call_impl(const __policy_storage* __buf, __fast_forward<_ArgTypes>... __args) {608_Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value ? &__buf->__small : __buf->__large);609return (*__f)(std::forward<_ArgTypes>(__args)...);610}611};612613// __policy_func uses a __policy and __policy_invoker to create a type-erased,614// copyable functor.615616template <class _Fp>617class __policy_func;618619template <class _Rp, class... _ArgTypes>620class __policy_func<_Rp(_ArgTypes...)> {621// Inline storage for small objects.622__policy_storage __buf_;623624// Calls the value stored in __buf_. This could technically be part of625// policy, but storing it here eliminates a level of indirection inside626// operator().627typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;628__invoker __invoker_;629630// The policy that describes how to move / copy / destroy __buf_. Never631// null, even if the function is empty.632const __policy* __policy_;633634public:635_LIBCPP_HIDE_FROM_ABI __policy_func() : __policy_(__policy::__create_empty()) {}636637template <class _Fp, class _Alloc>638_LIBCPP_HIDE_FROM_ABI __policy_func(_Fp&& __f, const _Alloc& __a) : __policy_(__policy::__create_empty()) {639typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;640typedef allocator_traits<_Alloc> __alloc_traits;641typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;642643if (__function::__not_null(__f)) {644__invoker_ = __invoker::template __create<_Fun>();645__policy_ = __policy::__create<_Fun>();646647_FunAlloc __af(__a);648if (__use_small_storage<_Fun>()) {649::new ((void*)&__buf_.__small) _Fun(std::move(__f), _Alloc(__af));650} else {651typedef __allocator_destructor<_FunAlloc> _Dp;652unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));653::new ((void*)__hold.get()) _Fun(std::move(__f), _Alloc(__af));654__buf_.__large = __hold.release();655}656}657}658659template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __policy_func>::value, int> = 0>660_LIBCPP_HIDE_FROM_ABI explicit __policy_func(_Fp&& __f) : __policy_(__policy::__create_empty()) {661typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;662663if (__function::__not_null(__f)) {664__invoker_ = __invoker::template __create<_Fun>();665__policy_ = __policy::__create<_Fun>();666if (__use_small_storage<_Fun>()) {667::new ((void*)&__buf_.__small) _Fun(std::move(__f));668} else {669__builtin_new_allocator::__holder_t __hold = __builtin_new_allocator::__allocate_type<_Fun>(1);670__buf_.__large = ::new ((void*)__hold.get()) _Fun(std::move(__f));671(void)__hold.release();672}673}674}675676_LIBCPP_HIDE_FROM_ABI __policy_func(const __policy_func& __f)677: __buf_(__f.__buf_), __invoker_(__f.__invoker_), __policy_(__f.__policy_) {678if (__policy_->__clone)679__buf_.__large = __policy_->__clone(__f.__buf_.__large);680}681682_LIBCPP_HIDE_FROM_ABI __policy_func(__policy_func&& __f)683: __buf_(__f.__buf_), __invoker_(__f.__invoker_), __policy_(__f.__policy_) {684if (__policy_->__destroy) {685__f.__policy_ = __policy::__create_empty();686__f.__invoker_ = __invoker();687}688}689690_LIBCPP_HIDE_FROM_ABI ~__policy_func() {691if (__policy_->__destroy)692__policy_->__destroy(__buf_.__large);693}694695_LIBCPP_HIDE_FROM_ABI __policy_func& operator=(__policy_func&& __f) {696*this = nullptr;697__buf_ = __f.__buf_;698__invoker_ = __f.__invoker_;699__policy_ = __f.__policy_;700__f.__policy_ = __policy::__create_empty();701__f.__invoker_ = __invoker();702return *this;703}704705_LIBCPP_HIDE_FROM_ABI __policy_func& operator=(nullptr_t) {706const __policy* __p = __policy_;707__policy_ = __policy::__create_empty();708__invoker_ = __invoker();709if (__p->__destroy)710__p->__destroy(__buf_.__large);711return *this;712}713714_LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes&&... __args) const {715return __invoker_.__call_(std::addressof(__buf_), std::forward<_ArgTypes>(__args)...);716}717718_LIBCPP_HIDE_FROM_ABI void swap(__policy_func& __f) {719std::swap(__invoker_, __f.__invoker_);720std::swap(__policy_, __f.__policy_);721std::swap(__buf_, __f.__buf_);722}723724_LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return !__policy_->__is_null; }725726# ifndef _LIBCPP_HAS_NO_RTTI727_LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT { return *__policy_->__type_info; }728729template <typename _Tp>730_LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT {731if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)732return nullptr;733if (__policy_->__clone) // Out of line storage.734return reinterpret_cast<const _Tp*>(__buf_.__large);735else736return reinterpret_cast<const _Tp*>(&__buf_.__small);737}738# endif // _LIBCPP_HAS_NO_RTTI739};740741# if defined(_LIBCPP_HAS_BLOCKS_RUNTIME)742743extern "C" void* _Block_copy(const void*);744extern "C" void _Block_release(const void*);745746template <class _Rp1, class... _ArgTypes1, class _Alloc, class _Rp, class... _ArgTypes>747class __func<_Rp1 (^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)> : public __base<_Rp(_ArgTypes...)> {748typedef _Rp1 (^__block_type)(_ArgTypes1...);749__block_type __f_;750751public:752_LIBCPP_HIDE_FROM_ABI explicit __func(__block_type const& __f)753# ifdef _LIBCPP_HAS_OBJC_ARC754: __f_(__f)755# else756: __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))757# endif758{759}760761// [TODO] add && to save on a retain762763_LIBCPP_HIDE_FROM_ABI explicit __func(__block_type __f, const _Alloc& /* unused */)764# ifdef _LIBCPP_HAS_OBJC_ARC765: __f_(__f)766# else767: __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))768# endif769{770}771772_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const {773_LIBCPP_ASSERT_INTERNAL(774false,775"Block pointers are just pointers, so they should always fit into "776"std::function's small buffer optimization. This function should "777"never be invoked.");778return nullptr;779}780781_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {782::new ((void*)__p) __func(__f_);783}784785_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT {786# ifndef _LIBCPP_HAS_OBJC_ARC787if (__f_)788_Block_release(__f_);789# endif790__f_ = 0;791}792793_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT {794_LIBCPP_ASSERT_INTERNAL(795false,796"Block pointers are just pointers, so they should always fit into "797"std::function's small buffer optimization. This function should "798"never be invoked.");799}800801_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg) {802return std::__invoke(__f_, std::forward<_ArgTypes>(__arg)...);803}804805# ifndef _LIBCPP_HAS_NO_RTTI806_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(type_info const& __ti) const _NOEXCEPT {807if (__ti == typeid(__func::__block_type))808return &__f_;809return (const void*)nullptr;810}811812_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT {813return typeid(__func::__block_type);814}815# endif // _LIBCPP_HAS_NO_RTTI816};817818# endif // _LIBCPP_HAS_EXTENSION_BLOCKS819820} // namespace __function821822template <class _Rp, class... _ArgTypes>823class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>824: public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,825public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)> {826# ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION827typedef __function::__value_func<_Rp(_ArgTypes...)> __func;828# else829typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;830# endif831832__func __f_;833834template <class _Fp,835bool = _And< _IsNotSame<__remove_cvref_t<_Fp>, function>, __invokable<_Fp, _ArgTypes...> >::value>836struct __callable;837template <class _Fp>838struct __callable<_Fp, true> {839static const bool value =840is_void<_Rp>::value || __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type, _Rp>::value;841};842template <class _Fp>843struct __callable<_Fp, false> {844static const bool value = false;845};846847template <class _Fp>848using _EnableIfLValueCallable = __enable_if_t<__callable<_Fp&>::value>;849850public:851typedef _Rp result_type;852853// construct/copy/destroy:854_LIBCPP_HIDE_FROM_ABI function() _NOEXCEPT {}855_LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI function(nullptr_t) _NOEXCEPT {}856_LIBCPP_HIDE_FROM_ABI function(const function&);857_LIBCPP_HIDE_FROM_ABI function(function&&) _NOEXCEPT;858template <class _Fp, class = _EnableIfLValueCallable<_Fp>>859_LIBCPP_HIDE_FROM_ABI function(_Fp);860861# if _LIBCPP_STD_VER <= 14862template <class _Alloc>863_LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}864template <class _Alloc>865_LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}866template <class _Alloc>867_LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, const function&);868template <class _Alloc>869_LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, function&&);870template <class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>871_LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc& __a, _Fp __f);872# endif873874_LIBCPP_HIDE_FROM_ABI function& operator=(const function&);875_LIBCPP_HIDE_FROM_ABI function& operator=(function&&) _NOEXCEPT;876_LIBCPP_HIDE_FROM_ABI function& operator=(nullptr_t) _NOEXCEPT;877template <class _Fp, class = _EnableIfLValueCallable<__decay_t<_Fp>>>878_LIBCPP_HIDE_FROM_ABI function& operator=(_Fp&&);879880_LIBCPP_HIDE_FROM_ABI ~function();881882// function modifiers:883_LIBCPP_HIDE_FROM_ABI void swap(function&) _NOEXCEPT;884885# if _LIBCPP_STD_VER <= 14886template <class _Fp, class _Alloc>887_LIBCPP_HIDE_FROM_ABI void assign(_Fp&& __f, const _Alloc& __a) {888function(allocator_arg, __a, std::forward<_Fp>(__f)).swap(*this);889}890# endif891892// function capacity:893_LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return static_cast<bool>(__f_); }894895// deleted overloads close possible hole in the type system896template <class _R2, class... _ArgTypes2>897bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;898# if _LIBCPP_STD_VER <= 17899template <class _R2, class... _ArgTypes2>900bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;901# endif902903public:904// function invocation:905_LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;906907# ifndef _LIBCPP_HAS_NO_RTTI908// function target access:909_LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT;910template <typename _Tp>911_LIBCPP_HIDE_FROM_ABI _Tp* target() _NOEXCEPT;912template <typename _Tp>913_LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT;914# endif // _LIBCPP_HAS_NO_RTTI915};916917# if _LIBCPP_STD_VER >= 17918template <class _Rp, class... _Ap>919function(_Rp (*)(_Ap...)) -> function<_Rp(_Ap...)>;920921template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>922function(_Fp) -> function<_Stripped>;923# endif // _LIBCPP_STD_VER >= 17924925template <class _Rp, class... _ArgTypes>926function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}927928# if _LIBCPP_STD_VER <= 14929template <class _Rp, class... _ArgTypes>930template <class _Alloc>931function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, const function& __f) : __f_(__f.__f_) {}932# endif933934template <class _Rp, class... _ArgTypes>935function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT : __f_(std::move(__f.__f_)) {}936937# if _LIBCPP_STD_VER <= 14938template <class _Rp, class... _ArgTypes>939template <class _Alloc>940function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&, function&& __f) : __f_(std::move(__f.__f_)) {}941# endif942943template <class _Rp, class... _ArgTypes>944template <class _Fp, class>945function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(std::move(__f)) {}946947# if _LIBCPP_STD_VER <= 14948template <class _Rp, class... _ArgTypes>949template <class _Fp, class _Alloc, class>950function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a, _Fp __f) : __f_(std::move(__f), __a) {}951# endif952953template <class _Rp, class... _ArgTypes>954function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(const function& __f) {955function(__f).swap(*this);956return *this;957}958959template <class _Rp, class... _ArgTypes>960function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT {961__f_ = std::move(__f.__f_);962return *this;963}964965template <class _Rp, class... _ArgTypes>966function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT {967__f_ = nullptr;968return *this;969}970971template <class _Rp, class... _ArgTypes>972template <class _Fp, class>973function<_Rp(_ArgTypes...)>& function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f) {974function(std::forward<_Fp>(__f)).swap(*this);975return *this;976}977978template <class _Rp, class... _ArgTypes>979function<_Rp(_ArgTypes...)>::~function() {}980981template <class _Rp, class... _ArgTypes>982void function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT {983__f_.swap(__f.__f_);984}985986template <class _Rp, class... _ArgTypes>987_Rp function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {988return __f_(std::forward<_ArgTypes>(__arg)...);989}990991# ifndef _LIBCPP_HAS_NO_RTTI992993template <class _Rp, class... _ArgTypes>994const std::type_info& function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT {995return __f_.target_type();996}997998template <class _Rp, class... _ArgTypes>999template <typename _Tp>1000_Tp* function<_Rp(_ArgTypes...)>::target() _NOEXCEPT {1001return (_Tp*)(__f_.template target<_Tp>());1002}10031004template <class _Rp, class... _ArgTypes>1005template <typename _Tp>1006const _Tp* function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT {1007return __f_.template target<_Tp>();1008}10091010# endif // _LIBCPP_HAS_NO_RTTI10111012template <class _Rp, class... _ArgTypes>1013inline _LIBCPP_HIDE_FROM_ABI bool operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {1014return !__f;1015}10161017# if _LIBCPP_STD_VER <= 1710181019template <class _Rp, class... _ArgTypes>1020inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {1021return !__f;1022}10231024template <class _Rp, class... _ArgTypes>1025inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {1026return (bool)__f;1027}10281029template <class _Rp, class... _ArgTypes>1030inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {1031return (bool)__f;1032}10331034# endif // _LIBCPP_STD_VER <= 1710351036template <class _Rp, class... _ArgTypes>1037inline _LIBCPP_HIDE_FROM_ABI void swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {1038return __x.swap(__y);1039}10401041_LIBCPP_END_NAMESPACE_STD10421043#endif // _LIBCPP_CXX03_LANG10441045_LIBCPP_POP_MACROS10461047#endif // _LIBCPP___FUNCTIONAL_FUNCTION_H104810491050