libstdc++
|
00001 // auto_ptr implementation -*- C++ -*- 00002 00003 // Copyright (C) 2007-2018 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 #pragma GCC diagnostic push 00057 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 00058 00059 /** 00060 * @brief A simple smart pointer providing strict ownership semantics. 00061 * 00062 * The Standard says: 00063 * <pre> 00064 * An @c auto_ptr owns the object it holds a pointer to. Copying 00065 * an @c auto_ptr copies the pointer and transfers ownership to the 00066 * destination. If more than one @c auto_ptr owns the same object 00067 * at the same time the behavior of the program is undefined. 00068 * 00069 * The uses of @c auto_ptr include providing temporary 00070 * exception-safety for dynamically allocated memory, passing 00071 * ownership of dynamically allocated memory to a function, and 00072 * returning dynamically allocated memory from a function. @c 00073 * auto_ptr does not meet the CopyConstructible and Assignable 00074 * requirements for Standard Library <a 00075 * href="tables.html#65">container</a> elements and thus 00076 * instantiating a Standard Library container with an @c auto_ptr 00077 * results in undefined behavior. 00078 * </pre> 00079 * Quoted from [20.4.5]/3. 00080 * 00081 * Good examples of what can and cannot be done with auto_ptr can 00082 * be found in the libstdc++ testsuite. 00083 * 00084 * _GLIBCXX_RESOLVE_LIB_DEFECTS 00085 * 127. auto_ptr<> conversion issues 00086 * These resolutions have all been incorporated. 00087 */ 00088 template<typename _Tp> 00089 class auto_ptr 00090 { 00091 private: 00092 _Tp* _M_ptr; 00093 00094 public: 00095 /// The pointed-to type. 00096 typedef _Tp element_type; 00097 00098 /** 00099 * @brief An %auto_ptr is usually constructed from a raw pointer. 00100 * @param __p A pointer (defaults to NULL). 00101 * 00102 * This object now @e owns the object pointed to by @a __p. 00103 */ 00104 explicit 00105 auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { } 00106 00107 /** 00108 * @brief An %auto_ptr can be constructed from another %auto_ptr. 00109 * @param __a Another %auto_ptr of the same type. 00110 * 00111 * This object now @e owns the object previously owned by @a __a, 00112 * which has given up ownership. 00113 */ 00114 auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { } 00115 00116 /** 00117 * @brief An %auto_ptr can be constructed from another %auto_ptr. 00118 * @param __a Another %auto_ptr of a different but related type. 00119 * 00120 * A pointer-to-Tp1 must be convertible to a 00121 * pointer-to-Tp/element_type. 00122 * 00123 * This object now @e owns the object previously owned by @a __a, 00124 * which has given up ownership. 00125 */ 00126 template<typename _Tp1> 00127 auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { } 00128 00129 /** 00130 * @brief %auto_ptr assignment operator. 00131 * @param __a Another %auto_ptr of the same type. 00132 * 00133 * This object now @e owns the object previously owned by @a __a, 00134 * which has given up ownership. The object that this one @e 00135 * used to own and track has been deleted. 00136 */ 00137 auto_ptr& 00138 operator=(auto_ptr& __a) throw() 00139 { 00140 reset(__a.release()); 00141 return *this; 00142 } 00143 00144 /** 00145 * @brief %auto_ptr assignment operator. 00146 * @param __a Another %auto_ptr of a different but related type. 00147 * 00148 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type. 00149 * 00150 * This object now @e owns the object previously owned by @a __a, 00151 * which has given up ownership. The object that this one @e 00152 * used to own and track has been deleted. 00153 */ 00154 template<typename _Tp1> 00155 auto_ptr& 00156 operator=(auto_ptr<_Tp1>& __a) throw() 00157 { 00158 reset(__a.release()); 00159 return *this; 00160 } 00161 00162 /** 00163 * When the %auto_ptr goes out of scope, the object it owns is 00164 * deleted. If it no longer owns anything (i.e., @c get() is 00165 * @c NULL), then this has no effect. 00166 * 00167 * The C++ standard says there is supposed to be an empty throw 00168 * specification here, but omitting it is standard conforming. Its 00169 * presence can be detected only if _Tp::~_Tp() throws, but this is 00170 * prohibited. [17.4.3.6]/2 00171 */ 00172 ~auto_ptr() { delete _M_ptr; } 00173 00174 /** 00175 * @brief Smart pointer dereferencing. 00176 * 00177 * If this %auto_ptr no longer owns anything, then this 00178 * operation will crash. (For a smart pointer, <em>no longer owns 00179 * anything</em> is the same as being a null pointer, and you know 00180 * what happens when you dereference one of those...) 00181 */ 00182 element_type& 00183 operator*() const throw() 00184 { 00185 __glibcxx_assert(_M_ptr != 0); 00186 return *_M_ptr; 00187 } 00188 00189 /** 00190 * @brief Smart pointer dereferencing. 00191 * 00192 * This returns the pointer itself, which the language then will 00193 * automatically cause to be dereferenced. 00194 */ 00195 element_type* 00196 operator->() const throw() 00197 { 00198 __glibcxx_assert(_M_ptr != 0); 00199 return _M_ptr; 00200 } 00201 00202 /** 00203 * @brief Bypassing the smart pointer. 00204 * @return The raw pointer being managed. 00205 * 00206 * You can get a copy of the pointer that this object owns, for 00207 * situations such as passing to a function which only accepts 00208 * a raw pointer. 00209 * 00210 * @note This %auto_ptr still owns the memory. 00211 */ 00212 element_type* 00213 get() const throw() { return _M_ptr; } 00214 00215 /** 00216 * @brief Bypassing the smart pointer. 00217 * @return The raw pointer being managed. 00218 * 00219 * You can get a copy of the pointer that this object owns, for 00220 * situations such as passing to a function which only accepts 00221 * a raw pointer. 00222 * 00223 * @note This %auto_ptr no longer owns the memory. When this object 00224 * goes out of scope, nothing will happen. 00225 */ 00226 element_type* 00227 release() throw() 00228 { 00229 element_type* __tmp = _M_ptr; 00230 _M_ptr = 0; 00231 return __tmp; 00232 } 00233 00234 /** 00235 * @brief Forcibly deletes the managed object. 00236 * @param __p A pointer (defaults to NULL). 00237 * 00238 * This object now @e owns the object pointed to by @a __p. The 00239 * previous object has been deleted. 00240 */ 00241 void 00242 reset(element_type* __p = 0) throw() 00243 { 00244 if (__p != _M_ptr) 00245 { 00246 delete _M_ptr; 00247 _M_ptr = __p; 00248 } 00249 } 00250 00251 /** 00252 * @brief Automatic conversions 00253 * 00254 * These operations are supposed to convert an %auto_ptr into and from 00255 * an auto_ptr_ref automatically as needed. This would allow 00256 * constructs such as 00257 * @code 00258 * auto_ptr<Derived> func_returning_auto_ptr(.....); 00259 * ... 00260 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....); 00261 * @endcode 00262 * 00263 * But it doesn't work, and won't be fixed. For further details see 00264 * http://cplusplus.github.io/LWG/lwg-closed.html#463 00265 */ 00266 auto_ptr(auto_ptr_ref<element_type> __ref) throw() 00267 : _M_ptr(__ref._M_ptr) { } 00268 00269 auto_ptr& 00270 operator=(auto_ptr_ref<element_type> __ref) throw() 00271 { 00272 if (__ref._M_ptr != this->get()) 00273 { 00274 delete _M_ptr; 00275 _M_ptr = __ref._M_ptr; 00276 } 00277 return *this; 00278 } 00279 00280 template<typename _Tp1> 00281 operator auto_ptr_ref<_Tp1>() throw() 00282 { return auto_ptr_ref<_Tp1>(this->release()); } 00283 00284 template<typename _Tp1> 00285 operator auto_ptr<_Tp1>() throw() 00286 { return auto_ptr<_Tp1>(this->release()); } 00287 } _GLIBCXX_DEPRECATED; 00288 00289 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00290 // 541. shared_ptr template assignment and void 00291 template<> 00292 class auto_ptr<void> 00293 { 00294 public: 00295 typedef void element_type; 00296 } _GLIBCXX_DEPRECATED; 00297 00298 #if __cplusplus >= 201103L 00299 template<_Lock_policy _Lp> 00300 template<typename _Tp> 00301 inline 00302 __shared_count<_Lp>::__shared_count(std::auto_ptr<_Tp>&& __r) 00303 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get())) 00304 { __r.release(); } 00305 00306 template<typename _Tp, _Lock_policy _Lp> 00307 template<typename _Tp1, typename> 00308 inline 00309 __shared_ptr<_Tp, _Lp>::__shared_ptr(std::auto_ptr<_Tp1>&& __r) 00310 : _M_ptr(__r.get()), _M_refcount() 00311 { 00312 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) 00313 static_assert( sizeof(_Tp1) > 0, "incomplete type" ); 00314 _Tp1* __tmp = __r.get(); 00315 _M_refcount = __shared_count<_Lp>(std::move(__r)); 00316 _M_enable_shared_from_this_with(__tmp); 00317 } 00318 00319 template<typename _Tp> 00320 template<typename _Tp1, typename> 00321 inline 00322 shared_ptr<_Tp>::shared_ptr(std::auto_ptr<_Tp1>&& __r) 00323 : __shared_ptr<_Tp>(std::move(__r)) { } 00324 00325 template<typename _Tp, typename _Dp> 00326 template<typename _Up, typename> 00327 inline 00328 unique_ptr<_Tp, _Dp>::unique_ptr(auto_ptr<_Up>&& __u) noexcept 00329 : _M_t(__u.release(), deleter_type()) { } 00330 #endif 00331 00332 #pragma GCC diagnostic pop 00333 00334 _GLIBCXX_END_NAMESPACE_VERSION 00335 } // namespace 00336 00337 #endif /* _BACKWARD_AUTO_PTR_H */