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
114 template<
typename _Tp>
115 struct _Simple_type_wrapper
117 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
122 template<
typename _Tp>
123 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
124 : __is_location_invariant<_Tp>
127 template<
typename _Signature>
134 static const size_t _M_max_size =
sizeof(_Nocopy_types);
135 static const size_t _M_max_align = __alignof__(_Nocopy_types);
137 template<
typename _Functor>
141 static const bool __stored_locally =
143 &&
sizeof(_Functor) <= _M_max_size
144 && __alignof__(_Functor) <= _M_max_align
145 && (_M_max_align % __alignof__(_Functor) == 0));
151 _M_get_pointer(
const _Any_data& __source)
153 if _GLIBCXX17_CONSTEXPR (__stored_locally)
155 const _Functor& __f = __source._M_access<_Functor>();
159 return __source._M_access<_Functor*>();
165 _M_clone(_Any_data& __dest,
const _Any_data& __source,
true_type)
167 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
173 _M_clone(_Any_data& __dest,
const _Any_data& __source,
false_type)
175 __dest._M_access<_Functor*>() =
176 new _Functor(*__source._M_access<
const _Functor*>());
182 _M_destroy(_Any_data& __victim,
true_type)
184 __victim._M_access<_Functor>().~_Functor();
191 delete __victim._M_access<_Functor*>();
196 _M_manager(_Any_data& __dest,
const _Any_data& __source,
197 _Manager_operation __op)
202 case __get_type_info:
203 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
206 case __get_functor_ptr:
207 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
210 case __clone_functor:
211 _M_clone(__dest, __source, _Local_storage());
214 case __destroy_functor:
215 _M_destroy(__dest, _Local_storage());
222 _M_init_functor(_Any_data& __functor, _Functor&& __f)
223 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
225 template<
typename _Signature>
227 _M_not_empty_function(
const function<_Signature>& __f)
228 {
return static_cast<bool>(__f); }
230 template<
typename _Tp>
232 _M_not_empty_function(_Tp* __fp)
233 {
return __fp !=
nullptr; }
235 template<
typename _Class,
typename _Tp>
237 _M_not_empty_function(_Tp _Class::* __mp)
238 {
return __mp !=
nullptr; }
240 template<
typename _Tp>
242 _M_not_empty_function(
const _Tp&)
247 _M_init_functor(_Any_data& __functor, _Functor&& __f,
true_type)
248 { ::new (__functor._M_access()) _Functor(std::move(__f)); }
251 _M_init_functor(_Any_data& __functor, _Functor&& __f,
false_type)
252 { __functor._M_access<_Functor*>() =
new _Functor(std::move(__f)); }
260 _M_manager(_M_functor, _M_functor, __destroy_functor);
263 bool _M_empty()
const {
return !_M_manager; }
265 typedef bool (*_Manager_type)(_Any_data&,
const _Any_data&,
268 _Any_data _M_functor;
269 _Manager_type _M_manager;
272 template<
typename _Signature,
typename _Functor>
273 class _Function_handler;
275 template<
typename _Res,
typename _Functor,
typename... _ArgTypes>
276 class _Function_handler<_Res(_ArgTypes...), _Functor>
277 :
public _Function_base::_Base_manager<_Functor>
279 typedef _Function_base::_Base_manager<_Functor> _Base;
283 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
285 return (*_Base::_M_get_pointer(__functor))(
286 std::forward<_ArgTypes>(__args)...);
290 template<
typename _Functor,
typename... _ArgTypes>
291 class _Function_handler<void(_ArgTypes...), _Functor>
292 :
public _Function_base::_Base_manager<_Functor>
294 typedef _Function_base::_Base_manager<_Functor> _Base;
298 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
300 (*_Base::_M_get_pointer(__functor))(
301 std::forward<_ArgTypes>(__args)...);
305 template<
typename _Class,
typename _Member,
typename _Res,
306 typename... _ArgTypes>
307 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
308 :
public _Function_handler<void(_ArgTypes...), _Member _Class::*>
310 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
315 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
317 return std::__invoke(_Base::_M_get_pointer(__functor)->__value,
318 std::forward<_ArgTypes>(__args)...);
322 template<
typename _Class,
typename _Member,
typename... _ArgTypes>
323 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
324 :
public _Function_base::_Base_manager<
325 _Simple_type_wrapper< _Member _Class::* > >
327 typedef _Member _Class::* _Functor;
328 typedef _Simple_type_wrapper<_Functor> _Wrapper;
329 typedef _Function_base::_Base_manager<_Wrapper> _Base;
333 _M_manager(_Any_data& __dest,
const _Any_data& __source,
334 _Manager_operation __op)
339 case __get_type_info:
340 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
343 case __get_functor_ptr:
344 __dest._M_access<_Functor*>() =
345 &_Base::_M_get_pointer(__source)->__value;
349 _Base::_M_manager(__dest, __source, __op);
355 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
358 std::forward<_ArgTypes>(__args)...);
368 template<
typename _Res,
typename... _ArgTypes>
369 class function<_Res(_ArgTypes...)>
373 template<
typename _Func,
374 typename _Res2 = __invoke_result<_Func&, _ArgTypes...>>
381 template<
typename _Tp>
382 struct _Callable<function, _Tp> :
false_type { };
384 template<
typename _Cond,
typename _Tp>
388 typedef _Res result_type;
403 function(nullptr_t) noexcept
414 function(
const function& __x);
444 template<
typename _Functor,
445 typename = _Requires<__not_<is_same<_Functor, function>>,
void>,
446 typename = _Requires<_Callable<_Functor>,
void>>
464 function(__x).swap(*
this);
482 function(std::move(__x)).swap(*
this);
498 _M_manager(_M_functor, _M_functor, __destroy_functor);
499 _M_manager =
nullptr;
500 _M_invoker =
nullptr;
521 template<
typename _Functor>
522 _Requires<_Callable<typename decay<_Functor>::type>,
function&>
525 function(std::forward<_Functor>(__f)).swap(*
this);
530 template<
typename _Functor>
534 function(__f).swap(*
this);
547 void swap(
function& __x) noexcept
549 std::swap(_M_functor, __x._M_functor);
550 std::swap(_M_manager, __x._M_manager);
551 std::swap(_M_invoker, __x._M_invoker);
564 explicit operator bool() const noexcept
565 {
return !_M_empty(); }
577 _Res operator()(_ArgTypes... __args)
const;
590 const type_info& target_type() const noexcept;
603 template<typename _Functor> _Functor* target() noexcept;
605 template<typename _Functor> const _Functor* target() const noexcept;
610 using _Invoker_type = _Res (*)(
const _Any_data&, _ArgTypes&&...);
611 _Invoker_type _M_invoker;
614 #if __cpp_deduction_guides >= 201606 616 struct __function_guide_helper
619 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
620 struct __function_guide_helper<
621 _Res (_Tp::*) (_Args...) noexcept(_Nx)
623 {
using type = _Res(_Args...); };
625 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
626 struct __function_guide_helper<
627 _Res (_Tp::*) (_Args...) & noexcept(_Nx)
629 {
using type = _Res(_Args...); };
631 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
632 struct __function_guide_helper<
633 _Res (_Tp::*) (_Args...) const noexcept(_Nx)
635 {
using type = _Res(_Args...); };
637 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
638 struct __function_guide_helper<
639 _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
641 {
using type = _Res(_Args...); };
643 template<
typename _Res,
typename... _ArgTypes>
644 function(_Res(*)(_ArgTypes...)) ->
function<_Res(_ArgTypes...)>;
646 template<
typename _Functor,
typename _Signature =
typename 647 __function_guide_helper<decltype(&_Functor::operator())>::type>
648 function(_Functor) -> function<_Signature>;
652 template<
typename _Res,
typename... _ArgTypes>
653 function<_Res(_ArgTypes...)>::
654 function(
const function& __x)
657 if (static_cast<bool>(__x))
659 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
660 _M_invoker = __x._M_invoker;
661 _M_manager = __x._M_manager;
665 template<
typename _Res,
typename... _ArgTypes>
666 template<
typename _Functor,
typename,
typename>
667 function<_Res(_ArgTypes...)>::
668 function(_Functor __f)
671 typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
673 if (_My_handler::_M_not_empty_function(__f))
675 _My_handler::_M_init_functor(_M_functor, std::move(__f));
676 _M_invoker = &_My_handler::_M_invoke;
677 _M_manager = &_My_handler::_M_manager;
681 template<
typename _Res,
typename... _ArgTypes>
683 function<_Res(_ArgTypes...)>::
684 operator()(_ArgTypes... __args)
const 687 __throw_bad_function_call();
688 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
692 template<
typename _Res,
typename... _ArgTypes>
694 function<_Res(_ArgTypes...)>::
695 target_type() const noexcept
699 _Any_data __typeinfo_result;
700 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
701 return *__typeinfo_result._M_access<
const type_info*>();
707 template<
typename _Res,
typename... _ArgTypes>
708 template<
typename _Functor>
710 function<_Res(_ArgTypes...)>::
713 const function* __const_this =
this;
714 const _Functor* __func = __const_this->template target<_Functor>();
715 return const_cast<_Functor*
>(__func);
718 template<
typename _Res,
typename... _ArgTypes>
719 template<
typename _Functor>
721 function<_Res(_ArgTypes...)>::
722 target() const noexcept
724 if (
typeid(_Functor) == target_type() && _M_manager)
727 _M_manager(__ptr, _M_functor, __get_functor_ptr);
728 return __ptr._M_access<
const _Functor*>();
744 template<
typename _Res,
typename... _Args>
746 operator==(
const function<_Res(_Args...)>& __f, nullptr_t) noexcept
747 {
return !
static_cast<bool>(__f); }
750 template<
typename _Res,
typename... _Args>
752 operator==(nullptr_t,
const function<_Res(_Args...)>& __f) noexcept
753 {
return !
static_cast<bool>(__f); }
762 template<
typename _Res,
typename... _Args>
764 operator!=(
const function<_Res(_Args...)>& __f, nullptr_t) noexcept
765 {
return static_cast<bool>(__f); }
768 template<
typename _Res,
typename... _Args>
770 operator!=(nullptr_t,
const function<_Res(_Args...)>& __f) noexcept
771 {
return static_cast<bool>(__f); }
783 template<
typename _Res,
typename... _Args>
785 swap(
function<_Res(_Args...)>& __x,
function<_Res(_Args...)>& __y) noexcept
788 #if __cplusplus >= 201703L 789 namespace __detail::__variant
791 template<
typename>
struct _Never_valueless_alt;
795 template<
typename _Signature>
796 struct _Never_valueless_alt<
std::function<_Signature>>
802 _GLIBCXX_END_NAMESPACE_VERSION
806 #endif // _GLIBCXX_STD_FUNCTION_H
void swap(function &__x) noexcept
Swap the targets of two function objects.
ISO C++ entities toplevel namespace is std.
Base class for all library exceptions.
function & operator=(reference_wrapper< _Functor > __f) noexcept
Primary class template for reference_wrapper.
function & operator=(nullptr_t) noexcept
Function assignment to zero.
function & operator=(const function &__x)
Function assignment operator.
const char * what() const noexcept
function & operator=(function &&__x) noexcept
Function move-assignment operator.
Exception class thrown when class template function's operator() is called with an empty target...
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
_Requires< _Callable< typename decay< _Functor >::type >, function & > operator=(_Functor &&__f)
Function assignment to a new target.
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Base class of all polymorphic function object wrappers.
Define a member typedef type only if a boolean constant is true.