auto_ptr.h 2.7 KB

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