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
80 const void* _M_const_object;
81 void (*_M_function_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
136 _M_get_pointer(
const _Any_data&
__source)
noexcept
140 const _Functor& __f =
__source._M_access<_Functor>();
144 return __source._M_access<_Functor*>();
150 template<
typename _Fn>
158 template<
typename _Fn>
162 __dest._M_access<_Functor*>()
170 __victim._M_access<_Functor>().~_Functor();
177 delete __victim._M_access<_Functor*>();
183 _Manager_operation
__op)
187 case __get_type_info:
195 case __get_functor_ptr:
199 case __clone_functor:
201 *
const_cast<const _Functor*
>(_M_get_pointer(
__source)));
204 case __destroy_functor:
205 _M_destroy(
__dest, _Local_storage());
211 template<
typename _Fn>
213 _M_init_functor(_Any_data&
__functor, _Fn&& __f)
214 noexcept(__and_<_Local_storage,
220 template<
typename _Signature>
223 {
return static_cast<bool>(__f); }
225 template<
typename _Tp>
227 _M_not_empty_function(_Tp*
__fp)
noexcept
228 {
return __fp !=
nullptr; }
230 template<
typename _Class,
typename _Tp>
232 _M_not_empty_function(_Tp _Class::*
__mp)
noexcept
233 {
return __mp !=
nullptr; }
235 template<
typename _Tp>
237 _M_not_empty_function(
const _Tp&)
noexcept
246 _M_manager(_M_functor, _M_functor, __destroy_functor);
249 bool _M_empty()
const {
return !_M_manager; }
252 = bool (*)(_Any_data&,
const _Any_data&, _Manager_operation);
254 _Any_data _M_functor{};
258 template<
typename _Signature,
typename _Functor>
259 class _Function_handler;
261 template<
typename _Res,
typename _Functor,
typename...
_ArgTypes>
263 :
public _Function_base::_Base_manager<_Functor>
265 using _Base = _Function_base::_Base_manager<_Functor>;
270 _Manager_operation
__op)
275 case __get_type_info:
279 case __get_functor_ptr:
290 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
296 template<
typename _Fn>
297 static constexpr bool
298 _S_nothrow_init() noexcept
300 return __and_<
typename _Base::_Local_storage,
301 is_nothrow_constructible<_Functor, _Fn>>::value;
307 class _Function_handler<void, void>
311 _M_manager(_Any_data&,
const _Any_data&, _Manager_operation)
319 template<
typename _Signature,
typename _Functor,
320 bool __valid = is_object<_Functor>::value>
321 struct _Target_handler
322 : _Function_handler<_Signature, typename remove_cv<_Functor>::type>
325 template<
typename _Signature,
typename _Functor>
326 struct _Target_handler<_Signature, _Functor, false>
327 : _Function_handler<void, void>
335 template<
typename _Res,
typename... _ArgTypes>
337 :
public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
342 template<
typename _Func,
347 template<
typename _Func,
351 : __is_invocable_impl<_Res2, _Res>::type
354 template<
typename _Cond,
typename _Tp =
void>
357 template<
typename _Functor>
391 if (
static_cast<bool>(__x))
393 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
394 _M_invoker = __x._M_invoker;
395 _M_manager = __x._M_manager;
409 if (
static_cast<bool>(__x))
411 _M_functor = __x._M_functor;
412 _M_manager = __x._M_manager;
413 __x._M_manager =
nullptr;
414 __x._M_invoker =
nullptr;
435 template<
typename _Functor,
442 "std::function target must be copy-constructible");
444 "std::function target must be constructible from the "
445 "constructor argument");
449 if (_My_handler::_M_not_empty_function(__f))
451 _My_handler::_M_init_functor(_M_functor,
453 _M_invoker = &_My_handler::_M_invoke;
454 _M_manager = &_My_handler::_M_manager;
473 function(__x).swap(*
this);
507 _M_manager(_M_functor, _M_functor, __destroy_functor);
508 _M_manager =
nullptr;
509 _M_invoker =
nullptr;
531 template<
typename _Functor>
541 template<
typename _Functor>
545 function(__f).swap(*
this);
558 void swap(function& __x)
noexcept
560 std::swap(_M_functor, __x._M_functor);
561 std::swap(_M_manager, __x._M_manager);
562 std::swap(_M_invoker, __x._M_invoker);
575 explicit operator bool()
const noexcept
576 {
return !_M_empty(); }
592 __throw_bad_function_call();
632 template<
typename _Functor>
640 return *
const_cast<_Functor**
>(&
__func);
643 template<
typename _Functor>
653 if (_M_manager == &_Handler::_M_manager
655 || (_M_manager &&
typeid(_Functor) == target_type())
660 _M_manager(
__ptr, _M_functor, __get_functor_ptr);
661 return __ptr._M_access<
const _Functor*>();
669 using _Invoker_type =
_Res (*)(
const _Any_data&,
_ArgTypes&&...);
670 _Invoker_type _M_invoker =
nullptr;
673#if __cpp_deduction_guides >= 201606
675 struct __function_guide_helper
678 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
679 struct __function_guide_helper<
680 _Res (_Tp::*) (_Args...) noexcept(_Nx)
682 {
using type = _Res(_Args...); };
684 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
685 struct __function_guide_helper<
686 _Res (_Tp::*) (_Args...) & noexcept(_Nx)
688 {
using type = _Res(_Args...); };
690 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
691 struct __function_guide_helper<
692 _Res (_Tp::*) (_Args...) const noexcept(_Nx)
694 {
using type = _Res(_Args...); };
696 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
697 struct __function_guide_helper<
698 _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
700 {
using type = _Res(_Args...); };
702#if __cpp_explicit_this_parameter >= 202110L
705 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
706 struct __function_guide_helper<_Res (*) (_Tp, _Args...) noexcept(_Nx)>
707 {
using type = _Res(_Args...); };
710#if __cpp_static_call_operator >= 202207L && __cpp_concepts >= 202002L
711 template<
typename _StaticCallOp>
712 struct __function_guide_static_helper
715 template<
typename _Res,
bool _Nx,
typename... _Args>
716 struct __function_guide_static_helper<_Res (*) (_Args...) noexcept(_Nx)>
717 {
using type = _Res(_Args...); };
719 template<
typename _Fn,
typename _Op>
720 using __function_guide_t =
typename __conditional_t<
721 requires (_Fn& __f) { (void) __f.operator(); },
722 __function_guide_static_helper<_Op>,
723 __function_guide_helper<_Op>>::type;
725 template<
typename _Fn,
typename _Op>
726 using __function_guide_t =
typename __function_guide_helper<_Op>::type;
729 template<
typename _Res,
typename... _ArgTypes>
730 function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
732 template<
typename _Fn,
typename _Signature
733 = __function_guide_t<_Fn,
decltype(&_Fn::operator())>>
734 function(_Fn) -> function<_Signature>;
745 template<
typename _Res,
typename... _Args>
747 operator==(
const function<
_Res(
_Args...)>& __f, nullptr_t)
noexcept
748 {
return !
static_cast<bool>(__f); }
750#if __cpp_impl_three_way_comparison < 201907L
752 template<
typename _Res,
typename... _Args>
754 operator==(nullptr_t,
const function<_Res(_Args...)>& __f)
noexcept
755 {
return !
static_cast<bool>(__f); }
763 template<
typename _Res,
typename... _Args>
765 operator!=(
const function<_Res(_Args...)>& __f, nullptr_t)
noexcept
766 {
return static_cast<bool>(__f); }
769 template<
typename _Res,
typename... _Args>
771 operator!=(nullptr_t,
const function<_Res(_Args...)>& __f)
noexcept
772 {
return static_cast<bool>(__f); }
784 template<
typename _Res,
typename... _Args>
789#if __cplusplus >= 201703L
790 namespace __detail::__variant
792 template<
typename>
struct _Never_valueless_alt;
796 template<
typename _Signature>
797 struct _Never_valueless_alt<
std::function<_Signature>>
803_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.
ISO C++ entities toplevel namespace is std.
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
Base class of all polymorphic function object wrappers.
const _Functor * target() const noexcept
Access the stored target function object.
_Functor * target() noexcept
Access the stored target function object.
function & operator=(const function &__x)
Function assignment operator.
function(nullptr_t) noexcept
Creates an empty function call wrapper.
_Requires< _Callable< _Functor >, function & > operator=(_Functor &&__f) noexcept(_Handler< _Functor >::template _S_nothrow_init< _Functor >())
Function assignment to a new target.
function & operator=(reference_wrapper< _Functor > __f) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
const type_info & target_type() const noexcept
Determine the type of the target of this function object wrapper.
function & operator=(function &&__x) noexcept
Function move-assignment operator.
function(const function &__x)
Function copy constructor.
function(function &&__x) noexcept
Function move constructor.
function & operator=(nullptr_t) noexcept
Function assignment to empty.
function(_Functor &&__f) noexcept(_Handler< _Functor >::template _S_nothrow_init< _Functor >())
Builds a function that targets a copy of the incoming function object.
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.