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