30 #ifndef _GLIBCXX_STD_FUNCTION_H
31 #define _GLIBCXX_STD_FUNCTION_H 1
33 #pragma GCC system_header
35 #if __cplusplus < 201103L
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 const char*
what() const noexcept;
68 template<typename _Tp>
70 : is_trivially_copyable<_Tp>::type
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 _M_clone(_Any_data& __dest,
const _Any_data& __source,
true_type)
150 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
156 _M_clone(_Any_data& __dest,
const _Any_data& __source,
false_type)
158 __dest._M_access<_Functor*>() =
159 new _Functor(*__source._M_access<
const _Functor*>());
165 _M_destroy(_Any_data& __victim,
true_type)
167 __victim._M_access<_Functor>().~_Functor();
174 delete __victim._M_access<_Functor*>();
179 _M_manager(_Any_data& __dest,
const _Any_data& __source,
180 _Manager_operation __op)
184 case __get_type_info:
186 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
188 __dest._M_access<
const type_info*>() =
nullptr;
191 case __get_functor_ptr:
192 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
195 case __clone_functor:
196 _M_clone(__dest, __source, _Local_storage());
199 case __destroy_functor:
200 _M_destroy(__dest, _Local_storage());
207 _M_init_functor(_Any_data& __functor, _Functor&& __f)
208 { _M_init_functor(__functor,
std::move(__f), _Local_storage()); }
210 template<
typename _Signature>
212 _M_not_empty_function(
const function<_Signature>& __f)
213 {
return static_cast<bool>(__f); }
215 template<
typename _Tp>
217 _M_not_empty_function(_Tp* __fp)
218 {
return __fp !=
nullptr; }
220 template<
typename _Class,
typename _Tp>
222 _M_not_empty_function(_Tp _Class::* __mp)
223 {
return __mp !=
nullptr; }
225 template<
typename _Tp>
227 _M_not_empty_function(
const _Tp&)
232 _M_init_functor(_Any_data& __functor, _Functor&& __f,
true_type)
233 { ::new (__functor._M_access()) _Functor(
std::move(__f)); }
236 _M_init_functor(_Any_data& __functor, _Functor&& __f,
false_type)
237 { __functor._M_access<_Functor*>() =
new _Functor(
std::move(__f)); }
245 _M_manager(_M_functor, _M_functor, __destroy_functor);
248 bool _M_empty()
const {
return !_M_manager; }
250 typedef bool (*_Manager_type)(_Any_data&,
const _Any_data&,
253 _Any_data _M_functor;
254 _Manager_type _M_manager;
257 template<
typename _Signature,
typename _Functor>
258 class _Function_handler;
260 template<
typename _Res,
typename _Functor,
typename... _ArgTypes>
261 class _Function_handler<_Res(_ArgTypes...), _Functor>
262 :
public _Function_base::_Base_manager<_Functor>
264 typedef _Function_base::_Base_manager<_Functor> _Base;
268 _M_manager(_Any_data& __dest,
const _Any_data& __source,
269 _Manager_operation __op)
274 case __get_type_info:
275 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
278 case __get_functor_ptr:
279 __dest._M_access<_Functor*>() = _Base::_M_get_pointer(__source);
283 _Base::_M_manager(__dest, __source, __op);
289 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
291 return std::__invoke_r<_Res>(*_Base::_M_get_pointer(__functor),
292 std::forward<_ArgTypes>(__args)...);
298 class _Function_handler<void, void>
302 _M_manager(_Any_data&,
const _Any_data&, _Manager_operation)
310 template<
typename _Signature,
typename _Functor,
311 bool __valid = is_object<_Functor>::value>
312 struct _Target_handler
313 : _Function_handler<_Signature, typename remove_cv<_Functor>::type>
316 template<
typename _Signature,
typename _Functor>
317 struct _Target_handler<_Signature, _Functor, false>
318 : _Function_handler<void, void>
327 template<
typename _Res,
typename... _ArgTypes>
328 class function<_Res(_ArgTypes...)>
329 :
public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
332 template<
typename _Func,
333 typename _Res2 = __invoke_result<_Func&, _ArgTypes...>>
340 template<
typename _Tp>
341 struct _Callable<function, _Tp> :
false_type { };
343 template<
typename _Cond,
typename _Tp>
347 typedef _Res result_type;
362 function(nullptr_t) noexcept
373 function(
const function& __x)
376 if (
static_cast<bool>(__x))
378 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
379 _M_invoker = __x._M_invoker;
380 _M_manager = __x._M_manager;
391 function(
function&& __x) noexcept
411 template<
typename _Functor,
412 typename = _Requires<__not_<is_same<_Functor, function>>,
void>,
413 typename = _Requires<_Callable<_Functor>,
void>>
414 function(_Functor __f)
417 typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
419 if (_My_handler::_M_not_empty_function(__f))
421 _My_handler::_M_init_functor(_M_functor,
std::move(__f));
422 _M_invoker = &_My_handler::_M_invoke;
423 _M_manager = &_My_handler::_M_manager;
442 function(__x).
swap(*
this);
476 _M_manager(_M_functor, _M_functor, __destroy_functor);
477 _M_manager =
nullptr;
478 _M_invoker =
nullptr;
499 template<
typename _Functor>
500 _Requires<_Callable<typename decay<_Functor>::type>,
function&>
503 function(std::forward<_Functor>(__f)).
swap(*
this);
508 template<
typename _Functor>
512 function(__f).
swap(*
this);
525 void swap(
function& __x) noexcept
542 explicit operator bool() const noexcept
543 {
return !_M_empty(); }
559 __throw_bad_function_call();
560 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
579 _Any_data __typeinfo_result;
580 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
581 if (
auto __ti = __typeinfo_result._M_access<
const type_info*>())
599 template<
typename _Functor>
603 const function* __const_this =
this;
604 const _Functor* __func = __const_this->template target<_Functor>();
607 return *
const_cast<_Functor**
>(&__func);
610 template<
typename _Functor>
618 using _Handler = _Target_handler<_Res(_ArgTypes...), _Functor>;
620 if (_M_manager == &_Handler::_M_manager
622 || (_M_manager &&
typeid(_Functor) == target_type())
627 _M_manager(__ptr, _M_functor, __get_functor_ptr);
628 return __ptr._M_access<
const _Functor*>();
636 using _Invoker_type = _Res (*)(
const _Any_data&, _ArgTypes&&...);
637 _Invoker_type _M_invoker;
640 #if __cpp_deduction_guides >= 201606
642 struct __function_guide_helper
645 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
646 struct __function_guide_helper<
647 _Res (_Tp::*) (_Args...) noexcept(_Nx)
649 {
using type = _Res(_Args...); };
651 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
652 struct __function_guide_helper<
653 _Res (_Tp::*) (_Args...) & noexcept(_Nx)
655 {
using type = _Res(_Args...); };
657 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
658 struct __function_guide_helper<
659 _Res (_Tp::*) (_Args...) const noexcept(_Nx)
661 {
using type = _Res(_Args...); };
663 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
664 struct __function_guide_helper<
665 _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
667 {
using type = _Res(_Args...); };
669 template<
typename _Res,
typename... _ArgTypes>
670 function(_Res(*)(_ArgTypes...)) ->
function<_Res(_ArgTypes...)>;
672 template<
typename _Functor,
typename _Signature =
typename
673 __function_guide_helper<decltype(&_Functor::operator())>::type>
674 function(_Functor) -> function<_Signature>;
686 template<
typename _Res,
typename... _Args>
688 operator==(
const function<_Res(_Args...)>& __f, nullptr_t) noexcept
689 {
return !
static_cast<bool>(__f); }
691 #if __cpp_impl_three_way_comparison < 201907L
693 template<
typename _Res,
typename... _Args>
695 operator==(nullptr_t,
const function<_Res(_Args...)>& __f) noexcept
696 {
return !
static_cast<bool>(__f); }
705 template<
typename _Res,
typename... _Args>
707 operator!=(
const function<_Res(_Args...)>& __f, nullptr_t) noexcept
708 {
return static_cast<bool>(__f); }
711 template<
typename _Res,
typename... _Args>
713 operator!=(nullptr_t,
const function<_Res(_Args...)>& __f) noexcept
714 {
return static_cast<bool>(__f); }
726 template<
typename _Res,
typename... _Args>
728 swap(
function<_Res(_Args...)>& __x,
function<_Res(_Args...)>& __y) noexcept
731 #if __cplusplus >= 201703L
732 namespace __detail::__variant
734 template<
typename>
struct _Never_valueless_alt;
738 template<
typename _Signature>
739 struct _Never_valueless_alt<
std::function<_Signature>>
745 _GLIBCXX_END_NAMESPACE_VERSION
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.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
ISO C++ entities toplevel namespace is std.
Primary class template for reference_wrapper.
Define a member typedef type only if a boolean constant is true.
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 zero.
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.
const _Functor * target() const noexcept
Access the stored target function object.
_Functor * target() noexcept
Access the stored target function object.
_Requires< _Callable< typename decay< _Functor >::type >, function & > operator=(_Functor &&__f)
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...