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 std::size_t _M_max_size =
sizeof(_Nocopy_types);
135 static const std::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 const _Functor* __ptr =
155 : __source._M_access<_Functor*>();
156 return const_cast<_Functor*>(__ptr);
162 _M_clone(_Any_data& __dest,
const _Any_data& __source,
true_type)
164 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
170 _M_clone(_Any_data& __dest,
const _Any_data& __source,
false_type)
172 __dest._M_access<_Functor*>() =
173 new _Functor(*__source._M_access<_Functor*>());
179 _M_destroy(_Any_data& __victim,
true_type)
181 __victim._M_access<_Functor>().~_Functor();
188 delete __victim._M_access<_Functor*>();
193 _M_manager(_Any_data& __dest,
const _Any_data& __source,
194 _Manager_operation __op)
199 case __get_type_info:
200 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
203 case __get_functor_ptr:
204 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
207 case __clone_functor:
208 _M_clone(__dest, __source, _Local_storage());
211 case __destroy_functor:
212 _M_destroy(__dest, _Local_storage());
219 _M_init_functor(_Any_data& __functor, _Functor&& __f)
220 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
222 template<
typename _Signature>
224 _M_not_empty_function(
const function<_Signature>& __f)
225 {
return static_cast<bool>(__f); }
227 template<
typename _Tp>
229 _M_not_empty_function(_Tp* __fp)
230 {
return __fp !=
nullptr; }
232 template<
typename _Class,
typename _Tp>
234 _M_not_empty_function(_Tp _Class::* __mp)
235 {
return __mp !=
nullptr; }
237 template<
typename _Tp>
239 _M_not_empty_function(
const _Tp&)
244 _M_init_functor(_Any_data& __functor, _Functor&& __f,
true_type)
245 { ::new (__functor._M_access()) _Functor(std::move(__f)); }
248 _M_init_functor(_Any_data& __functor, _Functor&& __f,
false_type)
249 { __functor._M_access<_Functor*>() =
new _Functor(std::move(__f)); }
257 _M_manager(_M_functor, _M_functor, __destroy_functor);
260 bool _M_empty()
const {
return !_M_manager; }
262 typedef bool (*_Manager_type)(_Any_data&,
const _Any_data&,
265 _Any_data _M_functor;
266 _Manager_type _M_manager;
269 template<
typename _Signature,
typename _Functor>
270 class _Function_handler;
272 template<
typename _Res,
typename _Functor,
typename... _ArgTypes>
273 class _Function_handler<_Res(_ArgTypes...), _Functor>
274 :
public _Function_base::_Base_manager<_Functor>
276 typedef _Function_base::_Base_manager<_Functor> _Base;
280 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
282 return (*_Base::_M_get_pointer(__functor))(
283 std::forward<_ArgTypes>(__args)...);
287 template<
typename _Functor,
typename... _ArgTypes>
288 class _Function_handler<void(_ArgTypes...), _Functor>
289 :
public _Function_base::_Base_manager<_Functor>
291 typedef _Function_base::_Base_manager<_Functor> _Base;
295 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
297 (*_Base::_M_get_pointer(__functor))(
298 std::forward<_ArgTypes>(__args)...);
302 template<
typename _Class,
typename _Member,
typename _Res,
303 typename... _ArgTypes>
304 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
305 :
public _Function_handler<void(_ArgTypes...), _Member _Class::*>
307 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
312 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
314 return std::__invoke(_Base::_M_get_pointer(__functor)->__value,
315 std::forward<_ArgTypes>(__args)...);
319 template<
typename _Class,
typename _Member,
typename... _ArgTypes>
320 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
321 :
public _Function_base::_Base_manager<
322 _Simple_type_wrapper< _Member _Class::* > >
324 typedef _Member _Class::* _Functor;
325 typedef _Simple_type_wrapper<_Functor> _Wrapper;
326 typedef _Function_base::_Base_manager<_Wrapper> _Base;
330 _M_manager(_Any_data& __dest,
const _Any_data& __source,
331 _Manager_operation __op)
336 case __get_type_info:
337 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
340 case __get_functor_ptr:
341 __dest._M_access<_Functor*>() =
342 &_Base::_M_get_pointer(__source)->__value;
346 _Base::_M_manager(__dest, __source, __op);
352 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
355 std::forward<_ArgTypes>(__args)...);
359 template<
typename _From,
typename _To>
360 using __check_func_return_type
361 = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
369 template<
typename _Res,
typename... _ArgTypes>
370 class function<_Res(_ArgTypes...)>
374 template<
typename _Func,
375 typename _Res2 =
typename result_of<_Func&(_ArgTypes...)>::type>
376 struct _Callable : __check_func_return_type<_Res2, _Res> { };
380 template<
typename _Tp>
381 struct _Callable<function, _Tp> :
false_type { };
383 template<
typename _Cond,
typename _Tp>
387 typedef _Res result_type;
402 function(nullptr_t) noexcept
413 function(
const function& __x);
443 template<
typename _Functor,
444 typename = _Requires<__not_<is_same<_Functor, function>>,
void>,
445 typename = _Requires<_Callable<_Functor>,
void>>
463 function(__x).swap(*
this);
481 function(std::move(__x)).swap(*
this);
497 _M_manager(_M_functor, _M_functor, __destroy_functor);
498 _M_manager =
nullptr;
499 _M_invoker =
nullptr;
520 template<
typename _Functor>
521 _Requires<_Callable<typename decay<_Functor>::type>,
function&>
524 function(std::forward<_Functor>(__f)).swap(*
this);
529 template<
typename _Functor>
533 function(__f).swap(*
this);
546 void swap(
function& __x) noexcept
548 std::swap(_M_functor, __x._M_functor);
549 std::swap(_M_manager, __x._M_manager);
550 std::swap(_M_invoker, __x._M_invoker);
563 explicit operator bool() const noexcept
564 {
return !_M_empty(); }
576 _Res operator()(_ArgTypes... __args)
const;
589 const type_info& target_type() const noexcept;
602 template<typename _Functor> _Functor* target() noexcept;
604 template<typename _Functor> const _Functor* target() const noexcept;
609 using _Invoker_type = _Res (*)(
const _Any_data&, _ArgTypes&&...);
610 _Invoker_type _M_invoker;
613 #if __cpp_deduction_guides >= 201606 615 struct __function_guide_helper
618 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
619 struct __function_guide_helper<
620 _Res (_Tp::*) (_Args...) noexcept(_Nx)
622 {
using type = _Res(_Args...); };
624 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
625 struct __function_guide_helper<
626 _Res (_Tp::*) (_Args...) & noexcept(_Nx)
628 {
using type = _Res(_Args...); };
630 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
631 struct __function_guide_helper<
632 _Res (_Tp::*) (_Args...) const noexcept(_Nx)
634 {
using type = _Res(_Args...); };
636 template<
typename _Res,
typename _Tp,
bool _Nx,
typename... _Args>
637 struct __function_guide_helper<
638 _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
640 {
using type = _Res(_Args...); };
642 template<
typename _Res,
typename... _ArgTypes>
643 function(_Res(*)(_ArgTypes...)) ->
function<_Res(_ArgTypes...)>;
645 template<
typename _Functor,
typename _Signature =
typename 646 __function_guide_helper<decltype(&_Functor::operator())>::type>
647 function(_Functor) -> function<_Signature>;
651 template<
typename _Res,
typename... _ArgTypes>
652 function<_Res(_ArgTypes...)>::
653 function(
const function& __x)
656 if (static_cast<bool>(__x))
658 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
659 _M_invoker = __x._M_invoker;
660 _M_manager = __x._M_manager;
664 template<
typename _Res,
typename... _ArgTypes>
665 template<
typename _Functor,
typename,
typename>
666 function<_Res(_ArgTypes...)>::
667 function(_Functor __f)
670 typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
672 if (_My_handler::_M_not_empty_function(__f))
674 _My_handler::_M_init_functor(_M_functor, std::move(__f));
675 _M_invoker = &_My_handler::_M_invoke;
676 _M_manager = &_My_handler::_M_manager;
680 template<
typename _Res,
typename... _ArgTypes>
682 function<_Res(_ArgTypes...)>::
683 operator()(_ArgTypes... __args)
const 686 __throw_bad_function_call();
687 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
691 template<
typename _Res,
typename... _ArgTypes>
693 function<_Res(_ArgTypes...)>::
694 target_type() const noexcept
698 _Any_data __typeinfo_result;
699 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
700 return *__typeinfo_result._M_access<
const type_info*>();
706 template<
typename _Res,
typename... _ArgTypes>
707 template<
typename _Functor>
709 function<_Res(_ArgTypes...)>::
712 const function* __const_this =
this;
713 const _Functor* __func = __const_this->template target<_Functor>();
714 return const_cast<_Functor*>(__func);
717 template<
typename _Res,
typename... _ArgTypes>
718 template<
typename _Functor>
720 function<_Res(_ArgTypes...)>::
721 target() const noexcept
723 if (
typeid(_Functor) == target_type() && _M_manager)
726 _M_manager(__ptr, _M_functor, __get_functor_ptr);
727 return __ptr._M_access<
const _Functor*>();
743 template<
typename _Res,
typename... _Args>
745 operator==(
const function<_Res(_Args...)>& __f, nullptr_t) noexcept
746 {
return !static_cast<bool>(__f); }
749 template<
typename _Res,
typename... _Args>
751 operator==(nullptr_t,
const function<_Res(_Args...)>& __f) noexcept
752 {
return !static_cast<bool>(__f); }
761 template<
typename _Res,
typename... _Args>
763 operator!=(
const function<_Res(_Args...)>& __f, nullptr_t) noexcept
764 {
return static_cast<bool>(__f); }
767 template<
typename _Res,
typename... _Args>
769 operator!=(nullptr_t,
const function<_Res(_Args...)>& __f) noexcept
770 {
return static_cast<bool>(__f); }
782 template<
typename _Res,
typename... _Args>
784 swap(
function<_Res(_Args...)>& __x,
function<_Res(_Args...)>& __y) noexcept
787 _GLIBCXX_END_NAMESPACE_VERSION
792 #endif // _GLIBCXX_STD_FUNCTION_H
function & operator=(reference_wrapper< _Functor > __f) noexcept
function & operator=(nullptr_t) noexcept
Function assignment to zero.
Define a member typedef type only if a boolean constant is true.
void swap(function &__x) noexcept
Swap the targets of two function objects.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Exception class thrown when class template function's operator() is called with an empty target.
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
function & operator=(const function &__x)
Function assignment operator.
const char * what() const noexcept
Base class of all polymorphic function object wrappers.
ISO C++ entities toplevel namespace is std.
Primary class template for reference_wrapper.
function & operator=(function &&__x) noexcept
Function move-assignment operator.
Base class for all library exceptions.
_Requires< _Callable< typename decay< _Functor >::type >, function & > operator=(_Functor &&__f)
Function assignment to a new target.