1 // Implementation of std::function -*- C++ -*-
3 // Copyright (C) 2004-2019 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/std_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 * @brief Exception class thrown when class template function's
53 * operator() is called with an empty target.
56 class bad_function_call
: public std::exception
59 virtual ~bad_function_call() noexcept
;
61 const char* what() const noexcept
;
65 * Trait identifying "location-invariant" types, meaning that the
66 * address of the object (or any of its members) will not escape.
67 * Trivially copyable types are location-invariant and users can
68 * specialize this trait for other types.
70 template<typename _Tp
>
71 struct __is_location_invariant
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 // Simple type wrapper that helps avoid annoying const problems
113 // when casting between void pointers and pointers-to-pointers.
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
>
130 /// Base class of all polymorphic function object wrappers.
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
=
142 (__is_location_invariant
<_Functor
>::value
143 && sizeof(_Functor
) <= _M_max_size
144 && __alignof__(_Functor
) <= _M_max_align
145 && (_M_max_align
% __alignof__(_Functor
) == 0));
147 typedef integral_constant
<bool, __stored_locally
> _Local_storage
;
149 // Retrieve a pointer to the function object
151 _M_get_pointer(const _Any_data
& __source
)
153 if _GLIBCXX17_CONSTEXPR (__stored_locally
)
155 const _Functor
& __f
= __source
._M_access
<_Functor
>();
156 return const_cast<_Functor
*>(std::__addressof(__f
));
158 else // have stored a pointer
159 return __source
._M_access
<_Functor
*>();
162 // Clone a location-invariant function object that fits within
163 // an _Any_data structure.
165 _M_clone(_Any_data
& __dest
, const _Any_data
& __source
, true_type
)
167 ::new (__dest
._M_access()) _Functor(__source
._M_access
<_Functor
>());
170 // Clone a function object that is not location-invariant or
171 // that cannot fit into an _Any_data structure.
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
*>());
179 // Destroying a location-invariant object may still require
182 _M_destroy(_Any_data
& __victim
, true_type
)
184 __victim
._M_access
<_Functor
>().~_Functor();
187 // Destroying an object located on the heap.
189 _M_destroy(_Any_data
& __victim
, false_type
)
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
)); }
255 _Function_base() : _M_manager(nullptr) { }
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
)
357 std::__invoke(_Base::_M_get_pointer(__functor
)->__value
,
358 std::forward
<_ArgTypes
>(__args
)...);
362 template<typename _From
, typename _To
>
363 using __check_func_return_type
364 = __or_
<is_void
<_To
>, is_same
<_From
, _To
>, is_convertible
<_From
, _To
>>;
367 * @brief Primary class template for std::function.
370 * Polymorphic function wrapper.
372 template<typename _Res
, typename
... _ArgTypes
>
373 class function
<_Res(_ArgTypes
...)>
374 : public _Maybe_unary_or_binary_function
<_Res
, _ArgTypes
...>,
375 private _Function_base
377 template<typename _Func
,
378 typename _Res2
= typename result_of
<_Func
&(_ArgTypes
...)>::type
>
379 struct _Callable
: __check_func_return_type
<_Res2
, _Res
> { };
381 // Used so the return type convertibility checks aren't done when
382 // performing overload resolution for copy construction/assignment.
383 template<typename _Tp
>
384 struct _Callable
<function
, _Tp
> : false_type
{ };
386 template<typename _Cond
, typename _Tp
>
387 using _Requires
= typename enable_if
<_Cond::value
, _Tp
>::type
;
390 typedef _Res result_type
;
392 // [3.7.2.1] construct/copy/destroy
395 * @brief Default construct creates an empty function call wrapper.
396 * @post @c !(bool)*this
399 : _Function_base() { }
402 * @brief Creates an empty function call wrapper.
403 * @post @c !(bool)*this
405 function(nullptr_t
) noexcept
406 : _Function_base() { }
409 * @brief %Function copy constructor.
410 * @param __x A %function object with identical call signature.
411 * @post @c bool(*this) == bool(__x)
413 * The newly-created %function contains a copy of the target of @a
414 * __x (if it has one).
416 function(const function
& __x
);
419 * @brief %Function move constructor.
420 * @param __x A %function object rvalue with identical call signature.
422 * The newly-created %function contains the target of @a __x
425 function(function
&& __x
) noexcept
: _Function_base()
431 * @brief Builds a %function that targets a copy of the incoming
433 * @param __f A %function object that is callable with parameters of
434 * type @c T1, @c T2, ..., @c TN and returns a value convertible
437 * The newly-created %function object will target a copy of
438 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
439 * object will contain a reference to the function object @c
440 * __f.get(). If @a __f is a NULL function pointer or NULL
441 * pointer-to-member, the newly-created object will be empty.
443 * If @a __f is a non-NULL function pointer or an object of type @c
444 * reference_wrapper<F>, this function will not throw.
446 template<typename _Functor
,
447 typename
= _Requires
<__not_
<is_same
<_Functor
, function
>>, void>,
448 typename
= _Requires
<_Callable
<_Functor
>, void>>
452 * @brief %Function assignment operator.
453 * @param __x A %function with identical call signature.
454 * @post @c (bool)*this == (bool)x
457 * The target of @a __x is copied to @c *this. If @a __x has no
458 * target, then @c *this will be empty.
460 * If @a __x targets a function pointer or a reference to a function
461 * object, then this operation will not throw an %exception.
464 operator=(const function
& __x
)
466 function(__x
).swap(*this);
471 * @brief %Function move-assignment operator.
472 * @param __x A %function rvalue with identical call signature.
475 * The target of @a __x is moved to @c *this. If @a __x has no
476 * target, then @c *this will be empty.
478 * If @a __x targets a function pointer or a reference to a function
479 * object, then this operation will not throw an %exception.
482 operator=(function
&& __x
) noexcept
484 function(std::move(__x
)).swap(*this);
489 * @brief %Function assignment to zero.
490 * @post @c !(bool)*this
493 * The target of @c *this is deallocated, leaving it empty.
496 operator=(nullptr_t
) noexcept
500 _M_manager(_M_functor
, _M_functor
, __destroy_functor
);
501 _M_manager
= nullptr;
502 _M_invoker
= nullptr;
508 * @brief %Function assignment to a new target.
509 * @param __f A %function object that is callable with parameters of
510 * type @c T1, @c T2, ..., @c TN and returns a value convertible
514 * This %function object wrapper will target a copy of @a
515 * __f. If @a __f is @c reference_wrapper<F>, then this function
516 * object will contain a reference to the function object @c
517 * __f.get(). If @a __f is a NULL function pointer or NULL
518 * pointer-to-member, @c this object will be empty.
520 * If @a __f is a non-NULL function pointer or an object of type @c
521 * reference_wrapper<F>, this function will not throw.
523 template<typename _Functor
>
524 _Requires
<_Callable
<typename decay
<_Functor
>::type
>, function
&>
525 operator=(_Functor
&& __f
)
527 function(std::forward
<_Functor
>(__f
)).swap(*this);
532 template<typename _Functor
>
534 operator=(reference_wrapper
<_Functor
> __f
) noexcept
536 function(__f
).swap(*this);
540 // [3.7.2.2] function modifiers
543 * @brief Swap the targets of two %function objects.
544 * @param __x A %function with identical call signature.
546 * Swap the targets of @c this function object and @a __f. This
547 * function will not throw an %exception.
549 void swap(function
& __x
) noexcept
551 std::swap(_M_functor
, __x
._M_functor
);
552 std::swap(_M_manager
, __x
._M_manager
);
553 std::swap(_M_invoker
, __x
._M_invoker
);
556 // [3.7.2.3] function capacity
559 * @brief Determine if the %function wrapper has a target.
561 * @return @c true when this %function object contains a target,
562 * or @c false when it is empty.
564 * This function will not throw an %exception.
566 explicit operator bool() const noexcept
567 { return !_M_empty(); }
569 // [3.7.2.4] function invocation
572 * @brief Invokes the function targeted by @c *this.
573 * @returns the result of the target.
574 * @throws bad_function_call when @c !(bool)*this
576 * The function call operator invokes the target function object
579 _Res
operator()(_ArgTypes
... __args
) const;
582 // [3.7.2.5] function target access
584 * @brief Determine the type of the target of this function object
587 * @returns the type identifier of the target function object, or
588 * @c typeid(void) if @c !(bool)*this.
590 * This function will not throw an %exception.
592 const type_info
& target_type() const noexcept
;
595 * @brief Access the stored target function object.
597 * @return Returns a pointer to the stored target function object,
598 * if @c typeid(_Functor).equals(target_type()); otherwise, a NULL
601 * This function does not throw exceptions.
605 template<typename _Functor
> _Functor
* target() noexcept
;
607 template<typename _Functor
> const _Functor
* target() const noexcept
;
612 using _Invoker_type
= _Res (*)(const _Any_data
&, _ArgTypes
&&...);
613 _Invoker_type _M_invoker
;
616 #if __cpp_deduction_guides >= 201606
618 struct __function_guide_helper
621 template<typename _Res
, typename _Tp
, bool _Nx
, typename
... _Args
>
622 struct __function_guide_helper
<
623 _Res (_Tp::*) (_Args
...) noexcept(_Nx
)
625 { using type
= _Res(_Args
...); };
627 template<typename _Res
, typename _Tp
, bool _Nx
, typename
... _Args
>
628 struct __function_guide_helper
<
629 _Res (_Tp::*) (_Args
...) & noexcept(_Nx
)
631 { using type
= _Res(_Args
...); };
633 template<typename _Res
, typename _Tp
, bool _Nx
, typename
... _Args
>
634 struct __function_guide_helper
<
635 _Res (_Tp::*) (_Args
...) const noexcept(_Nx
)
637 { using type
= _Res(_Args
...); };
639 template<typename _Res
, typename _Tp
, bool _Nx
, typename
... _Args
>
640 struct __function_guide_helper
<
641 _Res (_Tp::*) (_Args
...) const & noexcept(_Nx
)
643 { using type
= _Res(_Args
...); };
645 template<typename _Res
, typename
... _ArgTypes
>
646 function(_Res(*)(_ArgTypes
...)) -> function
<_Res(_ArgTypes
...)>;
648 template<typename _Functor
, typename _Signature
= typename
649 __function_guide_helper
<decltype(&_Functor::operator())>::type
>
650 function(_Functor
) -> function
<_Signature
>;
653 // Out-of-line member definitions.
654 template<typename _Res
, typename
... _ArgTypes
>
655 function
<_Res(_ArgTypes
...)>::
656 function(const function
& __x
)
659 if (static_cast<bool>(__x
))
661 __x
._M_manager(_M_functor
, __x
._M_functor
, __clone_functor
);
662 _M_invoker
= __x
._M_invoker
;
663 _M_manager
= __x
._M_manager
;
667 template<typename _Res
, typename
... _ArgTypes
>
668 template<typename _Functor
, typename
, typename
>
669 function
<_Res(_ArgTypes
...)>::
670 function(_Functor __f
)
673 typedef _Function_handler
<_Res(_ArgTypes
...), _Functor
> _My_handler
;
675 if (_My_handler::_M_not_empty_function(__f
))
677 _My_handler::_M_init_functor(_M_functor
, std::move(__f
));
678 _M_invoker
= &_My_handler::_M_invoke
;
679 _M_manager
= &_My_handler::_M_manager
;
683 template<typename _Res
, typename
... _ArgTypes
>
685 function
<_Res(_ArgTypes
...)>::
686 operator()(_ArgTypes
... __args
) const
689 __throw_bad_function_call();
690 return _M_invoker(_M_functor
, std::forward
<_ArgTypes
>(__args
)...);
694 template<typename _Res
, typename
... _ArgTypes
>
696 function
<_Res(_ArgTypes
...)>::
697 target_type() const noexcept
701 _Any_data __typeinfo_result
;
702 _M_manager(__typeinfo_result
, _M_functor
, __get_type_info
);
703 return *__typeinfo_result
._M_access
<const type_info
*>();
709 template<typename _Res
, typename
... _ArgTypes
>
710 template<typename _Functor
>
712 function
<_Res(_ArgTypes
...)>::
715 const function
* __const_this
= this;
716 const _Functor
* __func
= __const_this
->template target
<_Functor
>();
717 return const_cast<_Functor
*>(__func
);
720 template<typename _Res
, typename
... _ArgTypes
>
721 template<typename _Functor
>
723 function
<_Res(_ArgTypes
...)>::
724 target() const noexcept
726 if (typeid(_Functor
) == target_type() && _M_manager
)
729 _M_manager(__ptr
, _M_functor
, __get_functor_ptr
);
730 return __ptr
._M_access
<const _Functor
*>();
737 // [20.7.15.2.6] null pointer comparisons
740 * @brief Compares a polymorphic function object wrapper against 0
741 * (the NULL pointer).
742 * @returns @c true if the wrapper has no target, @c false otherwise
744 * This function will not throw an %exception.
746 template<typename _Res
, typename
... _Args
>
748 operator==(const function
<_Res(_Args
...)>& __f
, nullptr_t
) noexcept
749 { return !static_cast<bool>(__f
); }
752 template<typename _Res
, typename
... _Args
>
754 operator==(nullptr_t
, const function
<_Res(_Args
...)>& __f
) noexcept
755 { return !static_cast<bool>(__f
); }
758 * @brief Compares a polymorphic function object wrapper against 0
759 * (the NULL pointer).
760 * @returns @c false if the wrapper has no target, @c true otherwise
762 * This function will not throw an %exception.
764 template<typename _Res
, typename
... _Args
>
766 operator!=(const function
<_Res(_Args
...)>& __f
, nullptr_t
) noexcept
767 { return static_cast<bool>(__f
); }
770 template<typename _Res
, typename
... _Args
>
772 operator!=(nullptr_t
, const function
<_Res(_Args
...)>& __f
) noexcept
773 { return static_cast<bool>(__f
); }
776 // [20.7.15.2.7] specialized algorithms
779 * @brief Swap the targets of two polymorphic function object wrappers.
781 * This function will not throw an %exception.
783 // _GLIBCXX_RESOLVE_LIB_DEFECTS
784 // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
785 template<typename _Res
, typename
... _Args
>
787 swap(function
<_Res(_Args
...)>& __x
, function
<_Res(_Args
...)>& __y
) noexcept
790 _GLIBCXX_END_NAMESPACE_VERSION
795 #endif // _GLIBCXX_STD_FUNCTION_H