libstdc++
mofunc_impl.h
Go to the documentation of this file.
1// Implementation of std::move_only_function -*- C++ -*-
2
3// Copyright The GNU Toolchain Authors.
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/mofunc_impl.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_MOF_CV
31# define _GLIBCXX_MOF_CV
32#endif
33
34#ifdef _GLIBCXX_MOF_REF
35# define _GLIBCXX_MOF_INV_QUALS _GLIBCXX_MOF_CV _GLIBCXX_MOF_REF
36#else
37# define _GLIBCXX_MOF_REF
38# define _GLIBCXX_MOF_INV_QUALS _GLIBCXX_MOF_CV &
39#endif
40
41#define _GLIBCXX_MOF_CV_REF _GLIBCXX_MOF_CV _GLIBCXX_MOF_REF
42
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 /**
48 * @brief Polymorphic function wrapper.
49 * @ingroup functors
50 * @since C++23
51 * @headerfile functional
52 *
53 * The `std::move_only_function` class template is a call wrapper similar
54 * to `std::function`, but does not require the stored target function
55 * to be copyable.
56 *
57 * It also supports const-qualification, ref-qualification, and
58 * no-throw guarantees. The qualifications and exception-specification
59 * of the `move_only_function::operator()` member function are respected
60 * when invoking the target function.
61 */
62 template<typename _Res, typename... _ArgTypes, bool _Noex>
63 class move_only_function<_Res(_ArgTypes...) _GLIBCXX_MOF_CV
64 _GLIBCXX_MOF_REF noexcept(_Noex)>
65 : __polyfunc::_Mo_base
66 {
67 static_assert(
68 (std::__is_complete_or_unbounded(__type_identity<_ArgTypes>()) && ...),
69 "each parameter type must be a complete class");
70
71 using _Base = __polyfunc::_Mo_base;
72 using _Invoker = __polyfunc::_Invoker<_Noex, _Res, _ArgTypes...>;
73
74 template<typename _Tp>
75 using __callable
76 = __conditional_t<_Noex,
77 is_nothrow_invocable_r<_Res, _Tp, _ArgTypes...>,
78 is_invocable_r<_Res, _Tp, _ArgTypes...>>;
79
80 // [func.wrap.mov.con]/1 is-callable-from<VT>
81 template<typename _Vt>
82 static constexpr bool __is_callable_from
83 = __and_v<__callable<_Vt _GLIBCXX_MOF_CV_REF>,
84 __callable<_Vt _GLIBCXX_MOF_INV_QUALS>>;
85
86 public:
87 using result_type = _Res;
88
89 /// Creates an empty object.
90 move_only_function() noexcept { }
91
92 /// Creates an empty object.
93 move_only_function(nullptr_t) noexcept { }
94
95 /// Moves the target object, leaving the source empty.
97 : _Base(static_cast<_Base&&>(__x)),
98 _M_invoke(std::__exchange(__x._M_invoke, nullptr))
99 { }
100
101 /// Stores a target object initialized from the argument.
102 template<typename _Fn, typename _Vt = decay_t<_Fn>>
103 requires (!is_same_v<_Vt, move_only_function>)
104 && (!__is_in_place_type_v<_Vt>) && __is_callable_from<_Vt>
105 move_only_function(_Fn&& __f) noexcept(_S_nothrow_init<_Vt, _Fn>())
106 {
107 // _GLIBCXX_RESOLVE_LIB_DEFECTS
108 // 4255. move_only_function constructor should recognize empty
109 // copyable_functions
110 if constexpr (is_function_v<remove_pointer_t<_Vt>>
111 || is_member_pointer_v<_Vt>
112 || __is_polymorphic_function_v<_Vt>)
113 {
114 if (__f == nullptr)
115 return;
116 }
117
118 if constexpr (__is_polymorphic_function_v<_Vt>
119 && __polyfunc::__is_invoker_convertible<_Vt, move_only_function>())
120 {
121 // Handle cases where _Fn is const reference to copyable_function,
122 // by firstly creating temporary and moving from it.
123 _Vt __tmp(std::forward<_Fn>(__f));
124 _M_move(__polyfunc::__base_of(__tmp));
125 _M_invoke = std::__exchange(__polyfunc::__invoker_of(__tmp), nullptr);
126 }
127 else
128 {
129 _M_init<_Vt>(std::forward<_Fn>(__f));
130 _M_invoke = _Invoker::template _S_storage<_Vt _GLIBCXX_MOF_INV_QUALS>();
131 }
132 }
133
134 /// Stores a target object initialized from the arguments.
135 template<typename _Tp, typename... _Args>
136 requires is_constructible_v<_Tp, _Args...>
137 && __is_callable_from<_Tp>
138 explicit
139 move_only_function(in_place_type_t<_Tp>, _Args&&... __args)
140 noexcept(_S_nothrow_init<_Tp, _Args...>())
141 : _M_invoke(_Invoker::template _S_storage<_Tp _GLIBCXX_MOF_INV_QUALS>())
142 {
143 static_assert(is_same_v<decay_t<_Tp>, _Tp>);
144 _M_init<_Tp>(std::forward<_Args>(__args)...);
145 }
146
147 /// Stores a target object initialized from the arguments.
148 template<typename _Tp, typename _Up, typename... _Args>
149 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
150 && __is_callable_from<_Tp>
151 explicit
152 move_only_function(in_place_type_t<_Tp>, initializer_list<_Up> __il,
153 _Args&&... __args)
154 noexcept(_S_nothrow_init<_Tp, initializer_list<_Up>&, _Args...>())
155 : _M_invoke(_Invoker::template _S_storage<_Tp _GLIBCXX_MOF_INV_QUALS>())
156 {
157 static_assert(is_same_v<decay_t<_Tp>, _Tp>);
158 _M_init<_Tp>(__il, std::forward<_Args>(__args)...);
159 }
160
161 /// Stores a new target object, leaving `x` empty.
163 operator=(move_only_function&& __x) noexcept
164 {
165 // Standard requires support of self assigment, by specifying it as
166 // copy and swap.
167 if (this != std::addressof(__x)) [[likely]]
168 {
169 _Base::operator=(static_cast<_Base&&>(__x));
170 _M_invoke = std::__exchange(__x._M_invoke, nullptr);
171 }
172 return *this;
173 }
174
175 /// Destroys the target object (if any).
177 operator=(nullptr_t) noexcept
178 {
179 _M_reset();
180 _M_invoke = nullptr;
181 return *this;
182 }
183
184 /// Stores a new target object, initialized from the argument.
185 template<typename _Fn>
186 requires is_constructible_v<move_only_function, _Fn>
188 operator=(_Fn&& __f)
189 noexcept(is_nothrow_constructible_v<move_only_function, _Fn>)
190 {
191 move_only_function(std::forward<_Fn>(__f)).swap(*this);
192 return *this;
193 }
194
195 ~move_only_function() = default;
196
197 /// True if a target object is present, false otherwise.
198 explicit operator bool() const noexcept
199 { return _M_invoke != nullptr; }
200
201 /** Invoke the target object.
202 *
203 * The target object will be invoked using the supplied arguments,
204 * and as an lvalue or rvalue, and as const or non-const, as dictated
205 * by the template arguments of the `move_only_function` specialization.
206 *
207 * @pre Must not be empty.
208 */
209 _Res
210 operator()(_ArgTypes... __args) _GLIBCXX_MOF_CV_REF noexcept(_Noex)
211 {
212 __glibcxx_assert(*this != nullptr);
213 return _M_invoke(this->_M_storage, std::forward<_ArgTypes>(__args)...);
214 }
215
216 /// Exchange the target objects (if any).
217 void
218 swap(move_only_function& __x) noexcept
219 {
220 _Base::swap(__x);
221 std::swap(_M_invoke, __x._M_invoke);
222 }
223
224 /// Exchange the target objects (if any).
225 friend void
226 swap(move_only_function& __x, move_only_function& __y) noexcept
227 { __x.swap(__y); }
228
229 /// Check for emptiness by comparing with `nullptr`.
230 friend bool
231 operator==(const move_only_function& __x, nullptr_t) noexcept
232 { return __x._M_invoke == nullptr; }
233
234 private:
235 typename _Invoker::__storage_func_t _M_invoke = nullptr;
236
237 template<typename _Func>
238 friend constexpr auto&
239 __polyfunc::__invoker_of(_Func&) noexcept;
240
241 template<typename _Func>
242 friend constexpr auto&
243 __polyfunc::__base_of(_Func&) noexcept;
244
245 template<typename _Src, typename _Dst>
246 friend consteval bool
247 __polyfunc::__is_invoker_convertible() noexcept;
248 };
249
250#undef _GLIBCXX_MOF_CV_REF
251#undef _GLIBCXX_MOF_CV
252#undef _GLIBCXX_MOF_REF
253#undef _GLIBCXX_MOF_INV_QUALS
254
255_GLIBCXX_END_NAMESPACE_VERSION
256} // namespace std
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
Definition move.h:176
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
ISO C++ entities toplevel namespace is std.
initializer_list
_Res operator()(_ArgTypes... __args) _GLIBCXX_MOF_CV noexcept(_Noex)
move_only_function & operator=(move_only_function &&__x) noexcept
Stores a new target object, leaving x empty.
friend bool operator==(const move_only_function &__x, nullptr_t) noexcept
Check for emptiness by comparing with nullptr.
void swap(move_only_function &__x) noexcept
Exchange the target objects (if any).