00001 // auto_ptr implementation -*- C++ -*- 00002 00003 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file backward/auto_ptr.h 00026 * This is an internal header file, included by other library headers. 00027 * You should not attempt to use it directly. 00028 */ 00029 00030 #ifndef _BACKWARD_AUTO_PTR_H 00031 #define _BACKWARD_AUTO_PTR_H 1 00032 00033 #include <bits/c++config.h> 00034 #include <debug/debug.h> 00035 00036 _GLIBCXX_BEGIN_NAMESPACE(std) 00037 00038 /** 00039 * A wrapper class to provide auto_ptr with reference semantics. 00040 * For example, an auto_ptr can be assigned (or constructed from) 00041 * the result of a function which returns an auto_ptr by value. 00042 * 00043 * All the auto_ptr_ref stuff should happen behind the scenes. 00044 */ 00045 template<typename _Tp1> 00046 struct auto_ptr_ref 00047 { 00048 _Tp1* _M_ptr; 00049 00050 explicit 00051 auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { } 00052 } _GLIBCXX_DEPRECATED_ATTR; 00053 00054 00055 /** 00056 * @brief A simple smart pointer providing strict ownership semantics. 00057 * 00058 * The Standard says: 00059 * <pre> 00060 * An @c auto_ptr owns the object it holds a pointer to. Copying 00061 * an @c auto_ptr copies the pointer and transfers ownership to the 00062 * destination. If more than one @c auto_ptr owns the same object 00063 * at the same time the behavior of the program is undefined. 00064 * 00065 * The uses of @c auto_ptr include providing temporary 00066 * exception-safety for dynamically allocated memory, passing 00067 * ownership of dynamically allocated memory to a function, and 00068 * returning dynamically allocated memory from a function. @c 00069 * auto_ptr does not meet the CopyConstructible and Assignable 00070 * requirements for Standard Library <a 00071 * href="tables.html#65">container</a> elements and thus 00072 * instantiating a Standard Library container with an @c auto_ptr 00073 * results in undefined behavior. 00074 * </pre> 00075 * Quoted from [20.4.5]/3. 00076 * 00077 * Good examples of what can and cannot be done with auto_ptr can 00078 * be found in the libstdc++ testsuite. 00079 * 00080 * _GLIBCXX_RESOLVE_LIB_DEFECTS 00081 * 127. auto_ptr<> conversion issues 00082 * These resolutions have all been incorporated. 00083 */ 00084 template<typename _Tp> 00085 class auto_ptr 00086 { 00087 private: 00088 _Tp* _M_ptr; 00089 00090 public: 00091 /// The pointed-to type. 00092 typedef _Tp element_type; 00093 00094 /** 00095 * @brief An %auto_ptr is usually constructed from a raw pointer. 00096 * @param p A pointer (defaults to NULL). 00097 * 00098 * This object now @e owns the object pointed to by @a p. 00099 */ 00100 explicit 00101 auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { } 00102 00103 /** 00104 * @brief An %auto_ptr can be constructed from another %auto_ptr. 00105 * @param a Another %auto_ptr of the same type. 00106 * 00107 * This object now @e owns the object previously owned by @a a, 00108 * which has given up ownership. 00109 */ 00110 auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { } 00111 00112 /** 00113 * @brief An %auto_ptr can be constructed from another %auto_ptr. 00114 * @param a Another %auto_ptr of a different but related type. 00115 * 00116 * A pointer-to-Tp1 must be convertible to a 00117 * pointer-to-Tp/element_type. 00118 * 00119 * This object now @e owns the object previously owned by @a a, 00120 * which has given up ownership. 00121 */ 00122 template<typename _Tp1> 00123 auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { } 00124 00125 /** 00126 * @brief %auto_ptr assignment operator. 00127 * @param a Another %auto_ptr of the same type. 00128 * 00129 * This object now @e owns the object previously owned by @a a, 00130 * which has given up ownership. The object that this one @e 00131 * used to own and track has been deleted. 00132 */ 00133 auto_ptr& 00134 operator=(auto_ptr& __a) throw() 00135 { 00136 reset(__a.release()); 00137 return *this; 00138 } 00139 00140 /** 00141 * @brief %auto_ptr assignment operator. 00142 * @param a Another %auto_ptr of a different but related type. 00143 * 00144 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type. 00145 * 00146 * This object now @e owns the object previously owned by @a a, 00147 * which has given up ownership. The object that this one @e 00148 * used to own and track has been deleted. 00149 */ 00150 template<typename _Tp1> 00151 auto_ptr& 00152 operator=(auto_ptr<_Tp1>& __a) throw() 00153 { 00154 reset(__a.release()); 00155 return *this; 00156 } 00157 00158 /** 00159 * When the %auto_ptr goes out of scope, the object it owns is 00160 * deleted. If it no longer owns anything (i.e., @c get() is 00161 * @c NULL), then this has no effect. 00162 * 00163 * The C++ standard says there is supposed to be an empty throw 00164 * specification here, but omitting it is standard conforming. Its 00165 * presence can be detected only if _Tp::~_Tp() throws, but this is 00166 * prohibited. [17.4.3.6]/2 00167 */ 00168 ~auto_ptr() { delete _M_ptr; } 00169 00170 /** 00171 * @brief Smart pointer dereferencing. 00172 * 00173 * If this %auto_ptr no longer owns anything, then this 00174 * operation will crash. (For a smart pointer, "no longer owns 00175 * anything" is the same as being a null pointer, and you know 00176 * what happens when you dereference one of those...) 00177 */ 00178 element_type& 00179 operator*() const throw() 00180 { 00181 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0); 00182 return *_M_ptr; 00183 } 00184 00185 /** 00186 * @brief Smart pointer dereferencing. 00187 * 00188 * This returns the pointer itself, which the language then will 00189 * automatically cause to be dereferenced. 00190 */ 00191 element_type* 00192 operator->() const throw() 00193 { 00194 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0); 00195 return _M_ptr; 00196 } 00197 00198 /** 00199 * @brief Bypassing the smart pointer. 00200 * @return The raw pointer being managed. 00201 * 00202 * You can get a copy of the pointer that this object owns, for 00203 * situations such as passing to a function which only accepts 00204 * a raw pointer. 00205 * 00206 * @note This %auto_ptr still owns the memory. 00207 */ 00208 element_type* 00209 get() const throw() { return _M_ptr; } 00210 00211 /** 00212 * @brief Bypassing the smart pointer. 00213 * @return The raw pointer being managed. 00214 * 00215 * You can get a copy of the pointer that this object owns, for 00216 * situations such as passing to a function which only accepts 00217 * a raw pointer. 00218 * 00219 * @note This %auto_ptr no longer owns the memory. When this object 00220 * goes out of scope, nothing will happen. 00221 */ 00222 element_type* 00223 release() throw() 00224 { 00225 element_type* __tmp = _M_ptr; 00226 _M_ptr = 0; 00227 return __tmp; 00228 } 00229 00230 /** 00231 * @brief Forcibly deletes the managed object. 00232 * @param p A pointer (defaults to NULL). 00233 * 00234 * This object now @e owns the object pointed to by @a p. The 00235 * previous object has been deleted. 00236 */ 00237 void 00238 reset(element_type* __p = 0) throw() 00239 { 00240 if (__p != _M_ptr) 00241 { 00242 delete _M_ptr; 00243 _M_ptr = __p; 00244 } 00245 } 00246 00247 /** 00248 * @brief Automatic conversions 00249 * 00250 * These operations convert an %auto_ptr into and from an auto_ptr_ref 00251 * automatically as needed. This allows constructs such as 00252 * @code 00253 * auto_ptr<Derived> func_returning_auto_ptr(.....); 00254 * ... 00255 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....); 00256 * @endcode 00257 */ 00258 auto_ptr(auto_ptr_ref<element_type> __ref) throw() 00259 : _M_ptr(__ref._M_ptr) { } 00260 00261 auto_ptr& 00262 operator=(auto_ptr_ref<element_type> __ref) throw() 00263 { 00264 if (__ref._M_ptr != this->get()) 00265 { 00266 delete _M_ptr; 00267 _M_ptr = __ref._M_ptr; 00268 } 00269 return *this; 00270 } 00271 00272 template<typename _Tp1> 00273 operator auto_ptr_ref<_Tp1>() throw() 00274 { return auto_ptr_ref<_Tp1>(this->release()); } 00275 00276 template<typename _Tp1> 00277 operator auto_ptr<_Tp1>() throw() 00278 { return auto_ptr<_Tp1>(this->release()); } 00279 } _GLIBCXX_DEPRECATED_ATTR; 00280 00281 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00282 // 541. shared_ptr template assignment and void 00283 template<> 00284 class auto_ptr<void> 00285 { 00286 public: 00287 typedef void element_type; 00288 } _GLIBCXX_DEPRECATED_ATTR; 00289 00290 _GLIBCXX_END_NAMESPACE 00291 00292 #endif /* _BACKWARD_AUTO_PTR_H */