xref: /freebsd/contrib/llvm-project/libcxx/include/__memory/auto_ptr.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
1fe6060f1SDimitry Andric // -*- C++ -*-
2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
3fe6060f1SDimitry Andric //
4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7fe6060f1SDimitry Andric //
8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
9fe6060f1SDimitry Andric 
10fe6060f1SDimitry Andric #ifndef _LIBCPP___MEMORY_AUTO_PTR_H
11fe6060f1SDimitry Andric #define _LIBCPP___MEMORY_AUTO_PTR_H
12fe6060f1SDimitry Andric 
13fe6060f1SDimitry Andric #include <__config>
14fe6060f1SDimitry Andric 
15fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16fe6060f1SDimitry Andric #  pragma GCC system_header
17fe6060f1SDimitry Andric #endif
18fe6060f1SDimitry Andric 
1981ad6265SDimitry Andric #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2081ad6265SDimitry Andric 
21fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
22fe6060f1SDimitry Andric 
23fe6060f1SDimitry Andric template <class _Tp>
24*cb14a3feSDimitry Andric struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref {
25fe6060f1SDimitry Andric   _Tp* __ptr_;
26fe6060f1SDimitry Andric };
27fe6060f1SDimitry Andric 
28fe6060f1SDimitry Andric template <class _Tp>
29*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr {
30fe6060f1SDimitry Andric private:
31fe6060f1SDimitry Andric   _Tp* __ptr_;
32*cb14a3feSDimitry Andric 
33fe6060f1SDimitry Andric public:
34fe6060f1SDimitry Andric   typedef _Tp element_type;
35fe6060f1SDimitry Andric 
365f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {}
375f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {}
38*cb14a3feSDimitry Andric   template <class _Up>
39*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT : __ptr_(__p.release()) {}
40*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT {
41*cb14a3feSDimitry Andric     reset(__p.release());
42*cb14a3feSDimitry Andric     return *this;
43*cb14a3feSDimitry Andric   }
44*cb14a3feSDimitry Andric   template <class _Up>
45*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT {
46*cb14a3feSDimitry Andric     reset(__p.release());
47*cb14a3feSDimitry Andric     return *this;
48*cb14a3feSDimitry Andric   }
49*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT {
50*cb14a3feSDimitry Andric     reset(__p.__ptr_);
51*cb14a3feSDimitry Andric     return *this;
52*cb14a3feSDimitry Andric   }
535f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI ~auto_ptr() _NOEXCEPT { delete __ptr_; }
54fe6060f1SDimitry Andric 
55*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp& operator*() const _NOEXCEPT { return *__ptr_; }
565f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* operator->() const _NOEXCEPT { return __ptr_; }
575f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* get() const _NOEXCEPT { return __ptr_; }
58*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* release() _NOEXCEPT {
59fe6060f1SDimitry Andric     _Tp* __t = __ptr_;
60fe6060f1SDimitry Andric     __ptr_   = nullptr;
61fe6060f1SDimitry Andric     return __t;
62fe6060f1SDimitry Andric   }
63*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void reset(_Tp* __p = 0) _NOEXCEPT {
64fe6060f1SDimitry Andric     if (__ptr_ != __p)
65fe6060f1SDimitry Andric       delete __ptr_;
66fe6060f1SDimitry Andric     __ptr_ = __p;
67fe6060f1SDimitry Andric   }
68fe6060f1SDimitry Andric 
695f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
70*cb14a3feSDimitry Andric   template <class _Up>
71*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI operator auto_ptr_ref<_Up>() _NOEXCEPT {
72*cb14a3feSDimitry Andric     auto_ptr_ref<_Up> __t;
73*cb14a3feSDimitry Andric     __t.__ptr_ = release();
74*cb14a3feSDimitry Andric     return __t;
75*cb14a3feSDimitry Andric   }
76*cb14a3feSDimitry Andric   template <class _Up>
77*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI operator auto_ptr<_Up>() _NOEXCEPT {
78*cb14a3feSDimitry Andric     return auto_ptr<_Up>(release());
79*cb14a3feSDimitry Andric   }
80fe6060f1SDimitry Andric };
81fe6060f1SDimitry Andric 
82fe6060f1SDimitry Andric template <>
83*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void> {
84fe6060f1SDimitry Andric public:
85fe6060f1SDimitry Andric   typedef void element_type;
86fe6060f1SDimitry Andric };
87fe6060f1SDimitry Andric 
88fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
89fe6060f1SDimitry Andric 
9081ad6265SDimitry Andric #endif // _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
9181ad6265SDimitry Andric 
92fe6060f1SDimitry Andric #endif // _LIBCPP___MEMORY_AUTO_PTR_H
93