30 #ifndef _GLIBCXX_STD_FUNCTION_H
31 #define _GLIBCXX_STD_FUNCTION_H 1
33 #pragma GCC system_header
35 #if __cplusplus < 201103L
47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61 const char*
what() const noexcept;
70 template<typename _Tp>
72 : is_trivially_copyable<_Tp>::type
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() {
return &_M_pod_data[0]; }
88 const void* _M_access()
const {
return &_M_pod_data[0]; }
90 template<
typename _Tp>
93 {
return *
static_cast<_Tp*
>(_M_access()); }
95 template<
typename _Tp>
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)
138 if _GLIBCXX17_CONSTEXPR (__stored_locally)
140 const _Functor& __f = __source._M_access<_Functor>();
144 return __source._M_access<_Functor*>();
150 _M_clone(_Any_data& __dest,
const _Any_data& __source,
true_type)
152 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
158 _M_clone(_Any_data& __dest,
const _Any_data& __source,
false_type)
160 __dest._M_access<_Functor*>() =
161 new _Functor(*__source._M_access<
const _Functor*>());
167 _M_destroy(_Any_data& __victim,
true_type)
169 __victim._M_access<_Functor>().~_Functor();
176 delete __victim._M_access<_Functor*>();
181 _M_manager(_Any_data& __dest,
const _Any_data& __source,
182 _Manager_operation __op)
187 case __get_type_info:
188 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
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)...);
302 template<
typename _Res,
typename... _ArgTypes>
303 class function<_Res(_ArgTypes...)>
304 :
public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
307 template<
typename _Func,
308 typename _Res2 = __invoke_result<_Func&, _ArgTypes...>>
315 template<
typename _Tp>
316 struct _Callable<function, _Tp> :
false_type { };
318 template<
typename _Cond,
typename _Tp>
322 typedef _Res result_type;
337 function(nullptr_t) noexcept
348 function(
const function& __x);
378 template<
typename _Functor,
379 typename = _Requires<__not_<is_same<_Functor, function>>,
void>,
380 typename = _Requires<_Callable<_Functor>,
void>>
398 function(__x).swap(*
this);
432 _M_manager(_M_functor, _M_functor, __destroy_functor);
433 _M_manager =
nullptr;
434 _M_invoker =
nullptr;
455 template<
typename _Functor>
456 _Requires<_Callable<typename decay<_Functor>::type>,
function&>
459 function(std::forward<_Functor>(__f)).swap(*
this);
464 template<
typename _Functor>
468 function(__f).swap(*
this);
481 void swap(
function& __x) noexcept
483 std::swap(_M_functor, __x._M_functor);
484 std::swap(_M_manager, __x._M_manager);
485 std::swap(_M_invoker, __x._M_invoker);
498 explicit operator bool() const noexcept
499 {
return !_M_empty(); }
511 _Res operator()(_ArgTypes... __args)
const;
524 const type_info& target_type() const noexcept;
537 template<typename _Functor> _Functor* target() noexcept;
539 template<typename _Functor> const _Functor* target() const noexcept;
544 using _Invoker_type = _Res (*)(
const _Any_data&, _ArgTypes&&...);
545 _Invoker_type _M_invoker;
548 #if __cpp_deduction_guides >= 201606
550 struct __function_guide_helper
553 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
554 struct __function_guide_helper<
555 _Res (_Tp::*) (_Args...) noexcept(_Nx)
557 {
using type = _Res(_Args...); };
559 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
560 struct __function_guide_helper<
561 _Res (_Tp::*) (_Args...) & noexcept(_Nx)
563 {
using type = _Res(_Args...); };
565 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
566 struct __function_guide_helper<
567 _Res (_Tp::*) (_Args...) const noexcept(_Nx)
569 {
using type = _Res(_Args...); };
571 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
572 struct __function_guide_helper<
573 _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
575 {
using type = _Res(_Args...); };
577 template<
typename _Res,
typename... _ArgTypes>
578 function(_Res(*)(_ArgTypes...)) ->
function<_Res(_ArgTypes...)>;
580 template<
typename _Functor,
typename _Signature =
typename
581 __function_guide_helper<decltype(&_Functor::operator())>::type>
582 function(_Functor) -> function<_Signature>;
586 template<
typename _Res,
typename... _ArgTypes>
587 function<_Res(_ArgTypes...)>::
588 function(
const function& __x)
591 if (
static_cast<bool>(__x))
593 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
594 _M_invoker = __x._M_invoker;
595 _M_manager = __x._M_manager;
599 template<
typename _Res,
typename... _ArgTypes>
600 template<
typename _Functor,
typename,
typename>
601 function<_Res(_ArgTypes...)>::
602 function(_Functor __f)
605 typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
607 if (_My_handler::_M_not_empty_function(__f))
609 _My_handler::_M_init_functor(_M_functor,
std::move(__f));
610 _M_invoker = &_My_handler::_M_invoke;
611 _M_manager = &_My_handler::_M_manager;
615 template<
typename _Res,
typename... _ArgTypes>
617 function<_Res(_ArgTypes...)>::
618 operator()(_ArgTypes... __args)
const
621 __throw_bad_function_call();
622 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
626 template<
typename _Res,
typename... _ArgTypes>
628 function<_Res(_ArgTypes...)>::
629 target_type() const noexcept
633 _Any_data __typeinfo_result;
634 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
635 return *__typeinfo_result._M_access<
const type_info*>();
641 template<
typename _Res,
typename... _ArgTypes>
642 template<
typename _Functor>
644 function<_Res(_ArgTypes...)>::
647 const function* __const_this =
this;
648 const _Functor* __func = __const_this->template target<_Functor>();
649 return const_cast<_Functor*
>(__func);
652 template<
typename _Res,
typename... _ArgTypes>
653 template<
typename _Functor>
655 function<_Res(_ArgTypes...)>::
656 target() const noexcept
658 if (
typeid(_Functor) == target_type() && _M_manager)
661 _M_manager(__ptr, _M_functor, __get_functor_ptr);
662 return __ptr._M_access<
const _Functor*>();
678 template<
typename _Res,
typename... _Args>
680 operator==(
const function<_Res(_Args...)>& __f, nullptr_t) noexcept
681 {
return !
static_cast<bool>(__f); }
683 #if __cpp_impl_three_way_comparison < 201907L
685 template<
typename _Res,
typename... _Args>
687 operator==(nullptr_t,
const function<_Res(_Args...)>& __f) noexcept
688 {
return !
static_cast<bool>(__f); }
697 template<
typename _Res,
typename... _Args>
699 operator!=(
const function<_Res(_Args...)>& __f, nullptr_t) noexcept
700 {
return static_cast<bool>(__f); }
703 template<
typename _Res,
typename... _Args>
705 operator!=(nullptr_t,
const function<_Res(_Args...)>& __f) noexcept
706 {
return static_cast<bool>(__f); }
718 template<
typename _Res,
typename... _Args>
720 swap(
function<_Res(_Args...)>& __x,
function<_Res(_Args...)>& __y) noexcept
723 #if __cplusplus >= 201703L
724 namespace __detail::__variant
726 template<
typename>
struct _Never_valueless_alt;
730 template<
typename _Signature>
731 struct _Never_valueless_alt<
std::function<_Signature>>
737 _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.
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.
void swap(function &__x) noexcept
Swap the targets of two function objects.
_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...