1// Implementation of std::function -*- C++ -*-
2
3// Copyright (C) 2004-2021 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.
10
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.
15
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.
19
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/>.
24
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}
28 */
29
30#ifndef _GLIBCXX_STD_FUNCTION_H
31#define _GLIBCXX_STD_FUNCTION_H 1
32
33#pragma GCC system_header
34
35#if __cplusplus < 201103L
36# include <bits/c++0x_warning.h>
37#else
38
39#include <typeinfo>
40#include <bits/stl_function.h>
41#include <bits/invoke.h>
42#include <bits/refwrap.h>
43#include <bits/functexcept.h>
44
45namespace std _GLIBCXX_VISIBILITY(default)
46{
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
48
49 /**
50 * @brief Exception class thrown when class template function's
51 * operator() is called with an empty target.
52 * @ingroup exceptions
53 */
54 class bad_function_call : public std::exception
55 {
56 public:
57 virtual ~bad_function_call() noexcept;
58
59 const char* what() const noexcept;
60 };
61
62 /**
63 * Trait identifying "location-invariant" types, meaning that the
64 * address of the object (or any of its members) will not escape.
65 * Trivially copyable types are location-invariant and users can
66 * specialize this trait for other types.
67 */
68 template<typename _Tp>
69 struct __is_location_invariant
70 : is_trivially_copyable<_Tp>::type
71 { };
72
73 class _Undefined_class;
74
75 union _Nocopy_types
76 {
77 void* _M_object;
78 const void* _M_const_object;
79 void (*_M_function_pointer)();
80 void (_Undefined_class::*_M_member_pointer)();
81 };
82
83 union [[gnu::may_alias]] _Any_data
84 {
85 void* _M_access() { return &_M_pod_data[0]; }
86 const void* _M_access() const { return &_M_pod_data[0]; }
87
88 template<typename _Tp>
89 _Tp&
90 _M_access()
91 { return *static_cast<_Tp*>(_M_access()); }
92
93 template<typename _Tp>
94 const _Tp&
95 _M_access() const
96 { return *static_cast<const _Tp*>(_M_access()); }
97
98 _Nocopy_types _M_unused;
99 char _M_pod_data[sizeof(_Nocopy_types)];
100 };
101
102 enum _Manager_operation
103 {
104 __get_type_info,
105 __get_functor_ptr,
106 __clone_functor,
107 __destroy_functor
108 };
109
110 template<typename _Signature>
111 class function;
112
113 /// Base class of all polymorphic function object wrappers.
114 class _Function_base
115 {
116 public:
117 static const size_t _M_max_size = sizeof(_Nocopy_types);
118 static const size_t _M_max_align = __alignof__(_Nocopy_types);
119
120 template<typename _Functor>
121 class _Base_manager
122 {
123 protected:
124 static const bool __stored_locally =
125 (__is_location_invariant<_Functor>::value
126 && sizeof(_Functor) <= _M_max_size
127 && __alignof__(_Functor) <= _M_max_align
128 && (_M_max_align % __alignof__(_Functor) == 0));
129
130 typedef integral_constant<bool, __stored_locally> _Local_storage;
131
132 // Retrieve a pointer to the function object
133 static _Functor*
134 _M_get_pointer(const _Any_data& __source)
135 {
136 if _GLIBCXX17_CONSTEXPR (__stored_locally)
137 {
138 const _Functor& __f = __source._M_access<_Functor>();
139 return const_cast<_Functor*>(std::__addressof(__f));
140 }
141 else // have stored a pointer
142 return __source._M_access<_Functor*>();
143 }
144
145 // Clone a location-invariant function object that fits within
146 // an _Any_data structure.
147 static void
148 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
149 {
150 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
151 }
152
153 // Clone a function object that is not location-invariant or
154 // that cannot fit into an _Any_data structure.
155 static void
156 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
157 {
158 __dest._M_access<_Functor*>() =
159 new _Functor(*__source._M_access<const _Functor*>());
160 }
161
162 // Destroying a location-invariant object may still require
163 // destruction.
164 static void
165 _M_destroy(_Any_data& __victim, true_type)
166 {
167 __victim._M_access<_Functor>().~_Functor();
168 }
169
170 // Destroying an object located on the heap.
171 static void
172 _M_destroy(_Any_data& __victim, false_type)
173 {
174 delete __victim._M_access<_Functor*>();
175 }
176
177 public:
178 static bool
179 _M_manager(_Any_data& __dest, const _Any_data& __source,
180 _Manager_operation __op)
181 {
182 switch (__op)
183 {
184 case __get_type_info:
185#if __cpp_rtti
186 __dest._M_access<const type_info*>() = &typeid(_Functor);
187#else
188 __dest._M_access<const type_info*>() = nullptr;
189#endif
190 break;
191 case __get_functor_ptr:
192 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
193 break;
194
195 case __clone_functor:
196 _M_clone(__dest, __source, _Local_storage());
197 break;
198
199 case __destroy_functor:
200 _M_destroy(__dest, _Local_storage());
201 break;
202 }
203 return false;
204 }
205
206 static void
207 _M_init_functor(_Any_data& __functor, _Functor&& __f)
208 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
209
210 template<typename _Signature>
211 static bool
212 _M_not_empty_function(const function<_Signature>& __f)
213 { return static_cast<bool>(__f); }
214
215 template<typename _Tp>
216 static bool
217 _M_not_empty_function(_Tp* __fp)
218 { return __fp != nullptr; }
219
220 template<typename _Class, typename _Tp>
221 static bool
222 _M_not_empty_function(_Tp _Class::* __mp)
223 { return __mp != nullptr; }
224
225 template<typename _Tp>
226 static bool
227 _M_not_empty_function(const _Tp&)
228 { return true; }
229
230 private:
231 static void
232 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
233 { ::new (__functor._M_access()) _Functor(std::move(__f)); }
234
235 static void
236 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
237 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
238 };
239
240 _Function_base() : _M_manager(nullptr) { }
241
242 ~_Function_base()
243 {
244 if (_M_manager)
245 _M_manager(_M_functor, _M_functor, __destroy_functor);
246 }
247
248 bool _M_empty() const { return !_M_manager; }
249
250 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
251 _Manager_operation);
252
253 _Any_data _M_functor;
254 _Manager_type _M_manager;
255 };
256
257 template<typename _Signature, typename _Functor>
258 class _Function_handler;
259
260 template<typename _Res, typename _Functor, typename... _ArgTypes>
261 class _Function_handler<_Res(_ArgTypes...), _Functor>
262 : public _Function_base::_Base_manager<_Functor>
263 {
264 typedef _Function_base::_Base_manager<_Functor> _Base;
265
266 public:
267 static bool
268 _M_manager(_Any_data& __dest, const _Any_data& __source,
269 _Manager_operation __op)
270 {
271 switch (__op)
272 {
273#if __cpp_rtti
274 case __get_type_info:
275 __dest._M_access<const type_info*>() = &typeid(_Functor);
276 break;
277#endif
278 case __get_functor_ptr:
279 __dest._M_access<_Functor*>() = _Base::_M_get_pointer(__source);
280 break;
281
282 default:
283 _Base::_M_manager(__dest, __source, __op);
284 }
285 return false;
286 }
287
288 static _Res
289 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
290 {
291 return std::__invoke_r<_Res>(*_Base::_M_get_pointer(__functor),
292 std::forward<_ArgTypes>(__args)...);
293 }
294 };
295
296 // Specialization for invalid types
297 template<>
298 class _Function_handler<void, void>
299 {
300 public:
301 static bool
302 _M_manager(_Any_data&, const _Any_data&, _Manager_operation)
303 { return false; }
304 };
305
306 // Avoids instantiating ill-formed specializations of _Function_handler
307 // in std::function<_Signature>::target<_Functor>().
308 // e.g. _Function_handler<Sig, void()> and _Function_handler<Sig, void>
309 // would be ill-formed.
310 template<typename _Signature, typename _Functor,
311 bool __valid = is_object<_Functor>::value>
312 struct _Target_handler
313 : _Function_handler<_Signature, typename remove_cv<_Functor>::type>
314 { };
315
316 template<typename _Signature, typename _Functor>
317 struct _Target_handler<_Signature, _Functor, false>
318 : _Function_handler<void, void>
319 { };
320
321 /**
322 * @brief Primary class template for std::function.
323 * @ingroup functors
324 *
325 * Polymorphic function wrapper.
326 */
327 template<typename _Res, typename... _ArgTypes>
328 class function<_Res(_ArgTypes...)>
329 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
330 private _Function_base
331 {
332 template<typename _Func,
333 typename _Res2 = __invoke_result<_Func&, _ArgTypes...>>
334 struct _Callable
335 : __is_invocable_impl<_Res2, _Res>::type
336 { };
337
338 // Used so the return type convertibility checks aren't done when
339 // performing overload resolution for copy construction/assignment.
340 template<typename _Tp>
341 struct _Callable<function, _Tp> : false_type { };
342
343 template<typename _Cond, typename _Tp>
344 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
345
346 public:
347 typedef _Res result_type;
348
349 // [3.7.2.1] construct/copy/destroy
350
351 /**
352 * @brief Default construct creates an empty function call wrapper.
353 * @post @c !(bool)*this
354 */
355 function() noexcept
356 : _Function_base() { }
357
358 /**
359 * @brief Creates an empty function call wrapper.
360 * @post @c !(bool)*this
361 */
362 function(nullptr_t) noexcept
363 : _Function_base() { }
364
365 /**
366 * @brief %Function copy constructor.
367 * @param __x A %function object with identical call signature.
368 * @post @c bool(*this) == bool(__x)
369 *
370 * The newly-created %function contains a copy of the target of @a
371 * __x (if it has one).
372 */
373 function(const function& __x)
374 : _Function_base()
375 {
376 if (static_cast<bool>(__x))
377 {
378 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
379 _M_invoker = __x._M_invoker;
380 _M_manager = __x._M_manager;
381 }
382 }
383
384 /**
385 * @brief %Function move constructor.
386 * @param __x A %function object rvalue with identical call signature.
387 *
388 * The newly-created %function contains the target of @a __x
389 * (if it has one).
390 */
391 function(function&& __x) noexcept
392 : _Function_base(), _M_invoker(__x._M_invoker)
393 {
394 if (static_cast<bool>(__x))
395 {
396 _M_functor = __x._M_functor;
397 _M_manager = __x._M_manager;
398 __x._M_manager = nullptr;
399 __x._M_invoker = nullptr;
400 }
401 }
402
403 /**
404 * @brief Builds a %function that targets a copy of the incoming
405 * function object.
406 * @param __f A %function object that is callable with parameters of
407 * type @c T1, @c T2, ..., @c TN and returns a value convertible
408 * to @c Res.
409 *
410 * The newly-created %function object will target a copy of
411 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
412 * object will contain a reference to the function object @c
413 * __f.get(). If @a __f is a NULL function pointer or NULL
414 * pointer-to-member, the newly-created object will be empty.
415 *
416 * If @a __f is a non-NULL function pointer or an object of type @c
417 * reference_wrapper<F>, this function will not throw.
418 */
419 template<typename _Functor,
420 typename = _Requires<__not_<is_same<_Functor, function>>, void>,
421 typename = _Requires<_Callable<_Functor>, void>>
422 function(_Functor __f)
423 : _Function_base()
424 {
425 typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
426
427 if (_My_handler::_M_not_empty_function(__f))
428 {
429 _My_handler::_M_init_functor(_M_functor, std::move(__f));
430 _M_invoker = &_My_handler::_M_invoke;
431 _M_manager = &_My_handler::_M_manager;
432 }
433 }
434
435 /**
436 * @brief %Function assignment operator.
437 * @param __x A %function with identical call signature.
438 * @post @c (bool)*this == (bool)x
439 * @returns @c *this
440 *
441 * The target of @a __x is copied to @c *this. If @a __x has no
442 * target, then @c *this will be empty.
443 *
444 * If @a __x targets a function pointer or a reference to a function
445 * object, then this operation will not throw an %exception.
446 */
447 function&
448 operator=(const function& __x)
449 {
450 function(__x).swap(*this);
451 return *this;
452 }
453
454 /**
455 * @brief %Function move-assignment operator.
456 * @param __x A %function rvalue with identical call signature.
457 * @returns @c *this
458 *
459 * The target of @a __x is moved to @c *this. If @a __x has no
460 * target, then @c *this will be empty.
461 *
462 * If @a __x targets a function pointer or a reference to a function
463 * object, then this operation will not throw an %exception.
464 */
465 function&
466 operator=(function&& __x) noexcept
467 {
468 function(std::move(__x)).swap(*this);
469 return *this;
470 }
471
472 /**
473 * @brief %Function assignment to zero.
474 * @post @c !(bool)*this
475 * @returns @c *this
476 *
477 * The target of @c *this is deallocated, leaving it empty.
478 */
479 function&
480 operator=(nullptr_t) noexcept
481 {
482 if (_M_manager)
483 {
484 _M_manager(_M_functor, _M_functor, __destroy_functor);
485 _M_manager = nullptr;
486 _M_invoker = nullptr;
487 }
488 return *this;
489 }
490
491 /**
492 * @brief %Function assignment to a new target.
493 * @param __f A %function object that is callable with parameters of
494 * type @c T1, @c T2, ..., @c TN and returns a value convertible
495 * to @c Res.
496 * @return @c *this
497 *
498 * This %function object wrapper will target a copy of @a
499 * __f. If @a __f is @c reference_wrapper<F>, then this function
500 * object will contain a reference to the function object @c
501 * __f.get(). If @a __f is a NULL function pointer or NULL
502 * pointer-to-member, @c this object will be empty.
503 *
504 * If @a __f is a non-NULL function pointer or an object of type @c
505 * reference_wrapper<F>, this function will not throw.
506 */
507 template<typename _Functor>
508 _Requires<_Callable<typename decay<_Functor>::type>, function&>
509 operator=(_Functor&& __f)
510 {
511 function(std::forward<_Functor>(__f)).swap(*this);
512 return *this;
513 }
514
515 /// @overload
516 template<typename _Functor>
517 function&
518 operator=(reference_wrapper<_Functor> __f) noexcept
519 {
520 function(__f).swap(*this);
521 return *this;
522 }
523
524 // [3.7.2.2] function modifiers
525
526 /**
527 * @brief Swap the targets of two %function objects.
528 * @param __x A %function with identical call signature.
529 *
530 * Swap the targets of @c this function object and @a __f. This
531 * function will not throw an %exception.
532 */
533 void swap(function& __x) noexcept
534 {
535 std::swap(_M_functor, __x._M_functor);
536 std::swap(_M_manager, __x._M_manager);
537 std::swap(_M_invoker, __x._M_invoker);
538 }
539
540 // [3.7.2.3] function capacity
541
542 /**
543 * @brief Determine if the %function wrapper has a target.
544 *
545 * @return @c true when this %function object contains a target,
546 * or @c false when it is empty.
547 *
548 * This function will not throw an %exception.
549 */
550 explicit operator bool() const noexcept
551 { return !_M_empty(); }
552
553 // [3.7.2.4] function invocation
554
555 /**
556 * @brief Invokes the function targeted by @c *this.
557 * @returns the result of the target.
558 * @throws bad_function_call when @c !(bool)*this
559 *
560 * The function call operator invokes the target function object
561 * stored by @c this.
562 */
563 _Res
564 operator()(_ArgTypes... __args) const
565 {
566 if (_M_empty())
567 __throw_bad_function_call();
568 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
569 }
570
571#if __cpp_rtti
572 // [3.7.2.5] function target access
573 /**
574 * @brief Determine the type of the target of this function object
575 * wrapper.
576 *
577 * @returns the type identifier of the target function object, or
578 * @c typeid(void) if @c !(bool)*this.
579 *
580 * This function will not throw an %exception.
581 */
582 const type_info&
583 target_type() const noexcept
584 {
585 if (_M_manager)
586 {
587 _Any_data __typeinfo_result;
588 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
589 if (auto __ti = __typeinfo_result._M_access<const type_info*>())
590 return *__ti;
591 }
592 return typeid(void);
593 }
594#endif
595
596 /**
597 * @brief Access the stored target function object.
598 *
599 * @return Returns a pointer to the stored target function object,
600 * if @c typeid(_Functor).equals(target_type()); otherwise, a null
601 * pointer.
602 *
603 * This function does not throw exceptions.
604 *
605 * @{
606 */
607 template<typename _Functor>
608 _Functor*
609 target() noexcept
610 {
611 const function* __const_this = this;
612 const _Functor* __func = __const_this->template target<_Functor>();
613 // If is_function_v<_Functor> is true then const_cast<_Functor*>
614 // would be ill-formed, so use *const_cast<_Functor**> instead.
615 return *const_cast<_Functor**>(&__func);
616 }
617
618 template<typename _Functor>
619 const _Functor*
620 target() const noexcept
621 {
622 if _GLIBCXX17_CONSTEXPR (is_object<_Functor>::value)
623 {
624 // For C++11 and C++14 if-constexpr is not used above, so
625 // _Target_handler avoids ill-formed _Function_handler types.
626 using _Handler = _Target_handler<_Res(_ArgTypes...), _Functor>;
627
628 if (_M_manager == &_Handler::_M_manager
629#if __cpp_rtti
630 || (_M_manager && typeid(_Functor) == target_type())
631#endif
632 )
633 {
634 _Any_data __ptr;
635 _M_manager(__ptr, _M_functor, __get_functor_ptr);
636 return __ptr._M_access<const _Functor*>();
637 }
638 }
639 return nullptr;
640 }
641 /// @}
642
643 private:
644 using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
645 _Invoker_type _M_invoker;
646 };
647
648#if __cpp_deduction_guides >= 201606
649 template<typename>
650 struct __function_guide_helper
651 { };
652
653 template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
654 struct __function_guide_helper<
655 _Res (_Tp::*) (_Args...) noexcept(_Nx)
656 >
657 { using type = _Res(_Args...); };
658
659 template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
660 struct __function_guide_helper<
661 _Res (_Tp::*) (_Args...) & noexcept(_Nx)
662 >
663 { using type = _Res(_Args...); };
664
665 template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
666 struct __function_guide_helper<
667 _Res (_Tp::*) (_Args...) const noexcept(_Nx)
668 >
669 { using type = _Res(_Args...); };
670
671 template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
672 struct __function_guide_helper<
673 _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
674 >
675 { using type = _Res(_Args...); };
676
677 template<typename _Res, typename... _ArgTypes>
678 function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
679
680 template<typename _Functor, typename _Signature = typename
681 __function_guide_helper<decltype(&_Functor::operator())>::type>
682 function(_Functor) -> function<_Signature>;
683#endif
684
685 // [20.7.15.2.6] null pointer comparisons
686
687 /**
688 * @brief Compares a polymorphic function object wrapper against 0
689 * (the NULL pointer).
690 * @returns @c true if the wrapper has no target, @c false otherwise
691 *
692 * This function will not throw an %exception.
693 */
694 template<typename _Res, typename... _Args>
695 inline bool
696 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
697 { return !static_cast<bool>(__f); }
698
699#if __cpp_impl_three_way_comparison < 201907L
700 /// @overload
701 template<typename _Res, typename... _Args>
702 inline bool
703 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
704 { return !static_cast<bool>(__f); }
705
706 /**
707 * @brief Compares a polymorphic function object wrapper against 0
708 * (the NULL pointer).
709 * @returns @c false if the wrapper has no target, @c true otherwise
710 *
711 * This function will not throw an %exception.
712 */
713 template<typename _Res, typename... _Args>
714 inline bool
715 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
716 { return static_cast<bool>(__f); }
717
718 /// @overload
719 template<typename _Res, typename... _Args>
720 inline bool
721 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
722 { return static_cast<bool>(__f); }
723#endif
724
725 // [20.7.15.2.7] specialized algorithms
726
727 /**
728 * @brief Swap the targets of two polymorphic function object wrappers.
729 *
730 * This function will not throw an %exception.
731 */
732 // _GLIBCXX_RESOLVE_LIB_DEFECTS
733 // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
734 template<typename _Res, typename... _Args>
735 inline void
736 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
737 { __x.swap(__y); }
738
739#if __cplusplus >= 201703L
740 namespace __detail::__variant
741 {
742 template<typename> struct _Never_valueless_alt; // see <variant>
743
744 // Provide the strong exception-safety guarantee when emplacing a
745 // function into a variant.
746 template<typename _Signature>
747 struct _Never_valueless_alt<std::function<_Signature>>
748 : std::true_type
749 { };
750 } // namespace __detail::__variant
751#endif // C++17
752
753_GLIBCXX_END_NAMESPACE_VERSION
754} // namespace std
755
756#endif // C++11
757#endif // _GLIBCXX_STD_FUNCTION_H
758