Path: blob/main_old/src/libANGLE/CLRefPointer.h
1693 views
//1// Copyright 2021 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// CLRefPointer.h: A non-owning intrinsic reference counting smart pointer for CL objects.67#ifndef LIBANGLE_CLREFPOINTER_H_8#define LIBANGLE_CLREFPOINTER_H_910#include <algorithm>1112namespace cl13{1415template <typename T>16class RefPointer17{18public:19RefPointer() noexcept : mCLObject(nullptr) {}2021explicit RefPointer(T *object) noexcept : mCLObject(object)22{23if (mCLObject != nullptr)24{25mCLObject->retain();26}27}28~RefPointer()29{30if (mCLObject != nullptr && mCLObject->release())31{32delete mCLObject;33}34}3536RefPointer(std::nullptr_t) noexcept : mCLObject(nullptr) {}37RefPointer &operator=(std::nullptr_t)38{39reset();40return *this;41}4243RefPointer(RefPointer &&other) noexcept : mCLObject(nullptr) { this->swap(other); }44RefPointer &operator=(RefPointer &&other)45{46this->swap(other);47return *this;48}4950RefPointer(const RefPointer<T> &other) : mCLObject(other.mCLObject)51{52if (mCLObject != nullptr)53{54mCLObject->retain();55}56}57RefPointer &operator=(const RefPointer<T> &other)58{59if (this != &other)60{61reset();62mCLObject = other.mCLObject;63if (mCLObject != nullptr)64{65mCLObject->retain();66}67}68return *this;69}7071T *operator->() const { return mCLObject; }72T &operator*() const { return *mCLObject; }7374T *get() const { return mCLObject; }75explicit operator bool() const { return mCLObject != nullptr; }7677T *release() noexcept78{79T *const object = mCLObject;80mCLObject = nullptr;81return object;82}8384void swap(RefPointer &other) noexcept { std::swap(mCLObject, other.mCLObject); }8586void reset()87{88if (mCLObject != nullptr)89{90T *const object = release();91object->release();92}93}9495private:96T *mCLObject;97};9899template <typename T>100void swap(RefPointer<T> &left, RefPointer<T> &right)101{102left.swap(right);103}104105template <typename T>106bool operator==(const RefPointer<T> &ptr, nullptr_t) noexcept107{108return ptr.get() == nullptr;109}110111template <typename T>112bool operator==(nullptr_t, const RefPointer<T> &ptr) noexcept113{114return ptr.get() == nullptr;115}116117template <typename T>118bool operator!=(const RefPointer<T> &ptr, nullptr_t) noexcept119{120return ptr.get() != nullptr;121}122123template <typename T>124bool operator!=(nullptr_t, const RefPointer<T> &ptr) noexcept125{126return ptr.get() != nullptr;127}128129template <typename T, typename U>130bool operator==(const RefPointer<T> &left, const RefPointer<U> &right) noexcept131{132return left.get() == right.get();133}134135template <typename T, typename U>136bool operator!=(const RefPointer<T> &left, const RefPointer<U> &right) noexcept137{138return left.get() != right.get();139}140141template <typename T, typename U>142bool operator==(const RefPointer<T> &left, const U *right) noexcept143{144return left.get() == right;145}146147template <typename T, typename U>148bool operator==(const T *left, const RefPointer<U> &right) noexcept149{150return left == right.get();151}152153template <typename T, typename U>154bool operator!=(const RefPointer<T> &left, const U *right) noexcept155{156return left.get() != right;157}158159template <typename T, typename U>160bool operator!=(const T *left, const RefPointer<U> &right) noexcept161{162return left != right.get();163}164165} // namespace cl166167#endif // LIBANGLE_CLREFPOINTER_H_168169170