]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/backward/auto_ptr.h
documentation.html: First pass at unified table of contents.
[thirdparty/gcc.git] / libstdc++-v3 / include / backward / auto_ptr.h
1 // auto_ptr implementation -*- C++ -*-
2
3 // Copyright (C) 2007 Free Software Foundation, Inc.
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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file backward/auto_ptr.h
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
33 */
34
35 #ifndef _STL_AUTO_PTR_H
36 #define _STL_AUTO_PTR_H 1
37
38 #include <bits/c++config.h>
39 #include <debug/debug.h>
40
41 _GLIBCXX_BEGIN_NAMESPACE(std)
42
43 /**
44 * A wrapper class to provide auto_ptr with reference semantics.
45 * For example, an auto_ptr can be assigned (or constructed from)
46 * the result of a function which returns an auto_ptr by value.
47 *
48 * All the auto_ptr_ref stuff should happen behind the scenes.
49 */
50 template<typename _Tp1>
51 struct auto_ptr_ref
52 {
53 _Tp1* _M_ptr;
54
55 explicit
56 auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
57 } _GLIBCXX_DEPRECATED_ATTR;
58
59
60 /**
61 * @brief A simple smart pointer providing strict ownership semantics.
62 *
63 * The Standard says:
64 * <pre>
65 * An @c auto_ptr owns the object it holds a pointer to. Copying
66 * an @c auto_ptr copies the pointer and transfers ownership to the
67 * destination. If more than one @c auto_ptr owns the same object
68 * at the same time the behavior of the program is undefined.
69 *
70 * The uses of @c auto_ptr include providing temporary
71 * exception-safety for dynamically allocated memory, passing
72 * ownership of dynamically allocated memory to a function, and
73 * returning dynamically allocated memory from a function. @c
74 * auto_ptr does not meet the CopyConstructible and Assignable
75 * requirements for Standard Library <a
76 * href="tables.html#65">container</a> elements and thus
77 * instantiating a Standard Library container with an @c auto_ptr
78 * results in undefined behavior.
79 * </pre>
80 * Quoted from [20.4.5]/3.
81 *
82 * Good examples of what can and cannot be done with auto_ptr can
83 * be found in the libstdc++ testsuite.
84 *
85 * @if maint
86 * _GLIBCXX_RESOLVE_LIB_DEFECTS
87 * 127. auto_ptr<> conversion issues
88 * These resolutions have all been incorporated.
89 * @endif
90 */
91 template<typename _Tp>
92 class auto_ptr
93 {
94 private:
95 _Tp* _M_ptr;
96
97 public:
98 /// The pointed-to type.
99 typedef _Tp element_type;
100
101 /**
102 * @brief An %auto_ptr is usually constructed from a raw pointer.
103 * @param p A pointer (defaults to NULL).
104 *
105 * This object now @e owns the object pointed to by @a p.
106 */
107 explicit
108 auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
109
110 /**
111 * @brief An %auto_ptr can be constructed from another %auto_ptr.
112 * @param a Another %auto_ptr of the same type.
113 *
114 * This object now @e owns the object previously owned by @a a,
115 * which has given up ownsership.
116 */
117 auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
118
119 /**
120 * @brief An %auto_ptr can be constructed from another %auto_ptr.
121 * @param a Another %auto_ptr of a different but related type.
122 *
123 * A pointer-to-Tp1 must be convertible to a
124 * pointer-to-Tp/element_type.
125 *
126 * This object now @e owns the object previously owned by @a a,
127 * which has given up ownsership.
128 */
129 template<typename _Tp1>
130 auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
131
132 /**
133 * @brief %auto_ptr assignment operator.
134 * @param a Another %auto_ptr of the same type.
135 *
136 * This object now @e owns the object previously owned by @a a,
137 * which has given up ownsership. The object that this one @e
138 * used to own and track has been deleted.
139 */
140 auto_ptr&
141 operator=(auto_ptr& __a) throw()
142 {
143 reset(__a.release());
144 return *this;
145 }
146
147 /**
148 * @brief %auto_ptr assignment operator.
149 * @param a Another %auto_ptr of a different but related type.
150 *
151 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
152 *
153 * This object now @e owns the object previously owned by @a a,
154 * which has given up ownsership. The object that this one @e
155 * used to own and track has been deleted.
156 */
157 template<typename _Tp1>
158 auto_ptr&
159 operator=(auto_ptr<_Tp1>& __a) throw()
160 {
161 reset(__a.release());
162 return *this;
163 }
164
165 /**
166 * When the %auto_ptr goes out of scope, the object it owns is
167 * deleted. If it no longer owns anything (i.e., @c get() is
168 * @c NULL), then this has no effect.
169 *
170 * @if maint
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
175 * @endif
176 */
177 ~auto_ptr() { delete _M_ptr; }
178
179 /**
180 * @brief Smart pointer dereferencing.
181 *
182 * If this %auto_ptr no longer owns anything, then this
183 * operation will crash. (For a smart pointer, "no longer owns
184 * anything" is the same as being a null pointer, and you know
185 * what happens when you dereference one of those...)
186 */
187 element_type&
188 operator*() const throw()
189 {
190 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
191 return *_M_ptr;
192 }
193
194 /**
195 * @brief Smart pointer dereferencing.
196 *
197 * This returns the pointer itself, which the language then will
198 * automatically cause to be dereferenced.
199 */
200 element_type*
201 operator->() const throw()
202 {
203 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
204 return _M_ptr;
205 }
206
207 /**
208 * @brief Bypassing the smart pointer.
209 * @return The raw pointer being managed.
210 *
211 * You can get a copy of the pointer that this object owns, for
212 * situations such as passing to a function which only accepts
213 * a raw pointer.
214 *
215 * @note This %auto_ptr still owns the memory.
216 */
217 element_type*
218 get() const throw() { return _M_ptr; }
219
220 /**
221 * @brief Bypassing the smart pointer.
222 * @return The raw pointer being managed.
223 *
224 * You can get a copy of the pointer that this object owns, for
225 * situations such as passing to a function which only accepts
226 * a raw pointer.
227 *
228 * @note This %auto_ptr no longer owns the memory. When this object
229 * goes out of scope, nothing will happen.
230 */
231 element_type*
232 release() throw()
233 {
234 element_type* __tmp = _M_ptr;
235 _M_ptr = 0;
236 return __tmp;
237 }
238
239 /**
240 * @brief Forcibly deletes the managed object.
241 * @param p A pointer (defaults to NULL).
242 *
243 * This object now @e owns the object pointed to by @a p. The
244 * previous object has been deleted.
245 */
246 void
247 reset(element_type* __p = 0) throw()
248 {
249 if (__p != _M_ptr)
250 {
251 delete _M_ptr;
252 _M_ptr = __p;
253 }
254 }
255
256 /**
257 * @brief Automatic conversions
258 *
259 * These operations convert an %auto_ptr into and from an auto_ptr_ref
260 * automatically as needed. This allows constructs such as
261 * @code
262 * auto_ptr<Derived> func_returning_auto_ptr(.....);
263 * ...
264 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
265 * @endcode
266 */
267 auto_ptr(auto_ptr_ref<element_type> __ref) throw()
268 : _M_ptr(__ref._M_ptr) { }
269
270 auto_ptr&
271 operator=(auto_ptr_ref<element_type> __ref) throw()
272 {
273 if (__ref._M_ptr != this->get())
274 {
275 delete _M_ptr;
276 _M_ptr = __ref._M_ptr;
277 }
278 return *this;
279 }
280
281 template<typename _Tp1>
282 operator auto_ptr_ref<_Tp1>() throw()
283 { return auto_ptr_ref<_Tp1>(this->release()); }
284
285 template<typename _Tp1>
286 operator auto_ptr<_Tp1>() throw()
287 { return auto_ptr<_Tp1>(this->release()); }
288 } _GLIBCXX_DEPRECATED_ATTR;
289
290 // _GLIBCXX_RESOLVE_LIB_DEFECTS
291 // 541. shared_ptr template assignment and void
292 template<>
293 class auto_ptr<void>
294 {
295 public:
296 typedef void element_type;
297 } _GLIBCXX_DEPRECATED_ATTR;
298
299 _GLIBCXX_END_NAMESPACE
300
301 #endif /* _STL_AUTO_PTR_H */