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