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)81AutoPtr<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)89AutoPtr<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)97AutoPtr<T>::AutoPtr(AutoPtr<T> &&other) : mPtr(other.mPtr) 98 { 99 other.mPtr = nullptr; 100 } 101 102 template <class T> ~AutoPtr()103AutoPtr<T>::~AutoPtr() 104 { 105 if (mPtr != nullptr) { 106 mPtr->Release(); 107 } 108 } 109 110 template <class T> 111 AutoPtr<T> &AutoPtr<T>::operator=(T *other) 112 { 113 if (mPtr == other) { 114 return *this; 115 } 116 if (other != nullptr) { 117 other->AddRef(); 118 } 119 if (mPtr != nullptr) { 120 mPtr->Release(); 121 } 122 mPtr = other; 123 return *this; 124 } 125 126 template <class T> 127 AutoPtr<T> &AutoPtr<T>::operator=(const AutoPtr<T> &other) 128 { 129 if (mPtr == other.mPtr) { 130 return *this; 131 } 132 if (other.mPtr != nullptr) { 133 other.mPtr->AddRef(); 134 } 135 if (mPtr != nullptr) { 136 mPtr->Release(); 137 } 138 mPtr = other.mPtr; 139 return *this; 140 } 141 142 template <class T> 143 AutoPtr<T> &AutoPtr<T>::operator=(AutoPtr<T> &&other) 144 { 145 if (mPtr != nullptr) { 146 mPtr->Release(); 147 } 148 mPtr = other.mPtr; 149 other.mPtr = nullptr; 150 return *this; 151 } 152 153 template <class T> MoveTo(T ** other)154void AutoPtr<T>::MoveTo(T **other) 155 { 156 if (other != nullptr) { 157 *other = mPtr; 158 mPtr = nullptr; 159 } 160 } 161 162 template <class T> 163 AutoPtr<T>::operator T *() const 164 { 165 return mPtr; 166 } 167 168 template <class T> 169 T **AutoPtr<T>::operator&() 170 { 171 return &mPtr; 172 } 173 174 template <class T> 175 T *AutoPtr<T>::operator->() const 176 { 177 return mPtr; 178 } 179 180 template <class T> 181 T &AutoPtr<T>::operator*() const 182 { 183 return *mPtr; 184 } 185 186 template <class T> Get()187T *AutoPtr<T>::Get() const 188 { 189 return mPtr; 190 } 191 192 template <class T> 193 bool AutoPtr<T>::operator==(T *other) const 194 { 195 return mPtr == other; 196 } 197 198 template <class T> 199 bool AutoPtr<T>::operator==(const AutoPtr<T> &other) const 200 { 201 return mPtr == other.mPtr; 202 } 203 204 template <class T> 205 bool AutoPtr<T>::operator!=(T *other) const 206 { 207 return mPtr != other; 208 } 209 210 template <class T> 211 bool AutoPtr<T>::operator!=(const AutoPtr<T> &other) const 212 { 213 return mPtr != other.mPtr; 214 } 215 216 template <class T> 217 bool AutoPtr<T>::operator>(T *other) const 218 { 219 return mPtr > other; 220 } 221 222 template <class T> 223 bool AutoPtr<T>::operator>(const AutoPtr<T> &other) const 224 { 225 return mPtr > other.mPtr; 226 } 227 228 template <class T> 229 bool AutoPtr<T>::operator<(T *other) const 230 { 231 return mPtr < other; 232 } 233 234 template <class T> 235 bool AutoPtr<T>::operator<(const AutoPtr<T> &other) const 236 { 237 return mPtr < other.mPtr; 238 } 239 240 template <class T> 241 bool AutoPtr<T>::operator<=(T *other) const 242 { 243 return mPtr <= other; 244 } 245 246 template <class T> 247 bool AutoPtr<T>::operator<=(const AutoPtr<T> &other) const 248 { 249 return mPtr <= other.mPtr; 250 } 251 252 template <class T> 253 bool AutoPtr<T>::operator>=(T *other) const 254 { 255 return mPtr >= other; 256 } 257 258 template <class T> 259 bool AutoPtr<T>::operator>=(const AutoPtr<T> &other) const 260 { 261 return mPtr >= other.mPtr; 262 } 263 } // namespace Idl 264 } // namespace OHOS 265 266 #endif // OHOS_IDL_AUTOPTR_H