2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H
11 #define _LIBCPP___FUNCTIONAL_FUNCTION_H
13 #include <__cxx03/__assert>
14 #include <__cxx03/__config>
15 #include <__cxx03/__exception/exception.h>
16 #include <__cxx03/__functional/binary_function.h>
17 #include <__cxx03/__functional/invoke.h>
18 #include <__cxx03/__functional/unary_function.h>
19 #include <__cxx03/__iterator/iterator_traits.h>
20 #include <__cxx03/__memory/addressof.h>
21 #include <__cxx03/__memory/allocator.h>
22 #include <__cxx03/__memory/allocator_destructor.h>
23 #include <__cxx03/__memory/allocator_traits.h>
24 #include <__cxx03/__memory/builtin_new_allocator.h>
25 #include <__cxx03/__memory/compressed_pair.h>
26 #include <__cxx03/__memory/unique_ptr.h>
27 #include <__cxx03/__type_traits/aligned_storage.h>
28 #include <__cxx03/__type_traits/decay.h>
29 #include <__cxx03/__type_traits/is_core_convertible.h>
30 #include <__cxx03/__type_traits/is_scalar.h>
31 #include <__cxx03/__type_traits/is_trivially_constructible.h>
32 #include <__cxx03/__type_traits/is_trivially_destructible.h>
33 #include <__cxx03/__type_traits/is_void.h>
34 #include <__cxx03/__type_traits/strip_signature.h>
35 #include <__cxx03/__utility/forward.h>
36 #include <__cxx03/__utility/move.h>
37 #include <__cxx03/__utility/piecewise_construct.h>
38 #include <__cxx03/__utility/swap.h>
39 #include <__cxx03/__verbose_abort>
40 #include <__cxx03/new>
41 #include <__cxx03/tuple>
42 #include <__cxx03/typeinfo>
44 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45 # pragma GCC system_header
49 #include <__cxx03/__undef_macros>
51 #ifndef _LIBCPP_CXX03_LANG
53 _LIBCPP_BEGIN_NAMESPACE_STD
57 _LIBCPP_DIAGNOSTIC_PUSH
58 # if !_LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
59 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
61 class _LIBCPP_EXPORTED_FROM_ABI bad_function_call
: public exception
{
63 _LIBCPP_HIDE_FROM_ABI
bad_function_call() _NOEXCEPT
= default;
64 _LIBCPP_HIDE_FROM_ABI
bad_function_call(const bad_function_call
&) _NOEXCEPT
= default;
65 _LIBCPP_HIDE_FROM_ABI bad_function_call
& operator=(const bad_function_call
&) _NOEXCEPT
= default;
66 // Note that when a key function is not used, every translation unit that uses
67 // bad_function_call will end up containing a weak definition of the vtable and
69 # if _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_KEY_FUNCTION
70 ~bad_function_call() _NOEXCEPT override
;
72 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
~bad_function_call() _NOEXCEPT override
{}
75 # ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
76 const char* what() const _NOEXCEPT override
;
79 _LIBCPP_DIAGNOSTIC_POP
81 _LIBCPP_NORETURN
inline _LIBCPP_HIDE_FROM_ABI
void __throw_bad_function_call() {
82 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
83 throw bad_function_call();
85 _LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode");
90 class _LIBCPP_TEMPLATE_VIS function
; // undefined
92 namespace __function
{
95 struct __maybe_derive_from_unary_function
{};
97 template <class _Rp
, class _A1
>
98 struct __maybe_derive_from_unary_function
<_Rp(_A1
)> : public __unary_function
<_A1
, _Rp
> {};
101 struct __maybe_derive_from_binary_function
{};
103 template <class _Rp
, class _A1
, class _A2
>
104 struct __maybe_derive_from_binary_function
<_Rp(_A1
, _A2
)> : public __binary_function
<_A1
, _A2
, _Rp
> {};
107 _LIBCPP_HIDE_FROM_ABI
bool __not_null(_Fp
const&) {
112 _LIBCPP_HIDE_FROM_ABI
bool __not_null(_Fp
* __ptr
) {
116 template <class _Ret
, class _Class
>
117 _LIBCPP_HIDE_FROM_ABI
bool __not_null(_Ret
_Class::*__ptr
) {
122 _LIBCPP_HIDE_FROM_ABI
bool __not_null(function
<_Fp
> const& __f
) {
126 # ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
127 template <class _Rp
, class... _Args
>
128 _LIBCPP_HIDE_FROM_ABI
bool __not_null(_Rp (^__p
)(_Args
...)) {
133 } // namespace __function
135 namespace __function
{
137 // __alloc_func holds a functor and an allocator.
139 template <class _Fp
, class _Ap
, class _FB
>
141 template <class _Fp
, class _FB
>
142 class __default_alloc_func
;
144 template <class _Fp
, class _Ap
, class _Rp
, class... _ArgTypes
>
145 class __alloc_func
<_Fp
, _Ap
, _Rp(_ArgTypes
...)> {
146 __compressed_pair
<_Fp
, _Ap
> __f_
;
149 typedef _LIBCPP_NODEBUG _Fp _Target
;
150 typedef _LIBCPP_NODEBUG _Ap _Alloc
;
152 _LIBCPP_HIDE_FROM_ABI
const _Target
& __target() const { return __f_
.first(); }
154 // WIN32 APIs may define __allocator, so use __get_allocator instead.
155 _LIBCPP_HIDE_FROM_ABI
const _Alloc
& __get_allocator() const { return __f_
.second(); }
157 _LIBCPP_HIDE_FROM_ABI
explicit __alloc_func(_Target
&& __f
)
158 : __f_(piecewise_construct
, std::forward_as_tuple(std::move(__f
)), std::forward_as_tuple()) {}
160 _LIBCPP_HIDE_FROM_ABI
explicit __alloc_func(const _Target
& __f
, const _Alloc
& __a
)
161 : __f_(piecewise_construct
, std::forward_as_tuple(__f
), std::forward_as_tuple(__a
)) {}
163 _LIBCPP_HIDE_FROM_ABI
explicit __alloc_func(const _Target
& __f
, _Alloc
&& __a
)
164 : __f_(piecewise_construct
, std::forward_as_tuple(__f
), std::forward_as_tuple(std::move(__a
))) {}
166 _LIBCPP_HIDE_FROM_ABI
explicit __alloc_func(_Target
&& __f
, _Alloc
&& __a
)
167 : __f_(piecewise_construct
, std::forward_as_tuple(std::move(__f
)), std::forward_as_tuple(std::move(__a
))) {}
169 _LIBCPP_HIDE_FROM_ABI _Rp
operator()(_ArgTypes
&&... __arg
) {
170 typedef __invoke_void_return_wrapper
<_Rp
> _Invoker
;
171 return _Invoker::__call(__f_
.first(), std::forward
<_ArgTypes
>(__arg
)...);
174 _LIBCPP_HIDE_FROM_ABI __alloc_func
* __clone() const {
175 typedef allocator_traits
<_Alloc
> __alloc_traits
;
176 typedef __rebind_alloc
<__alloc_traits
, __alloc_func
> _AA
;
177 _AA
__a(__f_
.second());
178 typedef __allocator_destructor
<_AA
> _Dp
;
179 unique_ptr
<__alloc_func
, _Dp
> __hold(__a
.allocate(1), _Dp(__a
, 1));
180 ::new ((void*)__hold
.get()) __alloc_func(__f_
.first(), _Alloc(__a
));
181 return __hold
.release();
184 _LIBCPP_HIDE_FROM_ABI
void destroy() _NOEXCEPT
{ __f_
.~__compressed_pair
<_Target
, _Alloc
>(); }
186 _LIBCPP_HIDE_FROM_ABI
static void __destroy_and_delete(__alloc_func
* __f
) {
187 typedef allocator_traits
<_Alloc
> __alloc_traits
;
188 typedef __rebind_alloc
<__alloc_traits
, __alloc_func
> _FunAlloc
;
189 _FunAlloc
__a(__f
->__get_allocator());
191 __a
.deallocate(__f
, 1);
195 template <class _Fp
, class _Rp
, class... _ArgTypes
>
196 class __default_alloc_func
<_Fp
, _Rp(_ArgTypes
...)> {
200 typedef _LIBCPP_NODEBUG _Fp _Target
;
202 _LIBCPP_HIDE_FROM_ABI
const _Target
& __target() const { return __f_
; }
204 _LIBCPP_HIDE_FROM_ABI
explicit __default_alloc_func(_Target
&& __f
) : __f_(std::move(__f
)) {}
206 _LIBCPP_HIDE_FROM_ABI
explicit __default_alloc_func(const _Target
& __f
) : __f_(__f
) {}
208 _LIBCPP_HIDE_FROM_ABI _Rp
operator()(_ArgTypes
&&... __arg
) {
209 typedef __invoke_void_return_wrapper
<_Rp
> _Invoker
;
210 return _Invoker::__call(__f_
, std::forward
<_ArgTypes
>(__arg
)...);
213 _LIBCPP_HIDE_FROM_ABI __default_alloc_func
* __clone() const {
214 __builtin_new_allocator::__holder_t __hold
= __builtin_new_allocator::__allocate_type
<__default_alloc_func
>(1);
215 __default_alloc_func
* __res
= ::new ((void*)__hold
.get()) __default_alloc_func(__f_
);
216 (void)__hold
.release();
220 _LIBCPP_HIDE_FROM_ABI
void destroy() _NOEXCEPT
{ __f_
.~_Target(); }
222 _LIBCPP_HIDE_FROM_ABI
static void __destroy_and_delete(__default_alloc_func
* __f
) {
224 __builtin_new_allocator::__deallocate_type
<__default_alloc_func
>(__f
, 1);
228 // __base provides an abstract interface for copyable functors.
231 class _LIBCPP_TEMPLATE_VIS __base
;
233 template <class _Rp
, class... _ArgTypes
>
234 class __base
<_Rp(_ArgTypes
...)> {
236 __base(const __base
&) = delete;
237 __base
& operator=(const __base
&) = delete;
239 _LIBCPP_HIDE_FROM_ABI
__base() {}
240 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual ~__base() {}
241 virtual __base
* __clone() const = 0;
242 virtual void __clone(__base
*) const = 0;
243 virtual void destroy() _NOEXCEPT
= 0;
244 virtual void destroy_deallocate() _NOEXCEPT
= 0;
245 virtual _Rp
operator()(_ArgTypes
&&...) = 0;
246 # ifndef _LIBCPP_HAS_NO_RTTI
247 virtual const void* target(const type_info
&) const _NOEXCEPT
= 0;
248 virtual const std::type_info
& target_type() const _NOEXCEPT
= 0;
249 # endif // _LIBCPP_HAS_NO_RTTI
252 // __func implements __base for a given functor type.
254 template <class _FD
, class _Alloc
, class _FB
>
257 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
258 class __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)> : public __base
<_Rp(_ArgTypes
...)> {
259 __alloc_func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)> __f_
;
262 _LIBCPP_HIDE_FROM_ABI
explicit __func(_Fp
&& __f
) : __f_(std::move(__f
)) {}
264 _LIBCPP_HIDE_FROM_ABI
explicit __func(const _Fp
& __f
, const _Alloc
& __a
) : __f_(__f
, __a
) {}
266 _LIBCPP_HIDE_FROM_ABI
explicit __func(const _Fp
& __f
, _Alloc
&& __a
) : __f_(__f
, std::move(__a
)) {}
268 _LIBCPP_HIDE_FROM_ABI
explicit __func(_Fp
&& __f
, _Alloc
&& __a
) : __f_(std::move(__f
), std::move(__a
)) {}
270 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual __base
<_Rp(_ArgTypes
...)>* __clone() const;
271 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual void __clone(__base
<_Rp(_ArgTypes
...)>*) const;
272 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual void destroy() _NOEXCEPT
;
273 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual void destroy_deallocate() _NOEXCEPT
;
274 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual _Rp
operator()(_ArgTypes
&&... __arg
);
275 # ifndef _LIBCPP_HAS_NO_RTTI
276 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual const void* target(const type_info
&) const _NOEXCEPT
;
277 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual const std::type_info
& target_type() const _NOEXCEPT
;
278 # endif // _LIBCPP_HAS_NO_RTTI
281 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
282 __base
<_Rp(_ArgTypes
...)>* __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::__clone() const {
283 typedef allocator_traits
<_Alloc
> __alloc_traits
;
284 typedef __rebind_alloc
<__alloc_traits
, __func
> _Ap
;
285 _Ap
__a(__f_
.__get_allocator());
286 typedef __allocator_destructor
<_Ap
> _Dp
;
287 unique_ptr
<__func
, _Dp
> __hold(__a
.allocate(1), _Dp(__a
, 1));
288 ::new ((void*)__hold
.get()) __func(__f_
.__target(), _Alloc(__a
));
289 return __hold
.release();
292 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
293 void __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::__clone(__base
<_Rp(_ArgTypes
...)>* __p
) const {
294 ::new ((void*)__p
) __func(__f_
.__target(), __f_
.__get_allocator());
297 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
298 void __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::destroy() _NOEXCEPT
{
302 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
303 void __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::destroy_deallocate() _NOEXCEPT
{
304 typedef allocator_traits
<_Alloc
> __alloc_traits
;
305 typedef __rebind_alloc
<__alloc_traits
, __func
> _Ap
;
306 _Ap
__a(__f_
.__get_allocator());
308 __a
.deallocate(this, 1);
311 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
312 _Rp __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::operator()(_ArgTypes
&&... __arg
) {
313 return __f_(std::forward
<_ArgTypes
>(__arg
)...);
316 # ifndef _LIBCPP_HAS_NO_RTTI
318 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
319 const void* __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::target(const type_info
& __ti
) const _NOEXCEPT
{
320 if (__ti
== typeid(_Fp
))
321 return std::addressof(__f_
.__target());
325 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
326 const std::type_info
& __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::target_type() const _NOEXCEPT
{
330 # endif // _LIBCPP_HAS_NO_RTTI
332 // __value_func creates a value-type from a __func.
337 template <class _Rp
, class... _ArgTypes
>
338 class __value_func
<_Rp(_ArgTypes
...)> {
339 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
340 typename aligned_storage
<3 * sizeof(void*)>::type __buf_
;
341 _LIBCPP_SUPPRESS_DEPRECATED_POP
343 typedef __base
<_Rp(_ArgTypes
...)> __func
;
346 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI
static __func
* __as_base(void* __p
) { return reinterpret_cast<__func
*>(__p
); }
349 _LIBCPP_HIDE_FROM_ABI
__value_func() _NOEXCEPT
: __f_(nullptr) {}
351 template <class _Fp
, class _Alloc
>
352 _LIBCPP_HIDE_FROM_ABI
__value_func(_Fp
&& __f
, const _Alloc
& __a
) : __f_(nullptr) {
353 typedef allocator_traits
<_Alloc
> __alloc_traits
;
354 typedef __function::__func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)> _Fun
;
355 typedef __rebind_alloc
<__alloc_traits
, _Fun
> _FunAlloc
;
357 if (__function::__not_null(__f
)) {
359 if (sizeof(_Fun
) <= sizeof(__buf_
) && is_nothrow_copy_constructible
<_Fp
>::value
&&
360 is_nothrow_copy_constructible
<_FunAlloc
>::value
) {
361 __f_
= ::new ((void*)&__buf_
) _Fun(std::move(__f
), _Alloc(__af
));
363 typedef __allocator_destructor
<_FunAlloc
> _Dp
;
364 unique_ptr
<__func
, _Dp
> __hold(__af
.allocate(1), _Dp(__af
, 1));
365 ::new ((void*)__hold
.get()) _Fun(std::move(__f
), _Alloc(__a
));
366 __f_
= __hold
.release();
371 template <class _Fp
, __enable_if_t
<!is_same
<__decay_t
<_Fp
>, __value_func
>::value
, int> = 0>
372 _LIBCPP_HIDE_FROM_ABI
explicit __value_func(_Fp
&& __f
) : __value_func(std::forward
<_Fp
>(__f
), allocator
<_Fp
>()) {}
374 _LIBCPP_HIDE_FROM_ABI
__value_func(const __value_func
& __f
) {
375 if (__f
.__f_
== nullptr)
377 else if ((void*)__f
.__f_
== &__f
.__buf_
) {
378 __f_
= __as_base(&__buf_
);
379 __f
.__f_
->__clone(__f_
);
381 __f_
= __f
.__f_
->__clone();
384 _LIBCPP_HIDE_FROM_ABI
__value_func(__value_func
&& __f
) _NOEXCEPT
{
385 if (__f
.__f_
== nullptr)
387 else if ((void*)__f
.__f_
== &__f
.__buf_
) {
388 __f_
= __as_base(&__buf_
);
389 __f
.__f_
->__clone(__f_
);
396 _LIBCPP_HIDE_FROM_ABI
~__value_func() {
397 if ((void*)__f_
== &__buf_
)
400 __f_
->destroy_deallocate();
403 _LIBCPP_HIDE_FROM_ABI __value_func
& operator=(__value_func
&& __f
) {
405 if (__f
.__f_
== nullptr)
407 else if ((void*)__f
.__f_
== &__f
.__buf_
) {
408 __f_
= __as_base(&__buf_
);
409 __f
.__f_
->__clone(__f_
);
417 _LIBCPP_HIDE_FROM_ABI __value_func
& operator=(nullptr_t
) {
420 if ((void*)__f
== &__buf_
)
423 __f
->destroy_deallocate();
427 _LIBCPP_HIDE_FROM_ABI _Rp
operator()(_ArgTypes
&&... __args
) const {
429 __throw_bad_function_call();
430 return (*__f_
)(std::forward
<_ArgTypes
>(__args
)...);
433 _LIBCPP_HIDE_FROM_ABI
void swap(__value_func
& __f
) _NOEXCEPT
{
436 if ((void*)__f_
== &__buf_
&& (void*)__f
.__f_
== &__f
.__buf_
) {
437 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
438 typename aligned_storage
<sizeof(__buf_
)>::type __tempbuf
;
439 _LIBCPP_SUPPRESS_DEPRECATED_POP
440 __func
* __t
= __as_base(&__tempbuf
);
444 __f
.__f_
->__clone(__as_base(&__buf_
));
447 __f_
= __as_base(&__buf_
);
448 __t
->__clone(__as_base(&__f
.__buf_
));
450 __f
.__f_
= __as_base(&__f
.__buf_
);
451 } else if ((void*)__f_
== &__buf_
) {
452 __f_
->__clone(__as_base(&__f
.__buf_
));
455 __f
.__f_
= __as_base(&__f
.__buf_
);
456 } else if ((void*)__f
.__f_
== &__f
.__buf_
) {
457 __f
.__f_
->__clone(__as_base(&__buf_
));
460 __f_
= __as_base(&__buf_
);
462 std::swap(__f_
, __f
.__f_
);
465 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const _NOEXCEPT
{ return __f_
!= nullptr; }
467 # ifndef _LIBCPP_HAS_NO_RTTI
468 _LIBCPP_HIDE_FROM_ABI
const std::type_info
& target_type() const _NOEXCEPT
{
471 return __f_
->target_type();
474 template <typename _Tp
>
475 _LIBCPP_HIDE_FROM_ABI
const _Tp
* target() const _NOEXCEPT
{
478 return (const _Tp
*)__f_
->target(typeid(_Tp
));
480 # endif // _LIBCPP_HAS_NO_RTTI
483 // Storage for a functor object, to be used with __policy to manage copy and
485 union __policy_storage
{
486 mutable char __small
[sizeof(void*) * 2];
490 // True if _Fun can safely be held in __policy_storage.__small.
491 template <typename _Fun
>
492 struct __use_small_storage
493 : public integral_constant
<
495 sizeof(_Fun
) <= sizeof(__policy_storage
)&& _LIBCPP_ALIGNOF(_Fun
) <= _LIBCPP_ALIGNOF(__policy_storage
) &&
496 is_trivially_copy_constructible
<_Fun
>::value
&& is_trivially_destructible
<_Fun
>::value
> {};
498 // Policy contains information about how to copy, destroy, and move the
499 // underlying functor. You can think of it as a vtable of sorts.
501 // Used to copy or destroy __large values. null for trivial objects.
502 void* (*const __clone
)(const void*);
503 void (*const __destroy
)(void*);
505 // True if this is the null policy (no value).
506 const bool __is_null
;
508 // The target type. May be null if RTTI is disabled.
509 const std::type_info
* const __type_info
;
511 // Returns a pointer to a static policy object suitable for the functor
513 template <typename _Fun
>
514 _LIBCPP_HIDE_FROM_ABI
static const __policy
* __create() {
515 return __choose_policy
<_Fun
>(__use_small_storage
<_Fun
>());
518 _LIBCPP_HIDE_FROM_ABI
static const __policy
* __create_empty() {
519 static constexpr __policy __policy
= {
523 # ifndef _LIBCPP_HAS_NO_RTTI
533 template <typename _Fun
>
534 _LIBCPP_HIDE_FROM_ABI
static void* __large_clone(const void* __s
) {
535 const _Fun
* __f
= static_cast<const _Fun
*>(__s
);
536 return __f
->__clone();
539 template <typename _Fun
>
540 _LIBCPP_HIDE_FROM_ABI
static void __large_destroy(void* __s
) {
541 _Fun::__destroy_and_delete(static_cast<_Fun
*>(__s
));
544 template <typename _Fun
>
545 _LIBCPP_HIDE_FROM_ABI
static const __policy
* __choose_policy(/* is_small = */ false_type
) {
546 static constexpr __policy __policy
= {
547 &__large_clone
<_Fun
>,
548 &__large_destroy
<_Fun
>,
550 # ifndef _LIBCPP_HAS_NO_RTTI
551 &typeid(typename
_Fun::_Target
)
559 template <typename _Fun
>
560 _LIBCPP_HIDE_FROM_ABI
static const __policy
* __choose_policy(/* is_small = */ true_type
) {
561 static constexpr __policy __policy
= {
565 # ifndef _LIBCPP_HAS_NO_RTTI
566 &typeid(typename
_Fun::_Target
)
575 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
576 // faster for types that can be passed in registers.
577 template <typename _Tp
>
578 using __fast_forward
= __conditional_t
<is_scalar
<_Tp
>::value
, _Tp
, _Tp
&&>;
580 // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
583 struct __policy_invoker
;
585 template <class _Rp
, class... _ArgTypes
>
586 struct __policy_invoker
<_Rp(_ArgTypes
...)> {
587 typedef _Rp (*__Call
)(const __policy_storage
*, __fast_forward
<_ArgTypes
>...);
591 // Creates an invoker that throws bad_function_call.
592 _LIBCPP_HIDE_FROM_ABI
__policy_invoker() : __call_(&__call_empty
) {}
594 // Creates an invoker that calls the given instance of __func.
595 template <typename _Fun
>
596 _LIBCPP_HIDE_FROM_ABI
static __policy_invoker
__create() {
597 return __policy_invoker(&__call_impl
<_Fun
>);
601 _LIBCPP_HIDE_FROM_ABI
explicit __policy_invoker(__Call __c
) : __call_(__c
) {}
603 _LIBCPP_HIDE_FROM_ABI
static _Rp
__call_empty(const __policy_storage
*, __fast_forward
<_ArgTypes
>...) {
604 __throw_bad_function_call();
607 template <typename _Fun
>
608 _LIBCPP_HIDE_FROM_ABI
static _Rp
__call_impl(const __policy_storage
* __buf
, __fast_forward
<_ArgTypes
>... __args
) {
609 _Fun
* __f
= reinterpret_cast<_Fun
*>(__use_small_storage
<_Fun
>::value
? &__buf
->__small
: __buf
->__large
);
610 return (*__f
)(std::forward
<_ArgTypes
>(__args
)...);
614 // __policy_func uses a __policy and __policy_invoker to create a type-erased,
620 template <class _Rp
, class... _ArgTypes
>
621 class __policy_func
<_Rp(_ArgTypes
...)> {
622 // Inline storage for small objects.
623 __policy_storage __buf_
;
625 // Calls the value stored in __buf_. This could technically be part of
626 // policy, but storing it here eliminates a level of indirection inside
628 typedef __function::__policy_invoker
<_Rp(_ArgTypes
...)> __invoker
;
629 __invoker __invoker_
;
631 // The policy that describes how to move / copy / destroy __buf_. Never
632 // null, even if the function is empty.
633 const __policy
* __policy_
;
636 _LIBCPP_HIDE_FROM_ABI
__policy_func() : __policy_(__policy::__create_empty()) {}
638 template <class _Fp
, class _Alloc
>
639 _LIBCPP_HIDE_FROM_ABI
__policy_func(_Fp
&& __f
, const _Alloc
& __a
) : __policy_(__policy::__create_empty()) {
640 typedef __alloc_func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)> _Fun
;
641 typedef allocator_traits
<_Alloc
> __alloc_traits
;
642 typedef __rebind_alloc
<__alloc_traits
, _Fun
> _FunAlloc
;
644 if (__function::__not_null(__f
)) {
645 __invoker_
= __invoker::template __create
<_Fun
>();
646 __policy_
= __policy::__create
<_Fun
>();
649 if (__use_small_storage
<_Fun
>()) {
650 ::new ((void*)&__buf_
.__small
) _Fun(std::move(__f
), _Alloc(__af
));
652 typedef __allocator_destructor
<_FunAlloc
> _Dp
;
653 unique_ptr
<_Fun
, _Dp
> __hold(__af
.allocate(1), _Dp(__af
, 1));
654 ::new ((void*)__hold
.get()) _Fun(std::move(__f
), _Alloc(__af
));
655 __buf_
.__large
= __hold
.release();
660 template <class _Fp
, __enable_if_t
<!is_same
<__decay_t
<_Fp
>, __policy_func
>::value
, int> = 0>
661 _LIBCPP_HIDE_FROM_ABI
explicit __policy_func(_Fp
&& __f
) : __policy_(__policy::__create_empty()) {
662 typedef __default_alloc_func
<_Fp
, _Rp(_ArgTypes
...)> _Fun
;
664 if (__function::__not_null(__f
)) {
665 __invoker_
= __invoker::template __create
<_Fun
>();
666 __policy_
= __policy::__create
<_Fun
>();
667 if (__use_small_storage
<_Fun
>()) {
668 ::new ((void*)&__buf_
.__small
) _Fun(std::move(__f
));
670 __builtin_new_allocator::__holder_t __hold
= __builtin_new_allocator::__allocate_type
<_Fun
>(1);
671 __buf_
.__large
= ::new ((void*)__hold
.get()) _Fun(std::move(__f
));
672 (void)__hold
.release();
677 _LIBCPP_HIDE_FROM_ABI
__policy_func(const __policy_func
& __f
)
678 : __buf_(__f
.__buf_
), __invoker_(__f
.__invoker_
), __policy_(__f
.__policy_
) {
679 if (__policy_
->__clone
)
680 __buf_
.__large
= __policy_
->__clone(__f
.__buf_
.__large
);
683 _LIBCPP_HIDE_FROM_ABI
__policy_func(__policy_func
&& __f
)
684 : __buf_(__f
.__buf_
), __invoker_(__f
.__invoker_
), __policy_(__f
.__policy_
) {
685 if (__policy_
->__destroy
) {
686 __f
.__policy_
= __policy::__create_empty();
687 __f
.__invoker_
= __invoker();
691 _LIBCPP_HIDE_FROM_ABI
~__policy_func() {
692 if (__policy_
->__destroy
)
693 __policy_
->__destroy(__buf_
.__large
);
696 _LIBCPP_HIDE_FROM_ABI __policy_func
& operator=(__policy_func
&& __f
) {
699 __invoker_
= __f
.__invoker_
;
700 __policy_
= __f
.__policy_
;
701 __f
.__policy_
= __policy::__create_empty();
702 __f
.__invoker_
= __invoker();
706 _LIBCPP_HIDE_FROM_ABI __policy_func
& operator=(nullptr_t
) {
707 const __policy
* __p
= __policy_
;
708 __policy_
= __policy::__create_empty();
709 __invoker_
= __invoker();
711 __p
->__destroy(__buf_
.__large
);
715 _LIBCPP_HIDE_FROM_ABI _Rp
operator()(_ArgTypes
&&... __args
) const {
716 return __invoker_
.__call_(std::addressof(__buf_
), std::forward
<_ArgTypes
>(__args
)...);
719 _LIBCPP_HIDE_FROM_ABI
void swap(__policy_func
& __f
) {
720 std::swap(__invoker_
, __f
.__invoker_
);
721 std::swap(__policy_
, __f
.__policy_
);
722 std::swap(__buf_
, __f
.__buf_
);
725 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const _NOEXCEPT
{ return !__policy_
->__is_null
; }
727 # ifndef _LIBCPP_HAS_NO_RTTI
728 _LIBCPP_HIDE_FROM_ABI
const std::type_info
& target_type() const _NOEXCEPT
{ return *__policy_
->__type_info
; }
730 template <typename _Tp
>
731 _LIBCPP_HIDE_FROM_ABI
const _Tp
* target() const _NOEXCEPT
{
732 if (__policy_
->__is_null
|| typeid(_Tp
) != *__policy_
->__type_info
)
734 if (__policy_
->__clone
) // Out of line storage.
735 return reinterpret_cast<const _Tp
*>(__buf_
.__large
);
737 return reinterpret_cast<const _Tp
*>(&__buf_
.__small
);
739 # endif // _LIBCPP_HAS_NO_RTTI
742 # if defined(_LIBCPP_HAS_BLOCKS_RUNTIME)
744 extern "C" void* _Block_copy(const void*);
745 extern "C" void _Block_release(const void*);
747 template <class _Rp1
, class... _ArgTypes1
, class _Alloc
, class _Rp
, class... _ArgTypes
>
748 class __func
<_Rp1 (^)(_ArgTypes1
...), _Alloc
, _Rp(_ArgTypes
...)> : public __base
<_Rp(_ArgTypes
...)> {
749 typedef _Rp1 (^__block_type
)(_ArgTypes1
...);
753 _LIBCPP_HIDE_FROM_ABI
explicit __func(__block_type
const& __f
)
754 # ifdef _LIBCPP_HAS_OBJC_ARC
757 : __f_(reinterpret_cast<__block_type
>(__f
? _Block_copy(__f
) : nullptr))
762 // [TODO] add && to save on a retain
764 _LIBCPP_HIDE_FROM_ABI
explicit __func(__block_type __f
, const _Alloc
& /* unused */)
765 # ifdef _LIBCPP_HAS_OBJC_ARC
768 : __f_(reinterpret_cast<__block_type
>(__f
? _Block_copy(__f
) : nullptr))
773 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual __base
<_Rp(_ArgTypes
...)>* __clone() const {
774 _LIBCPP_ASSERT_INTERNAL(
776 "Block pointers are just pointers, so they should always fit into "
777 "std::function's small buffer optimization. This function should "
778 "never be invoked.");
782 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual void __clone(__base
<_Rp(_ArgTypes
...)>* __p
) const {
783 ::new ((void*)__p
) __func(__f_
);
786 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual void destroy() _NOEXCEPT
{
787 # ifndef _LIBCPP_HAS_OBJC_ARC
789 _Block_release(__f_
);
794 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual void destroy_deallocate() _NOEXCEPT
{
795 _LIBCPP_ASSERT_INTERNAL(
797 "Block pointers are just pointers, so they should always fit into "
798 "std::function's small buffer optimization. This function should "
799 "never be invoked.");
802 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual _Rp
operator()(_ArgTypes
&&... __arg
) {
803 return std::__invoke(__f_
, std::forward
<_ArgTypes
>(__arg
)...);
806 # ifndef _LIBCPP_HAS_NO_RTTI
807 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual const void* target(type_info
const& __ti
) const _NOEXCEPT
{
808 if (__ti
== typeid(__func::__block_type
))
810 return (const void*)nullptr;
813 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual const std::type_info
& target_type() const _NOEXCEPT
{
814 return typeid(__func::__block_type
);
816 # endif // _LIBCPP_HAS_NO_RTTI
819 # endif // _LIBCPP_HAS_EXTENSION_BLOCKS
821 } // namespace __function
823 template <class _Rp
, class... _ArgTypes
>
824 class _LIBCPP_TEMPLATE_VIS function
<_Rp(_ArgTypes
...)>
825 : public __function::__maybe_derive_from_unary_function
<_Rp(_ArgTypes
...)>,
826 public __function::__maybe_derive_from_binary_function
<_Rp(_ArgTypes
...)> {
827 # ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
828 typedef __function::__value_func
<_Rp(_ArgTypes
...)> __func
;
830 typedef __function::__policy_func
<_Rp(_ArgTypes
...)> __func
;
836 bool = _And
< _IsNotSame
<__remove_cvref_t
<_Fp
>, function
>, __invokable
<_Fp
, _ArgTypes
...> >::value
>
839 struct __callable
<_Fp
, true> {
840 static const bool value
=
841 is_void
<_Rp
>::value
|| __is_core_convertible
<typename __invoke_of
<_Fp
, _ArgTypes
...>::type
, _Rp
>::value
;
844 struct __callable
<_Fp
, false> {
845 static const bool value
= false;
849 using _EnableIfLValueCallable
= __enable_if_t
<__callable
<_Fp
&>::value
>;
852 typedef _Rp result_type
;
854 // construct/copy/destroy:
855 _LIBCPP_HIDE_FROM_ABI
function() _NOEXCEPT
{}
856 _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI
function(nullptr_t
) _NOEXCEPT
{}
857 _LIBCPP_HIDE_FROM_ABI
function(const function
&);
858 _LIBCPP_HIDE_FROM_ABI
function(function
&&) _NOEXCEPT
;
859 template <class _Fp
, class = _EnableIfLValueCallable
<_Fp
>>
860 _LIBCPP_HIDE_FROM_ABI
function(_Fp
);
862 # if _LIBCPP_STD_VER <= 14
863 template <class _Alloc
>
864 _LIBCPP_HIDE_FROM_ABI
function(allocator_arg_t
, const _Alloc
&) _NOEXCEPT
{}
865 template <class _Alloc
>
866 _LIBCPP_HIDE_FROM_ABI
function(allocator_arg_t
, const _Alloc
&, nullptr_t
) _NOEXCEPT
{}
867 template <class _Alloc
>
868 _LIBCPP_HIDE_FROM_ABI
function(allocator_arg_t
, const _Alloc
&, const function
&);
869 template <class _Alloc
>
870 _LIBCPP_HIDE_FROM_ABI
function(allocator_arg_t
, const _Alloc
&, function
&&);
871 template <class _Fp
, class _Alloc
, class = _EnableIfLValueCallable
<_Fp
>>
872 _LIBCPP_HIDE_FROM_ABI
function(allocator_arg_t
, const _Alloc
& __a
, _Fp __f
);
875 _LIBCPP_HIDE_FROM_ABI function
& operator=(const function
&);
876 _LIBCPP_HIDE_FROM_ABI function
& operator=(function
&&) _NOEXCEPT
;
877 _LIBCPP_HIDE_FROM_ABI function
& operator=(nullptr_t
) _NOEXCEPT
;
878 template <class _Fp
, class = _EnableIfLValueCallable
<__decay_t
<_Fp
>>>
879 _LIBCPP_HIDE_FROM_ABI function
& operator=(_Fp
&&);
881 _LIBCPP_HIDE_FROM_ABI
~function();
883 // function modifiers:
884 _LIBCPP_HIDE_FROM_ABI
void swap(function
&) _NOEXCEPT
;
886 # if _LIBCPP_STD_VER <= 14
887 template <class _Fp
, class _Alloc
>
888 _LIBCPP_HIDE_FROM_ABI
void assign(_Fp
&& __f
, const _Alloc
& __a
) {
889 function(allocator_arg
, __a
, std::forward
<_Fp
>(__f
)).swap(*this);
893 // function capacity:
894 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const _NOEXCEPT
{ return static_cast<bool>(__f_
); }
896 // deleted overloads close possible hole in the type system
897 template <class _R2
, class... _ArgTypes2
>
898 bool operator==(const function
<_R2(_ArgTypes2
...)>&) const = delete;
899 # if _LIBCPP_STD_VER <= 17
900 template <class _R2
, class... _ArgTypes2
>
901 bool operator!=(const function
<_R2(_ArgTypes2
...)>&) const = delete;
905 // function invocation:
906 _LIBCPP_HIDE_FROM_ABI _Rp
operator()(_ArgTypes
...) const;
908 # ifndef _LIBCPP_HAS_NO_RTTI
909 // function target access:
910 _LIBCPP_HIDE_FROM_ABI
const std::type_info
& target_type() const _NOEXCEPT
;
911 template <typename _Tp
>
912 _LIBCPP_HIDE_FROM_ABI _Tp
* target() _NOEXCEPT
;
913 template <typename _Tp
>
914 _LIBCPP_HIDE_FROM_ABI
const _Tp
* target() const _NOEXCEPT
;
915 # endif // _LIBCPP_HAS_NO_RTTI
918 # if _LIBCPP_STD_VER >= 17
919 template <class _Rp
, class... _Ap
>
920 function(_Rp (*)(_Ap
...)) -> function
<_Rp(_Ap
...)>;
922 template <class _Fp
, class _Stripped
= typename __strip_signature
<decltype(&_Fp::operator())>::type
>
923 function(_Fp
) -> function
<_Stripped
>;
924 # endif // _LIBCPP_STD_VER >= 17
926 template <class _Rp
, class... _ArgTypes
>
927 function
<_Rp(_ArgTypes
...)>::function(const function
& __f
) : __f_(__f
.__f_
) {}
929 # if _LIBCPP_STD_VER <= 14
930 template <class _Rp
, class... _ArgTypes
>
931 template <class _Alloc
>
932 function
<_Rp(_ArgTypes
...)>::function(allocator_arg_t
, const _Alloc
&, const function
& __f
) : __f_(__f
.__f_
) {}
935 template <class _Rp
, class... _ArgTypes
>
936 function
<_Rp(_ArgTypes
...)>::function(function
&& __f
) _NOEXCEPT
: __f_(std::move(__f
.__f_
)) {}
938 # if _LIBCPP_STD_VER <= 14
939 template <class _Rp
, class... _ArgTypes
>
940 template <class _Alloc
>
941 function
<_Rp(_ArgTypes
...)>::function(allocator_arg_t
, const _Alloc
&, function
&& __f
) : __f_(std::move(__f
.__f_
)) {}
944 template <class _Rp
, class... _ArgTypes
>
945 template <class _Fp
, class>
946 function
<_Rp(_ArgTypes
...)>::function(_Fp __f
) : __f_(std::move(__f
)) {}
948 # if _LIBCPP_STD_VER <= 14
949 template <class _Rp
, class... _ArgTypes
>
950 template <class _Fp
, class _Alloc
, class>
951 function
<_Rp(_ArgTypes
...)>::function(allocator_arg_t
, const _Alloc
& __a
, _Fp __f
) : __f_(std::move(__f
), __a
) {}
954 template <class _Rp
, class... _ArgTypes
>
955 function
<_Rp(_ArgTypes
...)>& function
<_Rp(_ArgTypes
...)>::operator=(const function
& __f
) {
956 function(__f
).swap(*this);
960 template <class _Rp
, class... _ArgTypes
>
961 function
<_Rp(_ArgTypes
...)>& function
<_Rp(_ArgTypes
...)>::operator=(function
&& __f
) _NOEXCEPT
{
962 __f_
= std::move(__f
.__f_
);
966 template <class _Rp
, class... _ArgTypes
>
967 function
<_Rp(_ArgTypes
...)>& function
<_Rp(_ArgTypes
...)>::operator=(nullptr_t
) _NOEXCEPT
{
972 template <class _Rp
, class... _ArgTypes
>
973 template <class _Fp
, class>
974 function
<_Rp(_ArgTypes
...)>& function
<_Rp(_ArgTypes
...)>::operator=(_Fp
&& __f
) {
975 function(std::forward
<_Fp
>(__f
)).swap(*this);
979 template <class _Rp
, class... _ArgTypes
>
980 function
<_Rp(_ArgTypes
...)>::~function() {}
982 template <class _Rp
, class... _ArgTypes
>
983 void function
<_Rp(_ArgTypes
...)>::swap(function
& __f
) _NOEXCEPT
{
987 template <class _Rp
, class... _ArgTypes
>
988 _Rp function
<_Rp(_ArgTypes
...)>::operator()(_ArgTypes
... __arg
) const {
989 return __f_(std::forward
<_ArgTypes
>(__arg
)...);
992 # ifndef _LIBCPP_HAS_NO_RTTI
994 template <class _Rp
, class... _ArgTypes
>
995 const std::type_info
& function
<_Rp(_ArgTypes
...)>::target_type() const _NOEXCEPT
{
996 return __f_
.target_type();
999 template <class _Rp
, class... _ArgTypes
>
1000 template <typename _Tp
>
1001 _Tp
* function
<_Rp(_ArgTypes
...)>::target() _NOEXCEPT
{
1002 return (_Tp
*)(__f_
.template target
<_Tp
>());
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
>();
1011 # endif // _LIBCPP_HAS_NO_RTTI
1013 template <class _Rp
, class... _ArgTypes
>
1014 inline _LIBCPP_HIDE_FROM_ABI
bool operator==(const function
<_Rp(_ArgTypes
...)>& __f
, nullptr_t
) _NOEXCEPT
{
1018 # if _LIBCPP_STD_VER <= 17
1020 template <class _Rp
, class... _ArgTypes
>
1021 inline _LIBCPP_HIDE_FROM_ABI
bool operator==(nullptr_t
, const function
<_Rp(_ArgTypes
...)>& __f
) _NOEXCEPT
{
1025 template <class _Rp
, class... _ArgTypes
>
1026 inline _LIBCPP_HIDE_FROM_ABI
bool operator!=(const function
<_Rp(_ArgTypes
...)>& __f
, nullptr_t
) _NOEXCEPT
{
1030 template <class _Rp
, class... _ArgTypes
>
1031 inline _LIBCPP_HIDE_FROM_ABI
bool operator!=(nullptr_t
, const function
<_Rp(_ArgTypes
...)>& __f
) _NOEXCEPT
{
1035 # endif // _LIBCPP_STD_VER <= 17
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
);
1042 _LIBCPP_END_NAMESPACE_STD
1044 #endif // _LIBCPP_CXX03_LANG
1048 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H