1 // Implementation of std::function -*- C++ -*-
3 // Copyright (C) 2004-2017 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/bits/function.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{functional}
30 #ifndef _GLIBCXX_STD_FUNCTION_H
31 #define _GLIBCXX_STD_FUNCTION_H 1
33 #pragma GCC system_header
35 #if __cplusplus < 201103L
36 # include <bits/c++0x_warning.h>
42 #include <bits/stl_function.h>
43 #include <bits/invoke.h>
44 #include <bits/refwrap.h>
45 #include <bits/functexcept.h>
47 namespace std
_GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 * Derives from @c unary_function or @c binary_function, or perhaps
53 * nothing, depending on the number of arguments provided. The
54 * primary template is the basis case, which derives nothing.
56 template<typename _Res
, typename
... _ArgTypes
>
57 struct _Maybe_unary_or_binary_function
{ };
59 /// Derives from @c unary_function, as appropriate.
60 template<typename _Res
, typename _T1
>
61 struct _Maybe_unary_or_binary_function
<_Res
, _T1
>
62 : std::unary_function
<_T1
, _Res
> { };
64 /// Derives from @c binary_function, as appropriate.
65 template<typename _Res
, typename _T1
, typename _T2
>
66 struct _Maybe_unary_or_binary_function
<_Res
, _T1
, _T2
>
67 : std::binary_function
<_T1
, _T2
, _Res
> { };
71 * @brief Exception class thrown when class template function's
72 * operator() is called with an empty target.
75 class bad_function_call
: public std::exception
78 virtual ~bad_function_call() noexcept
;
80 const char* what() const noexcept
;
84 * Trait identifying "location-invariant" types, meaning that the
85 * address of the object (or any of its members) will not escape.
86 * Trivially copyable types are location-invariant and users can
87 * specialize this trait for other types.
89 template<typename _Tp
>
90 struct __is_location_invariant
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
131 // Simple type wrapper that helps avoid annoying const problems
132 // when casting between void pointers and pointers-to-pointers.
133 template<typename _Tp
>
134 struct _Simple_type_wrapper
136 _Simple_type_wrapper(_Tp __value
) : __value(__value
) { }
141 template<typename _Tp
>
142 struct __is_location_invariant
<_Simple_type_wrapper
<_Tp
> >
143 : __is_location_invariant
<_Tp
>
146 template<typename _Signature
>
149 /// Base class of all polymorphic function object wrappers.
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
=
161 (__is_location_invariant
<_Functor
>::value
162 && sizeof(_Functor
) <= _M_max_size
163 && __alignof__(_Functor
) <= _M_max_align
164 && (_M_max_align
% __alignof__(_Functor
) == 0));
166 typedef integral_constant
<bool, __stored_locally
> _Local_storage
;
168 // Retrieve a pointer to the function object
170 _M_get_pointer(const _Any_data
& __source
)
172 const _Functor
* __ptr
=
173 __stored_locally
? std::__addressof(__source
._M_access
<_Functor
>())
174 /* have stored a pointer */ : __source
._M_access
<_Functor
*>();
175 return const_cast<_Functor
*>(__ptr
);
178 // Clone a location-invariant function object that fits within
179 // an _Any_data structure.
181 _M_clone(_Any_data
& __dest
, const _Any_data
& __source
, true_type
)
183 ::new (__dest
._M_access()) _Functor(__source
._M_access
<_Functor
>());
186 // Clone a function object that is not location-invariant or
187 // that cannot fit into an _Any_data structure.
189 _M_clone(_Any_data
& __dest
, const _Any_data
& __source
, false_type
)
191 __dest
._M_access
<_Functor
*>() =
192 new _Functor(*__source
._M_access
<_Functor
*>());
195 // Destroying a location-invariant object may still require
198 _M_destroy(_Any_data
& __victim
, true_type
)
200 __victim
._M_access
<_Functor
>().~_Functor();
203 // Destroying an object located on the heap.
205 _M_destroy(_Any_data
& __victim
, false_type
)
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
)); }
271 _Function_base() : _M_manager(nullptr) { }
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
)
373 std::__invoke(_Base::_M_get_pointer(__functor
)->__value
,
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
>>;
383 * @brief Primary class template for std::function.
386 * Polymorphic function wrapper.
388 template<typename _Res
, typename
... _ArgTypes
>
389 class function
<_Res(_ArgTypes
...)>
390 : public _Maybe_unary_or_binary_function
<_Res
, _ArgTypes
...>,
391 private _Function_base
393 template<typename _Func
,
394 typename _Res2
= typename result_of
<_Func
&(_ArgTypes
...)>::type
>
395 struct _Callable
: __check_func_return_type
<_Res2
, _Res
> { };
397 // Used so the return type convertibility checks aren't done when
398 // performing overload resolution for copy construction/assignment.
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
;
408 // [3.7.2.1] construct/copy/destroy
411 * @brief Default construct creates an empty function call wrapper.
412 * @post @c !(bool)*this
415 : _Function_base() { }
418 * @brief Creates an empty function call wrapper.
419 * @post @c !(bool)*this
421 function(nullptr_t
) noexcept
422 : _Function_base() { }
425 * @brief %Function copy constructor.
426 * @param __x A %function object with identical call signature.
427 * @post @c bool(*this) == bool(__x)
429 * The newly-created %function contains a copy of the target of @a
430 * __x (if it has one).
432 function(const function
& __x
);
435 * @brief %Function move constructor.
436 * @param __x A %function object rvalue with identical call signature.
438 * The newly-created %function contains the target of @a __x
441 function(function
&& __x
) : _Function_base()
447 * @brief Builds a %function that targets a copy of the incoming
449 * @param __f A %function object that is callable with parameters of
450 * type @c T1, @c T2, ..., @c TN and returns a value convertible
453 * The newly-created %function object will target a copy of
454 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
455 * object will contain a reference to the function object @c
456 * __f.get(). If @a __f is a NULL function pointer or NULL
457 * pointer-to-member, the newly-created object will be empty.
459 * If @a __f is a non-NULL function pointer or an object of type @c
460 * reference_wrapper<F>, this function will not throw.
462 template<typename _Functor
,
463 typename
= _Requires
<__not_
<is_same
<_Functor
, function
>>, void>,
464 typename
= _Requires
<_Callable
<_Functor
>, void>>
468 * @brief %Function assignment operator.
469 * @param __x A %function with identical call signature.
470 * @post @c (bool)*this == (bool)x
473 * The target of @a __x is copied to @c *this. If @a __x has no
474 * target, then @c *this will be empty.
476 * If @a __x targets a function pointer or a reference to a function
477 * object, then this operation will not throw an %exception.
480 operator=(const function
& __x
)
482 function(__x
).swap(*this);
487 * @brief %Function move-assignment operator.
488 * @param __x A %function rvalue with identical call signature.
491 * The target of @a __x is moved to @c *this. If @a __x has no
492 * target, then @c *this will be empty.
494 * If @a __x targets a function pointer or a reference to a function
495 * object, then this operation will not throw an %exception.
498 operator=(function
&& __x
)
500 function(std::move(__x
)).swap(*this);
505 * @brief %Function assignment to zero.
506 * @post @c !(bool)*this
509 * The target of @c *this is deallocated, leaving it empty.
512 operator=(nullptr_t
) noexcept
516 _M_manager(_M_functor
, _M_functor
, __destroy_functor
);
517 _M_manager
= nullptr;
518 _M_invoker
= nullptr;
524 * @brief %Function assignment to a new target.
525 * @param __f A %function object that is callable with parameters of
526 * type @c T1, @c T2, ..., @c TN and returns a value convertible
530 * This %function object wrapper will target a copy of @a
531 * __f. If @a __f is @c reference_wrapper<F>, then this function
532 * object will contain a reference to the function object @c
533 * __f.get(). If @a __f is a NULL function pointer or NULL
534 * pointer-to-member, @c this object will be empty.
536 * If @a __f is a non-NULL function pointer or an object of type @c
537 * reference_wrapper<F>, this function will not throw.
539 template<typename _Functor
>
540 _Requires
<_Callable
<typename decay
<_Functor
>::type
>, function
&>
541 operator=(_Functor
&& __f
)
543 function(std::forward
<_Functor
>(__f
)).swap(*this);
548 template<typename _Functor
>
550 operator=(reference_wrapper
<_Functor
> __f
) noexcept
552 function(__f
).swap(*this);
556 // [3.7.2.2] function modifiers
559 * @brief Swap the targets of two %function objects.
560 * @param __x A %function with identical call signature.
562 * Swap the targets of @c this function object and @a __f. This
563 * function will not throw an %exception.
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
);
572 // [3.7.2.3] function capacity
575 * @brief Determine if the %function wrapper has a target.
577 * @return @c true when this %function object contains a target,
578 * or @c false when it is empty.
580 * This function will not throw an %exception.
582 explicit operator bool() const noexcept
583 { return !_M_empty(); }
585 // [3.7.2.4] function invocation
588 * @brief Invokes the function targeted by @c *this.
589 * @returns the result of the target.
590 * @throws bad_function_call when @c !(bool)*this
592 * The function call operator invokes the target function object
595 _Res
operator()(_ArgTypes
... __args
) const;
598 // [3.7.2.5] function target access
600 * @brief Determine the type of the target of this function object
603 * @returns the type identifier of the target function object, or
604 * @c typeid(void) if @c !(bool)*this.
606 * This function will not throw an %exception.
608 const type_info
& target_type() const noexcept
;
611 * @brief Access the stored target function object.
613 * @return Returns a pointer to the stored target function object,
614 * if @c typeid(_Functor).equals(target_type()); otherwise, a NULL
617 * This function does not throw exceptions.
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
;
632 #if __cpp_deduction_guides >= 201606
634 struct __function_guide_helper
637 template<typename _Res
, typename _Tp
, bool _Nx
, typename
... _Args
>
638 struct __function_guide_helper
<
639 _Res (_Tp::*) (_Args
...) noexcept(_Nx
)
641 { using type
= _Res(_Args
...); };
643 template<typename _Res
, typename _Tp
, bool _Nx
, typename
... _Args
>
644 struct __function_guide_helper
<
645 _Res (_Tp::*) (_Args
...) & noexcept(_Nx
)
647 { using type
= _Res(_Args
...); };
649 template<typename _Res
, typename _Tp
, bool _Nx
, typename
... _Args
>
650 struct __function_guide_helper
<
651 _Res (_Tp::*) (_Args
...) const noexcept(_Nx
)
653 { using type
= _Res(_Args
...); };
655 template<typename _Res
, typename _Tp
, bool _Nx
, typename
... _Args
>
656 struct __function_guide_helper
<
657 _Res (_Tp::*) (_Args
...) const & noexcept(_Nx
)
659 { using type
= _Res(_Args
...); };
661 template<typename _Res
, typename
... _ArgTypes
>
662 function(_Res(*)(_ArgTypes
...)) -> function
<_Res(_ArgTypes
...)>;
664 template<typename _Functor
, typename _Signature
= typename
665 __function_guide_helper
<decltype(&_Functor::operator())>::type
>
666 function(_Functor
) -> function
<_Signature
>;
669 // Out-of-line member definitions.
670 template<typename _Res
, typename
... _ArgTypes
>
671 function
<_Res(_ArgTypes
...)>::
672 function(const function
& __x
)
675 if (static_cast<bool>(__x
))
677 __x
._M_manager(_M_functor
, __x
._M_functor
, __clone_functor
);
678 _M_invoker
= __x
._M_invoker
;
679 _M_manager
= __x
._M_manager
;
683 template<typename _Res
, typename
... _ArgTypes
>
684 template<typename _Functor
, typename
, typename
>
685 function
<_Res(_ArgTypes
...)>::
686 function(_Functor __f
)
689 typedef _Function_handler
<_Res(_ArgTypes
...), _Functor
> _My_handler
;
691 if (_My_handler::_M_not_empty_function(__f
))
693 _My_handler::_M_init_functor(_M_functor
, std::move(__f
));
694 _M_invoker
= &_My_handler::_M_invoke
;
695 _M_manager
= &_My_handler::_M_manager
;
699 template<typename _Res
, typename
... _ArgTypes
>
701 function
<_Res(_ArgTypes
...)>::
702 operator()(_ArgTypes
... __args
) const
705 __throw_bad_function_call();
706 return _M_invoker(_M_functor
, std::forward
<_ArgTypes
>(__args
)...);
710 template<typename _Res
, typename
... _ArgTypes
>
712 function
<_Res(_ArgTypes
...)>::
713 target_type() const noexcept
717 _Any_data __typeinfo_result
;
718 _M_manager(__typeinfo_result
, _M_functor
, __get_type_info
);
719 return *__typeinfo_result
._M_access
<const type_info
*>();
725 template<typename _Res
, typename
... _ArgTypes
>
726 template<typename _Functor
>
728 function
<_Res(_ArgTypes
...)>::
731 const function
* __const_this
= this;
732 const _Functor
* __func
= __const_this
->template target
<_Functor
>();
733 return const_cast<_Functor
*>(__func
);
736 template<typename _Res
, typename
... _ArgTypes
>
737 template<typename _Functor
>
739 function
<_Res(_ArgTypes
...)>::
740 target() const noexcept
742 if (typeid(_Functor
) == target_type() && _M_manager
)
745 _M_manager(__ptr
, _M_functor
, __get_functor_ptr
);
746 return __ptr
._M_access
<const _Functor
*>();
753 // [20.7.15.2.6] null pointer comparisons
756 * @brief Compares a polymorphic function object wrapper against 0
757 * (the NULL pointer).
758 * @returns @c true if the wrapper has no target, @c false otherwise
760 * This function will not throw an %exception.
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
); }
774 * @brief Compares a polymorphic function object wrapper against 0
775 * (the NULL pointer).
776 * @returns @c false if the wrapper has no target, @c true otherwise
778 * This function will not throw an %exception.
780 template<typename _Res
, typename
... _Args
>
782 operator!=(const function
<_Res(_Args
...)>& __f
, nullptr_t
) noexcept
783 { return static_cast<bool>(__f
); }
786 template<typename _Res
, typename
... _Args
>
788 operator!=(nullptr_t
, const function
<_Res(_Args
...)>& __f
) noexcept
789 { return static_cast<bool>(__f
); }
792 // [20.7.15.2.7] specialized algorithms
795 * @brief Swap the targets of two polymorphic function object wrappers.
797 * This function will not throw an %exception.
799 // _GLIBCXX_RESOLVE_LIB_DEFECTS
800 // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
801 template<typename _Res
, typename
... _Args
>
803 swap(function
<_Res(_Args
...)>& __x
, function
<_Res(_Args
...)>& __y
) noexcept
806 _GLIBCXX_END_NAMESPACE_VERSION
811 #endif // _GLIBCXX_STD_FUNCTION_H