1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef OHOS_HDI_AUTOPTR_H
10 #define OHOS_HDI_AUTOPTR_H
11 
12 namespace OHOS {
13 namespace HDI {
14 template <class T>
15 class AutoPtr {
16 public:
AutoPtr()17     inline AutoPtr() : mPtr(nullptr) {}
18 
19     AutoPtr(T *other);
20 
21     AutoPtr(const AutoPtr<T> &other);
22 
23     AutoPtr(AutoPtr<T> &&other);
24 
25     ~AutoPtr();
26 
27     AutoPtr &operator=(T *other);
28 
29     AutoPtr &operator=(const AutoPtr<T> &other);
30 
31     AutoPtr &operator=(AutoPtr<T> &&other);
32 
33     void MoveTo(T **other);
34 
35     inline operator T *() const;
36 
37     inline T **operator&();
38 
39     inline T *operator->() const;
40 
41     inline T &operator*() const;
42 
43     inline T *Get() const;
44 
45     inline bool operator==(T *other) const;
46 
47     inline bool operator==(const AutoPtr<T> &other) const;
48 
49     inline bool operator!=(T *other) const;
50 
51     inline bool operator!=(const AutoPtr<T> &other) const;
52 
53     inline bool operator>(T *other) const;
54 
55     inline bool operator>(const AutoPtr<T> &other) const;
56 
57     inline bool operator<(T *other) const;
58 
59     inline bool operator<(const AutoPtr<T> &other) const;
60 
61     inline bool operator<=(T *other) const;
62 
63     inline bool operator<=(const AutoPtr<T> &other) const;
64 
65     inline bool operator>=(T *other) const;
66 
67     inline bool operator>=(const AutoPtr<T> &other) const;
68 
69 private:
70     T *mPtr;
71 };
72 
73 template <class T>
AutoPtr(T * other)74 AutoPtr<T>::AutoPtr(T *other) : mPtr(other)
75 {
76     if (mPtr != nullptr) {
77         mPtr->AddRef();
78     }
79 }
80 
81 template <class T>
AutoPtr(const AutoPtr<T> & other)82 AutoPtr<T>::AutoPtr(const AutoPtr<T> &other) : mPtr(other.mPtr)
83 {
84     if (mPtr != nullptr) {
85         mPtr->AddRef();
86     }
87 }
88 
89 template <class T>
AutoPtr(AutoPtr<T> && other)90 AutoPtr<T>::AutoPtr(AutoPtr<T> &&other) : mPtr(other.mPtr)
91 {
92     other.mPtr = nullptr;
93 }
94 
95 template <class T>
~AutoPtr()96 AutoPtr<T>::~AutoPtr()
97 {
98     if (mPtr != nullptr) {
99         mPtr->Release();
100     }
101 }
102 
103 template <class T>
104 AutoPtr<T> &AutoPtr<T>::operator=(T *other)
105 {
106     if (mPtr == other) {
107         return *this;
108     }
109     if (other != nullptr) {
110         other->AddRef();
111     }
112     if (mPtr != nullptr) {
113         mPtr->Release();
114     }
115     mPtr = other;
116     return *this;
117 }
118 
119 template <class T>
120 AutoPtr<T> &AutoPtr<T>::operator=(const AutoPtr<T> &other)
121 {
122     if (mPtr == other.mPtr) {
123         return *this;
124     }
125     if (other.mPtr != nullptr) {
126         other.mPtr->AddRef();
127     }
128     if (mPtr != nullptr) {
129         mPtr->Release();
130     }
131     mPtr = other.mPtr;
132     return *this;
133 }
134 
135 template <class T>
136 AutoPtr<T> &AutoPtr<T>::operator=(AutoPtr<T> &&other)
137 {
138     if (mPtr != nullptr) {
139         mPtr->Release();
140     }
141     mPtr = other.mPtr;
142     other.mPtr = nullptr;
143     return *this;
144 }
145 
146 template <class T>
MoveTo(T ** other)147 void AutoPtr<T>::MoveTo(T **other)
148 {
149     if (other != nullptr) {
150         *other = mPtr;
151         mPtr = nullptr;
152     }
153 }
154 
155 template <class T>
156 AutoPtr<T>::operator T *() const
157 {
158     return mPtr;
159 }
160 
161 template <class T>
162 T **AutoPtr<T>::operator&()
163 {
164     return &mPtr;
165 }
166 
167 template <class T>
168 T *AutoPtr<T>::operator->() const
169 {
170     return mPtr;
171 }
172 
173 template <class T>
174 T &AutoPtr<T>::operator*() const
175 {
176     return *mPtr;
177 }
178 
179 template <class T>
Get()180 T *AutoPtr<T>::Get() const
181 {
182     return mPtr;
183 }
184 
185 template <class T>
186 bool AutoPtr<T>::operator==(T *other) const
187 {
188     return mPtr == other;
189 }
190 
191 template <class T>
192 bool AutoPtr<T>::operator==(const AutoPtr<T> &other) const
193 {
194     return mPtr == other.mPtr;
195 }
196 
197 template <class T>
198 bool AutoPtr<T>::operator!=(T *other) const
199 {
200     return mPtr != other;
201 }
202 
203 template <class T>
204 bool AutoPtr<T>::operator!=(const AutoPtr<T> &other) const
205 {
206     return mPtr != other.mPtr;
207 }
208 
209 template <class T>
210 bool AutoPtr<T>::operator>(T *other) const
211 {
212     return mPtr > other;
213 }
214 
215 template <class T>
216 bool AutoPtr<T>::operator>(const AutoPtr<T> &other) const
217 {
218     return mPtr > other.mPtr;
219 }
220 
221 template <class T>
222 bool AutoPtr<T>::operator<(T *other) const
223 {
224     return mPtr < other;
225 }
226 
227 template <class T>
228 bool AutoPtr<T>::operator<(const AutoPtr<T> &other) const
229 {
230     return mPtr < other.mPtr;
231 }
232 
233 template <class T>
234 bool AutoPtr<T>::operator<=(T *other) const
235 {
236     return mPtr <= other;
237 }
238 
239 template <class T>
240 bool AutoPtr<T>::operator<=(const AutoPtr<T> &other) const
241 {
242     return mPtr <= other.mPtr;
243 }
244 
245 template <class T>
246 bool AutoPtr<T>::operator>=(T *other) const
247 {
248     return mPtr >= other;
249 }
250 
251 template <class T>
252 bool AutoPtr<T>::operator>=(const AutoPtr<T> &other) const
253 {
254     return mPtr >= other.mPtr;
255 }
256 } // namespace HDI
257 } // namespace OHOS
258 
259 #endif // OHOS_HDI_AUTOPTR_H