1 /* 2 * Copyright (c) 2022 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 AutoPtr() 25 : mPtr(nullptr) 26 {} 27 28 AutoPtr(T* other); 29 30 AutoPtr(const AutoPtr<T>& other); 31 32 AutoPtr(AutoPtr<T>&& other); 33 34 ~AutoPtr(); 35 36 AutoPtr& operator=(T* other); 37 38 AutoPtr& operator=(const AutoPtr<T>& other); 39 40 AutoPtr& operator=(AutoPtr<T>&& other); 41 42 void MoveTo(T** other); 43 44 inline operator T*() const; 45 46 inline T** operator&(); 47 48 inline T* operator->() const; 49 50 inline T& operator*() const; 51 52 inline T* Get() const; 53 54 inline bool operator==(T* other) const; 55 56 inline bool operator==(const AutoPtr<T>& other) const; 57 58 inline bool operator!=(T* other) const; 59 60 inline bool operator!=(const AutoPtr<T>& other) const; 61 62 inline bool operator>(T* other) const; 63 64 inline bool operator>(const AutoPtr<T>& other) const; 65 66 inline bool operator<(T* other) const; 67 68 inline bool operator<(const AutoPtr<T>& other) const; 69 70 inline bool operator<=(T* other) const; 71 72 inline bool operator<=(const AutoPtr<T>& other) const; 73 74 inline bool operator>=(T* other) const; 75 76 inline bool operator>=(const AutoPtr<T>& other) const; 77 78 private: 79 T* mPtr; 80 }; 81 82 template<class T> AutoPtr(T * other)83AutoPtr<T>::AutoPtr(T* other) 84 : mPtr(other) 85 { 86 if (mPtr != nullptr) { 87 mPtr->AddRef(); 88 } 89 } 90 91 template<class T> AutoPtr(const AutoPtr<T> & other)92AutoPtr<T>::AutoPtr(const AutoPtr<T>& other) 93 : mPtr(other.mPtr) 94 { 95 if (mPtr != nullptr) { 96 mPtr->AddRef(); 97 } 98 } 99 100 template<class T> AutoPtr(AutoPtr<T> && other)101AutoPtr<T>::AutoPtr(AutoPtr<T>&& other) 102 : mPtr(other.mPtr) 103 { 104 other.mPtr = nullptr; 105 } 106 107 template<class T> ~AutoPtr()108AutoPtr<T>::~AutoPtr() 109 { 110 if (mPtr != nullptr) { 111 mPtr->Release(); 112 } 113 } 114 115 template<class T> 116 AutoPtr<T>& AutoPtr<T>::operator=(T* other) 117 { 118 if (mPtr == other) return *this; 119 120 if (other != nullptr) { 121 other->AddRef(); 122 } 123 if (mPtr != nullptr) { 124 mPtr->Release(); 125 } 126 mPtr = other; 127 return *this; 128 } 129 130 template<class T> 131 AutoPtr<T>& AutoPtr<T>::operator=(const AutoPtr<T>& other) 132 { 133 if (mPtr == other.mPtr) return *this; 134 135 if (other.mPtr != nullptr) { 136 other.mPtr->AddRef(); 137 } 138 if (mPtr != nullptr) { 139 mPtr->Release(); 140 } 141 mPtr = other.mPtr; 142 return *this; 143 } 144 145 template<class T> 146 AutoPtr<T>& AutoPtr<T>::operator=(AutoPtr<T>&& other) 147 { 148 if (mPtr != nullptr) { 149 mPtr->Release(); 150 } 151 mPtr = other.mPtr; 152 other.mPtr = nullptr; 153 return *this; 154 } 155 156 template<class T> MoveTo(T ** other)157void AutoPtr<T>::MoveTo(T** other) 158 { 159 if (other != nullptr) { 160 *other = mPtr; 161 mPtr = nullptr; 162 } 163 } 164 165 template<class T> 166 AutoPtr<T>::operator T*() const 167 { 168 return mPtr; 169 } 170 171 template<class T> 172 T** AutoPtr<T>::operator&() 173 { 174 return &mPtr; 175 } 176 177 template<class T> 178 T* AutoPtr<T>::operator->() const 179 { 180 return mPtr; 181 } 182 183 template<class T> 184 T& AutoPtr<T>::operator*() const 185 { 186 return *mPtr; 187 } 188 189 template<class T> Get()190T* AutoPtr<T>::Get() const 191 { 192 return mPtr; 193 } 194 195 template<class T> 196 bool AutoPtr<T>::operator==(T* other) const 197 { 198 return mPtr == other; 199 } 200 201 template<class T> 202 bool AutoPtr<T>::operator==(const AutoPtr<T>& other) const 203 { 204 return mPtr == other.mPtr; 205 } 206 207 template<class T> 208 bool AutoPtr<T>::operator!=(T* other) const 209 { 210 return mPtr != other; 211 } 212 213 template<class T> 214 bool AutoPtr<T>::operator!=(const AutoPtr<T>& other) const 215 { 216 return mPtr != other.mPtr; 217 } 218 219 template<class T> 220 bool AutoPtr<T>::operator>(T* other) const 221 { 222 return mPtr > other; 223 } 224 225 template<class T> 226 bool AutoPtr<T>::operator>(const AutoPtr<T>& other) const 227 { 228 return mPtr > other.mPtr; 229 } 230 231 template<class T> 232 bool AutoPtr<T>::operator<(T* other) const 233 { 234 return mPtr < other; 235 } 236 237 template<class T> 238 bool AutoPtr<T>::operator<(const AutoPtr<T>& other) const 239 { 240 return mPtr < other.mPtr; 241 } 242 243 template<class T> 244 bool AutoPtr<T>::operator<=(T* other) const 245 { 246 return mPtr <= other; 247 } 248 249 template<class T> 250 bool AutoPtr<T>::operator<=(const AutoPtr<T>& other) const 251 { 252 return mPtr <= other.mPtr; 253 } 254 255 template<class T> 256 bool AutoPtr<T>::operator>=(T* other) const 257 { 258 return mPtr >= other; 259 } 260 261 template<class T> 262 bool AutoPtr<T>::operator>=(const AutoPtr<T>& other) const 263 { 264 return mPtr >= other.mPtr; 265 } 266 } 267 } 268 #endif // OHOS_IDL_AUTOPTR_H 269