]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/backward/auto_ptr.h
1 // auto_ptr implementation -*- C++ -*-
3 // Copyright (C) 2007 Free Software Foundation, Inc.
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)
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
)
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
>
56 auto_ptr_ref(_Tp1
* __p
): _M_ptr(__p
) { }
57 } _GLIBCXX_DEPRECATED_ATTR
;
61 * @brief A simple smart pointer providing strict ownership semantics.
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.
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.
86 * _GLIBCXX_RESOLVE_LIB_DEFECTS
87 * 127. auto_ptr<> conversion issues
88 * These resolutions have all been incorporated.
91 template<typename _Tp
>
98 /// The pointed-to type.
99 typedef _Tp element_type
;
102 * @brief An %auto_ptr is usually constructed from a raw pointer.
103 * @param p A pointer (defaults to NULL).
105 * This object now @e owns the object pointed to by @a p.
108 auto_ptr(element_type
* __p
= 0) throw() : _M_ptr(__p
) { }
111 * @brief An %auto_ptr can be constructed from another %auto_ptr.
112 * @param a Another %auto_ptr of the same type.
114 * This object now @e owns the object previously owned by @a a,
115 * which has given up ownsership.
117 auto_ptr(auto_ptr
& __a
) throw() : _M_ptr(__a
.release()) { }
120 * @brief An %auto_ptr can be constructed from another %auto_ptr.
121 * @param a Another %auto_ptr of a different but related type.
123 * A pointer-to-Tp1 must be convertible to a
124 * pointer-to-Tp/element_type.
126 * This object now @e owns the object previously owned by @a a,
127 * which has given up ownsership.
129 template<typename _Tp1
>
130 auto_ptr(auto_ptr
<_Tp1
>& __a
) throw() : _M_ptr(__a
.release()) { }
133 * @brief %auto_ptr assignment operator.
134 * @param a Another %auto_ptr of the same type.
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.
141 operator=(auto_ptr
& __a
) throw()
143 reset(__a
.release());
148 * @brief %auto_ptr assignment operator.
149 * @param a Another %auto_ptr of a different but related type.
151 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
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.
157 template<typename _Tp1
>
159 operator=(auto_ptr
<_Tp1
>& __a
) throw()
161 reset(__a
.release());
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.
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
177 ~auto_ptr() { delete _M_ptr
; }
180 * @brief Smart pointer dereferencing.
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...)
188 operator*() const throw()
190 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
195 * @brief Smart pointer dereferencing.
197 * This returns the pointer itself, which the language then will
198 * automatically cause to be dereferenced.
201 operator->() const throw()
203 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
208 * @brief Bypassing the smart pointer.
209 * @return The raw pointer being managed.
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
215 * @note This %auto_ptr still owns the memory.
218 get() const throw() { return _M_ptr
; }
221 * @brief Bypassing the smart pointer.
222 * @return The raw pointer being managed.
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
228 * @note This %auto_ptr no longer owns the memory. When this object
229 * goes out of scope, nothing will happen.
234 element_type
* __tmp
= _M_ptr
;
240 * @brief Forcibly deletes the managed object.
241 * @param p A pointer (defaults to NULL).
243 * This object now @e owns the object pointed to by @a p. The
244 * previous object has been deleted.
247 reset(element_type
* __p
= 0) throw()
257 * @brief Automatic conversions
259 * These operations convert an %auto_ptr into and from an auto_ptr_ref
260 * automatically as needed. This allows constructs such as
262 * auto_ptr<Derived> func_returning_auto_ptr(.....);
264 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
267 auto_ptr(auto_ptr_ref
<element_type
> __ref
) throw()
268 : _M_ptr(__ref
._M_ptr
) { }
271 operator=(auto_ptr_ref
<element_type
> __ref
) throw()
273 if (__ref
._M_ptr
!= this->get())
276 _M_ptr
= __ref
._M_ptr
;
281 template<typename _Tp1
>
282 operator auto_ptr_ref
<_Tp1
>() throw()
283 { return auto_ptr_ref
<_Tp1
>(this->release()); }
285 template<typename _Tp1
>
286 operator auto_ptr
<_Tp1
>() throw()
287 { return auto_ptr
<_Tp1
>(this->release()); }
288 } _GLIBCXX_DEPRECATED_ATTR
;
290 // _GLIBCXX_RESOLVE_LIB_DEFECTS
291 // 541. shared_ptr template assignment and void
296 typedef void element_type
;
297 } _GLIBCXX_DEPRECATED_ATTR
;
299 _GLIBCXX_END_NAMESPACE
301 #endif /* _STL_AUTO_PTR_H */