30#ifndef _GLIBCXX_STD_FUNCTION_H
31#define _GLIBCXX_STD_FUNCTION_H 1
34#pragma GCC system_header
37#if __cplusplus < 201103L
47namespace std _GLIBCXX_VISIBILITY(default)
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
61 const char*
what() const noexcept;
70 template<typename _Tp>
75 class _Undefined_class;
80 const void* _M_const_object;
81 void (*_M_function_pointer)();
82 void (_Undefined_class::*_M_member_pointer)();
85 union [[gnu::may_alias]] _Any_data
87 void* _M_access() noexcept {
return &_M_pod_data[0]; }
88 const void* _M_access() const noexcept {
return &_M_pod_data[0]; }
90 template<
typename _Tp>
93 {
return *
static_cast<_Tp*
>(_M_access()); }
95 template<
typename _Tp>
97 _M_access() const noexcept
98 {
return *
static_cast<const _Tp*
>(_M_access()); }
100 _Nocopy_types _M_unused;
101 char _M_pod_data[
sizeof(_Nocopy_types)];
104 enum _Manager_operation
112 template<
typename _Signature>
119 static const size_t _M_max_size =
sizeof(_Nocopy_types);
120 static const size_t _M_max_align = __alignof__(_Nocopy_types);
122 template<
typename _Functor>
126 static const bool __stored_locally =
128 &&
sizeof(_Functor) <= _M_max_size
129 && __alignof__(_Functor) <= _M_max_align
130 && (_M_max_align % __alignof__(_Functor) == 0));
136 _M_get_pointer(
const _Any_data& __source)
noexcept
138#pragma GCC diagnostic push
139#pragma GCC diagnostic ignored "-Wc++17-extensions"
140 if constexpr (__stored_locally)
142 const _Functor& __f = __source._M_access<_Functor>();
146 return __source._M_access<_Functor*>();
147#pragma GCC diagnostic pop
153 template<
typename _Fn>
155 _M_create(_Any_data& __dest, _Fn&& __f,
true_type)
161 template<
typename _Fn>
163 _M_create(_Any_data& __dest, _Fn&& __f,
false_type)
165 __dest._M_access<_Functor*>()
171 _M_destroy(_Any_data& __victim,
true_type)
173 __victim._M_access<_Functor>().~_Functor();
180 delete __victim._M_access<_Functor*>();
185 _M_manager(_Any_data& __dest,
const _Any_data& __source,
186 _Manager_operation __op)
190 case __get_type_info:
192 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
194 __dest._M_access<
const type_info*>() =
nullptr;
198 case __get_functor_ptr:
199 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
202 case __clone_functor:
203 _M_init_functor(__dest,
204 *
const_cast<const _Functor*
>(_M_get_pointer(__source)));
207 case __destroy_functor:
208 _M_destroy(__dest, _Local_storage());
214 template<
typename _Fn>
216 _M_init_functor(_Any_data& __functor, _Fn&& __f)
217 noexcept(__and_<_Local_storage,
223 template<
typename _Signature>
225 _M_not_empty_function(
const function<_Signature>& __f)
noexcept
226 {
return static_cast<bool>(__f); }
228 template<
typename _Tp>
230 _M_not_empty_function(_Tp* __fp)
noexcept
231 {
return __fp !=
nullptr; }
233 template<
typename _Class,
typename _Tp>
235 _M_not_empty_function(_Tp _Class::* __mp)
noexcept
236 {
return __mp !=
nullptr; }
238 template<
typename _Tp>
240 _M_not_empty_function(
const _Tp&)
noexcept
244 _Function_base() =
default;
249 _M_manager(_M_functor, _M_functor, __destroy_functor);
252 bool _M_empty()
const {
return !_M_manager; }
255 = bool (*)(_Any_data&,
const _Any_data&, _Manager_operation);
257 _Any_data _M_functor{};
258 _Manager_type _M_manager{};
261 template<
typename _Signature,
typename _Functor>
262 class _Function_handler;
264 template<
typename _Res,
typename _Functor,
typename... _ArgTypes>
265 class _Function_handler<_Res(_ArgTypes...), _Functor>
266 :
public _Function_base::_Base_manager<_Functor>
268 using _Base = _Function_base::_Base_manager<_Functor>;
272 _M_manager(_Any_data& __dest,
const _Any_data& __source,
273 _Manager_operation __op)
278 case __get_type_info:
279 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
282 case __get_functor_ptr:
283 __dest._M_access<_Functor*>() = _Base::_M_get_pointer(__source);
287 _Base::_M_manager(__dest, __source, __op);
293 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
295 return std::__invoke_r<_Res>(*_Base::_M_get_pointer(__functor),
299 template<
typename _Fn>
300 static constexpr bool
301 _S_nothrow_init() noexcept
303 return __and_<
typename _Base::_Local_storage,
304 is_nothrow_constructible<_Functor, _Fn>>::value;
310 class _Function_handler<void, void>
314 _M_manager(_Any_data&,
const _Any_data&, _Manager_operation)
323 template<
typename _Res,
typename... _ArgTypes>
325 :
public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
326 private _Function_base
330 template<
typename _Func,
333 =
typename __enable_if_t<!_Self, decay<_Func>>
::type;
335 template<
typename _Func,
336 typename _DFunc = _Decay_t<_Func>,
337 typename _Res2 = __invoke_result<_DFunc&, _ArgTypes...>>
339 : __is_invocable_impl<_Res2, _Res>::type
342 template<
typename _Cond,
typename _Tp =
void>
343 using _Requires = __enable_if_t<_Cond::value, _Tp>;
345 template<
typename _Functor>
347 = _Function_handler<_Res(_ArgTypes...), __decay_t<_Functor>>;
350 typedef _Res result_type;
359 : _Function_base() { }
366 : _Function_base() { }
379 if (
static_cast<bool>(__x))
381 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
382 _M_invoker = __x._M_invoker;
383 _M_manager = __x._M_manager;
395 : _Function_base(), _M_invoker(__x._M_invoker)
397 if (
static_cast<bool>(__x))
399 _M_functor = __x._M_functor;
400 _M_manager = __x._M_manager;
401 __x._M_manager =
nullptr;
402 __x._M_invoker =
nullptr;
423 template<
typename _Functor,
424 typename _Constraints = _Requires<_Callable<_Functor>>>
426 noexcept(_Handler<_Functor>::template _S_nothrow_init<_Functor>())
430 "std::function target must be copy-constructible");
432 "std::function target must be constructible from the "
433 "constructor argument");
435 using _My_handler = _Handler<_Functor>;
437 if (_My_handler::_M_not_empty_function(__f))
439 _My_handler::_M_init_functor(_M_functor,
441 _M_invoker = &_My_handler::_M_invoke;
442 _M_manager = &_My_handler::_M_manager;
495 _M_manager(_M_functor, _M_functor, __destroy_functor);
496 _M_manager =
nullptr;
497 _M_invoker =
nullptr;
519 template<
typename _Functor>
520 _Requires<_Callable<_Functor>,
function&>
522 noexcept(_Handler<_Functor>::template _S_nothrow_init<_Functor>())
529 template<
typename _Functor>
548 std::swap(_M_functor, __x._M_functor);
549 std::swap(_M_manager, __x._M_manager);
550 std::swap(_M_invoker, __x._M_invoker);
563 explicit operator bool() const noexcept
564 {
return !_M_empty(); }
580 __throw_bad_function_call();
600 _Any_data __typeinfo_result;
601 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
602 if (
auto __ti = __typeinfo_result._M_access<
const type_info*>())
620 template<
typename _Functor>
624 const function* __const_this =
this;
628 return *
const_cast<_Functor**
>(&__func);
631 template<
typename _Functor>
635#pragma GCC diagnostic push
636#pragma GCC diagnostic ignored "-Wc++17-extensions"
639 if (_M_manager == &_Handler<_Functor>::_M_manager
641 || (_M_manager &&
typeid(_Functor) ==
target_type())
646 _M_manager(__ptr, _M_functor, __get_functor_ptr);
647 return __ptr._M_access<
const _Functor*>();
650#pragma GCC diagnostic pop
656 using _Invoker_type = _Res (*)(
const _Any_data&, _ArgTypes&&...);
657 _Invoker_type _M_invoker =
nullptr;
660#if __cpp_deduction_guides >= 201606
662 struct __function_guide_helper
665 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
666 struct __function_guide_helper<
667 _Res (_Tp::*) (_Args...) noexcept(_Nx)
669 {
using type = _Res(_Args...); };
671 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
672 struct __function_guide_helper<
673 _Res (_Tp::*) (_Args...) & noexcept(_Nx)
675 {
using type = _Res(_Args...); };
677 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
678 struct __function_guide_helper<
679 _Res (_Tp::*) (_Args...) const noexcept(_Nx)
681 {
using type = _Res(_Args...); };
683 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
684 struct __function_guide_helper<
685 _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
687 {
using type = _Res(_Args...); };
689#if __cpp_explicit_this_parameter >= 202110L
692 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
693 struct __function_guide_helper<_Res (*) (_Tp, _Args...) noexcept(_Nx)>
694 {
using type = _Res(_Args...); };
697#if __cpp_static_call_operator >= 202207L && __cpp_concepts >= 202002L
698 template<
typename _StaticCallOp>
699 struct __function_guide_static_helper
702 template<
typename _Res,
bool _Nx,
typename... _Args>
703 struct __function_guide_static_helper<_Res (*) (_Args...) noexcept(_Nx)>
704 {
using type = _Res(_Args...); };
706 template<
typename _Fn,
typename _Op>
707 using __function_guide_t =
typename __conditional_t<
708 requires (_Fn& __f) { (void) __f.operator(); },
709 __function_guide_static_helper<_Op>,
710 __function_guide_helper<_Op>>::type;
712 template<
typename _Fn,
typename _Op>
713 using __function_guide_t =
typename __function_guide_helper<_Op>::type;
716 template<
typename _Res,
typename... _ArgTypes>
717 function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
719 template<
typename _Fn,
typename _Signature
720 = __function_guide_t<_Fn,
decltype(&_Fn::operator())>>
721 function(_Fn) -> function<_Signature>;
732 template<
typename _Res,
typename... _Args>
734 operator==(
const function<_Res(_Args...)>& __f, nullptr_t)
noexcept
735 {
return !
static_cast<bool>(__f); }
737#if __cpp_impl_three_way_comparison < 201907L
739 template<
typename _Res,
typename... _Args>
741 operator==(nullptr_t,
const function<_Res(_Args...)>& __f)
noexcept
742 {
return !
static_cast<bool>(__f); }
750 template<
typename _Res,
typename... _Args>
752 operator!=(
const function<_Res(_Args...)>& __f, nullptr_t)
noexcept
753 {
return static_cast<bool>(__f); }
756 template<
typename _Res,
typename... _Args>
758 operator!=(nullptr_t,
const function<_Res(_Args...)>& __f)
noexcept
759 {
return static_cast<bool>(__f); }
771 template<
typename _Res,
typename... _Args>
773 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
noexcept
776#if __cplusplus >= 201703L
777 namespace __detail::__variant
779 template<
typename>
struct _Never_valueless_alt;
783 template<
typename _Signature>
784 struct _Never_valueless_alt<
std::function<_Signature>>
790_GLIBCXX_END_NAMESPACE_VERSION
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.
Primary class template for reference_wrapper.
Base class for all library exceptions.
Exception class thrown when class template function's operator() is called with an empty target.
const char * what() const noexcept
_Functor * target() noexcept
Access the stored target function object.
function & operator=(const function &__x)
Function assignment operator.
const type_info & target_type() const noexcept
Determine the type of the target of this function object wrapper.
function() noexcept
Default construct creates an empty function call wrapper.
_Res operator()(_ArgTypes... __args) const
Invokes the function targeted by *this.
void swap(function &__x) noexcept
Swap the targets of two function objects.