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 10 #ifndef _LIBCPP___MEMORY_AUTO_PTR_H 11 #define _LIBCPP___MEMORY_AUTO_PTR_H 12 13 #include <__config> 14 #include <__nullptr> 15 16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 17 #pragma GCC system_header 18 #endif 19 20 _LIBCPP_PUSH_MACROS 21 #include <__undef_macros> 22 23 _LIBCPP_BEGIN_NAMESPACE_STD 24 25 template <class _Tp> 26 struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref 27 { 28 _Tp* __ptr_; 29 }; 30 31 template<class _Tp> 32 class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr 33 { 34 private: 35 _Tp* __ptr_; 36 public: 37 typedef _Tp element_type; 38 39 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {} 40 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {} 41 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT 42 : __ptr_(__p.release()) {} 43 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT 44 {reset(__p.release()); return *this;} 45 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT 46 {reset(__p.release()); return *this;} 47 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT 48 {reset(__p.__ptr_); return *this;} 49 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;} 50 51 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT 52 {return *__ptr_;} 53 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;} 54 _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;} 55 _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT 56 { 57 _Tp* __t = __ptr_; 58 __ptr_ = nullptr; 59 return __t; 60 } 61 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT 62 { 63 if (__ptr_ != __p) 64 delete __ptr_; 65 __ptr_ = __p; 66 } 67 68 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {} 69 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT 70 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;} 71 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT 72 {return auto_ptr<_Up>(release());} 73 }; 74 75 template <> 76 class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void> 77 { 78 public: 79 typedef void element_type; 80 }; 81 82 _LIBCPP_END_NAMESPACE_STD 83 84 _LIBCPP_POP_MACROS 85 86 #endif // _LIBCPP___MEMORY_AUTO_PTR_H 87