1*e4b17023SJohn Marino // auto_ptr implementation -*- C++ -*-
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino // Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4*e4b17023SJohn Marino //
5*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free
6*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
7*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
8*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
9*e4b17023SJohn Marino // any later version.
10*e4b17023SJohn Marino
11*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
12*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*e4b17023SJohn Marino // GNU General Public License for more details.
15*e4b17023SJohn Marino
16*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
17*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
18*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
19*e4b17023SJohn Marino
20*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
21*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
22*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
24*e4b17023SJohn Marino
25*e4b17023SJohn Marino /** @file backward/auto_ptr.h
26*e4b17023SJohn Marino * This is an internal header file, included by other library headers.
27*e4b17023SJohn Marino * Do not attempt to use it directly. @headername{memory}
28*e4b17023SJohn Marino */
29*e4b17023SJohn Marino
30*e4b17023SJohn Marino #ifndef _BACKWARD_AUTO_PTR_H
31*e4b17023SJohn Marino #define _BACKWARD_AUTO_PTR_H 1
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino #include <bits/c++config.h>
34*e4b17023SJohn Marino #include <debug/debug.h>
35*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)36*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
37*e4b17023SJohn Marino {
38*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
39*e4b17023SJohn Marino
40*e4b17023SJohn Marino /**
41*e4b17023SJohn Marino * A wrapper class to provide auto_ptr with reference semantics.
42*e4b17023SJohn Marino * For example, an auto_ptr can be assigned (or constructed from)
43*e4b17023SJohn Marino * the result of a function which returns an auto_ptr by value.
44*e4b17023SJohn Marino *
45*e4b17023SJohn Marino * All the auto_ptr_ref stuff should happen behind the scenes.
46*e4b17023SJohn Marino */
47*e4b17023SJohn Marino template<typename _Tp1>
48*e4b17023SJohn Marino struct auto_ptr_ref
49*e4b17023SJohn Marino {
50*e4b17023SJohn Marino _Tp1* _M_ptr;
51*e4b17023SJohn Marino
52*e4b17023SJohn Marino explicit
53*e4b17023SJohn Marino auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
54*e4b17023SJohn Marino } _GLIBCXX_DEPRECATED;
55*e4b17023SJohn Marino
56*e4b17023SJohn Marino
57*e4b17023SJohn Marino /**
58*e4b17023SJohn Marino * @brief A simple smart pointer providing strict ownership semantics.
59*e4b17023SJohn Marino *
60*e4b17023SJohn Marino * The Standard says:
61*e4b17023SJohn Marino * <pre>
62*e4b17023SJohn Marino * An @c auto_ptr owns the object it holds a pointer to. Copying
63*e4b17023SJohn Marino * an @c auto_ptr copies the pointer and transfers ownership to the
64*e4b17023SJohn Marino * destination. If more than one @c auto_ptr owns the same object
65*e4b17023SJohn Marino * at the same time the behavior of the program is undefined.
66*e4b17023SJohn Marino *
67*e4b17023SJohn Marino * The uses of @c auto_ptr include providing temporary
68*e4b17023SJohn Marino * exception-safety for dynamically allocated memory, passing
69*e4b17023SJohn Marino * ownership of dynamically allocated memory to a function, and
70*e4b17023SJohn Marino * returning dynamically allocated memory from a function. @c
71*e4b17023SJohn Marino * auto_ptr does not meet the CopyConstructible and Assignable
72*e4b17023SJohn Marino * requirements for Standard Library <a
73*e4b17023SJohn Marino * href="tables.html#65">container</a> elements and thus
74*e4b17023SJohn Marino * instantiating a Standard Library container with an @c auto_ptr
75*e4b17023SJohn Marino * results in undefined behavior.
76*e4b17023SJohn Marino * </pre>
77*e4b17023SJohn Marino * Quoted from [20.4.5]/3.
78*e4b17023SJohn Marino *
79*e4b17023SJohn Marino * Good examples of what can and cannot be done with auto_ptr can
80*e4b17023SJohn Marino * be found in the libstdc++ testsuite.
81*e4b17023SJohn Marino *
82*e4b17023SJohn Marino * _GLIBCXX_RESOLVE_LIB_DEFECTS
83*e4b17023SJohn Marino * 127. auto_ptr<> conversion issues
84*e4b17023SJohn Marino * These resolutions have all been incorporated.
85*e4b17023SJohn Marino */
86*e4b17023SJohn Marino template<typename _Tp>
87*e4b17023SJohn Marino class auto_ptr
88*e4b17023SJohn Marino {
89*e4b17023SJohn Marino private:
90*e4b17023SJohn Marino _Tp* _M_ptr;
91*e4b17023SJohn Marino
92*e4b17023SJohn Marino public:
93*e4b17023SJohn Marino /// The pointed-to type.
94*e4b17023SJohn Marino typedef _Tp element_type;
95*e4b17023SJohn Marino
96*e4b17023SJohn Marino /**
97*e4b17023SJohn Marino * @brief An %auto_ptr is usually constructed from a raw pointer.
98*e4b17023SJohn Marino * @param __p A pointer (defaults to NULL).
99*e4b17023SJohn Marino *
100*e4b17023SJohn Marino * This object now @e owns the object pointed to by @a __p.
101*e4b17023SJohn Marino */
102*e4b17023SJohn Marino explicit
103*e4b17023SJohn Marino auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
104*e4b17023SJohn Marino
105*e4b17023SJohn Marino /**
106*e4b17023SJohn Marino * @brief An %auto_ptr can be constructed from another %auto_ptr.
107*e4b17023SJohn Marino * @param __a Another %auto_ptr of the same type.
108*e4b17023SJohn Marino *
109*e4b17023SJohn Marino * This object now @e owns the object previously owned by @a __a,
110*e4b17023SJohn Marino * which has given up ownership.
111*e4b17023SJohn Marino */
112*e4b17023SJohn Marino auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
113*e4b17023SJohn Marino
114*e4b17023SJohn Marino /**
115*e4b17023SJohn Marino * @brief An %auto_ptr can be constructed from another %auto_ptr.
116*e4b17023SJohn Marino * @param __a Another %auto_ptr of a different but related type.
117*e4b17023SJohn Marino *
118*e4b17023SJohn Marino * A pointer-to-Tp1 must be convertible to a
119*e4b17023SJohn Marino * pointer-to-Tp/element_type.
120*e4b17023SJohn Marino *
121*e4b17023SJohn Marino * This object now @e owns the object previously owned by @a __a,
122*e4b17023SJohn Marino * which has given up ownership.
123*e4b17023SJohn Marino */
124*e4b17023SJohn Marino template<typename _Tp1>
125*e4b17023SJohn Marino auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
126*e4b17023SJohn Marino
127*e4b17023SJohn Marino /**
128*e4b17023SJohn Marino * @brief %auto_ptr assignment operator.
129*e4b17023SJohn Marino * @param __a Another %auto_ptr of the same type.
130*e4b17023SJohn Marino *
131*e4b17023SJohn Marino * This object now @e owns the object previously owned by @a __a,
132*e4b17023SJohn Marino * which has given up ownership. The object that this one @e
133*e4b17023SJohn Marino * used to own and track has been deleted.
134*e4b17023SJohn Marino */
135*e4b17023SJohn Marino auto_ptr&
136*e4b17023SJohn Marino operator=(auto_ptr& __a) throw()
137*e4b17023SJohn Marino {
138*e4b17023SJohn Marino reset(__a.release());
139*e4b17023SJohn Marino return *this;
140*e4b17023SJohn Marino }
141*e4b17023SJohn Marino
142*e4b17023SJohn Marino /**
143*e4b17023SJohn Marino * @brief %auto_ptr assignment operator.
144*e4b17023SJohn Marino * @param __a Another %auto_ptr of a different but related type.
145*e4b17023SJohn Marino *
146*e4b17023SJohn Marino * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
147*e4b17023SJohn Marino *
148*e4b17023SJohn Marino * This object now @e owns the object previously owned by @a __a,
149*e4b17023SJohn Marino * which has given up ownership. The object that this one @e
150*e4b17023SJohn Marino * used to own and track has been deleted.
151*e4b17023SJohn Marino */
152*e4b17023SJohn Marino template<typename _Tp1>
153*e4b17023SJohn Marino auto_ptr&
154*e4b17023SJohn Marino operator=(auto_ptr<_Tp1>& __a) throw()
155*e4b17023SJohn Marino {
156*e4b17023SJohn Marino reset(__a.release());
157*e4b17023SJohn Marino return *this;
158*e4b17023SJohn Marino }
159*e4b17023SJohn Marino
160*e4b17023SJohn Marino /**
161*e4b17023SJohn Marino * When the %auto_ptr goes out of scope, the object it owns is
162*e4b17023SJohn Marino * deleted. If it no longer owns anything (i.e., @c get() is
163*e4b17023SJohn Marino * @c NULL), then this has no effect.
164*e4b17023SJohn Marino *
165*e4b17023SJohn Marino * The C++ standard says there is supposed to be an empty throw
166*e4b17023SJohn Marino * specification here, but omitting it is standard conforming. Its
167*e4b17023SJohn Marino * presence can be detected only if _Tp::~_Tp() throws, but this is
168*e4b17023SJohn Marino * prohibited. [17.4.3.6]/2
169*e4b17023SJohn Marino */
170*e4b17023SJohn Marino ~auto_ptr() { delete _M_ptr; }
171*e4b17023SJohn Marino
172*e4b17023SJohn Marino /**
173*e4b17023SJohn Marino * @brief Smart pointer dereferencing.
174*e4b17023SJohn Marino *
175*e4b17023SJohn Marino * If this %auto_ptr no longer owns anything, then this
176*e4b17023SJohn Marino * operation will crash. (For a smart pointer, <em>no longer owns
177*e4b17023SJohn Marino * anything</em> is the same as being a null pointer, and you know
178*e4b17023SJohn Marino * what happens when you dereference one of those...)
179*e4b17023SJohn Marino */
180*e4b17023SJohn Marino element_type&
181*e4b17023SJohn Marino operator*() const throw()
182*e4b17023SJohn Marino {
183*e4b17023SJohn Marino _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
184*e4b17023SJohn Marino return *_M_ptr;
185*e4b17023SJohn Marino }
186*e4b17023SJohn Marino
187*e4b17023SJohn Marino /**
188*e4b17023SJohn Marino * @brief Smart pointer dereferencing.
189*e4b17023SJohn Marino *
190*e4b17023SJohn Marino * This returns the pointer itself, which the language then will
191*e4b17023SJohn Marino * automatically cause to be dereferenced.
192*e4b17023SJohn Marino */
193*e4b17023SJohn Marino element_type*
194*e4b17023SJohn Marino operator->() const throw()
195*e4b17023SJohn Marino {
196*e4b17023SJohn Marino _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
197*e4b17023SJohn Marino return _M_ptr;
198*e4b17023SJohn Marino }
199*e4b17023SJohn Marino
200*e4b17023SJohn Marino /**
201*e4b17023SJohn Marino * @brief Bypassing the smart pointer.
202*e4b17023SJohn Marino * @return The raw pointer being managed.
203*e4b17023SJohn Marino *
204*e4b17023SJohn Marino * You can get a copy of the pointer that this object owns, for
205*e4b17023SJohn Marino * situations such as passing to a function which only accepts
206*e4b17023SJohn Marino * a raw pointer.
207*e4b17023SJohn Marino *
208*e4b17023SJohn Marino * @note This %auto_ptr still owns the memory.
209*e4b17023SJohn Marino */
210*e4b17023SJohn Marino element_type*
211*e4b17023SJohn Marino get() const throw() { return _M_ptr; }
212*e4b17023SJohn Marino
213*e4b17023SJohn Marino /**
214*e4b17023SJohn Marino * @brief Bypassing the smart pointer.
215*e4b17023SJohn Marino * @return The raw pointer being managed.
216*e4b17023SJohn Marino *
217*e4b17023SJohn Marino * You can get a copy of the pointer that this object owns, for
218*e4b17023SJohn Marino * situations such as passing to a function which only accepts
219*e4b17023SJohn Marino * a raw pointer.
220*e4b17023SJohn Marino *
221*e4b17023SJohn Marino * @note This %auto_ptr no longer owns the memory. When this object
222*e4b17023SJohn Marino * goes out of scope, nothing will happen.
223*e4b17023SJohn Marino */
224*e4b17023SJohn Marino element_type*
225*e4b17023SJohn Marino release() throw()
226*e4b17023SJohn Marino {
227*e4b17023SJohn Marino element_type* __tmp = _M_ptr;
228*e4b17023SJohn Marino _M_ptr = 0;
229*e4b17023SJohn Marino return __tmp;
230*e4b17023SJohn Marino }
231*e4b17023SJohn Marino
232*e4b17023SJohn Marino /**
233*e4b17023SJohn Marino * @brief Forcibly deletes the managed object.
234*e4b17023SJohn Marino * @param __p A pointer (defaults to NULL).
235*e4b17023SJohn Marino *
236*e4b17023SJohn Marino * This object now @e owns the object pointed to by @a __p. The
237*e4b17023SJohn Marino * previous object has been deleted.
238*e4b17023SJohn Marino */
239*e4b17023SJohn Marino void
240*e4b17023SJohn Marino reset(element_type* __p = 0) throw()
241*e4b17023SJohn Marino {
242*e4b17023SJohn Marino if (__p != _M_ptr)
243*e4b17023SJohn Marino {
244*e4b17023SJohn Marino delete _M_ptr;
245*e4b17023SJohn Marino _M_ptr = __p;
246*e4b17023SJohn Marino }
247*e4b17023SJohn Marino }
248*e4b17023SJohn Marino
249*e4b17023SJohn Marino /**
250*e4b17023SJohn Marino * @brief Automatic conversions
251*e4b17023SJohn Marino *
252*e4b17023SJohn Marino * These operations convert an %auto_ptr into and from an auto_ptr_ref
253*e4b17023SJohn Marino * automatically as needed. This allows constructs such as
254*e4b17023SJohn Marino * @code
255*e4b17023SJohn Marino * auto_ptr<Derived> func_returning_auto_ptr(.....);
256*e4b17023SJohn Marino * ...
257*e4b17023SJohn Marino * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
258*e4b17023SJohn Marino * @endcode
259*e4b17023SJohn Marino */
260*e4b17023SJohn Marino auto_ptr(auto_ptr_ref<element_type> __ref) throw()
261*e4b17023SJohn Marino : _M_ptr(__ref._M_ptr) { }
262*e4b17023SJohn Marino
263*e4b17023SJohn Marino auto_ptr&
264*e4b17023SJohn Marino operator=(auto_ptr_ref<element_type> __ref) throw()
265*e4b17023SJohn Marino {
266*e4b17023SJohn Marino if (__ref._M_ptr != this->get())
267*e4b17023SJohn Marino {
268*e4b17023SJohn Marino delete _M_ptr;
269*e4b17023SJohn Marino _M_ptr = __ref._M_ptr;
270*e4b17023SJohn Marino }
271*e4b17023SJohn Marino return *this;
272*e4b17023SJohn Marino }
273*e4b17023SJohn Marino
274*e4b17023SJohn Marino template<typename _Tp1>
275*e4b17023SJohn Marino operator auto_ptr_ref<_Tp1>() throw()
276*e4b17023SJohn Marino { return auto_ptr_ref<_Tp1>(this->release()); }
277*e4b17023SJohn Marino
278*e4b17023SJohn Marino template<typename _Tp1>
279*e4b17023SJohn Marino operator auto_ptr<_Tp1>() throw()
280*e4b17023SJohn Marino { return auto_ptr<_Tp1>(this->release()); }
281*e4b17023SJohn Marino } _GLIBCXX_DEPRECATED;
282*e4b17023SJohn Marino
283*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS
284*e4b17023SJohn Marino // 541. shared_ptr template assignment and void
285*e4b17023SJohn Marino template<>
286*e4b17023SJohn Marino class auto_ptr<void>
287*e4b17023SJohn Marino {
288*e4b17023SJohn Marino public:
289*e4b17023SJohn Marino typedef void element_type;
290*e4b17023SJohn Marino } _GLIBCXX_DEPRECATED;
291*e4b17023SJohn Marino
292*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
293*e4b17023SJohn Marino } // namespace
294*e4b17023SJohn Marino
295*e4b17023SJohn Marino #endif /* _BACKWARD_AUTO_PTR_H */
296