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;
68 template<typename _Tp>
73 class _Undefined_class;
78 const void* _M_const_object;
79 void (*_M_function_pointer)();
80 void (_Undefined_class::*_M_member_pointer)();
83 union [[gnu::may_alias]] _Any_data
85 void* _M_access() {
return &_M_pod_data[0]; }
86 const void* _M_access()
const {
return &_M_pod_data[0]; }
88 template<
typename _Tp>
91 {
return *
static_cast<_Tp*
>(_M_access()); }
93 template<
typename _Tp>
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
127 && __alignof__(_Functor) <= _M_max_align
128 && (_M_max_align % __alignof__(_Functor) == 0));
134 _M_get_pointer(
const _Any_data& __source)
136 if _GLIBCXX17_CONSTEXPR (__stored_locally)
138 const _Functor& __f = __source._M_access<_Functor>();
142 return __source._M_access<_Functor*>();
148 template<
typename _Fn>
150 _M_create(_Any_data& __dest, _Fn&& __f,
true_type)
152 ::new (__dest._M_access()) _Functor(std::forward<_Fn>(__f));
156 template<
typename _Fn>
158 _M_create(_Any_data& __dest, _Fn&& __f,
false_type)
160 __dest._M_access<_Functor*>()
161 =
new _Functor(std::forward<_Fn>(__f));
166 _M_destroy(_Any_data& __victim,
true_type)
168 __victim._M_access<_Functor>().~_Functor();
175 delete __victim._M_access<_Functor*>();
180 _M_manager(_Any_data& __dest,
const _Any_data& __source,
181 _Manager_operation __op)
185 case __get_type_info:
187 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
189 __dest._M_access<
const type_info*>() =
nullptr;
193 case __get_functor_ptr:
194 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
197 case __clone_functor:
198 _M_init_functor(__dest,
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,
215 _M_create(__functor, std::forward<_Fn>(__f), _Local_storage());
218 template<
typename _Signature>
220 _M_not_empty_function(
const function<_Signature>& __f)
221 {
return static_cast<bool>(__f); }
223 template<
typename _Tp>
225 _M_not_empty_function(_Tp* __fp)
226 {
return __fp !=
nullptr; }
228 template<
typename _Class,
typename _Tp>
230 _M_not_empty_function(_Tp _Class::* __mp)
231 {
return __mp !=
nullptr; }
233 template<
typename _Tp>
235 _M_not_empty_function(
const _Tp&)
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{};
253 _Manager_type _M_manager{};
256 template<
typename _Signature,
typename _Functor>
257 class _Function_handler;
259 template<
typename _Res,
typename _Functor,
typename... _ArgTypes>
260 class _Function_handler<_Res(_ArgTypes...), _Functor>
261 :
public _Function_base::_Base_manager<_Functor>
263 using _Base = _Function_base::_Base_manager<_Functor>;
267 _M_manager(_Any_data& __dest,
const _Any_data& __source,
268 _Manager_operation __op)
273 case __get_type_info:
274 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
277 case __get_functor_ptr:
278 __dest._M_access<_Functor*>() = _Base::_M_get_pointer(__source);
282 _Base::_M_manager(__dest, __source, __op);
288 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
290 return std::__invoke_r<_Res>(*_Base::_M_get_pointer(__functor),
291 std::forward<_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>
334 class function<_Res(_ArgTypes...)>
335 :
public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
340 template<
typename _Func,
343 =
typename __enable_if_t<!_Self, decay<_Func>>::type;
345 template<
typename _Func,
346 typename _DFunc = _Decay_t<_Func>,
347 typename _Res2 = __invoke_result<_DFunc&, _ArgTypes...>>
349 : __is_invocable_impl<_Res2, _Res>::type
352 template<
typename _Cond,
typename _Tp =
void>
353 using _Requires = __enable_if_t<_Cond::value, _Tp>;
355 template<
typename _Functor>
357 = _Function_handler<_Res(_ArgTypes...), __decay_t<_Functor>>;
360 typedef _Res result_type;
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;
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,
434 typename _Constraints = _Requires<_Callable<_Functor>>>
436 noexcept(_Handler<_Functor>::template _S_nothrow_init<_Functor>())
440 "std::function target must be copy-constructible");
442 "std::function target must be constructible from the "
443 "constructor argument");
445 using _My_handler = _Handler<_Functor>;
447 if (_My_handler::_M_not_empty_function(__f))
449 _My_handler::_M_init_functor(_M_functor,
450 std::forward<_Functor>(__f));
451 _M_invoker = &_My_handler::_M_invoke;
452 _M_manager = &_My_handler::_M_manager;
471 function(__x).swap(*
this);
505 _M_manager(_M_functor, _M_functor, __destroy_functor);
506 _M_manager =
nullptr;
507 _M_invoker =
nullptr;
528 template<
typename _Functor>
529 _Requires<_Callable<_Functor>, function&>
531 noexcept(_Handler<_Functor>::template _S_nothrow_init<_Functor>())
533 function(std::forward<_Functor>(__f)).swap(*
this);
538 template<
typename _Functor>
542 function(__f).swap(*
this);
555 void swap(function& __x)
noexcept
572 explicit operator bool() const noexcept
573 {
return !_M_empty(); }
589 __throw_bad_function_call();
590 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
609 _Any_data __typeinfo_result;
610 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
611 if (
auto __ti = __typeinfo_result._M_access<
const type_info*>())
629 template<
typename _Functor>
633 const function* __const_this =
this;
634 const _Functor* __func = __const_this->template target<_Functor>();
637 return *
const_cast<_Functor**
>(&__func);
640 template<
typename _Functor>
648 using _Handler = _Target_handler<_Res(_ArgTypes...), _Functor>;
650 if (_M_manager == &_Handler::_M_manager
652 || (_M_manager &&
typeid(_Functor) == target_type())
657 _M_manager(__ptr, _M_functor, __get_functor_ptr);
658 return __ptr._M_access<
const _Functor*>();
666 using _Invoker_type = _Res (*)(
const _Any_data&, _ArgTypes&&...);
667 _Invoker_type _M_invoker =
nullptr;
670#if __cpp_deduction_guides >= 201606
672 struct __function_guide_helper
675 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
676 struct __function_guide_helper<
677 _Res (_Tp::*) (_Args...) noexcept(_Nx)
679 {
using type = _Res(_Args...); };
681 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
682 struct __function_guide_helper<
683 _Res (_Tp::*) (_Args...) & noexcept(_Nx)
685 {
using type = _Res(_Args...); };
687 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
688 struct __function_guide_helper<
689 _Res (_Tp::*) (_Args...) const noexcept(_Nx)
691 {
using type = _Res(_Args...); };
693 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
694 struct __function_guide_helper<
695 _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
697 {
using type = _Res(_Args...); };
699 template<
typename _Res,
typename... _ArgTypes>
700 function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
702 template<
typename _Functor,
typename _Signature =
typename
703 __function_guide_helper<
decltype(&_Functor::operator())>::type>
704 function(_Functor) -> function<_Signature>;
716 template<
typename _Res,
typename... _Args>
718 operator==(
const function<_Res(_Args...)>& __f, nullptr_t)
noexcept
719 {
return !
static_cast<bool>(__f); }
721#if __cpp_impl_three_way_comparison < 201907L
723 template<
typename _Res,
typename... _Args>
725 operator==(nullptr_t,
const function<_Res(_Args...)>& __f)
noexcept
726 {
return !
static_cast<bool>(__f); }
735 template<
typename _Res,
typename... _Args>
737 operator!=(
const function<_Res(_Args...)>& __f, nullptr_t)
noexcept
738 {
return static_cast<bool>(__f); }
741 template<
typename _Res,
typename... _Args>
743 operator!=(nullptr_t,
const function<_Res(_Args...)>& __f)
noexcept
744 {
return static_cast<bool>(__f); }
756 template<
typename _Res,
typename... _Args>
758 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
noexcept
761#if __cplusplus >= 201703L
762 namespace __detail::__variant
764 template<
typename>
struct _Never_valueless_alt;
768 template<
typename _Signature>
769 struct _Never_valueless_alt<
std::function<_Signature>>
775_GLIBCXX_END_NAMESPACE_VERSION
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
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
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 zero.
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.