30 #ifndef _GLIBCXX_STD_FUNCTION_H 31 #define _GLIBCXX_STD_FUNCTION_H 1 33 #pragma GCC system_header 35 #if __cplusplus < 201103L 44 #include <bits/refwrap.h> 47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 template<
typename _Res,
typename... _ArgTypes>
60 template<
typename _Res,
typename _T1>
65 template<
typename _Res,
typename _T1,
typename _T2>
80 const char* what()
const noexcept;
89 template<
typename _Tp>
91 : is_trivially_copyable<_Tp>::type
94 class _Undefined_class;
99 const void* _M_const_object;
100 void (*_M_function_pointer)();
101 void (_Undefined_class::*_M_member_pointer)();
104 union [[gnu::may_alias]] _Any_data
106 void* _M_access() {
return &_M_pod_data[0]; }
107 const void* _M_access()
const {
return &_M_pod_data[0]; }
109 template<
typename _Tp>
112 {
return *
static_cast<_Tp*
>(_M_access()); }
114 template<
typename _Tp>
117 {
return *
static_cast<const _Tp*
>(_M_access()); }
119 _Nocopy_types _M_unused;
120 char _M_pod_data[
sizeof(_Nocopy_types)];
123 enum _Manager_operation
133 template<
typename _Tp>
134 struct _Simple_type_wrapper
136 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
141 template<
typename _Tp>
146 template<
typename _Signature>
153 static const std::size_t _M_max_size =
sizeof(_Nocopy_types);
154 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
156 template<
typename _Functor>
160 static const bool __stored_locally =
162 &&
sizeof(_Functor) <= _M_max_size
163 && __alignof__(_Functor) <= _M_max_align
164 && (_M_max_align % __alignof__(_Functor) == 0));
170 _M_get_pointer(
const _Any_data& __source)
172 const _Functor* __ptr =
174 : __source._M_access<_Functor*>();
175 return const_cast<_Functor*
>(__ptr);
181 _M_clone(_Any_data& __dest,
const _Any_data& __source,
true_type)
183 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
189 _M_clone(_Any_data& __dest,
const _Any_data& __source,
false_type)
191 __dest._M_access<_Functor*>() =
192 new _Functor(*__source._M_access<_Functor*>());
198 _M_destroy(_Any_data& __victim,
true_type)
200 __victim._M_access<_Functor>().~_Functor();
207 delete __victim._M_access<_Functor*>();
212 _M_manager(_Any_data& __dest,
const _Any_data& __source,
213 _Manager_operation __op)
218 case __get_type_info:
219 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
222 case __get_functor_ptr:
223 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
226 case __clone_functor:
227 _M_clone(__dest, __source, _Local_storage());
230 case __destroy_functor:
231 _M_destroy(__dest, _Local_storage());
238 _M_init_functor(_Any_data& __functor, _Functor&& __f)
239 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
241 template<
typename _Signature>
243 _M_not_empty_function(
const function<_Signature>& __f)
244 {
return static_cast<bool>(__f); }
246 template<
typename _Tp>
248 _M_not_empty_function(_Tp* __fp)
249 {
return __fp !=
nullptr; }
251 template<
typename _Class,
typename _Tp>
253 _M_not_empty_function(_Tp _Class::* __mp)
254 {
return __mp !=
nullptr; }
256 template<
typename _Tp>
258 _M_not_empty_function(
const _Tp&)
263 _M_init_functor(_Any_data& __functor, _Functor&& __f,
true_type)
264 { ::new (__functor._M_access()) _Functor(std::move(__f)); }
267 _M_init_functor(_Any_data& __functor, _Functor&& __f,
false_type)
268 { __functor._M_access<_Functor*>() =
new _Functor(std::move(__f)); }
276 _M_manager(_M_functor, _M_functor, __destroy_functor);
279 bool _M_empty()
const {
return !_M_manager; }
281 typedef bool (*_Manager_type)(_Any_data&,
const _Any_data&,
284 _Any_data _M_functor;
285 _Manager_type _M_manager;
288 template<
typename _Signature,
typename _Functor>
289 class _Function_handler;
291 template<
typename _Res,
typename _Functor,
typename... _ArgTypes>
292 class _Function_handler<_Res(_ArgTypes...), _Functor>
293 :
public _Function_base::_Base_manager<_Functor>
295 typedef _Function_base::_Base_manager<_Functor> _Base;
299 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
301 return (*_Base::_M_get_pointer(__functor))(
302 std::forward<_ArgTypes>(__args)...);
306 template<
typename _Functor,
typename... _ArgTypes>
307 class _Function_handler<void(_ArgTypes...), _Functor>
308 :
public _Function_base::_Base_manager<_Functor>
310 typedef _Function_base::_Base_manager<_Functor> _Base;
314 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
316 (*_Base::_M_get_pointer(__functor))(
317 std::forward<_ArgTypes>(__args)...);
321 template<
typename _Class,
typename _Member,
typename _Res,
322 typename... _ArgTypes>
323 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
324 :
public _Function_handler<void(_ArgTypes...), _Member _Class::*>
326 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
331 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
333 return std::__invoke(_Base::_M_get_pointer(__functor)->__value,
334 std::forward<_ArgTypes>(__args)...);
338 template<
typename _Class,
typename _Member,
typename... _ArgTypes>
339 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
340 :
public _Function_base::_Base_manager<
341 _Simple_type_wrapper< _Member _Class::* > >
343 typedef _Member _Class::* _Functor;
344 typedef _Simple_type_wrapper<_Functor> _Wrapper;
345 typedef _Function_base::_Base_manager<_Wrapper> _Base;
349 _M_manager(_Any_data& __dest,
const _Any_data& __source,
350 _Manager_operation __op)
355 case __get_type_info:
356 __dest._M_access<
const type_info*>() = &
typeid(_Functor);
359 case __get_functor_ptr:
360 __dest._M_access<_Functor*>() =
361 &_Base::_M_get_pointer(__source)->__value;
365 _Base::_M_manager(__dest, __source, __op);
371 _M_invoke(
const _Any_data& __functor, _ArgTypes&&... __args)
374 std::forward<_ArgTypes>(__args)...);
378 template<
typename _From,
typename _To>
379 using __check_func_return_type
380 = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
388 template<
typename _Res,
typename... _ArgTypes>
389 class function<_Res(_ArgTypes...)>
393 template<
typename _Func,
394 typename _Res2 =
typename result_of<_Func&(_ArgTypes...)>::type>
395 struct _Callable : __check_func_return_type<_Res2, _Res> { };
399 template<
typename _Tp>
400 struct _Callable<function, _Tp> :
false_type { };
402 template<
typename _Cond,
typename _Tp>
403 using _Requires =
typename enable_if<_Cond::value, _Tp>::type;
406 typedef _Res result_type;
421 function(nullptr_t) noexcept
432 function(
const function& __x);
462 template<
typename _Functor,
463 typename = _Requires<__not_<is_same<_Functor, function>>,
void>,
464 typename = _Requires<_Callable<_Functor>,
void>>
482 function(__x).swap(*
this);
500 function(std::move(__x)).swap(*
this);
516 _M_manager(_M_functor, _M_functor, __destroy_functor);
517 _M_manager =
nullptr;
518 _M_invoker =
nullptr;
539 template<
typename _Functor>
540 _Requires<_Callable<typename decay<_Functor>::type>,
function&>
543 function(std::forward<_Functor>(__f)).swap(*
this);
548 template<
typename _Functor>
552 function(__f).swap(*
this);
565 void swap(
function& __x) noexcept
567 std::swap(_M_functor, __x._M_functor);
568 std::swap(_M_manager, __x._M_manager);
569 std::swap(_M_invoker, __x._M_invoker);
582 explicit operator bool() const noexcept
583 {
return !_M_empty(); }
595 _Res operator()(_ArgTypes... __args)
const;
608 const type_info& target_type()
const noexcept;
621 template<
typename _Functor> _Functor* target() noexcept;
623 template<
typename _Functor>
const _Functor* target()
const noexcept;
628 using _Invoker_type = _Res (*)(
const _Any_data&, _ArgTypes&&...);
629 _Invoker_type _M_invoker;
633 template<
typename _Res,
typename... _ArgTypes>
634 function<_Res(_ArgTypes...)>::
635 function(
const function& __x)
638 if (static_cast<bool>(__x))
640 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
641 _M_invoker = __x._M_invoker;
642 _M_manager = __x._M_manager;
646 template<
typename _Res,
typename... _ArgTypes>
647 template<
typename _Functor,
typename,
typename>
648 function<_Res(_ArgTypes...)>::
649 function(_Functor __f)
652 typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
654 if (_My_handler::_M_not_empty_function(__f))
656 _My_handler::_M_init_functor(_M_functor, std::move(__f));
657 _M_invoker = &_My_handler::_M_invoke;
658 _M_manager = &_My_handler::_M_manager;
662 template<
typename _Res,
typename... _ArgTypes>
664 function<_Res(_ArgTypes...)>::
665 operator()(_ArgTypes... __args)
const 668 __throw_bad_function_call();
669 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
673 template<
typename _Res,
typename... _ArgTypes>
675 function<_Res(_ArgTypes...)>::
676 target_type() const noexcept
680 _Any_data __typeinfo_result;
681 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
682 return *__typeinfo_result._M_access<
const type_info*>();
688 template<
typename _Res,
typename... _ArgTypes>
689 template<
typename _Functor>
691 function<_Res(_ArgTypes...)>::
694 const function* __const_this =
this;
695 const _Functor* __func = __const_this->template target<_Functor>();
696 return const_cast<_Functor*
>(__func);
699 template<
typename _Res,
typename... _ArgTypes>
700 template<
typename _Functor>
702 function<_Res(_ArgTypes...)>::
703 target() const noexcept
705 if (
typeid(_Functor) == target_type() && _M_manager)
708 _M_manager(__ptr, _M_functor, __get_functor_ptr);
709 return __ptr._M_access<
const _Functor*>();
725 template<
typename _Res,
typename... _Args>
727 operator==(
const function<_Res(_Args...)>& __f, nullptr_t) noexcept
728 {
return !
static_cast<bool>(__f); }
731 template<
typename _Res,
typename... _Args>
733 operator==(nullptr_t,
const function<_Res(_Args...)>& __f) noexcept
734 {
return !
static_cast<bool>(__f); }
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); }
764 template<
typename _Res,
typename... _Args>
766 swap(
function<_Res(_Args...)>& __x,
function<_Res(_Args...)>& __y) noexcept
769 _GLIBCXX_END_NAMESPACE_VERSION
774 #endif // _GLIBCXX_STD_FUNCTION_H
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
ISO C++ entities toplevel namespace is std.
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=(function &&__x)
Function move-assignment operator.
Exception class thrown when class template function's operator() is called with an empty target...
Primary class template for reference_wrapper.
Base class of all polymorphic function object wrappers.
Base class for all library exceptions.
function & operator=(reference_wrapper< _Functor > __f) noexcept
constexpr result_of< _Callable &&(_Args &&...)>::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_callable< _Callable &&(_Args &&...)>::value)
Invoke a callable object.
function & operator=(nullptr_t) noexcept
Function assignment to zero.