30 #ifndef _GLIBCXX_STD_FUNCTION_H
31 #define _GLIBCXX_STD_FUNCTION_H 1
33 #ifdef _GLIBCXX_SYSHDR
34 #pragma GCC system_header
37 #if __cplusplus < 201103L
47 namespace 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 if _GLIBCXX17_CONSTEXPR (__stored_locally)
140 const _Functor& __f = __source._M_access<_Functor>();
144 return __source._M_access<_Functor*>();
150 template<
typename _Fn>
152 _M_create(_Any_data& __dest, _Fn&& __f,
true_type)
154 ::new (__dest._M_access()) _Functor(std::forward<_Fn>(__f));
158 template<
typename _Fn>
160 _M_create(_Any_data& __dest, _Fn&& __f,
false_type)
162 __dest._M_access<_Functor*>()
163 =
new _Functor(std::forward<_Fn>(__f));
168 _M_destroy(_Any_data& __victim,
true_type)
170 __victim._M_access<_Functor>().~_Functor();
177 delete __victim._M_access<_Functor*>();
182 _M_manager(_Any_data& __dest,
const _Any_data& __source,
183 _Manager_operation __op)
187 case __get_type_info:
189 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
191 __dest._M_access<
const type_info*>() =
nullptr;
195 case __get_functor_ptr:
196 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
199 case __clone_functor:
200 _M_init_functor(__dest,
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,
217 _M_create(__functor, std::forward<_Fn>(__f), _Local_storage());
220 template<
typename _Signature>
222 _M_not_empty_function(
const function<_Signature>& __f) noexcept
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{};
255 _Manager_type _M_manager{};
258 template<
typename _Signature,
typename _Functor>
259 class _Function_handler;
261 template<
typename _Res,
typename _Functor,
typename... _ArgTypes>
262 class _Function_handler<_Res(_ArgTypes...), _Functor>
263 :
public _Function_base::_Base_manager<_Functor>
265 using _Base = _Function_base::_Base_manager<_Functor>;
269 _M_manager(_Any_data& __dest,
const _Any_data& __source,
270 _Manager_operation __op)
275 case __get_type_info:
276 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
279 case __get_functor_ptr:
280 __dest._M_access<_Functor*>() = _Base::_M_get_pointer(__source);
284 _Base::_M_manager(__dest, __source, __op);
290 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
292 return std::__invoke_r<_Res>(*_Base::_M_get_pointer(__functor),
293 std::forward<_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>
336 class function<_Res(_ArgTypes...)>
337 :
public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
342 template<
typename _Func,
345 =
typename __enable_if_t<!_Self, decay<_Func>>::type;
347 template<
typename _Func,
348 typename _DFunc = _Decay_t<_Func>,
349 typename _Res2 = __invoke_result<_DFunc&, _ArgTypes...>>
351 : __is_invocable_impl<_Res2, _Res>::type
354 template<
typename _Cond,
typename _Tp =
void>
355 using _Requires = __enable_if_t<_Cond::value, _Tp>;
357 template<
typename _Functor>
359 = _Function_handler<_Res(_ArgTypes...), __decay_t<_Functor>>;
362 typedef _Res result_type;
377 function(nullptr_t) noexcept
388 function(
const function& __x)
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;
406 function(
function&& __x) noexcept
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,
436 typename _Constraints = _Requires<_Callable<_Functor>>>
437 function(_Functor&& __f)
438 noexcept(_Handler<_Functor>::template _S_nothrow_init<_Functor>())
442 "std::function target must be copy-constructible");
444 "std::function target must be constructible from the "
445 "constructor argument");
447 using _My_handler = _Handler<_Functor>;
449 if (_My_handler::_M_not_empty_function(__f))
451 _My_handler::_M_init_functor(_M_functor,
452 std::forward<_Functor>(__f));
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>
532 _Requires<_Callable<_Functor>,
function&>
534 noexcept(_Handler<_Functor>::template _S_nothrow_init<_Functor>())
536 function(std::forward<_Functor>(__f)).swap(*
this);
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();
593 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
612 _Any_data __typeinfo_result;
613 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
614 if (
auto __ti = __typeinfo_result._M_access<
const type_info*>())
632 template<
typename _Functor>
636 const function* __const_this =
this;
637 const _Functor* __func = __const_this->template target<_Functor>();
640 return *
const_cast<_Functor**
>(&__func);
643 template<
typename _Functor>
651 using _Handler = _Target_handler<_Res(_ArgTypes...), _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>
786 swap(
function<_Res(_Args...)>& __x,
function<_Res(_Args...)>& __y) noexcept
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 _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
ISO C++ entities toplevel namespace is std.
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y) noexcept
Swap the targets of two polymorphic function object wrappers.
bool operator==(const function< _Res(_Args...)> &__f, nullptr_t) noexcept
Test whether a polymorphic function object wrapper is empty.
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
Base class of all polymorphic function object wrappers.
function & operator=(function &&__x) noexcept
Function move-assignment operator.
function & operator=(nullptr_t) noexcept
Function assignment to empty.
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.
_Res operator()(_ArgTypes... __args) const
Invokes the function targeted by *this.
void swap(function &__x) noexcept
Swap the targets of two function objects.
_Requires< _Callable< _Functor >, function & > operator=(_Functor &&__f) noexcept(_Handler< _Functor >::template _S_nothrow_init< _Functor >())
Function assignment to a new target.
const _Functor * target() const noexcept
Access the stored target function object.
_Functor * target() noexcept
Access the stored target function object.
function & operator=(reference_wrapper< _Functor > __f) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...