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