Install gcc-4.4.0-tdm-1-core-2.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / backward / auto_ptr.h
blob2787d1a711469a467ceea261eff0467a7121fa00
1 // auto_ptr implementation -*- C++ -*-
3 // Copyright (C) 2007, 2008 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.
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.
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.
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.
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.
35 #ifndef _STL_AUTO_PTR_H
36 #define _STL_AUTO_PTR_H 1
38 #include <bits/c++config.h>
39 #include <debug/debug.h>
41 _GLIBCXX_BEGIN_NAMESPACE(std)
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.
48 * All the auto_ptr_ref stuff should happen behind the scenes.
50 template<typename _Tp1>
51 struct auto_ptr_ref
53 _Tp1* _M_ptr;
55 explicit
56 auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
57 } _GLIBCXX_DEPRECATED_ATTR;
60 /**
61 * @brief A simple smart pointer providing strict ownership semantics.
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.
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.
82 * Good examples of what can and cannot be done with auto_ptr can
83 * be found in the libstdc++ testsuite.
85 * _GLIBCXX_RESOLVE_LIB_DEFECTS
86 * 127. auto_ptr<> conversion issues
87 * These resolutions have all been incorporated.
89 template<typename _Tp>
90 class auto_ptr
92 private:
93 _Tp* _M_ptr;
95 public:
96 /// The pointed-to type.
97 typedef _Tp element_type;
99 /**
100 * @brief An %auto_ptr is usually constructed from a raw pointer.
101 * @param p A pointer (defaults to NULL).
103 * This object now @e owns the object pointed to by @a p.
105 explicit
106 auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
109 * @brief An %auto_ptr can be constructed from another %auto_ptr.
110 * @param a Another %auto_ptr of the same type.
112 * This object now @e owns the object previously owned by @a a,
113 * which has given up ownership.
115 auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
118 * @brief An %auto_ptr can be constructed from another %auto_ptr.
119 * @param a Another %auto_ptr of a different but related type.
121 * A pointer-to-Tp1 must be convertible to a
122 * pointer-to-Tp/element_type.
124 * This object now @e owns the object previously owned by @a a,
125 * which has given up ownership.
127 template<typename _Tp1>
128 auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
131 * @brief %auto_ptr assignment operator.
132 * @param a Another %auto_ptr of the same type.
134 * This object now @e owns the object previously owned by @a a,
135 * which has given up ownership. The object that this one @e
136 * used to own and track has been deleted.
138 auto_ptr&
139 operator=(auto_ptr& __a) throw()
141 reset(__a.release());
142 return *this;
146 * @brief %auto_ptr assignment operator.
147 * @param a Another %auto_ptr of a different but related type.
149 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
151 * This object now @e owns the object previously owned by @a a,
152 * which has given up ownership. The object that this one @e
153 * used to own and track has been deleted.
155 template<typename _Tp1>
156 auto_ptr&
157 operator=(auto_ptr<_Tp1>& __a) throw()
159 reset(__a.release());
160 return *this;
164 * When the %auto_ptr goes out of scope, the object it owns is
165 * deleted. If it no longer owns anything (i.e., @c get() is
166 * @c NULL), then this has no effect.
168 * The C++ standard says there is supposed to be an empty throw
169 * specification here, but omitting it is standard conforming. Its
170 * presence can be detected only if _Tp::~_Tp() throws, but this is
171 * prohibited. [17.4.3.6]/2
173 ~auto_ptr() { delete _M_ptr; }
176 * @brief Smart pointer dereferencing.
178 * If this %auto_ptr no longer owns anything, then this
179 * operation will crash. (For a smart pointer, "no longer owns
180 * anything" is the same as being a null pointer, and you know
181 * what happens when you dereference one of those...)
183 element_type&
184 operator*() const throw()
186 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
187 return *_M_ptr;
191 * @brief Smart pointer dereferencing.
193 * This returns the pointer itself, which the language then will
194 * automatically cause to be dereferenced.
196 element_type*
197 operator->() const throw()
199 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
200 return _M_ptr;
204 * @brief Bypassing the smart pointer.
205 * @return The raw pointer being managed.
207 * You can get a copy of the pointer that this object owns, for
208 * situations such as passing to a function which only accepts
209 * a raw pointer.
211 * @note This %auto_ptr still owns the memory.
213 element_type*
214 get() const throw() { return _M_ptr; }
217 * @brief Bypassing the smart pointer.
218 * @return The raw pointer being managed.
220 * You can get a copy of the pointer that this object owns, for
221 * situations such as passing to a function which only accepts
222 * a raw pointer.
224 * @note This %auto_ptr no longer owns the memory. When this object
225 * goes out of scope, nothing will happen.
227 element_type*
228 release() throw()
230 element_type* __tmp = _M_ptr;
231 _M_ptr = 0;
232 return __tmp;
236 * @brief Forcibly deletes the managed object.
237 * @param p A pointer (defaults to NULL).
239 * This object now @e owns the object pointed to by @a p. The
240 * previous object has been deleted.
242 void
243 reset(element_type* __p = 0) throw()
245 if (__p != _M_ptr)
247 delete _M_ptr;
248 _M_ptr = __p;
252 /**
253 * @brief Automatic conversions
255 * These operations convert an %auto_ptr into and from an auto_ptr_ref
256 * automatically as needed. This allows constructs such as
257 * @code
258 * auto_ptr<Derived> func_returning_auto_ptr(.....);
259 * ...
260 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
261 * @endcode
263 auto_ptr(auto_ptr_ref<element_type> __ref) throw()
264 : _M_ptr(__ref._M_ptr) { }
266 auto_ptr&
267 operator=(auto_ptr_ref<element_type> __ref) throw()
269 if (__ref._M_ptr != this->get())
271 delete _M_ptr;
272 _M_ptr = __ref._M_ptr;
274 return *this;
277 template<typename _Tp1>
278 operator auto_ptr_ref<_Tp1>() throw()
279 { return auto_ptr_ref<_Tp1>(this->release()); }
281 template<typename _Tp1>
282 operator auto_ptr<_Tp1>() throw()
283 { return auto_ptr<_Tp1>(this->release()); }
284 } _GLIBCXX_DEPRECATED_ATTR;
286 // _GLIBCXX_RESOLVE_LIB_DEFECTS
287 // 541. shared_ptr template assignment and void
288 template<>
289 class auto_ptr<void>
291 public:
292 typedef void element_type;
293 } _GLIBCXX_DEPRECATED_ATTR;
295 _GLIBCXX_END_NAMESPACE
297 #endif /* _STL_AUTO_PTR_H */