/* * Copyright (c) 2021 Huawei Device Co., Ltd. * * HDF is dual licensed: you can use it either under the terms of * the GPL, or the BSD license, at your option. * See the LICENSE file in the root of this repository for complete details. */ #ifndef OHOS_HDI_AUTOPTR_H #define OHOS_HDI_AUTOPTR_H namespace OHOS { namespace HDI { template class AutoPtr { public: inline AutoPtr() : mPtr(nullptr) {} AutoPtr(T *other); AutoPtr(const AutoPtr &other); AutoPtr(AutoPtr &&other); ~AutoPtr(); AutoPtr &operator=(T *other); AutoPtr &operator=(const AutoPtr &other); AutoPtr &operator=(AutoPtr &&other); void MoveTo(T **other); inline operator T *() const; inline T **operator&(); inline T *operator->() const; inline T &operator*() const; inline T *Get() const; inline bool operator==(T *other) const; inline bool operator==(const AutoPtr &other) const; inline bool operator!=(T *other) const; inline bool operator!=(const AutoPtr &other) const; inline bool operator>(T *other) const; inline bool operator>(const AutoPtr &other) const; inline bool operator<(T *other) const; inline bool operator<(const AutoPtr &other) const; inline bool operator<=(T *other) const; inline bool operator<=(const AutoPtr &other) const; inline bool operator>=(T *other) const; inline bool operator>=(const AutoPtr &other) const; private: T *mPtr; }; template AutoPtr::AutoPtr(T *other) : mPtr(other) { if (mPtr != nullptr) { mPtr->AddRef(); } } template AutoPtr::AutoPtr(const AutoPtr &other) : mPtr(other.mPtr) { if (mPtr != nullptr) { mPtr->AddRef(); } } template AutoPtr::AutoPtr(AutoPtr &&other) : mPtr(other.mPtr) { other.mPtr = nullptr; } template AutoPtr::~AutoPtr() { if (mPtr != nullptr) { mPtr->Release(); } } template AutoPtr &AutoPtr::operator=(T *other) { if (mPtr == other) { return *this; } if (other != nullptr) { other->AddRef(); } if (mPtr != nullptr) { mPtr->Release(); } mPtr = other; return *this; } template AutoPtr &AutoPtr::operator=(const AutoPtr &other) { if (mPtr == other.mPtr) { return *this; } if (other.mPtr != nullptr) { other.mPtr->AddRef(); } if (mPtr != nullptr) { mPtr->Release(); } mPtr = other.mPtr; return *this; } template AutoPtr &AutoPtr::operator=(AutoPtr &&other) { if (mPtr != nullptr) { mPtr->Release(); } mPtr = other.mPtr; other.mPtr = nullptr; return *this; } template void AutoPtr::MoveTo(T **other) { if (other != nullptr) { *other = mPtr; mPtr = nullptr; } } template AutoPtr::operator T *() const { return mPtr; } template T **AutoPtr::operator&() { return &mPtr; } template T *AutoPtr::operator->() const { return mPtr; } template T &AutoPtr::operator*() const { return *mPtr; } template T *AutoPtr::Get() const { return mPtr; } template bool AutoPtr::operator==(T *other) const { return mPtr == other; } template bool AutoPtr::operator==(const AutoPtr &other) const { return mPtr == other.mPtr; } template bool AutoPtr::operator!=(T *other) const { return mPtr != other; } template bool AutoPtr::operator!=(const AutoPtr &other) const { return mPtr != other.mPtr; } template bool AutoPtr::operator>(T *other) const { return mPtr > other; } template bool AutoPtr::operator>(const AutoPtr &other) const { return mPtr > other.mPtr; } template bool AutoPtr::operator<(T *other) const { return mPtr < other; } template bool AutoPtr::operator<(const AutoPtr &other) const { return mPtr < other.mPtr; } template bool AutoPtr::operator<=(T *other) const { return mPtr <= other; } template bool AutoPtr::operator<=(const AutoPtr &other) const { return mPtr <= other.mPtr; } template bool AutoPtr::operator>=(T *other) const { return mPtr >= other; } template bool AutoPtr::operator>=(const AutoPtr &other) const { return mPtr >= other.mPtr; } } // namespace HDI } // namespace OHOS #endif // OHOS_HDI_AUTOPTR_H