]>
Commit | Line | Data |
---|---|---|
acb8a4ef | 1 | // auto_ptr implementation -*- C++ -*- |
b2c50382 | 2 | |
83ffe9cd | 3 | // Copyright (C) 2007-2023 Free Software Foundation, Inc. |
b2c50382 PC |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
b2c50382 PC |
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 | ||
748086b7 JJ |
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. | |
b2c50382 | 19 | |
748086b7 JJ |
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/>. | |
b2c50382 | 24 | |
40abbf1f | 25 | /** @file backward/auto_ptr.h |
b2c50382 | 26 | * This is an internal header file, included by other library headers. |
30f276c1 | 27 | * Do not attempt to use it directly. @headername{memory} |
b2c50382 PC |
28 | */ |
29 | ||
5ecfce5c PC |
30 | #ifndef _BACKWARD_AUTO_PTR_H |
31 | #define _BACKWARD_AUTO_PTR_H 1 | |
b2c50382 | 32 | |
acb8a4ef | 33 | #include <bits/c++config.h> |
b2c50382 | 34 | #include <debug/debug.h> |
b2c50382 | 35 | |
12ffa228 BK |
36 | namespace std _GLIBCXX_VISIBILITY(default) |
37 | { | |
38 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
b2c50382 | 39 | |
b2c50382 PC |
40 | /** |
41 | * A wrapper class to provide auto_ptr with reference semantics. | |
42 | * For example, an auto_ptr can be assigned (or constructed from) | |
43 | * the result of a function which returns an auto_ptr by value. | |
44 | * | |
45 | * All the auto_ptr_ref stuff should happen behind the scenes. | |
46 | */ | |
47 | template<typename _Tp1> | |
48 | struct auto_ptr_ref | |
49 | { | |
50 | _Tp1* _M_ptr; | |
51 | ||
52 | explicit | |
53 | auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { } | |
eef9bf4c | 54 | } _GLIBCXX11_DEPRECATED; |
b2c50382 | 55 | |
a2c0a194 JW |
56 | #pragma GCC diagnostic push |
57 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
b2c50382 PC |
58 | |
59 | /** | |
60 | * @brief A simple smart pointer providing strict ownership semantics. | |
61 | * | |
62 | * The Standard says: | |
63 | * <pre> | |
64 | * An @c auto_ptr owns the object it holds a pointer to. Copying | |
65 | * an @c auto_ptr copies the pointer and transfers ownership to the | |
66 | * destination. If more than one @c auto_ptr owns the same object | |
67 | * at the same time the behavior of the program is undefined. | |
68 | * | |
69 | * The uses of @c auto_ptr include providing temporary | |
70 | * exception-safety for dynamically allocated memory, passing | |
71 | * ownership of dynamically allocated memory to a function, and | |
72 | * returning dynamically allocated memory from a function. @c | |
73 | * auto_ptr does not meet the CopyConstructible and Assignable | |
74 | * requirements for Standard Library <a | |
75 | * href="tables.html#65">container</a> elements and thus | |
76 | * instantiating a Standard Library container with an @c auto_ptr | |
77 | * results in undefined behavior. | |
78 | * </pre> | |
79 | * Quoted from [20.4.5]/3. | |
80 | * | |
81 | * Good examples of what can and cannot be done with auto_ptr can | |
82 | * be found in the libstdc++ testsuite. | |
83 | * | |
b2c50382 PC |
84 | * _GLIBCXX_RESOLVE_LIB_DEFECTS |
85 | * 127. auto_ptr<> conversion issues | |
86 | * These resolutions have all been incorporated. | |
a2784022 JW |
87 | * |
88 | * @headerfile memory | |
89 | * @deprecated Deprecated in C++11, no longer in the standard since C++17. | |
90 | * Use `unique_ptr` instead. | |
b2c50382 PC |
91 | */ |
92 | template<typename _Tp> | |
93 | class auto_ptr | |
94 | { | |
95 | private: | |
96 | _Tp* _M_ptr; | |
97 | ||
98 | public: | |
99 | /// The pointed-to type. | |
100 | typedef _Tp element_type; | |
101 | ||
102 | /** | |
103 | * @brief An %auto_ptr is usually constructed from a raw pointer. | |
93c66bc6 | 104 | * @param __p A pointer (defaults to NULL). |
b2c50382 | 105 | * |
93c66bc6 | 106 | * This object now @e owns the object pointed to by @a __p. |
b2c50382 PC |
107 | */ |
108 | explicit | |
109 | auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { } | |
110 | ||
111 | /** | |
112 | * @brief An %auto_ptr can be constructed from another %auto_ptr. | |
93c66bc6 | 113 | * @param __a Another %auto_ptr of the same type. |
b2c50382 | 114 | * |
93c66bc6 | 115 | * This object now @e owns the object previously owned by @a __a, |
28dac70a | 116 | * which has given up ownership. |
b2c50382 PC |
117 | */ |
118 | auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { } | |
119 | ||
120 | /** | |
121 | * @brief An %auto_ptr can be constructed from another %auto_ptr. | |
93c66bc6 | 122 | * @param __a Another %auto_ptr of a different but related type. |
b2c50382 PC |
123 | * |
124 | * A pointer-to-Tp1 must be convertible to a | |
125 | * pointer-to-Tp/element_type. | |
126 | * | |
93c66bc6 | 127 | * This object now @e owns the object previously owned by @a __a, |
28dac70a | 128 | * which has given up ownership. |
b2c50382 PC |
129 | */ |
130 | template<typename _Tp1> | |
131 | auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { } | |
132 | ||
133 | /** | |
134 | * @brief %auto_ptr assignment operator. | |
93c66bc6 | 135 | * @param __a Another %auto_ptr of the same type. |
b2c50382 | 136 | * |
93c66bc6 | 137 | * This object now @e owns the object previously owned by @a __a, |
28dac70a | 138 | * which has given up ownership. The object that this one @e |
b2c50382 PC |
139 | * used to own and track has been deleted. |
140 | */ | |
141 | auto_ptr& | |
142 | operator=(auto_ptr& __a) throw() | |
143 | { | |
144 | reset(__a.release()); | |
145 | return *this; | |
146 | } | |
147 | ||
148 | /** | |
149 | * @brief %auto_ptr assignment operator. | |
93c66bc6 | 150 | * @param __a Another %auto_ptr of a different but related type. |
b2c50382 PC |
151 | * |
152 | * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type. | |
153 | * | |
93c66bc6 | 154 | * This object now @e owns the object previously owned by @a __a, |
28dac70a | 155 | * which has given up ownership. The object that this one @e |
b2c50382 PC |
156 | * used to own and track has been deleted. |
157 | */ | |
158 | template<typename _Tp1> | |
159 | auto_ptr& | |
160 | operator=(auto_ptr<_Tp1>& __a) throw() | |
161 | { | |
162 | reset(__a.release()); | |
163 | return *this; | |
164 | } | |
165 | ||
166 | /** | |
167 | * When the %auto_ptr goes out of scope, the object it owns is | |
168 | * deleted. If it no longer owns anything (i.e., @c get() is | |
169 | * @c NULL), then this has no effect. | |
170 | * | |
b2c50382 PC |
171 | * The C++ standard says there is supposed to be an empty throw |
172 | * specification here, but omitting it is standard conforming. Its | |
173 | * presence can be detected only if _Tp::~_Tp() throws, but this is | |
174 | * prohibited. [17.4.3.6]/2 | |
b2c50382 PC |
175 | */ |
176 | ~auto_ptr() { delete _M_ptr; } | |
177 | ||
178 | /** | |
179 | * @brief Smart pointer dereferencing. | |
180 | * | |
181 | * If this %auto_ptr no longer owns anything, then this | |
2a60a9f6 BK |
182 | * operation will crash. (For a smart pointer, <em>no longer owns |
183 | * anything</em> is the same as being a null pointer, and you know | |
b2c50382 PC |
184 | * what happens when you dereference one of those...) |
185 | */ | |
186 | element_type& | |
187 | operator*() const throw() | |
188 | { | |
2f1e8e7c | 189 | __glibcxx_assert(_M_ptr != 0); |
b2c50382 PC |
190 | return *_M_ptr; |
191 | } | |
192 | ||
193 | /** | |
194 | * @brief Smart pointer dereferencing. | |
195 | * | |
196 | * This returns the pointer itself, which the language then will | |
197 | * automatically cause to be dereferenced. | |
198 | */ | |
199 | element_type* | |
200 | operator->() const throw() | |
201 | { | |
2f1e8e7c | 202 | __glibcxx_assert(_M_ptr != 0); |
b2c50382 PC |
203 | return _M_ptr; |
204 | } | |
205 | ||
206 | /** | |
207 | * @brief Bypassing the smart pointer. | |
208 | * @return The raw pointer being managed. | |
209 | * | |
210 | * You can get a copy of the pointer that this object owns, for | |
211 | * situations such as passing to a function which only accepts | |
212 | * a raw pointer. | |
213 | * | |
214 | * @note This %auto_ptr still owns the memory. | |
215 | */ | |
216 | element_type* | |
217 | get() const throw() { return _M_ptr; } | |
218 | ||
219 | /** | |
220 | * @brief Bypassing the smart pointer. | |
221 | * @return The raw pointer being managed. | |
222 | * | |
223 | * You can get a copy of the pointer that this object owns, for | |
224 | * situations such as passing to a function which only accepts | |
225 | * a raw pointer. | |
226 | * | |
227 | * @note This %auto_ptr no longer owns the memory. When this object | |
228 | * goes out of scope, nothing will happen. | |
229 | */ | |
230 | element_type* | |
231 | release() throw() | |
232 | { | |
233 | element_type* __tmp = _M_ptr; | |
234 | _M_ptr = 0; | |
235 | return __tmp; | |
236 | } | |
237 | ||
238 | /** | |
239 | * @brief Forcibly deletes the managed object. | |
93c66bc6 | 240 | * @param __p A pointer (defaults to NULL). |
b2c50382 | 241 | * |
93c66bc6 | 242 | * This object now @e owns the object pointed to by @a __p. The |
b2c50382 PC |
243 | * previous object has been deleted. |
244 | */ | |
245 | void | |
246 | reset(element_type* __p = 0) throw() | |
247 | { | |
248 | if (__p != _M_ptr) | |
249 | { | |
250 | delete _M_ptr; | |
251 | _M_ptr = __p; | |
252 | } | |
253 | } | |
254 | ||
255 | /** | |
256 | * @brief Automatic conversions | |
257 | * | |
181f50b6 JW |
258 | * These operations are supposed to convert an %auto_ptr into and from |
259 | * an auto_ptr_ref automatically as needed. This would allow | |
260 | * constructs such as | |
b2c50382 PC |
261 | * @code |
262 | * auto_ptr<Derived> func_returning_auto_ptr(.....); | |
263 | * ... | |
264 | * auto_ptr<Base> ptr = func_returning_auto_ptr(.....); | |
265 | * @endcode | |
181f50b6 JW |
266 | * |
267 | * But it doesn't work, and won't be fixed. For further details see | |
268 | * http://cplusplus.github.io/LWG/lwg-closed.html#463 | |
b2c50382 PC |
269 | */ |
270 | auto_ptr(auto_ptr_ref<element_type> __ref) throw() | |
271 | : _M_ptr(__ref._M_ptr) { } | |
272 | ||
273 | auto_ptr& | |
274 | operator=(auto_ptr_ref<element_type> __ref) throw() | |
275 | { | |
276 | if (__ref._M_ptr != this->get()) | |
277 | { | |
278 | delete _M_ptr; | |
279 | _M_ptr = __ref._M_ptr; | |
280 | } | |
281 | return *this; | |
282 | } | |
283 | ||
284 | template<typename _Tp1> | |
285 | operator auto_ptr_ref<_Tp1>() throw() | |
286 | { return auto_ptr_ref<_Tp1>(this->release()); } | |
287 | ||
288 | template<typename _Tp1> | |
289 | operator auto_ptr<_Tp1>() throw() | |
290 | { return auto_ptr<_Tp1>(this->release()); } | |
eef9bf4c | 291 | } _GLIBCXX11_DEPRECATED_SUGGEST("std::unique_ptr"); |
b2c50382 PC |
292 | |
293 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
294 | // 541. shared_ptr template assignment and void | |
295 | template<> | |
296 | class auto_ptr<void> | |
297 | { | |
298 | public: | |
299 | typedef void element_type; | |
eef9bf4c | 300 | } _GLIBCXX11_DEPRECATED; |
b2c50382 | 301 | |
734f5023 | 302 | #if __cplusplus >= 201103L |
cf0fded5 | 303 | #if _GLIBCXX_HOSTED |
6e48db73 JW |
304 | template<_Lock_policy _Lp> |
305 | template<typename _Tp> | |
306 | inline | |
307 | __shared_count<_Lp>::__shared_count(std::auto_ptr<_Tp>&& __r) | |
308 | : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get())) | |
309 | { __r.release(); } | |
310 | ||
311 | template<typename _Tp, _Lock_policy _Lp> | |
a2e0054e | 312 | template<typename _Tp1, typename> |
6e48db73 JW |
313 | inline |
314 | __shared_ptr<_Tp, _Lp>::__shared_ptr(std::auto_ptr<_Tp1>&& __r) | |
315 | : _M_ptr(__r.get()), _M_refcount() | |
316 | { | |
317 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) | |
318 | static_assert( sizeof(_Tp1) > 0, "incomplete type" ); | |
319 | _Tp1* __tmp = __r.get(); | |
320 | _M_refcount = __shared_count<_Lp>(std::move(__r)); | |
f3070dab | 321 | _M_enable_shared_from_this_with(__tmp); |
6e48db73 JW |
322 | } |
323 | ||
324 | template<typename _Tp> | |
a2e0054e | 325 | template<typename _Tp1, typename> |
6e48db73 JW |
326 | inline |
327 | shared_ptr<_Tp>::shared_ptr(std::auto_ptr<_Tp1>&& __r) | |
328 | : __shared_ptr<_Tp>(std::move(__r)) { } | |
cf0fded5 | 329 | #endif // HOSTED |
6e48db73 JW |
330 | |
331 | template<typename _Tp, typename _Dp> | |
332 | template<typename _Up, typename> | |
333 | inline | |
334 | unique_ptr<_Tp, _Dp>::unique_ptr(auto_ptr<_Up>&& __u) noexcept | |
335 | : _M_t(__u.release(), deleter_type()) { } | |
cf0fded5 | 336 | #endif // C++11 |
6e48db73 | 337 | |
a2c0a194 JW |
338 | #pragma GCC diagnostic pop |
339 | ||
12ffa228 BK |
340 | _GLIBCXX_END_NAMESPACE_VERSION |
341 | } // namespace | |
b2c50382 | 342 | |
5ecfce5c | 343 | #endif /* _BACKWARD_AUTO_PTR_H */ |