• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_IDL_AUTOPTR_H
17 #define OHOS_IDL_AUTOPTR_H
18 
19 namespace OHOS {
20 namespace Idl {
21 template <class T>
22 class AutoPtr {
23 public:
AutoPtr()24     inline AutoPtr() : mPtr(nullptr) {}
25 
26     AutoPtr(T *other);
27 
28     AutoPtr(const AutoPtr<T> &other);
29 
30     AutoPtr(AutoPtr<T> &&other);
31 
32     ~AutoPtr();
33 
34     AutoPtr &operator=(T *other);
35 
36     AutoPtr &operator=(const AutoPtr<T> &other);
37 
38     AutoPtr &operator=(AutoPtr<T> &&other);
39 
40     void MoveTo(T **other);
41 
42     inline operator T *() const;
43 
44     inline T **operator&();
45 
46     inline T *operator->() const;
47 
48     inline T &operator*() const;
49 
50     inline T *Get() const;
51 
52     inline bool operator==(T *other) const;
53 
54     inline bool operator==(const AutoPtr<T> &other) const;
55 
56     inline bool operator!=(T *other) const;
57 
58     inline bool operator!=(const AutoPtr<T> &other) const;
59 
60     inline bool operator>(T *other) const;
61 
62     inline bool operator>(const AutoPtr<T> &other) const;
63 
64     inline bool operator<(T *other) const;
65 
66     inline bool operator<(const AutoPtr<T> &other) const;
67 
68     inline bool operator<=(T *other) const;
69 
70     inline bool operator<=(const AutoPtr<T> &other) const;
71 
72     inline bool operator>=(T *other) const;
73 
74     inline bool operator>=(const AutoPtr<T> &other) const;
75 
76 private:
77     T *mPtr;
78 };
79 
80 template <class T>
AutoPtr(T * other)81 AutoPtr<T>::AutoPtr(T *other) : mPtr(other)
82 {
83     if (mPtr != nullptr) {
84         mPtr->AddRef();
85     }
86 }
87 
88 template <class T>
AutoPtr(const AutoPtr<T> & other)89 AutoPtr<T>::AutoPtr(const AutoPtr<T> &other) : mPtr(other.mPtr)
90 {
91     if (mPtr != nullptr) {
92         mPtr->AddRef();
93     }
94 }
95 
96 template <class T>
AutoPtr(AutoPtr<T> && other)97 AutoPtr<T>::AutoPtr(AutoPtr<T> &&other) : mPtr(other.mPtr)
98 {
99     other.mPtr = nullptr;
100 }
101 
102 template <class T>
~AutoPtr()103 AutoPtr<T>::~AutoPtr()
104 {
105     if (mPtr != nullptr) {
106         mPtr->Release();
107         mPtr = nullptr;
108     }
109 }
110 
111 template <class T>
112 AutoPtr<T> &AutoPtr<T>::operator=(T *other)
113 {
114     if (mPtr == other) {
115         return *this;
116     }
117     if (other != nullptr) {
118         other->AddRef();
119     }
120     if (mPtr != nullptr) {
121         mPtr->Release();
122     }
123     mPtr = other;
124     return *this;
125 }
126 
127 template <class T>
128 AutoPtr<T> &AutoPtr<T>::operator=(const AutoPtr<T> &other)
129 {
130     if (mPtr == other.mPtr) {
131         return *this;
132     }
133     if (other.mPtr != nullptr) {
134         other.mPtr->AddRef();
135     }
136     if (mPtr != nullptr) {
137         mPtr->Release();
138     }
139     mPtr = other.mPtr;
140     return *this;
141 }
142 
143 template <class T>
144 AutoPtr<T> &AutoPtr<T>::operator=(AutoPtr<T> &&other)
145 {
146     if (mPtr != nullptr) {
147         mPtr->Release();
148     }
149     mPtr = other.mPtr;
150     other.mPtr = nullptr;
151     return *this;
152 }
153 
154 template <class T>
MoveTo(T ** other)155 void AutoPtr<T>::MoveTo(T **other)
156 {
157     if (other != nullptr) {
158         *other = mPtr;
159         mPtr = nullptr;
160     }
161 }
162 
163 template <class T>
164 AutoPtr<T>::operator T *() const
165 {
166     return mPtr;
167 }
168 
169 template <class T>
170 T **AutoPtr<T>::operator&()
171 {
172     return &mPtr;
173 }
174 
175 template <class T>
176 T *AutoPtr<T>::operator->() const
177 {
178     return mPtr;
179 }
180 
181 template <class T>
182 T &AutoPtr<T>::operator*() const
183 {
184     return *mPtr;
185 }
186 
187 template <class T>
Get()188 T *AutoPtr<T>::Get() const
189 {
190     return mPtr;
191 }
192 
193 template <class T>
194 bool AutoPtr<T>::operator==(T *other) const
195 {
196     return mPtr == other;
197 }
198 
199 template <class T>
200 bool AutoPtr<T>::operator==(const AutoPtr<T> &other) const
201 {
202     return mPtr == other.mPtr;
203 }
204 
205 template <class T>
206 bool AutoPtr<T>::operator!=(T *other) const
207 {
208     return mPtr != other;
209 }
210 
211 template <class T>
212 bool AutoPtr<T>::operator!=(const AutoPtr<T> &other) const
213 {
214     return mPtr != other.mPtr;
215 }
216 
217 template <class T>
218 bool AutoPtr<T>::operator>(T *other) const
219 {
220     return mPtr > other;
221 }
222 
223 template <class T>
224 bool AutoPtr<T>::operator>(const AutoPtr<T> &other) const
225 {
226     return mPtr > other.mPtr;
227 }
228 
229 template <class T>
230 bool AutoPtr<T>::operator<(T *other) const
231 {
232     return mPtr < other;
233 }
234 
235 template <class T>
236 bool AutoPtr<T>::operator<(const AutoPtr<T> &other) const
237 {
238     return mPtr < other.mPtr;
239 }
240 
241 template <class T>
242 bool AutoPtr<T>::operator<=(T *other) const
243 {
244     return mPtr <= other;
245 }
246 
247 template <class T>
248 bool AutoPtr<T>::operator<=(const AutoPtr<T> &other) const
249 {
250     return mPtr <= other.mPtr;
251 }
252 
253 template <class T>
254 bool AutoPtr<T>::operator>=(T *other) const
255 {
256     return mPtr >= other;
257 }
258 
259 template <class T>
260 bool AutoPtr<T>::operator>=(const AutoPtr<T> &other) const
261 {
262     return mPtr >= other.mPtr;
263 }
264 } // namespace Idl
265 } // namespace OHOS
266 
267 #endif // OHOS_IDL_AUTOPTR_H