dawn-cmake/src/backend/RefCounted.h

127 lines
3.2 KiB
C
Raw Normal View History

// Copyright 2017 The NXT Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef BACKEND_REFCOUNTED_H_
#define BACKEND_REFCOUNTED_H_
#include <cstdint>
namespace backend {
class RefCounted {
public:
RefCounted();
virtual ~RefCounted();
void ReferenceInternal();
void ReleaseInternal();
uint32_t GetExternalRefs() const;
uint32_t GetInternalRefs() const;
// NXT API
void Reference();
void Release();
protected:
2017-11-23 18:32:51 +00:00
uint32_t mExternalRefs = 1;
uint32_t mInternalRefs = 1;
};
template<typename T>
class Ref {
public:
Ref() {}
2017-11-23 18:32:51 +00:00
Ref(T* p): mPointee(p) {
Reference();
}
2017-11-23 18:32:51 +00:00
Ref(const Ref<T>& other): mPointee(other.mPointee) {
Reference();
}
Ref<T>& operator=(const Ref<T>& other) {
if (&other == this) return *this;
other.Reference();
Release();
2017-11-23 18:32:51 +00:00
mPointee = other.mPointee;
return *this;
}
Ref(Ref<T>&& other) {
2017-11-23 18:32:51 +00:00
mPointee = other.mPointee;
other.mPointee = nullptr;
}
Ref<T>& operator=(Ref<T>&& other) {
if (&other == this) return *this;
Release();
2017-11-23 18:32:51 +00:00
mPointee = other.mPointee;
other.mPointee = nullptr;
return *this;
}
~Ref() {
Release();
2017-11-23 18:32:51 +00:00
mPointee = nullptr;
}
operator bool() {
2017-11-23 18:32:51 +00:00
return mPointee != nullptr;
}
const T& operator*() const {
2017-11-23 18:32:51 +00:00
return *mPointee;
}
T& operator*() {
2017-11-23 18:32:51 +00:00
return *mPointee;
}
const T* operator->() const {
2017-11-23 18:32:51 +00:00
return mPointee;
}
T* operator->() {
2017-11-23 18:32:51 +00:00
return mPointee;
}
const T* Get() const {
2017-11-23 18:32:51 +00:00
return mPointee;
}
T* Get() {
2017-11-23 18:32:51 +00:00
return mPointee;
}
private:
void Reference() const {
2017-11-23 18:32:51 +00:00
if (mPointee != nullptr) {
mPointee->ReferenceInternal();
}
}
void Release() const {
2017-11-23 18:32:51 +00:00
if (mPointee != nullptr) {
mPointee->ReleaseInternal();
}
}
//static_assert(std::is_base_of<RefCounted, T>::value, "");
2017-11-23 18:32:51 +00:00
T* mPointee = nullptr;
};
}
#endif // BACKEND_REFCOUNTED_H_