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
15 #include <__exception/exception.h>
16 #include <__functional/binary_function.h>
17 #include <__functional/invoke.h>
18 #include <__functional/unary_function.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__memory/addressof.h>
21 #include <__memory/allocator.h>
22 #include <__memory/allocator_destructor.h>
23 #include <__memory/allocator_traits.h>
24 #include <__memory/builtin_new_allocator.h>
25 #include <__memory/compressed_pair.h>
26 #include <__memory/unique_ptr.h>
27 #include <__type_traits/aligned_storage.h>
28 #include <__type_traits/decay.h>
29 #include <__type_traits/is_core_convertible.h>
30 #include <__type_traits/is_scalar.h>
31 #include <__type_traits/is_trivially_copy_constructible.h>
32 #include <__type_traits/is_trivially_destructible.h>
33 #include <__type_traits/is_void.h>
34 #include <__type_traits/strip_signature.h>
35 #include <__utility/forward.h>
36 #include <__utility/move.h>
37 #include <__utility/piecewise_construct.h>
38 #include <__utility/swap.h>
39 #include <__verbose_abort>
44 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45 # pragma GCC system_header
49 #include <__undef_macros>
51 #ifndef _LIBCPP_CXX03_LANG
53 _LIBCPP_BEGIN_NAMESPACE_STD
57 _LIBCPP_DIAGNOSTIC_PUSH
58 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
59 class _LIBCPP_EXPORTED_FROM_ABI bad_function_call
: public exception
{
61 _LIBCPP_HIDE_FROM_ABI
bad_function_call() _NOEXCEPT
= default;
62 _LIBCPP_HIDE_FROM_ABI
bad_function_call(const bad_function_call
&) _NOEXCEPT
= default;
63 _LIBCPP_HIDE_FROM_ABI bad_function_call
& operator=(const bad_function_call
&) _NOEXCEPT
= default;
64 // Note that when a key function is not used, every translation unit that uses
65 // bad_function_call will end up containing a weak definition of the vtable and
67 # ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
68 ~bad_function_call() _NOEXCEPT override
;
70 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
~bad_function_call() _NOEXCEPT override
{}
73 # ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
74 const char* what() const _NOEXCEPT override
;
77 _LIBCPP_DIAGNOSTIC_POP
79 _LIBCPP_NORETURN
inline _LIBCPP_HIDE_FROM_ABI
void __throw_bad_function_call() {
80 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
81 throw bad_function_call();
83 _LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode");
88 class _LIBCPP_TEMPLATE_VIS function
; // undefined
90 namespace __function
{
93 struct __maybe_derive_from_unary_function
{};
95 template <class _Rp
, class _A1
>
96 struct __maybe_derive_from_unary_function
<_Rp(_A1
)> : public __unary_function
<_A1
, _Rp
> {};
99 struct __maybe_derive_from_binary_function
{};
101 template <class _Rp
, class _A1
, class _A2
>
102 struct __maybe_derive_from_binary_function
<_Rp(_A1
, _A2
)> : public __binary_function
<_A1
, _A2
, _Rp
> {};
105 _LIBCPP_HIDE_FROM_ABI
bool __not_null(_Fp
const&) {
110 _LIBCPP_HIDE_FROM_ABI
bool __not_null(_Fp
* __ptr
) {
114 template <class _Ret
, class _Class
>
115 _LIBCPP_HIDE_FROM_ABI
bool __not_null(_Ret
_Class::*__ptr
) {
120 _LIBCPP_HIDE_FROM_ABI
bool __not_null(function
<_Fp
> const& __f
) {
124 # ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
125 template <class _Rp
, class... _Args
>
126 _LIBCPP_HIDE_FROM_ABI
bool __not_null(_Rp (^__p
)(_Args
...)) {
131 } // namespace __function
133 namespace __function
{
135 // __alloc_func holds a functor and an allocator.
137 template <class _Fp
, class _Ap
, class _FB
>
139 template <class _Fp
, class _FB
>
140 class __default_alloc_func
;
142 template <class _Fp
, class _Ap
, class _Rp
, class... _ArgTypes
>
143 class __alloc_func
<_Fp
, _Ap
, _Rp(_ArgTypes
...)> {
144 __compressed_pair
<_Fp
, _Ap
> __f_
;
147 typedef _LIBCPP_NODEBUG _Fp _Target
;
148 typedef _LIBCPP_NODEBUG _Ap _Alloc
;
150 _LIBCPP_HIDE_FROM_ABI
const _Target
& __target() const { return __f_
.first(); }
152 // WIN32 APIs may define __allocator, so use __get_allocator instead.
153 _LIBCPP_HIDE_FROM_ABI
const _Alloc
& __get_allocator() const { return __f_
.second(); }
155 _LIBCPP_HIDE_FROM_ABI
explicit __alloc_func(_Target
&& __f
)
156 : __f_(piecewise_construct
, std::forward_as_tuple(std::move(__f
)), std::forward_as_tuple()) {}
158 _LIBCPP_HIDE_FROM_ABI
explicit __alloc_func(const _Target
& __f
, const _Alloc
& __a
)
159 : __f_(piecewise_construct
, std::forward_as_tuple(__f
), std::forward_as_tuple(__a
)) {}
161 _LIBCPP_HIDE_FROM_ABI
explicit __alloc_func(const _Target
& __f
, _Alloc
&& __a
)
162 : __f_(piecewise_construct
, std::forward_as_tuple(__f
), std::forward_as_tuple(std::move(__a
))) {}
164 _LIBCPP_HIDE_FROM_ABI
explicit __alloc_func(_Target
&& __f
, _Alloc
&& __a
)
165 : __f_(piecewise_construct
, std::forward_as_tuple(std::move(__f
)), std::forward_as_tuple(std::move(__a
))) {}
167 _LIBCPP_HIDE_FROM_ABI _Rp
operator()(_ArgTypes
&&... __arg
) {
168 typedef __invoke_void_return_wrapper
<_Rp
> _Invoker
;
169 return _Invoker::__call(__f_
.first(), std::forward
<_ArgTypes
>(__arg
)...);
172 _LIBCPP_HIDE_FROM_ABI __alloc_func
* __clone() const {
173 typedef allocator_traits
<_Alloc
> __alloc_traits
;
174 typedef __rebind_alloc
<__alloc_traits
, __alloc_func
> _AA
;
175 _AA
__a(__f_
.second());
176 typedef __allocator_destructor
<_AA
> _Dp
;
177 unique_ptr
<__alloc_func
, _Dp
> __hold(__a
.allocate(1), _Dp(__a
, 1));
178 ::new ((void*)__hold
.get()) __alloc_func(__f_
.first(), _Alloc(__a
));
179 return __hold
.release();
182 _LIBCPP_HIDE_FROM_ABI
void destroy() _NOEXCEPT
{ __f_
.~__compressed_pair
<_Target
, _Alloc
>(); }
184 _LIBCPP_HIDE_FROM_ABI
static void __destroy_and_delete(__alloc_func
* __f
) {
185 typedef allocator_traits
<_Alloc
> __alloc_traits
;
186 typedef __rebind_alloc
<__alloc_traits
, __alloc_func
> _FunAlloc
;
187 _FunAlloc
__a(__f
->__get_allocator());
189 __a
.deallocate(__f
, 1);
193 template <class _Fp
, class _Rp
, class... _ArgTypes
>
194 class __default_alloc_func
<_Fp
, _Rp(_ArgTypes
...)> {
198 typedef _LIBCPP_NODEBUG _Fp _Target
;
200 _LIBCPP_HIDE_FROM_ABI
const _Target
& __target() const { return __f_
; }
202 _LIBCPP_HIDE_FROM_ABI
explicit __default_alloc_func(_Target
&& __f
) : __f_(std::move(__f
)) {}
204 _LIBCPP_HIDE_FROM_ABI
explicit __default_alloc_func(const _Target
& __f
) : __f_(__f
) {}
206 _LIBCPP_HIDE_FROM_ABI _Rp
operator()(_ArgTypes
&&... __arg
) {
207 typedef __invoke_void_return_wrapper
<_Rp
> _Invoker
;
208 return _Invoker::__call(__f_
, std::forward
<_ArgTypes
>(__arg
)...);
211 _LIBCPP_HIDE_FROM_ABI __default_alloc_func
* __clone() const {
212 __builtin_new_allocator::__holder_t __hold
= __builtin_new_allocator::__allocate_type
<__default_alloc_func
>(1);
213 __default_alloc_func
* __res
= ::new ((void*)__hold
.get()) __default_alloc_func(__f_
);
214 (void)__hold
.release();
218 _LIBCPP_HIDE_FROM_ABI
void destroy() _NOEXCEPT
{ __f_
.~_Target(); }
220 _LIBCPP_HIDE_FROM_ABI
static void __destroy_and_delete(__default_alloc_func
* __f
) {
222 __builtin_new_allocator::__deallocate_type
<__default_alloc_func
>(__f
, 1);
226 // __base provides an abstract interface for copyable functors.
229 class _LIBCPP_TEMPLATE_VIS __base
;
231 template <class _Rp
, class... _ArgTypes
>
232 class __base
<_Rp(_ArgTypes
...)> {
233 __base(const __base
&);
234 __base
& operator=(const __base
&);
237 _LIBCPP_HIDE_FROM_ABI
__base() {}
238 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual ~__base() {}
239 virtual __base
* __clone() const = 0;
240 virtual void __clone(__base
*) const = 0;
241 virtual void destroy() _NOEXCEPT
= 0;
242 virtual void destroy_deallocate() _NOEXCEPT
= 0;
243 virtual _Rp
operator()(_ArgTypes
&&...) = 0;
244 # ifndef _LIBCPP_HAS_NO_RTTI
245 virtual const void* target(const type_info
&) const _NOEXCEPT
= 0;
246 virtual const std::type_info
& target_type() const _NOEXCEPT
= 0;
247 # endif // _LIBCPP_HAS_NO_RTTI
250 // __func implements __base for a given functor type.
252 template <class _FD
, class _Alloc
, class _FB
>
255 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
256 class __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)> : public __base
<_Rp(_ArgTypes
...)> {
257 __alloc_func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)> __f_
;
260 _LIBCPP_HIDE_FROM_ABI
explicit __func(_Fp
&& __f
) : __f_(std::move(__f
)) {}
262 _LIBCPP_HIDE_FROM_ABI
explicit __func(const _Fp
& __f
, const _Alloc
& __a
) : __f_(__f
, __a
) {}
264 _LIBCPP_HIDE_FROM_ABI
explicit __func(const _Fp
& __f
, _Alloc
&& __a
) : __f_(__f
, std::move(__a
)) {}
266 _LIBCPP_HIDE_FROM_ABI
explicit __func(_Fp
&& __f
, _Alloc
&& __a
) : __f_(std::move(__f
), std::move(__a
)) {}
268 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual __base
<_Rp(_ArgTypes
...)>* __clone() const;
269 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual void __clone(__base
<_Rp(_ArgTypes
...)>*) const;
270 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual void destroy() _NOEXCEPT
;
271 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual void destroy_deallocate() _NOEXCEPT
;
272 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual _Rp
operator()(_ArgTypes
&&... __arg
);
273 # ifndef _LIBCPP_HAS_NO_RTTI
274 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual const void* target(const type_info
&) const _NOEXCEPT
;
275 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
virtual const std::type_info
& target_type() const _NOEXCEPT
;
276 # endif // _LIBCPP_HAS_NO_RTTI
279 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
280 __base
<_Rp(_ArgTypes
...)>* __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::__clone() const {
281 typedef allocator_traits
<_Alloc
> __alloc_traits
;
282 typedef __rebind_alloc
<__alloc_traits
, __func
> _Ap
;
283 _Ap
__a(__f_
.__get_allocator());
284 typedef __allocator_destructor
<_Ap
> _Dp
;
285 unique_ptr
<__func
, _Dp
> __hold(__a
.allocate(1), _Dp(__a
, 1));
286 ::new ((void*)__hold
.get()) __func(__f_
.__target(), _Alloc(__a
));
287 return __hold
.release();
290 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
291 void __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::__clone(__base
<_Rp(_ArgTypes
...)>* __p
) const {
292 ::new ((void*)__p
) __func(__f_
.__target(), __f_
.__get_allocator());
295 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
296 void __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::destroy() _NOEXCEPT
{
300 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
301 void __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::destroy_deallocate() _NOEXCEPT
{
302 typedef allocator_traits
<_Alloc
> __alloc_traits
;
303 typedef __rebind_alloc
<__alloc_traits
, __func
> _Ap
;
304 _Ap
__a(__f_
.__get_allocator());
306 __a
.deallocate(this, 1);
309 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
310 _Rp __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::operator()(_ArgTypes
&&... __arg
) {
311 return __f_(std::forward
<_ArgTypes
>(__arg
)...);
314 # ifndef _LIBCPP_HAS_NO_RTTI
316 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
317 const void* __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::target(const type_info
& __ti
) const _NOEXCEPT
{
318 if (__ti
== typeid(_Fp
))
319 return std::addressof(__f_
.__target());
323 template <class _Fp
, class _Alloc
, class _Rp
, class... _ArgTypes
>
324 const std::type_info
& __func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)>::target_type() const _NOEXCEPT
{
328 # endif // _LIBCPP_HAS_NO_RTTI
330 // __value_func creates a value-type from a __func.
335 template <class _Rp
, class... _ArgTypes
>
336 class __value_func
<_Rp(_ArgTypes
...)> {
337 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
338 typename aligned_storage
<3 * sizeof(void*)>::type __buf_
;
339 _LIBCPP_SUPPRESS_DEPRECATED_POP
341 typedef __base
<_Rp(_ArgTypes
...)> __func
;
344 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI
static __func
* __as_base(void* __p
) { return reinterpret_cast<__func
*>(__p
); }
347 _LIBCPP_HIDE_FROM_ABI
__value_func() _NOEXCEPT
: __f_(nullptr) {}
349 template <class _Fp
, class _Alloc
>
350 _LIBCPP_HIDE_FROM_ABI
__value_func(_Fp
&& __f
, const _Alloc
& __a
) : __f_(nullptr) {
351 typedef allocator_traits
<_Alloc
> __alloc_traits
;
352 typedef __function::__func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)> _Fun
;
353 typedef __rebind_alloc
<__alloc_traits
, _Fun
> _FunAlloc
;
355 if (__function::__not_null(__f
)) {
357 if (sizeof(_Fun
) <= sizeof(__buf_
) && is_nothrow_copy_constructible
<_Fp
>::value
&&
358 is_nothrow_copy_constructible
<_FunAlloc
>::value
) {
359 __f_
= ::new ((void*)&__buf_
) _Fun(std::move(__f
), _Alloc(__af
));
361 typedef __allocator_destructor
<_FunAlloc
> _Dp
;
362 unique_ptr
<__func
, _Dp
> __hold(__af
.allocate(1), _Dp(__af
, 1));
363 ::new ((void*)__hold
.get()) _Fun(std::move(__f
), _Alloc(__a
));
364 __f_
= __hold
.release();
369 template <class _Fp
, __enable_if_t
<!is_same
<__decay_t
<_Fp
>, __value_func
>::value
, int> = 0>
370 _LIBCPP_HIDE_FROM_ABI
explicit __value_func(_Fp
&& __f
) : __value_func(std::forward
<_Fp
>(__f
), allocator
<_Fp
>()) {}
372 _LIBCPP_HIDE_FROM_ABI
__value_func(const __value_func
& __f
) {
373 if (__f
.__f_
== nullptr)
375 else if ((void*)__f
.__f_
== &__f
.__buf_
) {
376 __f_
= __as_base(&__buf_
);
377 __f
.__f_
->__clone(__f_
);
379 __f_
= __f
.__f_
->__clone();
382 _LIBCPP_HIDE_FROM_ABI
__value_func(__value_func
&& __f
) _NOEXCEPT
{
383 if (__f
.__f_
== nullptr)
385 else if ((void*)__f
.__f_
== &__f
.__buf_
) {
386 __f_
= __as_base(&__buf_
);
387 __f
.__f_
->__clone(__f_
);
394 _LIBCPP_HIDE_FROM_ABI
~__value_func() {
395 if ((void*)__f_
== &__buf_
)
398 __f_
->destroy_deallocate();
401 _LIBCPP_HIDE_FROM_ABI __value_func
& operator=(__value_func
&& __f
) {
403 if (__f
.__f_
== nullptr)
405 else if ((void*)__f
.__f_
== &__f
.__buf_
) {
406 __f_
= __as_base(&__buf_
);
407 __f
.__f_
->__clone(__f_
);
415 _LIBCPP_HIDE_FROM_ABI __value_func
& operator=(nullptr_t
) {
418 if ((void*)__f
== &__buf_
)
421 __f
->destroy_deallocate();
425 _LIBCPP_HIDE_FROM_ABI _Rp
operator()(_ArgTypes
&&... __args
) const {
427 __throw_bad_function_call();
428 return (*__f_
)(std::forward
<_ArgTypes
>(__args
)...);
431 _LIBCPP_HIDE_FROM_ABI
void swap(__value_func
& __f
) _NOEXCEPT
{
434 if ((void*)__f_
== &__buf_
&& (void*)__f
.__f_
== &__f
.__buf_
) {
435 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
436 typename aligned_storage
<sizeof(__buf_
)>::type __tempbuf
;
437 _LIBCPP_SUPPRESS_DEPRECATED_POP
438 __func
* __t
= __as_base(&__tempbuf
);
442 __f
.__f_
->__clone(__as_base(&__buf_
));
445 __f_
= __as_base(&__buf_
);
446 __t
->__clone(__as_base(&__f
.__buf_
));
448 __f
.__f_
= __as_base(&__f
.__buf_
);
449 } else if ((void*)__f_
== &__buf_
) {
450 __f_
->__clone(__as_base(&__f
.__buf_
));
453 __f
.__f_
= __as_base(&__f
.__buf_
);
454 } else if ((void*)__f
.__f_
== &__f
.__buf_
) {
455 __f
.__f_
->__clone(__as_base(&__buf_
));
458 __f_
= __as_base(&__buf_
);
460 std::swap(__f_
, __f
.__f_
);
463 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const _NOEXCEPT
{ return __f_
!= nullptr; }
465 # ifndef _LIBCPP_HAS_NO_RTTI
466 _LIBCPP_HIDE_FROM_ABI
const std::type_info
& target_type() const _NOEXCEPT
{
469 return __f_
->target_type();
472 template <typename _Tp
>
473 _LIBCPP_HIDE_FROM_ABI
const _Tp
* target() const _NOEXCEPT
{
476 return (const _Tp
*)__f_
->target(typeid(_Tp
));
478 # endif // _LIBCPP_HAS_NO_RTTI
481 // Storage for a functor object, to be used with __policy to manage copy and
483 union __policy_storage
{
484 mutable char __small
[sizeof(void*) * 2];
488 // True if _Fun can safely be held in __policy_storage.__small.
489 template <typename _Fun
>
490 struct __use_small_storage
491 : public integral_constant
<
493 sizeof(_Fun
) <= sizeof(__policy_storage
)&& _LIBCPP_ALIGNOF(_Fun
) <= _LIBCPP_ALIGNOF(__policy_storage
) &&
494 is_trivially_copy_constructible
<_Fun
>::value
&& is_trivially_destructible
<_Fun
>::value
> {};
496 // Policy contains information about how to copy, destroy, and move the
497 // underlying functor. You can think of it as a vtable of sorts.
499 // Used to copy or destroy __large values. null for trivial objects.
500 void* (*const __clone
)(const void*);
501 void (*const __destroy
)(void*);
503 // True if this is the null policy (no value).
504 const bool __is_null
;
506 // The target type. May be null if RTTI is disabled.
507 const std::type_info
* const __type_info
;
509 // Returns a pointer to a static policy object suitable for the functor
511 template <typename _Fun
>
512 _LIBCPP_HIDE_FROM_ABI
static const __policy
* __create() {
513 return __choose_policy
<_Fun
>(__use_small_storage
<_Fun
>());
516 _LIBCPP_HIDE_FROM_ABI
static const __policy
* __create_empty() {
517 static const _LIBCPP_CONSTEXPR __policy __policy
= {
521 # ifndef _LIBCPP_HAS_NO_RTTI
531 template <typename _Fun
>
532 _LIBCPP_HIDE_FROM_ABI
static void* __large_clone(const void* __s
) {
533 const _Fun
* __f
= static_cast<const _Fun
*>(__s
);
534 return __f
->__clone();
537 template <typename _Fun
>
538 _LIBCPP_HIDE_FROM_ABI
static void __large_destroy(void* __s
) {
539 _Fun::__destroy_and_delete(static_cast<_Fun
*>(__s
));
542 template <typename _Fun
>
543 _LIBCPP_HIDE_FROM_ABI
static const __policy
* __choose_policy(/* is_small = */ false_type
) {
544 static const _LIBCPP_CONSTEXPR __policy __policy
= {
545 &__large_clone
<_Fun
>,
546 &__large_destroy
<_Fun
>,
548 # ifndef _LIBCPP_HAS_NO_RTTI
549 &typeid(typename
_Fun::_Target
)
557 template <typename _Fun
>
558 _LIBCPP_HIDE_FROM_ABI
static const __policy
* __choose_policy(/* is_small = */ true_type
) {
559 static const _LIBCPP_CONSTEXPR __policy __policy
= {
563 # ifndef _LIBCPP_HAS_NO_RTTI
564 &typeid(typename
_Fun::_Target
)
573 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
574 // faster for types that can be passed in registers.
575 template <typename _Tp
>
576 using __fast_forward
= __conditional_t
<is_scalar
<_Tp
>::value
, _Tp
, _Tp
&&>;
578 // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
581 struct __policy_invoker
;
583 template <class _Rp
, class... _ArgTypes
>
584 struct __policy_invoker
<_Rp(_ArgTypes
...)> {
585 typedef _Rp (*__Call
)(const __policy_storage
*, __fast_forward
<_ArgTypes
>...);
589 // Creates an invoker that throws bad_function_call.
590 _LIBCPP_HIDE_FROM_ABI
__policy_invoker() : __call_(&__call_empty
) {}
592 // Creates an invoker that calls the given instance of __func.
593 template <typename _Fun
>
594 _LIBCPP_HIDE_FROM_ABI
static __policy_invoker
__create() {
595 return __policy_invoker(&__call_impl
<_Fun
>);
599 _LIBCPP_HIDE_FROM_ABI
explicit __policy_invoker(__Call __c
) : __call_(__c
) {}
601 _LIBCPP_HIDE_FROM_ABI
static _Rp
__call_empty(const __policy_storage
*, __fast_forward
<_ArgTypes
>...) {
602 __throw_bad_function_call();
605 template <typename _Fun
>
606 _LIBCPP_HIDE_FROM_ABI
static _Rp
__call_impl(const __policy_storage
* __buf
, __fast_forward
<_ArgTypes
>... __args
) {
607 _Fun
* __f
= reinterpret_cast<_Fun
*>(__use_small_storage
<_Fun
>::value
? &__buf
->__small
: __buf
->__large
);
608 return (*__f
)(std::forward
<_ArgTypes
>(__args
)...);
612 // __policy_func uses a __policy and __policy_invoker to create a type-erased,
618 template <class _Rp
, class... _ArgTypes
>
619 class __policy_func
<_Rp(_ArgTypes
...)> {
620 // Inline storage for small objects.
621 __policy_storage __buf_
;
623 // Calls the value stored in __buf_. This could technically be part of
624 // policy, but storing it here eliminates a level of indirection inside
626 typedef __function::__policy_invoker
<_Rp(_ArgTypes
...)> __invoker
;
627 __invoker __invoker_
;
629 // The policy that describes how to move / copy / destroy __buf_. Never
630 // null, even if the function is empty.
631 const __policy
* __policy_
;
634 _LIBCPP_HIDE_FROM_ABI
__policy_func() : __policy_(__policy::__create_empty()) {}
636 template <class _Fp
, class _Alloc
>
637 _LIBCPP_HIDE_FROM_ABI
__policy_func(_Fp
&& __f
, const _Alloc
& __a
) : __policy_(__policy::__create_empty()) {
638 typedef __alloc_func
<_Fp
, _Alloc
, _Rp(_ArgTypes
...)> _Fun
;
639 typedef allocator_traits
<_Alloc
> __alloc_traits
;
640 typedef __rebind_alloc
<__alloc_traits
, _Fun
> _FunAlloc
;
642 if (__function::__not_null(__f
)) {
643 __invoker_
= __invoker::template __create
<_Fun
>();
644 __policy_
= __policy::__create
<_Fun
>();
647 if (__use_small_storage
<_Fun
>()) {
648 ::new ((void*)&__buf_
.__small
) _Fun(std::move(__f
), _Alloc(__af
));
650 typedef __allocator_destructor
<_FunAlloc
> _Dp
;
651 unique_ptr
<_Fun
, _Dp
> __hold(__af
.allocate(1), _Dp(__af
, 1));
652 ::new ((void*)__hold
.get()) _Fun(std::move(__f
), _Alloc(__af
));
653 __buf_
.__large
= __hold
.release();
658 template <class _Fp
, __enable_if_t
<!is_same
<__decay_t
<_Fp
>, __policy_func
>::value
, int> = 0>
659 _LIBCPP_HIDE_FROM_ABI
explicit __policy_func(_Fp
&& __f
) : __policy_(__policy::__create_empty()) {
660 typedef __default_alloc_func
<_Fp
, _Rp(_ArgTypes
...)> _Fun
;
662 if (__function::__not_null(__f
)) {
663 __invoker_
= __invoker::template __create
<_Fun
>();
664 __policy_
= __policy::__create
<_Fun
>();
665 if (__use_small_storage
<_Fun
>()) {
666 ::new ((void*)&__buf_
.__small
) _Fun(std::move(__f
));
668 __builtin_new_allocator::__holder_t __hold
= __builtin_new_allocator::__allocate_type
<_Fun
>(1);
669 __buf_
.__large
= ::new ((void*)__hold
.get()) _Fun(std::move(__f
));
670 (void)__hold
.release();
675 _LIBCPP_HIDE_FROM_ABI
__policy_func(const __policy_func
& __f
)
676 : __buf_(__f
.__buf_
), __invoker_(__f
.__invoker_
), __policy_(__f
.__policy_
) {
677 if (__policy_
->__clone
)
678 __buf_
.__large
= __policy_
->__clone(__f
.__buf_
.__large
);
681 _LIBCPP_HIDE_FROM_ABI
__policy_func(__policy_func
&& __f
)
682 : __buf_(__f
.__buf_
), __invoker_(__f
.__invoker_
), __policy_(__f
.__policy_
) {
683 if (__policy_
->__destroy
) {
684 __f
.__policy_
= __policy::__create_empty();
685 __f
.__invoker_
= __invoker();
689 _LIBCPP_HIDE_FROM_ABI
~__policy_func() {
690 if (__policy_
->__destroy
)
691 __policy_
->__destroy(__buf_
.__large
);
694 _LIBCPP_HIDE_FROM_ABI __policy_func
& operator=(__policy_func
&& __f
) {
697 __invoker_
= __f
.__invoker_
;
698 __policy_
= __f
.__policy_
;
699 __f
.__policy_
= __policy::__create_empty();
700 __f
.__invoker_
= __invoker();
704 _LIBCPP_HIDE_FROM_ABI __policy_func
& operator=(nullptr_t
) {
705 const __policy
* __p
= __policy_
;
706 __policy_
= __policy::__create_empty();
707 __invoker_
= __invoker();
709 __p
->__destroy(__buf_
.__large
);
713 _LIBCPP_HIDE_FROM_ABI _Rp
operator()(_ArgTypes
&&... __args
) const {
714 return __invoker_
.__call_(std::addressof(__buf_
), std::forward
<_ArgTypes
>(__args
)...);
717 _LIBCPP_HIDE_FROM_ABI
void swap(__policy_func
& __f
) {
718 std::swap(__invoker_
, __f
.__invoker_
);
719 std::swap(__policy_
, __f
.__policy_
);
720 std::swap(__buf_
, __f
.__buf_
);
723 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const _NOEXCEPT
{ return !__policy_
->__is_null
; }
725 # ifndef _LIBCPP_HAS_NO_RTTI
726 _LIBCPP_HIDE_FROM_ABI
const std::type_info
& target_type() const _NOEXCEPT
{ return *__policy_
->__type_info
; }
728 template <typename _Tp
>
729 _LIBCPP_HIDE_FROM_ABI
const _Tp
* target() const _NOEXCEPT
{
730 if (__policy_
->__is_null
|| typeid(_Tp
) != *__policy_
->__type_info
)
732 if (__policy_
->__clone
) // Out of line storage.
733 return reinterpret_cast<const _Tp
*>(__buf_
.__large
);
735 return reinterpret_cast<const _Tp
*>(&__buf_
.__small
);
737 # endif // _LIBCPP_HAS_NO_RTTI
740 # if defined(_LIBCPP_HAS_BLOCKS_RUNTIME)
742 extern "C" void* _Block_copy(const void*);
743 extern "C" void _Block_release(const void*);
745 template <class _Rp1
, class... _ArgTypes1
, class _Alloc
, class _Rp
, class... _ArgTypes
>
746 class __func
<_Rp1 (^)(_ArgTypes1
...), _Alloc
, _Rp(_ArgTypes
...)> : public __base
<_Rp(_ArgTypes
...)> {
747 typedef _Rp1 (^__block_type
)(_ArgTypes1
...);
751 _LIBCPP_HIDE_FROM_ABI
explicit __func(__block_type
const& __f
)
752 # ifdef _LIBCPP_HAS_OBJC_ARC
755 : __f_(reinterpret_cast<__block_type
>(__f
? _Block_copy(__f
) : nullptr))
760 // [TODO] add && to save on a retain
762 _LIBCPP_HIDE_FROM_ABI
explicit __func(__block_type __f
, const _Alloc
& /* unused */)
763 # ifdef _LIBCPP_HAS_OBJC_ARC
766 : __f_(reinterpret_cast<__block_type
>(__f
? _Block_copy(__f
) : nullptr))
771 virtual __base
<_Rp(_ArgTypes
...)>* __clone() const {
772 _LIBCPP_ASSERT_INTERNAL(
774 "Block pointers are just pointers, so they should always fit into "
775 "std::function's small buffer optimization. This function should "
776 "never be invoked.");
780 virtual void __clone(__base
<_Rp(_ArgTypes
...)>* __p
) const { ::new ((void*)__p
) __func(__f_
); }
782 virtual void destroy() _NOEXCEPT
{
783 # ifndef _LIBCPP_HAS_OBJC_ARC
785 _Block_release(__f_
);
790 virtual void destroy_deallocate() _NOEXCEPT
{
791 _LIBCPP_ASSERT_INTERNAL(
793 "Block pointers are just pointers, so they should always fit into "
794 "std::function's small buffer optimization. This function should "
795 "never be invoked.");
798 virtual _Rp
operator()(_ArgTypes
&&... __arg
) { return std::__invoke(__f_
, std::forward
<_ArgTypes
>(__arg
)...); }
800 # ifndef _LIBCPP_HAS_NO_RTTI
801 virtual const void* target(type_info
const& __ti
) const _NOEXCEPT
{
802 if (__ti
== typeid(__func::__block_type
))
804 return (const void*)nullptr;
807 virtual const std::type_info
& target_type() const _NOEXCEPT
{ return typeid(__func::__block_type
); }
808 # endif // _LIBCPP_HAS_NO_RTTI
811 # endif // _LIBCPP_HAS_EXTENSION_BLOCKS
813 } // namespace __function
815 template <class _Rp
, class... _ArgTypes
>
816 class _LIBCPP_TEMPLATE_VIS function
<_Rp(_ArgTypes
...)>
817 : public __function::__maybe_derive_from_unary_function
<_Rp(_ArgTypes
...)>,
818 public __function::__maybe_derive_from_binary_function
<_Rp(_ArgTypes
...)> {
819 # ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
820 typedef __function::__value_func
<_Rp(_ArgTypes
...)> __func
;
822 typedef __function::__policy_func
<_Rp(_ArgTypes
...)> __func
;
828 bool = _And
< _IsNotSame
<__remove_cvref_t
<_Fp
>, function
>, __invokable
<_Fp
, _ArgTypes
...> >::value
>
831 struct __callable
<_Fp
, true> {
832 static const bool value
=
833 is_void
<_Rp
>::value
|| __is_core_convertible
<typename __invoke_of
<_Fp
, _ArgTypes
...>::type
, _Rp
>::value
;
836 struct __callable
<_Fp
, false> {
837 static const bool value
= false;
841 using _EnableIfLValueCallable
= __enable_if_t
<__callable
<_Fp
&>::value
>;
844 typedef _Rp result_type
;
846 // construct/copy/destroy:
847 _LIBCPP_HIDE_FROM_ABI
function() _NOEXCEPT
{}
848 _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI
function(nullptr_t
) _NOEXCEPT
{}
849 _LIBCPP_HIDE_FROM_ABI
function(const function
&);
850 _LIBCPP_HIDE_FROM_ABI
function(function
&&) _NOEXCEPT
;
851 template <class _Fp
, class = _EnableIfLValueCallable
<_Fp
>>
852 _LIBCPP_HIDE_FROM_ABI
function(_Fp
);
854 # if _LIBCPP_STD_VER <= 14
855 template <class _Alloc
>
856 _LIBCPP_HIDE_FROM_ABI
function(allocator_arg_t
, const _Alloc
&) _NOEXCEPT
{}
857 template <class _Alloc
>
858 _LIBCPP_HIDE_FROM_ABI
function(allocator_arg_t
, const _Alloc
&, nullptr_t
) _NOEXCEPT
{}
859 template <class _Alloc
>
860 _LIBCPP_HIDE_FROM_ABI
function(allocator_arg_t
, const _Alloc
&, const function
&);
861 template <class _Alloc
>
862 _LIBCPP_HIDE_FROM_ABI
function(allocator_arg_t
, const _Alloc
&, function
&&);
863 template <class _Fp
, class _Alloc
, class = _EnableIfLValueCallable
<_Fp
>>
864 _LIBCPP_HIDE_FROM_ABI
function(allocator_arg_t
, const _Alloc
& __a
, _Fp __f
);
867 _LIBCPP_HIDE_FROM_ABI function
& operator=(const function
&);
868 _LIBCPP_HIDE_FROM_ABI function
& operator=(function
&&) _NOEXCEPT
;
869 _LIBCPP_HIDE_FROM_ABI function
& operator=(nullptr_t
) _NOEXCEPT
;
870 template <class _Fp
, class = _EnableIfLValueCallable
<__decay_t
<_Fp
>>>
871 _LIBCPP_HIDE_FROM_ABI function
& operator=(_Fp
&&);
873 _LIBCPP_HIDE_FROM_ABI
~function();
875 // function modifiers:
876 _LIBCPP_HIDE_FROM_ABI
void swap(function
&) _NOEXCEPT
;
878 # if _LIBCPP_STD_VER <= 14
879 template <class _Fp
, class _Alloc
>
880 _LIBCPP_HIDE_FROM_ABI
void assign(_Fp
&& __f
, const _Alloc
& __a
) {
881 function(allocator_arg
, __a
, std::forward
<_Fp
>(__f
)).swap(*this);
885 // function capacity:
886 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const _NOEXCEPT
{ return static_cast<bool>(__f_
); }
888 // deleted overloads close possible hole in the type system
889 template <class _R2
, class... _ArgTypes2
>
890 bool operator==(const function
<_R2(_ArgTypes2
...)>&) const = delete;
891 # if _LIBCPP_STD_VER <= 17
892 template <class _R2
, class... _ArgTypes2
>
893 bool operator!=(const function
<_R2(_ArgTypes2
...)>&) const = delete;
897 // function invocation:
898 _LIBCPP_HIDE_FROM_ABI _Rp
operator()(_ArgTypes
...) const;
900 # ifndef _LIBCPP_HAS_NO_RTTI
901 // function target access:
902 _LIBCPP_HIDE_FROM_ABI
const std::type_info
& target_type() const _NOEXCEPT
;
903 template <typename _Tp
>
904 _LIBCPP_HIDE_FROM_ABI _Tp
* target() _NOEXCEPT
;
905 template <typename _Tp
>
906 _LIBCPP_HIDE_FROM_ABI
const _Tp
* target() const _NOEXCEPT
;
907 # endif // _LIBCPP_HAS_NO_RTTI
910 # if _LIBCPP_STD_VER >= 17
911 template <class _Rp
, class... _Ap
>
912 function(_Rp (*)(_Ap
...)) -> function
<_Rp(_Ap
...)>;
914 template <class _Fp
, class _Stripped
= typename __strip_signature
<decltype(&_Fp::operator())>::type
>
915 function(_Fp
) -> function
<_Stripped
>;
916 # endif // _LIBCPP_STD_VER >= 17
918 template <class _Rp
, class... _ArgTypes
>
919 function
<_Rp(_ArgTypes
...)>::function(const function
& __f
) : __f_(__f
.__f_
) {}
921 # if _LIBCPP_STD_VER <= 14
922 template <class _Rp
, class... _ArgTypes
>
923 template <class _Alloc
>
924 function
<_Rp(_ArgTypes
...)>::function(allocator_arg_t
, const _Alloc
&, const function
& __f
) : __f_(__f
.__f_
) {}
927 template <class _Rp
, class... _ArgTypes
>
928 function
<_Rp(_ArgTypes
...)>::function(function
&& __f
) _NOEXCEPT
: __f_(std::move(__f
.__f_
)) {}
930 # if _LIBCPP_STD_VER <= 14
931 template <class _Rp
, class... _ArgTypes
>
932 template <class _Alloc
>
933 function
<_Rp(_ArgTypes
...)>::function(allocator_arg_t
, const _Alloc
&, function
&& __f
) : __f_(std::move(__f
.__f_
)) {}
936 template <class _Rp
, class... _ArgTypes
>
937 template <class _Fp
, class>
938 function
<_Rp(_ArgTypes
...)>::function(_Fp __f
) : __f_(std::move(__f
)) {}
940 # if _LIBCPP_STD_VER <= 14
941 template <class _Rp
, class... _ArgTypes
>
942 template <class _Fp
, class _Alloc
, class>
943 function
<_Rp(_ArgTypes
...)>::function(allocator_arg_t
, const _Alloc
& __a
, _Fp __f
) : __f_(std::move(__f
), __a
) {}
946 template <class _Rp
, class... _ArgTypes
>
947 function
<_Rp(_ArgTypes
...)>& function
<_Rp(_ArgTypes
...)>::operator=(const function
& __f
) {
948 function(__f
).swap(*this);
952 template <class _Rp
, class... _ArgTypes
>
953 function
<_Rp(_ArgTypes
...)>& function
<_Rp(_ArgTypes
...)>::operator=(function
&& __f
) _NOEXCEPT
{
954 __f_
= std::move(__f
.__f_
);
958 template <class _Rp
, class... _ArgTypes
>
959 function
<_Rp(_ArgTypes
...)>& function
<_Rp(_ArgTypes
...)>::operator=(nullptr_t
) _NOEXCEPT
{
964 template <class _Rp
, class... _ArgTypes
>
965 template <class _Fp
, class>
966 function
<_Rp(_ArgTypes
...)>& function
<_Rp(_ArgTypes
...)>::operator=(_Fp
&& __f
) {
967 function(std::forward
<_Fp
>(__f
)).swap(*this);
971 template <class _Rp
, class... _ArgTypes
>
972 function
<_Rp(_ArgTypes
...)>::~function() {}
974 template <class _Rp
, class... _ArgTypes
>
975 void function
<_Rp(_ArgTypes
...)>::swap(function
& __f
) _NOEXCEPT
{
979 template <class _Rp
, class... _ArgTypes
>
980 _Rp function
<_Rp(_ArgTypes
...)>::operator()(_ArgTypes
... __arg
) const {
981 return __f_(std::forward
<_ArgTypes
>(__arg
)...);
984 # ifndef _LIBCPP_HAS_NO_RTTI
986 template <class _Rp
, class... _ArgTypes
>
987 const std::type_info
& function
<_Rp(_ArgTypes
...)>::target_type() const _NOEXCEPT
{
988 return __f_
.target_type();
991 template <class _Rp
, class... _ArgTypes
>
992 template <typename _Tp
>
993 _Tp
* function
<_Rp(_ArgTypes
...)>::target() _NOEXCEPT
{
994 return (_Tp
*)(__f_
.template target
<_Tp
>());
997 template <class _Rp
, class... _ArgTypes
>
998 template <typename _Tp
>
999 const _Tp
* function
<_Rp(_ArgTypes
...)>::target() const _NOEXCEPT
{
1000 return __f_
.template target
<_Tp
>();
1003 # endif // _LIBCPP_HAS_NO_RTTI
1005 template <class _Rp
, class... _ArgTypes
>
1006 inline _LIBCPP_HIDE_FROM_ABI
bool operator==(const function
<_Rp(_ArgTypes
...)>& __f
, nullptr_t
) _NOEXCEPT
{
1010 # if _LIBCPP_STD_VER <= 17
1012 template <class _Rp
, class... _ArgTypes
>
1013 inline _LIBCPP_HIDE_FROM_ABI
bool operator==(nullptr_t
, const function
<_Rp(_ArgTypes
...)>& __f
) _NOEXCEPT
{
1017 template <class _Rp
, class... _ArgTypes
>
1018 inline _LIBCPP_HIDE_FROM_ABI
bool operator!=(const function
<_Rp(_ArgTypes
...)>& __f
, nullptr_t
) _NOEXCEPT
{
1022 template <class _Rp
, class... _ArgTypes
>
1023 inline _LIBCPP_HIDE_FROM_ABI
bool operator!=(nullptr_t
, const function
<_Rp(_ArgTypes
...)>& __f
) _NOEXCEPT
{
1027 # endif // _LIBCPP_STD_VER <= 17
1029 template <class _Rp
, class... _ArgTypes
>
1030 inline _LIBCPP_HIDE_FROM_ABI
void swap(function
<_Rp(_ArgTypes
...)>& __x
, function
<_Rp(_ArgTypes
...)>& __y
) _NOEXCEPT
{
1031 return __x
.swap(__y
);
1034 _LIBCPP_END_NAMESPACE_STD
1036 #endif // _LIBCPP_CXX03_LANG
1040 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H