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