You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 line
11KB

  1. // auto_ptr implementation -*- C++ -*-
  2. // Copyright (C) 2007-2020 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. #pragma GCC diagnostic push
  46. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  47. /**
  48. * @brief A simple smart pointer providing strict ownership semantics.
  49. *
  50. * The Standard says:
  51. * <pre>
  52. * An @c auto_ptr owns the object it holds a pointer to. Copying
  53. * an @c auto_ptr copies the pointer and transfers ownership to the
  54. * destination. If more than one @c auto_ptr owns the same object
  55. * at the same time the behavior of the program is undefined.
  56. *
  57. * The uses of @c auto_ptr include providing temporary
  58. * exception-safety for dynamically allocated memory, passing
  59. * ownership of dynamically allocated memory to a function, and
  60. * returning dynamically allocated memory from a function. @c
  61. * auto_ptr does not meet the CopyConstructible and Assignable
  62. * requirements for Standard Library <a
  63. * href="tables.html#65">container</a> elements and thus
  64. * instantiating a Standard Library container with an @c auto_ptr
  65. * results in undefined behavior.
  66. * </pre>
  67. * Quoted from [20.4.5]/3.
  68. *
  69. * Good examples of what can and cannot be done with auto_ptr can
  70. * be found in the libstdc++ testsuite.
  71. *
  72. * _GLIBCXX_RESOLVE_LIB_DEFECTS
  73. * 127. auto_ptr<> conversion issues
  74. * These resolutions have all been incorporated.
  75. */
  76. template<typename _Tp>
  77. class auto_ptr
  78. {
  79. private:
  80. _Tp* _M_ptr;
  81. public:
  82. /// The pointed-to type.
  83. typedef _Tp element_type;
  84. /**
  85. * @brief An %auto_ptr is usually constructed from a raw pointer.
  86. * @param __p A pointer (defaults to NULL).
  87. *
  88. * This object now @e owns the object pointed to by @a __p.
  89. */
  90. explicit
  91. auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
  92. /**
  93. * @brief An %auto_ptr can be constructed from another %auto_ptr.
  94. * @param __a Another %auto_ptr of the same type.
  95. *
  96. * This object now @e owns the object previously owned by @a __a,
  97. * which has given up ownership.
  98. */
  99. auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
  100. /**
  101. * @brief An %auto_ptr can be constructed from another %auto_ptr.
  102. * @param __a Another %auto_ptr of a different but related type.
  103. *
  104. * A pointer-to-Tp1 must be convertible to a
  105. * pointer-to-Tp/element_type.
  106. *
  107. * This object now @e owns the object previously owned by @a __a,
  108. * which has given up ownership.
  109. */
  110. template<typename _Tp1>
  111. auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
  112. /**
  113. * @brief %auto_ptr assignment operator.
  114. * @param __a Another %auto_ptr of the same type.
  115. *
  116. * This object now @e owns the object previously owned by @a __a,
  117. * which has given up ownership. The object that this one @e
  118. * used to own and track has been deleted.
  119. */
  120. auto_ptr&
  121. operator=(auto_ptr& __a) throw()
  122. {
  123. reset(__a.release());
  124. return *this;
  125. }
  126. /**
  127. * @brief %auto_ptr assignment operator.
  128. * @param __a Another %auto_ptr of a different but related type.
  129. *
  130. * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
  131. *
  132. * This object now @e owns the object previously owned by @a __a,
  133. * which has given up ownership. The object that this one @e
  134. * used to own and track has been deleted.
  135. */
  136. template<typename _Tp1>
  137. auto_ptr&
  138. operator=(auto_ptr<_Tp1>& __a) throw()
  139. {
  140. reset(__a.release());
  141. return *this;
  142. }
  143. /**
  144. * When the %auto_ptr goes out of scope, the object it owns is
  145. * deleted. If it no longer owns anything (i.e., @c get() is
  146. * @c NULL), then this has no effect.
  147. *
  148. * The C++ standard says there is supposed to be an empty throw
  149. * specification here, but omitting it is standard conforming. Its
  150. * presence can be detected only if _Tp::~_Tp() throws, but this is
  151. * prohibited. [17.4.3.6]/2
  152. */
  153. ~auto_ptr() { delete _M_ptr; }
  154. /**
  155. * @brief Smart pointer dereferencing.
  156. *
  157. * If this %auto_ptr no longer owns anything, then this
  158. * operation will crash. (For a smart pointer, <em>no longer owns
  159. * anything</em> is the same as being a null pointer, and you know
  160. * what happens when you dereference one of those...)
  161. */
  162. element_type&
  163. operator*() const throw()
  164. {
  165. __glibcxx_assert(_M_ptr != 0);
  166. return *_M_ptr;
  167. }
  168. /**
  169. * @brief Smart pointer dereferencing.
  170. *
  171. * This returns the pointer itself, which the language then will
  172. * automatically cause to be dereferenced.
  173. */
  174. element_type*
  175. operator->() const throw()
  176. {
  177. __glibcxx_assert(_M_ptr != 0);
  178. return _M_ptr;
  179. }
  180. /**
  181. * @brief Bypassing the smart pointer.
  182. * @return The raw pointer being managed.
  183. *
  184. * You can get a copy of the pointer that this object owns, for
  185. * situations such as passing to a function which only accepts
  186. * a raw pointer.
  187. *
  188. * @note This %auto_ptr still owns the memory.
  189. */
  190. element_type*
  191. get() const throw() { return _M_ptr; }
  192. /**
  193. * @brief Bypassing the smart pointer.
  194. * @return The raw pointer being managed.
  195. *
  196. * You can get a copy of the pointer that this object owns, for
  197. * situations such as passing to a function which only accepts
  198. * a raw pointer.
  199. *
  200. * @note This %auto_ptr no longer owns the memory. When this object
  201. * goes out of scope, nothing will happen.
  202. */
  203. element_type*
  204. release() throw()
  205. {
  206. element_type* __tmp = _M_ptr;
  207. _M_ptr = 0;
  208. return __tmp;
  209. }
  210. /**
  211. * @brief Forcibly deletes the managed object.
  212. * @param __p A pointer (defaults to NULL).
  213. *
  214. * This object now @e owns the object pointed to by @a __p. The
  215. * previous object has been deleted.
  216. */
  217. void
  218. reset(element_type* __p = 0) throw()
  219. {
  220. if (__p != _M_ptr)
  221. {
  222. delete _M_ptr;
  223. _M_ptr = __p;
  224. }
  225. }
  226. /**
  227. * @brief Automatic conversions
  228. *
  229. * These operations are supposed to convert an %auto_ptr into and from
  230. * an auto_ptr_ref automatically as needed. This would allow
  231. * constructs such as
  232. * @code
  233. * auto_ptr<Derived> func_returning_auto_ptr(.....);
  234. * ...
  235. * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
  236. * @endcode
  237. *
  238. * But it doesn't work, and won't be fixed. For further details see
  239. * http://cplusplus.github.io/LWG/lwg-closed.html#463
  240. */
  241. auto_ptr(auto_ptr_ref<element_type> __ref) throw()
  242. : _M_ptr(__ref._M_ptr) { }
  243. auto_ptr&
  244. operator=(auto_ptr_ref<element_type> __ref) throw()
  245. {
  246. if (__ref._M_ptr != this->get())
  247. {
  248. delete _M_ptr;
  249. _M_ptr = __ref._M_ptr;
  250. }
  251. return *this;
  252. }
  253. template<typename _Tp1>
  254. operator auto_ptr_ref<_Tp1>() throw()
  255. { return auto_ptr_ref<_Tp1>(this->release()); }
  256. template<typename _Tp1>
  257. operator auto_ptr<_Tp1>() throw()
  258. { return auto_ptr<_Tp1>(this->release()); }
  259. } _GLIBCXX_DEPRECATED;
  260. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  261. // 541. shared_ptr template assignment and void
  262. template<>
  263. class auto_ptr<void>
  264. {
  265. public:
  266. typedef void element_type;
  267. } _GLIBCXX_DEPRECATED;
  268. #if __cplusplus >= 201103L
  269. template<_Lock_policy _Lp>
  270. template<typename _Tp>
  271. inline
  272. __shared_count<_Lp>::__shared_count(std::auto_ptr<_Tp>&& __r)
  273. : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
  274. { __r.release(); }
  275. template<typename _Tp, _Lock_policy _Lp>
  276. template<typename _Tp1, typename>
  277. inline
  278. __shared_ptr<_Tp, _Lp>::__shared_ptr(std::auto_ptr<_Tp1>&& __r)
  279. : _M_ptr(__r.get()), _M_refcount()
  280. {
  281. __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
  282. static_assert( sizeof(_Tp1) > 0, "incomplete type" );
  283. _Tp1* __tmp = __r.get();
  284. _M_refcount = __shared_count<_Lp>(std::move(__r));
  285. _M_enable_shared_from_this_with(__tmp);
  286. }
  287. template<typename _Tp>
  288. template<typename _Tp1, typename>
  289. inline
  290. shared_ptr<_Tp>::shared_ptr(std::auto_ptr<_Tp1>&& __r)
  291. : __shared_ptr<_Tp>(std::move(__r)) { }
  292. template<typename _Tp, typename _Dp>
  293. template<typename _Up, typename>
  294. inline
  295. unique_ptr<_Tp, _Dp>::unique_ptr(auto_ptr<_Up>&& __u) noexcept
  296. : _M_t(__u.release(), deleter_type()) { }
  297. #endif
  298. #pragma GCC diagnostic pop
  299. _GLIBCXX_END_NAMESPACE_VERSION
  300. } // namespace
  301. #endif /* _BACKWARD_AUTO_PTR_H */