auto_ptr.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // auto_ptr implementation -*- C++ -*-
  2. // Copyright (C) 2007-2017 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file backward/auto_ptr.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{memory}
  23. */
  24. #ifndef _BACKWARD_AUTO_PTR_H
  25. #define _BACKWARD_AUTO_PTR_H 1
  26. #include <bits/c++config.h>
  27. #include <debug/debug.h>
  28. namespace std _GLIBCXX_VISIBILITY(default)
  29. {
  30. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  31. /**
  32. * A wrapper class to provide auto_ptr with reference semantics.
  33. * For example, an auto_ptr can be assigned (or constructed from)
  34. * the result of a function which returns an auto_ptr by value.
  35. *
  36. * All the auto_ptr_ref stuff should happen behind the scenes.
  37. */
  38. template<typename _Tp1>
  39. struct auto_ptr_ref
  40. {
  41. _Tp1* _M_ptr;
  42. explicit
  43. auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
  44. } _GLIBCXX_DEPRECATED;
  45. /**
  46. * @brief A simple smart pointer providing strict ownership semantics.
  47. *
  48. * The Standard says:
  49. * <pre>
  50. * An @c auto_ptr owns the object it holds a pointer to. Copying
  51. * an @c auto_ptr copies the pointer and transfers ownership to the
  52. * destination. If more than one @c auto_ptr owns the same object
  53. * at the same time the behavior of the program is undefined.
  54. *
  55. * The uses of @c auto_ptr include providing temporary
  56. * exception-safety for dynamically allocated memory, passing
  57. * ownership of dynamically allocated memory to a function, and
  58. * returning dynamically allocated memory from a function. @c
  59. * auto_ptr does not meet the CopyConstructible and Assignable
  60. * requirements for Standard Library <a
  61. * href="tables.html#65">container</a> elements and thus
  62. * instantiating a Standard Library container with an @c auto_ptr
  63. * results in undefined behavior.
  64. * </pre>
  65. * Quoted from [20.4.5]/3.
  66. *
  67. * Good examples of what can and cannot be done with auto_ptr can
  68. * be found in the libstdc++ testsuite.
  69. *
  70. * _GLIBCXX_RESOLVE_LIB_DEFECTS
  71. * 127. auto_ptr<> conversion issues
  72. * These resolutions have all been incorporated.
  73. */
  74. template<typename _Tp>
  75. class auto_ptr
  76. {
  77. private:
  78. _Tp* _M_ptr;
  79. public:
  80. /// The pointed-to type.
  81. typedef _Tp element_type;
  82. /**
  83. * @brief An %auto_ptr is usually constructed from a raw pointer.
  84. * @param __p A pointer (defaults to NULL).
  85. *
  86. * This object now @e owns the object pointed to by @a __p.
  87. */
  88. explicit
  89. auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
  90. /**
  91. * @brief An %auto_ptr can be constructed from another %auto_ptr.
  92. * @param __a Another %auto_ptr of the same type.
  93. *
  94. * This object now @e owns the object previously owned by @a __a,
  95. * which has given up ownership.
  96. */
  97. auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
  98. /**
  99. * @brief An %auto_ptr can be constructed from another %auto_ptr.
  100. * @param __a Another %auto_ptr of a different but related type.
  101. *
  102. * A pointer-to-Tp1 must be convertible to a
  103. * pointer-to-Tp/element_type.
  104. *
  105. * This object now @e owns the object previously owned by @a __a,
  106. * which has given up ownership.
  107. */
  108. template<typename _Tp1>
  109. auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
  110. /**
  111. * @brief %auto_ptr assignment operator.
  112. * @param __a Another %auto_ptr of the same type.
  113. *
  114. * This object now @e owns the object previously owned by @a __a,
  115. * which has given up ownership. The object that this one @e
  116. * used to own and track has been deleted.
  117. */
  118. auto_ptr&
  119. operator=(auto_ptr& __a) throw()
  120. {
  121. reset(__a.release());
  122. return *this;
  123. }
  124. /**
  125. * @brief %auto_ptr assignment operator.
  126. * @param __a Another %auto_ptr of a different but related type.
  127. *
  128. * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
  129. *
  130. * This object now @e owns the object previously owned by @a __a,
  131. * which has given up ownership. The object that this one @e
  132. * used to own and track has been deleted.
  133. */
  134. template<typename _Tp1>
  135. auto_ptr&
  136. operator=(auto_ptr<_Tp1>& __a) throw()
  137. {
  138. reset(__a.release());
  139. return *this;
  140. }
  141. /**
  142. * When the %auto_ptr goes out of scope, the object it owns is
  143. * deleted. If it no longer owns anything (i.e., @c get() is
  144. * @c NULL), then this has no effect.
  145. *
  146. * The C++ standard says there is supposed to be an empty throw
  147. * specification here, but omitting it is standard conforming. Its
  148. * presence can be detected only if _Tp::~_Tp() throws, but this is
  149. * prohibited. [17.4.3.6]/2
  150. */
  151. ~auto_ptr() { delete _M_ptr; }
  152. /**
  153. * @brief Smart pointer dereferencing.
  154. *
  155. * If this %auto_ptr no longer owns anything, then this
  156. * operation will crash. (For a smart pointer, <em>no longer owns
  157. * anything</em> is the same as being a null pointer, and you know
  158. * what happens when you dereference one of those...)
  159. */
  160. element_type&
  161. operator*() const throw()
  162. {
  163. __glibcxx_assert(_M_ptr != 0);
  164. return *_M_ptr;
  165. }
  166. /**
  167. * @brief Smart pointer dereferencing.
  168. *
  169. * This returns the pointer itself, which the language then will
  170. * automatically cause to be dereferenced.
  171. */
  172. element_type*
  173. operator->() const throw()
  174. {
  175. __glibcxx_assert(_M_ptr != 0);
  176. return _M_ptr;
  177. }
  178. /**
  179. * @brief Bypassing the smart pointer.
  180. * @return The raw pointer being managed.
  181. *
  182. * You can get a copy of the pointer that this object owns, for
  183. * situations such as passing to a function which only accepts
  184. * a raw pointer.
  185. *
  186. * @note This %auto_ptr still owns the memory.
  187. */
  188. element_type*
  189. get() const throw() { return _M_ptr; }
  190. /**
  191. * @brief Bypassing the smart pointer.
  192. * @return The raw pointer being managed.
  193. *
  194. * You can get a copy of the pointer that this object owns, for
  195. * situations such as passing to a function which only accepts
  196. * a raw pointer.
  197. *
  198. * @note This %auto_ptr no longer owns the memory. When this object
  199. * goes out of scope, nothing will happen.
  200. */
  201. element_type*
  202. release() throw()
  203. {
  204. element_type* __tmp = _M_ptr;
  205. _M_ptr = 0;
  206. return __tmp;
  207. }
  208. /**
  209. * @brief Forcibly deletes the managed object.
  210. * @param __p A pointer (defaults to NULL).
  211. *
  212. * This object now @e owns the object pointed to by @a __p. The
  213. * previous object has been deleted.
  214. */
  215. void
  216. reset(element_type* __p = 0) throw()
  217. {
  218. if (__p != _M_ptr)
  219. {
  220. delete _M_ptr;
  221. _M_ptr = __p;
  222. }
  223. }
  224. /**
  225. * @brief Automatic conversions
  226. *
  227. * These operations are supposed to convert an %auto_ptr into and from
  228. * an auto_ptr_ref automatically as needed. This would allow
  229. * constructs such as
  230. * @code
  231. * auto_ptr<Derived> func_returning_auto_ptr(.....);
  232. * ...
  233. * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
  234. * @endcode
  235. *
  236. * But it doesn't work, and won't be fixed. For further details see
  237. * http://cplusplus.github.io/LWG/lwg-closed.html#463
  238. */
  239. auto_ptr(auto_ptr_ref<element_type> __ref) throw()
  240. : _M_ptr(__ref._M_ptr) { }
  241. auto_ptr&
  242. operator=(auto_ptr_ref<element_type> __ref) throw()
  243. {
  244. if (__ref._M_ptr != this->get())
  245. {
  246. delete _M_ptr;
  247. _M_ptr = __ref._M_ptr;
  248. }
  249. return *this;
  250. }
  251. template<typename _Tp1>
  252. operator auto_ptr_ref<_Tp1>() throw()
  253. { return auto_ptr_ref<_Tp1>(this->release()); }
  254. template<typename _Tp1>
  255. operator auto_ptr<_Tp1>() throw()
  256. { return auto_ptr<_Tp1>(this->release()); }
  257. } _GLIBCXX_DEPRECATED;
  258. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  259. // 541. shared_ptr template assignment and void
  260. template<>
  261. class auto_ptr<void>
  262. {
  263. public:
  264. typedef void element_type;
  265. } _GLIBCXX_DEPRECATED;
  266. #if __cplusplus >= 201103L
  267. template<_Lock_policy _Lp>
  268. template<typename _Tp>
  269. inline
  270. __shared_count<_Lp>::__shared_count(std::auto_ptr<_Tp>&& __r)
  271. : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
  272. { __r.release(); }
  273. template<typename _Tp, _Lock_policy _Lp>
  274. template<typename _Tp1, typename>
  275. inline
  276. __shared_ptr<_Tp, _Lp>::__shared_ptr(std::auto_ptr<_Tp1>&& __r)
  277. : _M_ptr(__r.get()), _M_refcount()
  278. {
  279. __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
  280. static_assert( sizeof(_Tp1) > 0, "incomplete type" );
  281. _Tp1* __tmp = __r.get();
  282. _M_refcount = __shared_count<_Lp>(std::move(__r));
  283. _M_enable_shared_from_this_with(__tmp);
  284. }
  285. template<typename _Tp>
  286. template<typename _Tp1, typename>
  287. inline
  288. shared_ptr<_Tp>::shared_ptr(std::auto_ptr<_Tp1>&& __r)
  289. : __shared_ptr<_Tp>(std::move(__r)) { }
  290. template<typename _Tp, typename _Dp>
  291. template<typename _Up, typename>
  292. inline
  293. unique_ptr<_Tp, _Dp>::unique_ptr(auto_ptr<_Up>&& __u) noexcept
  294. : _M_t(__u.release(), deleter_type()) { }
  295. #endif
  296. _GLIBCXX_END_NAMESPACE_VERSION
  297. } // namespace
  298. #endif /* _BACKWARD_AUTO_PTR_H */