auto_ptr.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___MEMORY_AUTO_PTR_H
  10. #define _LIBCPP___MEMORY_AUTO_PTR_H
  11. #include <__config>
  12. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  13. # pragma GCC system_header
  14. #endif
  15. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  16. _LIBCPP_BEGIN_NAMESPACE_STD
  17. template <class _Tp>
  18. struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref {
  19. _Tp* __ptr_;
  20. };
  21. template <class _Tp>
  22. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr {
  23. private:
  24. _Tp* __ptr_;
  25. public:
  26. typedef _Tp element_type;
  27. _LIBCPP_HIDE_FROM_ABI explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
  28. _LIBCPP_HIDE_FROM_ABI auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
  29. template <class _Up>
  30. _LIBCPP_HIDE_FROM_ABI auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT : __ptr_(__p.release()) {}
  31. _LIBCPP_HIDE_FROM_ABI auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT {
  32. reset(__p.release());
  33. return *this;
  34. }
  35. template <class _Up>
  36. _LIBCPP_HIDE_FROM_ABI auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT {
  37. reset(__p.release());
  38. return *this;
  39. }
  40. _LIBCPP_HIDE_FROM_ABI auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT {
  41. reset(__p.__ptr_);
  42. return *this;
  43. }
  44. _LIBCPP_HIDE_FROM_ABI ~auto_ptr() _NOEXCEPT { delete __ptr_; }
  45. _LIBCPP_HIDE_FROM_ABI _Tp& operator*() const _NOEXCEPT { return *__ptr_; }
  46. _LIBCPP_HIDE_FROM_ABI _Tp* operator->() const _NOEXCEPT { return __ptr_; }
  47. _LIBCPP_HIDE_FROM_ABI _Tp* get() const _NOEXCEPT { return __ptr_; }
  48. _LIBCPP_HIDE_FROM_ABI _Tp* release() _NOEXCEPT {
  49. _Tp* __t = __ptr_;
  50. __ptr_ = nullptr;
  51. return __t;
  52. }
  53. _LIBCPP_HIDE_FROM_ABI void reset(_Tp* __p = 0) _NOEXCEPT {
  54. if (__ptr_ != __p)
  55. delete __ptr_;
  56. __ptr_ = __p;
  57. }
  58. _LIBCPP_HIDE_FROM_ABI auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
  59. template <class _Up>
  60. _LIBCPP_HIDE_FROM_ABI operator auto_ptr_ref<_Up>() _NOEXCEPT {
  61. auto_ptr_ref<_Up> __t;
  62. __t.__ptr_ = release();
  63. return __t;
  64. }
  65. template <class _Up>
  66. _LIBCPP_HIDE_FROM_ABI operator auto_ptr<_Up>() _NOEXCEPT {
  67. return auto_ptr<_Up>(release());
  68. }
  69. };
  70. template <>
  71. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void> {
  72. public:
  73. typedef void element_type;
  74. };
  75. _LIBCPP_END_NAMESPACE_STD
  76. #endif // _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  77. #endif // _LIBCPP___MEMORY_AUTO_PTR_H