auto_ptr.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. {
  20. _Tp* __ptr_;
  21. };
  22. template<class _Tp>
  23. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
  24. {
  25. private:
  26. _Tp* __ptr_;
  27. public:
  28. typedef _Tp element_type;
  29. _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
  30. _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
  31. template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT
  32. : __ptr_(__p.release()) {}
  33. _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT
  34. {reset(__p.release()); return *this;}
  35. template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT
  36. {reset(__p.release()); return *this;}
  37. _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT
  38. {reset(__p.__ptr_); return *this;}
  39. _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;}
  40. _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT
  41. {return *__ptr_;}
  42. _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;}
  43. _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;}
  44. _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT
  45. {
  46. _Tp* __t = __ptr_;
  47. __ptr_ = nullptr;
  48. return __t;
  49. }
  50. _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT
  51. {
  52. if (__ptr_ != __p)
  53. delete __ptr_;
  54. __ptr_ = __p;
  55. }
  56. _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
  57. template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT
  58. {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
  59. template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT
  60. {return auto_ptr<_Up>(release());}
  61. };
  62. template <>
  63. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
  64. {
  65. public:
  66. typedef void element_type;
  67. };
  68. _LIBCPP_END_NAMESPACE_STD
  69. #endif // _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  70. #endif // _LIBCPP___MEMORY_AUTO_PTR_H