30#ifndef _GLIBCXX_STD_FUNCTION_H
31#define _GLIBCXX_STD_FUNCTION_H 1
33#pragma GCC system_header
35#if __cplusplus < 201103L
45namespace std _GLIBCXX_VISIBILITY(default)
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
59 const char*
what() const noexcept;
78 const void* _M_const_object;
79 void (*_M_function_pointer)();
83 union [[gnu::may_alias]] _Any_data
85 void* _M_access() noexcept {
return &_M_pod_data[0]; }
86 const void* _M_access() const noexcept {
return &_M_pod_data[0]; }
88 template<
typename _Tp>
91 {
return *
static_cast<_Tp*
>(_M_access()); }
93 template<
typename _Tp>
95 _M_access() const noexcept
96 {
return *
static_cast<const _Tp*
>(_M_access()); }
98 _Nocopy_types _M_unused;
99 char _M_pod_data[
sizeof(_Nocopy_types)];
102 enum _Manager_operation
110 template<
typename _Signature>
117 static const size_t _M_max_size =
sizeof(_Nocopy_types);
118 static const size_t _M_max_align =
__alignof__(_Nocopy_types);
120 template<
typename _Functor>
124 static const bool __stored_locally =
126 &&
sizeof(_Functor) <= _M_max_size
134 _M_get_pointer(
const _Any_data&
__source)
noexcept
138 const _Functor& __f =
__source._M_access<_Functor>();
142 return __source._M_access<_Functor*>();
148 template<
typename _Fn>
156 template<
typename _Fn>
160 __dest._M_access<_Functor*>()
168 __victim._M_access<_Functor>().~_Functor();
175 delete __victim._M_access<_Functor*>();
181 _Manager_operation
__op)
185 case __get_type_info:
193 case __get_functor_ptr:
197 case __clone_functor:
199 *
const_cast<const _Functor*
>(_M_get_pointer(
__source)));
202 case __destroy_functor:
203 _M_destroy(
__dest, _Local_storage());
209 template<
typename _Fn>
211 _M_init_functor(_Any_data&
__functor, _Fn&& __f)
212 noexcept(__and_<_Local_storage,
218 template<
typename _Signature>
221 {
return static_cast<bool>(__f); }
223 template<
typename _Tp>
225 _M_not_empty_function(_Tp*
__fp)
noexcept
226 {
return __fp !=
nullptr; }
228 template<
typename _Class,
typename _Tp>
230 _M_not_empty_function(_Tp _Class::*
__mp)
noexcept
231 {
return __mp !=
nullptr; }
233 template<
typename _Tp>
235 _M_not_empty_function(
const _Tp&)
noexcept
244 _M_manager(_M_functor, _M_functor, __destroy_functor);
247 bool _M_empty()
const {
return !_M_manager; }
250 = bool (*)(_Any_data&,
const _Any_data&, _Manager_operation);
252 _Any_data _M_functor{};
256 template<
typename _Signature,
typename _Functor>
257 class _Function_handler;
259 template<
typename _Res,
typename _Functor,
typename...
_ArgTypes>
261 :
public _Function_base::_Base_manager<_Functor>
263 using _Base = _Function_base::_Base_manager<_Functor>;
268 _Manager_operation
__op)
273 case __get_type_info:
277 case __get_functor_ptr:
288 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
294 template<
typename _Fn>
295 static constexpr bool
296 _S_nothrow_init() noexcept
298 return __and_<
typename _Base::_Local_storage,
299 is_nothrow_constructible<_Functor, _Fn>>::value;
305 class _Function_handler<void, void>
309 _M_manager(_Any_data&,
const _Any_data&, _Manager_operation)
317 template<
typename _Signature,
typename _Functor,
318 bool __valid = is_object<_Functor>::value>
319 struct _Target_handler
320 : _Function_handler<_Signature, typename remove_cv<_Functor>::type>
323 template<
typename _Signature,
typename _Functor>
324 struct _Target_handler<_Signature, _Functor, false>
325 : _Function_handler<void, void>
333 template<
typename _Res,
typename... _ArgTypes>
335 :
public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
340 template<
typename _Func,
345 template<
typename _Func,
349 : __is_invocable_impl<_Res2, _Res>::type
352 template<
typename _Cond,
typename _Tp =
void>
355 template<
typename _Functor>
386 function(
const function& __x)
389 if (
static_cast<bool>(__x))
391 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
392 _M_invoker = __x._M_invoker;
393 _M_manager = __x._M_manager;
404 function(function&& __x) noexcept
407 if (
static_cast<bool>(__x))
409 _M_functor = __x._M_functor;
410 _M_manager = __x._M_manager;
411 __x._M_manager =
nullptr;
412 __x._M_invoker =
nullptr;
433 template<
typename _Functor,
435 function(_Functor&& __f)
440 "std::function target must be copy-constructible");
442 "std::function target must be constructible from the "
443 "constructor argument");
447 if (_My_handler::_M_not_empty_function(__f))
449 _My_handler::_M_init_functor(_M_functor,
451 _M_invoker = &_My_handler::_M_invoke;
452 _M_manager = &_My_handler::_M_manager;
469 operator=(
const function& __x)
471 function(__x).swap(*
this);
487 operator=(function&& __x)
noexcept
505 _M_manager(_M_functor, _M_functor, __destroy_functor);
506 _M_manager =
nullptr;
507 _M_invoker =
nullptr;
529 template<
typename _Functor>
531 operator=(_Functor&& __f)
539 template<
typename _Functor>
543 function(__f).swap(*
this);
556 void swap(function& __x)
noexcept
558 std::swap(_M_functor, __x._M_functor);
559 std::swap(_M_manager, __x._M_manager);
560 std::swap(_M_invoker, __x._M_invoker);
573 explicit operator bool() const noexcept
574 {
return !_M_empty(); }
590 __throw_bad_function_call();
606 target_type() const noexcept
630 template<
typename _Functor>
638 return *
const_cast<_Functor**
>(&
__func);
641 template<
typename _Functor>
643 target() const noexcept
651 if (_M_manager == &_Handler::_M_manager
653 || (_M_manager &&
typeid(_Functor) == target_type())
658 _M_manager(
__ptr, _M_functor, __get_functor_ptr);
659 return __ptr._M_access<
const _Functor*>();
667 using _Invoker_type =
_Res (*)(
const _Any_data&,
_ArgTypes&&...);
668 _Invoker_type _M_invoker =
nullptr;
671#if __cpp_deduction_guides >= 201606
673 struct __function_guide_helper
676 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
677 struct __function_guide_helper<
678 _Res (_Tp::*) (_Args...) noexcept(_Nx)
680 {
using type = _Res(_Args...); };
682 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
683 struct __function_guide_helper<
684 _Res (_Tp::*) (_Args...) & noexcept(_Nx)
686 {
using type = _Res(_Args...); };
688 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
689 struct __function_guide_helper<
690 _Res (_Tp::*) (_Args...) const noexcept(_Nx)
692 {
using type = _Res(_Args...); };
694 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
695 struct __function_guide_helper<
696 _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
698 {
using type = _Res(_Args...); };
700#if __cpp_static_call_operator >= 202207L && __cpp_concepts >= 202002L
701 template<
typename _StaticCallOp>
702 struct __function_guide_static_helper
705 template<
typename _Res,
bool _Nx,
typename... _Args>
706 struct __function_guide_static_helper<_Res (*) (_Args...) noexcept(_Nx)>
707 {
using type = _Res(_Args...); };
709 template<
typename _Fn,
typename _Op>
710 using __function_guide_t =
typename __conditional_t<
711 requires (_Fn& __f) { (void) __f.operator(); },
712 __function_guide_static_helper<_Op>,
713 __function_guide_helper<_Op>>::type;
715 template<
typename _Fn,
typename _Op>
716 using __function_guide_t =
typename __function_guide_helper<_Op>::type;
719 template<
typename _Res,
typename... _ArgTypes>
720 function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
722 template<
typename _Fn,
typename _Signature
723 = __function_guide_t<_Fn,
decltype(&_Fn::operator())>>
724 function(_Fn) -> function<_Signature>;
735 template<
typename _Res,
typename... _Args>
737 operator==(
const function<
_Res(
_Args...)>& __f, nullptr_t)
noexcept
738 {
return !
static_cast<bool>(__f); }
740#if __cpp_impl_three_way_comparison < 201907L
742 template<
typename _Res,
typename... _Args>
744 operator==(nullptr_t,
const function<_Res(_Args...)>& __f)
noexcept
745 {
return !
static_cast<bool>(__f); }
753 template<
typename _Res,
typename... _Args>
755 operator!=(
const function<_Res(_Args...)>& __f, nullptr_t)
noexcept
756 {
return static_cast<bool>(__f); }
759 template<
typename _Res,
typename... _Args>
761 operator!=(nullptr_t,
const function<_Res(_Args...)>& __f)
noexcept
762 {
return static_cast<bool>(__f); }
774 template<
typename _Res,
typename... _Args>
779#if __cplusplus >= 201703L
780 namespace __detail::__variant
782 template<
typename>
struct _Never_valueless_alt;
786 template<
typename _Signature>
787 struct _Never_valueless_alt<
std::function<_Signature>>
793_GLIBCXX_END_NAMESPACE_VERSION
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.