libstdc++
cpyfunc_impl.h
Go to the documentation of this file.
1// Implementation of std::copyable_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/cpyfunc_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 copyable function wrapper.
49 * @ingroup functors
50 * @since C++26
51 * @headerfile functional
52 *
53 * The `std::copyable_function` class template is a call wrapper similar
54 * to `std::function`, but it does not provide information about it's
55 * target, and preserves constness.
56 *
57 * It also supports const-qualification, ref-qualification, and
58 * no-throw guarantees. The qualifications and exception-specification
59 * of the `copyable_function::operator()` member function are respected
60 * when invoking the target function.
61 */
62 template<typename _Res, typename... _ArgTypes, bool _Noex>
63 class copyable_function<_Res(_ArgTypes...) _GLIBCXX_MOF_CV
64 _GLIBCXX_MOF_REF noexcept(_Noex)>
65 : __polyfunc::_Cpy_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::_Cpy_base;
72 using _Invoker = __polyfunc::_Invoker<_Noex, _Res, _ArgTypes...>;
73 using _Signature = _Invoker::_Signature;
74
75 template<typename _Tp>
76 using __callable
77 = __conditional_t<_Noex,
78 is_nothrow_invocable_r<_Res, _Tp, _ArgTypes...>,
79 is_invocable_r<_Res, _Tp, _ArgTypes...>>;
80
81 // [func.wrap.copy.con]/1 is-callable-from<VT>
82 template<typename _Vt>
83 static constexpr bool __is_callable_from
84 = __and_v<__callable<_Vt _GLIBCXX_MOF_CV_REF>,
85 __callable<_Vt _GLIBCXX_MOF_INV_QUALS>>;
86
87 public:
88 using result_type = _Res;
89
90 /// Creates an empty object.
91 copyable_function() noexcept { }
92
93 /// Creates an empty object.
94 copyable_function(nullptr_t) noexcept { }
95
96 /// Moves the target object, leaving the source empty.
98 : _Base(static_cast<_Base&&>(__x)),
99 _M_invoke(std::__exchange(__x._M_invoke, nullptr))
100 { }
101
102 /// Copies the target object.
104 : _Base(static_cast<const _Base&>(__x)),
105 _M_invoke(__x._M_invoke)
106 { }
107
108 /// Stores a target object initialized from the argument.
109 template<typename _Fn, typename _Vt = decay_t<_Fn>>
110 requires (!is_same_v<_Vt, copyable_function>)
111 && (!__is_in_place_type_v<_Vt>) && __is_callable_from<_Vt>
112 copyable_function(_Fn&& __f) noexcept(_S_nothrow_init<_Vt, _Fn>())
113 {
114 static_assert(is_copy_constructible_v<_Vt>);
115 if constexpr (is_function_v<remove_pointer_t<_Vt>>
116 || is_member_pointer_v<_Vt>
117 || __is_polymorphic_function_v<_Vt>)
118 {
119 if (__f == nullptr)
120 return;
121 }
122
123 if constexpr (!__is_polymorphic_function_v<_Vt>
124 || !__polyfunc::__is_invoker_convertible<_Vt, copyable_function>())
125 {
126 _M_init<_Vt>(std::forward<_Fn>(__f));
127 _M_invoke = _Invoker::template _S_storage<_Vt _GLIBCXX_MOF_INV_QUALS>();
128 }
129 else if constexpr (is_lvalue_reference_v<_Fn>)
130 {
131 _M_copy(__polyfunc::__base_of(__f));
132 _M_invoke = __polyfunc::__invoker_of(__f);
133 }
134 else
135 {
136 _M_move(__polyfunc::__base_of(__f));
137 _M_invoke = std::__exchange(__polyfunc::__invoker_of(__f), nullptr);
138 }
139 }
140
141 /// Stores a target object initialized from the arguments.
142 template<typename _Tp, typename... _Args>
143 requires is_constructible_v<_Tp, _Args...>
144 && __is_callable_from<_Tp>
145 explicit
146 copyable_function(in_place_type_t<_Tp>, _Args&&... __args)
147 noexcept(_S_nothrow_init<_Tp, _Args...>())
148 : _M_invoke(_Invoker::template _S_storage<_Tp _GLIBCXX_MOF_INV_QUALS>())
149 {
150 static_assert(is_same_v<decay_t<_Tp>, _Tp>);
151 static_assert(is_copy_constructible_v<_Tp>);
152 _M_init<_Tp>(std::forward<_Args>(__args)...);
153 }
154
155 /// Stores a target object initialized from the arguments.
156 template<typename _Tp, typename _Up, typename... _Args>
157 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
158 && __is_callable_from<_Tp>
159 explicit
160 copyable_function(in_place_type_t<_Tp>, initializer_list<_Up> __il,
161 _Args&&... __args)
162 noexcept(_S_nothrow_init<_Tp, initializer_list<_Up>&, _Args...>())
163 : _M_invoke(_Invoker::template _S_storage<_Tp _GLIBCXX_MOF_INV_QUALS>())
164 {
165 static_assert(is_same_v<decay_t<_Tp>, _Tp>);
166 static_assert(is_copy_constructible_v<_Tp>);
167 _M_init<_Tp>(__il, std::forward<_Args>(__args)...);
168 }
169
170 /// Stores a new target object, leaving `x` empty.
172 operator=(copyable_function&& __x) noexcept
173 {
174 // Standard requires support of self assigment, by specifying it as
175 // copy and swap.
176 if (this != std::addressof(__x)) [[likely]]
177 {
178 _Base::operator=(static_cast<_Base&&>(__x));
179 _M_invoke = std::__exchange(__x._M_invoke, nullptr);
180 }
181 return *this;
182 }
183
184 /// Stores a copy of the source target object
186 operator=(const copyable_function& __x)
187 {
188 copyable_function(__x).swap(*this);
189 return *this;
190 }
191
192 /// Destroys the target object (if any).
194 operator=(nullptr_t) noexcept
195 {
196 _M_reset();
197 _M_invoke = nullptr;
198 return *this;
199 }
200
201 /// Stores a new target object, initialized from the argument.
202 template<typename _Fn>
203 requires is_constructible_v<copyable_function, _Fn>
205 operator=(_Fn&& __f)
206 noexcept(is_nothrow_constructible_v<copyable_function, _Fn>)
207 {
208 copyable_function(std::forward<_Fn>(__f)).swap(*this);
209 return *this;
210 }
211
212 ~copyable_function() = default;
213
214 /// True if a target object is present, false otherwise.
215 explicit operator bool() const noexcept
216 { return _M_invoke != nullptr; }
217
218 /** Invoke the target object.
219 *
220 * The target object will be invoked using the supplied arguments,
221 * and as an lvalue or rvalue, and as const or non-const, as dictated
222 * by the template arguments of the `copyable_function` specialization.
223 *
224 * @pre Must not be empty.
225 */
226 _Res
227 operator()(_ArgTypes... __args) _GLIBCXX_MOF_CV_REF noexcept(_Noex)
228 {
229 __glibcxx_assert(*this != nullptr);
230 return _M_invoke(this->_M_storage, std::forward<_ArgTypes>(__args)...);
231 }
232
233 /// Exchange the target objects (if any).
234 void
235 swap(copyable_function& __x) noexcept
236 {
237 _Base::swap(__x);
238 std::swap(_M_invoke, __x._M_invoke);
239 }
240
241 /// Exchange the target objects (if any).
242 friend void
243 swap(copyable_function& __x, copyable_function& __y) noexcept
244 { __x.swap(__y); }
245
246 /// Check for emptiness by comparing with `nullptr`.
247 friend bool
248 operator==(const copyable_function& __x, nullptr_t) noexcept
249 { return __x._M_invoke == nullptr; }
250
251 private:
252 typename _Invoker::__storage_func_t _M_invoke = nullptr;
253
254 template<typename _Func>
255 friend auto&
256 __polyfunc::__invoker_of(_Func&) noexcept;
257
258 template<typename _Func>
259 friend auto&
260 __polyfunc::__base_of(_Func&) noexcept;
261
262 template<typename _Dst, typename _Src>
263 friend consteval bool
264 __polyfunc::__is_invoker_convertible() noexcept;
265 };
266
267#undef _GLIBCXX_MOF_CV_REF
268#undef _GLIBCXX_MOF_CV
269#undef _GLIBCXX_MOF_REF
270#undef _GLIBCXX_MOF_INV_QUALS
271
272_GLIBCXX_END_NAMESPACE_VERSION
273} // 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)
friend bool operator==(const copyable_function &__x, nullptr_t) noexcept
Check for emptiness by comparing with nullptr.
void swap(copyable_function &__x) noexcept
Exchange the target objects (if any).
copyable_function & operator=(copyable_function &&__x) noexcept
Stores a new target object, leaving x empty.