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 19namespace OHOS { 20namespace Idl { 21template <class T> 22class AutoPtr { 23public: 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 76private: 77 T *mPtr; 78}; 79 80template <class T> 81AutoPtr<T>::AutoPtr(T *other) : mPtr(other) 82{ 83 if (mPtr != nullptr) { 84 mPtr->AddRef(); 85 } 86} 87 88template <class T> 89AutoPtr<T>::AutoPtr(const AutoPtr<T> &other) : mPtr(other.mPtr) 90{ 91 if (mPtr != nullptr) { 92 mPtr->AddRef(); 93 } 94} 95 96template <class T> 97AutoPtr<T>::AutoPtr(AutoPtr<T> &&other) : mPtr(other.mPtr) 98{ 99 other.mPtr = nullptr; 100} 101 102template <class T> 103AutoPtr<T>::~AutoPtr() 104{ 105 if (mPtr != nullptr) { 106 mPtr->Release(); 107 } 108} 109 110template <class T> 111AutoPtr<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 126template <class T> 127AutoPtr<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 142template <class T> 143AutoPtr<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 153template <class T> 154void AutoPtr<T>::MoveTo(T **other) 155{ 156 if (other != nullptr) { 157 *other = mPtr; 158 mPtr = nullptr; 159 } 160} 161 162template <class T> 163AutoPtr<T>::operator T *() const 164{ 165 return mPtr; 166} 167 168template <class T> 169T **AutoPtr<T>::operator&() 170{ 171 return &mPtr; 172} 173 174template <class T> 175T *AutoPtr<T>::operator->() const 176{ 177 return mPtr; 178} 179 180template <class T> 181T &AutoPtr<T>::operator*() const 182{ 183 return *mPtr; 184} 185 186template <class T> 187T *AutoPtr<T>::Get() const 188{ 189 return mPtr; 190} 191 192template <class T> 193bool AutoPtr<T>::operator==(T *other) const 194{ 195 return mPtr == other; 196} 197 198template <class T> 199bool AutoPtr<T>::operator==(const AutoPtr<T> &other) const 200{ 201 return mPtr == other.mPtr; 202} 203 204template <class T> 205bool AutoPtr<T>::operator!=(T *other) const 206{ 207 return mPtr != other; 208} 209 210template <class T> 211bool AutoPtr<T>::operator!=(const AutoPtr<T> &other) const 212{ 213 return mPtr != other.mPtr; 214} 215 216template <class T> 217bool AutoPtr<T>::operator>(T *other) const 218{ 219 return mPtr > other; 220} 221 222template <class T> 223bool AutoPtr<T>::operator>(const AutoPtr<T> &other) const 224{ 225 return mPtr > other.mPtr; 226} 227 228template <class T> 229bool AutoPtr<T>::operator<(T *other) const 230{ 231 return mPtr < other; 232} 233 234template <class T> 235bool AutoPtr<T>::operator<(const AutoPtr<T> &other) const 236{ 237 return mPtr < other.mPtr; 238} 239 240template <class T> 241bool AutoPtr<T>::operator<=(T *other) const 242{ 243 return mPtr <= other; 244} 245 246template <class T> 247bool AutoPtr<T>::operator<=(const AutoPtr<T> &other) const 248{ 249 return mPtr <= other.mPtr; 250} 251 252template <class T> 253bool AutoPtr<T>::operator>=(T *other) const 254{ 255 return mPtr >= other; 256} 257 258template <class T> 259bool 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