Import GCC-8 to a new vendor branch
[dragonfly.git] / contrib / gcc-8.0 / libstdc++-v3 / include / bits / std_function.h
blob96261355a9d61fb3e83412b5a0e58d849902f4de
1 // Implementation of std::function -*- C++ -*-
3 // Copyright (C) 2004-2018 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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>
37 #else
39 #if __cpp_rtti
40 # include <typeinfo>
41 #endif
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
51 /**
52 * @brief Exception class thrown when class template function's
53 * operator() is called with an empty target.
54 * @ingroup exceptions
56 class bad_function_call : public std::exception
58 public:
59 virtual ~bad_function_call() noexcept;
61 const char* what() const noexcept;
64 /**
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
73 { };
75 class _Undefined_class;
77 union _Nocopy_types
79 void* _M_object;
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>
91 _Tp&
92 _M_access()
93 { return *static_cast<_Tp*>(_M_access()); }
95 template<typename _Tp>
96 const _Tp&
97 _M_access() const
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
106 __get_type_info,
107 __get_functor_ptr,
108 __clone_functor,
109 __destroy_functor
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) { }
119 _Tp __value;
122 template<typename _Tp>
123 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
124 : __is_location_invariant<_Tp>
125 { };
127 template<typename _Signature>
128 class function;
130 /// Base class of all polymorphic function object wrappers.
131 class _Function_base
133 public:
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>
138 class _Base_manager
140 protected:
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
150 static _Functor*
151 _M_get_pointer(const _Any_data& __source)
153 const _Functor* __ptr =
154 __stored_locally? std::__addressof(__source._M_access<_Functor>())
155 /* have stored a pointer */ : __source._M_access<_Functor*>();
156 return const_cast<_Functor*>(__ptr);
159 // Clone a location-invariant function object that fits within
160 // an _Any_data structure.
161 static void
162 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
164 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
167 // Clone a function object that is not location-invariant or
168 // that cannot fit into an _Any_data structure.
169 static void
170 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
172 __dest._M_access<_Functor*>() =
173 new _Functor(*__source._M_access<_Functor*>());
176 // Destroying a location-invariant object may still require
177 // destruction.
178 static void
179 _M_destroy(_Any_data& __victim, true_type)
181 __victim._M_access<_Functor>().~_Functor();
184 // Destroying an object located on the heap.
185 static void
186 _M_destroy(_Any_data& __victim, false_type)
188 delete __victim._M_access<_Functor*>();
191 public:
192 static bool
193 _M_manager(_Any_data& __dest, const _Any_data& __source,
194 _Manager_operation __op)
196 switch (__op)
198 #if __cpp_rtti
199 case __get_type_info:
200 __dest._M_access<const type_info*>() = &typeid(_Functor);
201 break;
202 #endif
203 case __get_functor_ptr:
204 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
205 break;
207 case __clone_functor:
208 _M_clone(__dest, __source, _Local_storage());
209 break;
211 case __destroy_functor:
212 _M_destroy(__dest, _Local_storage());
213 break;
215 return false;
218 static void
219 _M_init_functor(_Any_data& __functor, _Functor&& __f)
220 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
222 template<typename _Signature>
223 static bool
224 _M_not_empty_function(const function<_Signature>& __f)
225 { return static_cast<bool>(__f); }
227 template<typename _Tp>
228 static bool
229 _M_not_empty_function(_Tp* __fp)
230 { return __fp != nullptr; }
232 template<typename _Class, typename _Tp>
233 static bool
234 _M_not_empty_function(_Tp _Class::* __mp)
235 { return __mp != nullptr; }
237 template<typename _Tp>
238 static bool
239 _M_not_empty_function(const _Tp&)
240 { return true; }
242 private:
243 static void
244 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
245 { ::new (__functor._M_access()) _Functor(std::move(__f)); }
247 static void
248 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
249 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
252 _Function_base() : _M_manager(nullptr) { }
254 ~_Function_base()
256 if (_M_manager)
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&,
263 _Manager_operation);
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;
278 public:
279 static _Res
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;
293 public:
294 static void
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::*>
308 _Base;
310 public:
311 static _Res
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;
328 public:
329 static bool
330 _M_manager(_Any_data& __dest, const _Any_data& __source,
331 _Manager_operation __op)
333 switch (__op)
335 #if __cpp_rtti
336 case __get_type_info:
337 __dest._M_access<const type_info*>() = &typeid(_Functor);
338 break;
339 #endif
340 case __get_functor_ptr:
341 __dest._M_access<_Functor*>() =
342 &_Base::_M_get_pointer(__source)->__value;
343 break;
345 default:
346 _Base::_M_manager(__dest, __source, __op);
348 return false;
351 static void
352 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
354 std::__invoke(_Base::_M_get_pointer(__functor)->__value,
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>>;
364 * @brief Primary class template for std::function.
365 * @ingroup functors
367 * Polymorphic function wrapper.
369 template<typename _Res, typename... _ArgTypes>
370 class function<_Res(_ArgTypes...)>
371 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
372 private _Function_base
374 template<typename _Func,
375 typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type>
376 struct _Callable : __check_func_return_type<_Res2, _Res> { };
378 // Used so the return type convertibility checks aren't done when
379 // performing overload resolution for copy construction/assignment.
380 template<typename _Tp>
381 struct _Callable<function, _Tp> : false_type { };
383 template<typename _Cond, typename _Tp>
384 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
386 public:
387 typedef _Res result_type;
389 // [3.7.2.1] construct/copy/destroy
392 * @brief Default construct creates an empty function call wrapper.
393 * @post @c !(bool)*this
395 function() noexcept
396 : _Function_base() { }
399 * @brief Creates an empty function call wrapper.
400 * @post @c !(bool)*this
402 function(nullptr_t) noexcept
403 : _Function_base() { }
406 * @brief %Function copy constructor.
407 * @param __x A %function object with identical call signature.
408 * @post @c bool(*this) == bool(__x)
410 * The newly-created %function contains a copy of the target of @a
411 * __x (if it has one).
413 function(const function& __x);
416 * @brief %Function move constructor.
417 * @param __x A %function object rvalue with identical call signature.
419 * The newly-created %function contains the target of @a __x
420 * (if it has one).
422 function(function&& __x) noexcept : _Function_base()
424 __x.swap(*this);
428 * @brief Builds a %function that targets a copy of the incoming
429 * function object.
430 * @param __f A %function object that is callable with parameters of
431 * type @c T1, @c T2, ..., @c TN and returns a value convertible
432 * to @c Res.
434 * The newly-created %function object will target a copy of
435 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
436 * object will contain a reference to the function object @c
437 * __f.get(). If @a __f is a NULL function pointer or NULL
438 * pointer-to-member, the newly-created object will be empty.
440 * If @a __f is a non-NULL function pointer or an object of type @c
441 * reference_wrapper<F>, this function will not throw.
443 template<typename _Functor,
444 typename = _Requires<__not_<is_same<_Functor, function>>, void>,
445 typename = _Requires<_Callable<_Functor>, void>>
446 function(_Functor);
449 * @brief %Function assignment operator.
450 * @param __x A %function with identical call signature.
451 * @post @c (bool)*this == (bool)x
452 * @returns @c *this
454 * The target of @a __x is copied to @c *this. If @a __x has no
455 * target, then @c *this will be empty.
457 * If @a __x targets a function pointer or a reference to a function
458 * object, then this operation will not throw an %exception.
460 function&
461 operator=(const function& __x)
463 function(__x).swap(*this);
464 return *this;
468 * @brief %Function move-assignment operator.
469 * @param __x A %function rvalue with identical call signature.
470 * @returns @c *this
472 * The target of @a __x is moved to @c *this. If @a __x has no
473 * target, then @c *this will be empty.
475 * If @a __x targets a function pointer or a reference to a function
476 * object, then this operation will not throw an %exception.
478 function&
479 operator=(function&& __x) noexcept
481 function(std::move(__x)).swap(*this);
482 return *this;
486 * @brief %Function assignment to zero.
487 * @post @c !(bool)*this
488 * @returns @c *this
490 * The target of @c *this is deallocated, leaving it empty.
492 function&
493 operator=(nullptr_t) noexcept
495 if (_M_manager)
497 _M_manager(_M_functor, _M_functor, __destroy_functor);
498 _M_manager = nullptr;
499 _M_invoker = nullptr;
501 return *this;
505 * @brief %Function assignment to a new target.
506 * @param __f A %function object that is callable with parameters of
507 * type @c T1, @c T2, ..., @c TN and returns a value convertible
508 * to @c Res.
509 * @return @c *this
511 * This %function object wrapper will target a copy of @a
512 * __f. If @a __f is @c reference_wrapper<F>, then this function
513 * object will contain a reference to the function object @c
514 * __f.get(). If @a __f is a NULL function pointer or NULL
515 * pointer-to-member, @c this object will be empty.
517 * If @a __f is a non-NULL function pointer or an object of type @c
518 * reference_wrapper<F>, this function will not throw.
520 template<typename _Functor>
521 _Requires<_Callable<typename decay<_Functor>::type>, function&>
522 operator=(_Functor&& __f)
524 function(std::forward<_Functor>(__f)).swap(*this);
525 return *this;
528 /// @overload
529 template<typename _Functor>
530 function&
531 operator=(reference_wrapper<_Functor> __f) noexcept
533 function(__f).swap(*this);
534 return *this;
537 // [3.7.2.2] function modifiers
540 * @brief Swap the targets of two %function objects.
541 * @param __x A %function with identical call signature.
543 * Swap the targets of @c this function object and @a __f. This
544 * function will not throw an %exception.
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);
553 // [3.7.2.3] function capacity
556 * @brief Determine if the %function wrapper has a target.
558 * @return @c true when this %function object contains a target,
559 * or @c false when it is empty.
561 * This function will not throw an %exception.
563 explicit operator bool() const noexcept
564 { return !_M_empty(); }
566 // [3.7.2.4] function invocation
569 * @brief Invokes the function targeted by @c *this.
570 * @returns the result of the target.
571 * @throws bad_function_call when @c !(bool)*this
573 * The function call operator invokes the target function object
574 * stored by @c this.
576 _Res operator()(_ArgTypes... __args) const;
578 #if __cpp_rtti
579 // [3.7.2.5] function target access
581 * @brief Determine the type of the target of this function object
582 * wrapper.
584 * @returns the type identifier of the target function object, or
585 * @c typeid(void) if @c !(bool)*this.
587 * This function will not throw an %exception.
589 const type_info& target_type() const noexcept;
592 * @brief Access the stored target function object.
594 * @return Returns a pointer to the stored target function object,
595 * if @c typeid(_Functor).equals(target_type()); otherwise, a NULL
596 * pointer.
598 * This function does not throw exceptions.
600 * @{
602 template<typename _Functor> _Functor* target() noexcept;
604 template<typename _Functor> const _Functor* target() const noexcept;
605 // @}
606 #endif
608 private:
609 using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
610 _Invoker_type _M_invoker;
613 #if __cpp_deduction_guides >= 201606
614 template<typename>
615 struct __function_guide_helper
616 { };
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>;
648 #endif
650 // Out-of-line member definitions.
651 template<typename _Res, typename... _ArgTypes>
652 function<_Res(_ArgTypes...)>::
653 function(const function& __x)
654 : _Function_base()
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)
668 : _Function_base()
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>
681 _Res
682 function<_Res(_ArgTypes...)>::
683 operator()(_ArgTypes... __args) const
685 if (_M_empty())
686 __throw_bad_function_call();
687 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
690 #if __cpp_rtti
691 template<typename _Res, typename... _ArgTypes>
692 const type_info&
693 function<_Res(_ArgTypes...)>::
694 target_type() const noexcept
696 if (_M_manager)
698 _Any_data __typeinfo_result;
699 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
700 return *__typeinfo_result._M_access<const type_info*>();
702 else
703 return typeid(void);
706 template<typename _Res, typename... _ArgTypes>
707 template<typename _Functor>
708 _Functor*
709 function<_Res(_ArgTypes...)>::
710 target() noexcept
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>
719 const _Functor*
720 function<_Res(_ArgTypes...)>::
721 target() const noexcept
723 if (typeid(_Functor) == target_type() && _M_manager)
725 _Any_data __ptr;
726 _M_manager(__ptr, _M_functor, __get_functor_ptr);
727 return __ptr._M_access<const _Functor*>();
729 else
730 return nullptr;
732 #endif
734 // [20.7.15.2.6] null pointer comparisons
737 * @brief Compares a polymorphic function object wrapper against 0
738 * (the NULL pointer).
739 * @returns @c true if the wrapper has no target, @c false otherwise
741 * This function will not throw an %exception.
743 template<typename _Res, typename... _Args>
744 inline bool
745 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
746 { return !static_cast<bool>(__f); }
748 /// @overload
749 template<typename _Res, typename... _Args>
750 inline bool
751 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
752 { return !static_cast<bool>(__f); }
755 * @brief Compares a polymorphic function object wrapper against 0
756 * (the NULL pointer).
757 * @returns @c false if the wrapper has no target, @c true otherwise
759 * This function will not throw an %exception.
761 template<typename _Res, typename... _Args>
762 inline bool
763 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
764 { return static_cast<bool>(__f); }
766 /// @overload
767 template<typename _Res, typename... _Args>
768 inline bool
769 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
770 { return static_cast<bool>(__f); }
773 // [20.7.15.2.7] specialized algorithms
776 * @brief Swap the targets of two polymorphic function object wrappers.
778 * This function will not throw an %exception.
780 // _GLIBCXX_RESOLVE_LIB_DEFECTS
781 // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
782 template<typename _Res, typename... _Args>
783 inline void
784 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
785 { __x.swap(__y); }
787 _GLIBCXX_END_NAMESPACE_VERSION
788 } // namespace std
790 #endif // C++11
792 #endif // _GLIBCXX_STD_FUNCTION_H