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