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
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.copy.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 copyable_function() noexcept { }
91
92 /// Creates an empty object.
93 copyable_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 /// Copies the target object.
103 : _Base(static_cast<const _Base&>(__x)),
104 _M_invoke(__x._M_invoke)
105 { }
106
107 /// Stores a target object initialized from the argument.
108 template<typename _Fn, typename _Vt = decay_t<_Fn>>
109 requires (!is_same_v<_Vt, copyable_function>)
110 && (!__is_in_place_type_v<_Vt>) && __is_callable_from<_Vt>
111 copyable_function(_Fn&& __f) noexcept(_S_nothrow_init<_Vt, _Fn>())
112 {
113 static_assert(is_copy_constructible_v<_Vt>);
114 if constexpr (is_function_v<remove_pointer_t<_Vt>>
115 || is_member_pointer_v<_Vt>
116 || __is_polymorphic_function_v<_Vt>)
117 {
118 if (__f == nullptr)
119 return;
120 }
121
122 if constexpr (!__is_polymorphic_function_v<_Vt>
123 || !__polyfunc::__is_invoker_convertible<_Vt, copyable_function>())
124 {
125 _M_init<_Vt>(std::forward<_Fn>(__f));
126 _M_invoke = _Invoker::template _S_storage<_Vt _GLIBCXX_MOF_INV_QUALS>();
127 }
128 else if constexpr (is_lvalue_reference_v<_Fn>)
129 {
130 _M_copy(__polyfunc::__base_of(__f));
131 _M_invoke = __polyfunc::__invoker_of(__f);
132 }
133 else
134 {
135 _M_move(__polyfunc::__base_of(__f));
136 _M_invoke = std::__exchange(__polyfunc::__invoker_of(__f), nullptr);
137 }
138 }
139
140 /// Stores a target object initialized from the arguments.
141 template<typename _Tp, typename... _Args>
142 requires is_constructible_v<_Tp, _Args...>
143 && __is_callable_from<_Tp>
144 explicit
145 copyable_function(in_place_type_t<_Tp>, _Args&&... __args)
146 noexcept(_S_nothrow_init<_Tp, _Args...>())
147 : _M_invoke(_Invoker::template _S_storage<_Tp _GLIBCXX_MOF_INV_QUALS>())
148 {
149 static_assert(is_same_v<decay_t<_Tp>, _Tp>);
150 static_assert(is_copy_constructible_v<_Tp>);
151 _M_init<_Tp>(std::forward<_Args>(__args)...);
152 }
153
154 /// Stores a target object initialized from the arguments.
155 template<typename _Tp, typename _Up, typename... _Args>
156 requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
157 && __is_callable_from<_Tp>
158 explicit
159 copyable_function(in_place_type_t<_Tp>, initializer_list<_Up> __il,
160 _Args&&... __args)
161 noexcept(_S_nothrow_init<_Tp, initializer_list<_Up>&, _Args...>())
162 : _M_invoke(_Invoker::template _S_storage<_Tp _GLIBCXX_MOF_INV_QUALS>())
163 {
164 static_assert(is_same_v<decay_t<_Tp>, _Tp>);
165 static_assert(is_copy_constructible_v<_Tp>);
166 _M_init<_Tp>(__il, std::forward<_Args>(__args)...);
167 }
168
169 /// Stores a new target object, leaving `x` empty.
171 operator=(copyable_function&& __x) noexcept
172 {
173 // Standard requires support of self assigment, by specifying it as
174 // copy and swap.
175 if (this != std::addressof(__x)) [[likely]]
176 {
177 _Base::operator=(static_cast<_Base&&>(__x));
178 _M_invoke = std::__exchange(__x._M_invoke, nullptr);
179 }
180 return *this;
181 }
182
183 /// Stores a copy of the source target object
185 operator=(const copyable_function& __x)
186 {
187 copyable_function(__x).swap(*this);
188 return *this;
189 }
190
191 /// Destroys the target object (if any).
193 operator=(nullptr_t) noexcept
194 {
195 _M_reset();
196 _M_invoke = nullptr;
197 return *this;
198 }
199
200 /// Stores a new target object, initialized from the argument.
201 template<typename _Fn>
202 requires is_constructible_v<copyable_function, _Fn>
204 operator=(_Fn&& __f)
205 noexcept(is_nothrow_constructible_v<copyable_function, _Fn>)
206 {
207 copyable_function(std::forward<_Fn>(__f)).swap(*this);
208 return *this;
209 }
210
211 ~copyable_function() = default;
212
213 /// True if a target object is present, false otherwise.
214 explicit operator bool() const noexcept
215 { return _M_invoke != nullptr; }
216
217 /** Invoke the target object.
218 *
219 * The target object will be invoked using the supplied arguments,
220 * and as an lvalue or rvalue, and as const or non-const, as dictated
221 * by the template arguments of the `copyable_function` specialization.
222 *
223 * @pre Must not be empty.
224 */
225 _Res
226 operator()(_ArgTypes... __args) _GLIBCXX_MOF_CV_REF noexcept(_Noex)
227 {
228 __glibcxx_assert(*this != nullptr);
229 return _M_invoke(this->_M_storage, std::forward<_ArgTypes>(__args)...);
230 }
231
232 /// Exchange the target objects (if any).
233 void
234 swap(copyable_function& __x) noexcept
235 {
236 _Base::swap(__x);
237 std::swap(_M_invoke, __x._M_invoke);
238 }
239
240 /// Exchange the target objects (if any).
241 friend void
242 swap(copyable_function& __x, copyable_function& __y) noexcept
243 { __x.swap(__y); }
244
245 /// Check for emptiness by comparing with `nullptr`.
246 friend bool
247 operator==(const copyable_function& __x, nullptr_t) noexcept
248 { return __x._M_invoke == nullptr; }
249
250 private:
251 typename _Invoker::__storage_func_t _M_invoke = nullptr;
252
253 template<typename _Func>
254 friend constexpr auto&
255 __polyfunc::__invoker_of(_Func&) noexcept;
256
257 template<typename _Func>
258 friend constexpr auto&
259 __polyfunc::__base_of(_Func&) noexcept;
260
261 template<typename _Src, typename _Dst>
262 friend consteval bool
263 __polyfunc::__is_invoker_convertible() noexcept;
264 };
265
266#undef _GLIBCXX_MOF_CV_REF
267#undef _GLIBCXX_MOF_CV
268#undef _GLIBCXX_MOF_REF
269#undef _GLIBCXX_MOF_INV_QUALS
270
271_GLIBCXX_END_NAMESPACE_VERSION
272} // 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.