2023-06-06 19:05:03 -07:00
|
|
|
#pragma once
|
|
|
|
|
2023-06-21 15:07:40 -07:00
|
|
|
namespace glcr {
|
2023-06-07 00:04:53 -07:00
|
|
|
|
2023-06-06 19:05:03 -07:00
|
|
|
template <typename T>
|
|
|
|
class RefPtr;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
RefPtr<T> AdoptPtr(T* ptr);
|
|
|
|
|
2023-06-06 20:13:07 -07:00
|
|
|
template <typename T, typename U>
|
|
|
|
RefPtr<T> StaticCastRefPtr(const RefPtr<U>& ref);
|
|
|
|
|
2023-06-06 19:05:03 -07:00
|
|
|
template <typename T>
|
|
|
|
class RefPtr {
|
|
|
|
public:
|
|
|
|
RefPtr() : ptr_(nullptr) {}
|
|
|
|
RefPtr(decltype(nullptr)) : ptr_(nullptr) {}
|
|
|
|
RefPtr(const RefPtr& other) : ptr_(other.ptr_) {
|
|
|
|
if (ptr_) {
|
2023-11-19 20:33:15 -08:00
|
|
|
ptr_->AcquirePtr();
|
2023-06-06 19:05:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RefPtr& operator=(const RefPtr& other) {
|
|
|
|
T* old = ptr_;
|
|
|
|
ptr_ = other.ptr_;
|
|
|
|
if (ptr_) {
|
2023-11-19 20:33:15 -08:00
|
|
|
ptr_->AcquirePtr();
|
2023-06-06 19:05:03 -07:00
|
|
|
}
|
2023-11-19 20:33:15 -08:00
|
|
|
if (old && old->ReleasePtr()) {
|
2023-06-06 19:05:03 -07:00
|
|
|
delete old;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr(RefPtr&& other) : ptr_(other.ptr_) { other.ptr_ = nullptr; }
|
|
|
|
RefPtr& operator=(RefPtr&& other) {
|
|
|
|
// Swap
|
|
|
|
T* ptr = ptr_;
|
|
|
|
ptr_ = other.ptr_;
|
|
|
|
other.ptr_ = ptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-06-06 20:13:07 -07:00
|
|
|
enum DontAdoptTag {
|
|
|
|
DontAdopt,
|
|
|
|
};
|
2023-11-19 20:33:15 -08:00
|
|
|
RefPtr(T* ptr, DontAdoptTag) : ptr_(ptr) { ptr->AcquirePtr(); }
|
|
|
|
|
|
|
|
~RefPtr() {
|
|
|
|
if (ptr_) {
|
|
|
|
if (ptr_->ReleasePtr()) {
|
|
|
|
delete ptr_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-06 20:13:07 -07:00
|
|
|
|
2023-06-06 19:05:03 -07:00
|
|
|
T* get() const { return ptr_; };
|
|
|
|
T& operator*() const { return *ptr_; }
|
|
|
|
T* operator->() const { return ptr_; }
|
2023-11-03 19:46:27 -07:00
|
|
|
|
|
|
|
bool empty() const { return ptr_ == nullptr; }
|
2023-06-06 19:05:03 -07:00
|
|
|
operator bool() const { return ptr_ != nullptr; }
|
|
|
|
|
|
|
|
bool operator==(decltype(nullptr)) const { return (ptr_ == nullptr); }
|
|
|
|
bool operator!=(decltype(nullptr)) const { return (ptr_ != nullptr); }
|
|
|
|
|
|
|
|
bool operator==(const RefPtr<T>& other) const { return (ptr_ == other.ptr_); }
|
|
|
|
bool operator!=(const RefPtr<T>& other) const { return (ptr_ != other.ptr_); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* ptr_;
|
|
|
|
|
|
|
|
friend RefPtr<T> AdoptPtr<T>(T* ptr);
|
2023-11-19 20:33:15 -08:00
|
|
|
RefPtr(T* ptr) : ptr_(ptr) { ptr->AdoptPtr(); }
|
2023-06-06 19:05:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class MakeRefCountedFriend final {
|
|
|
|
public:
|
|
|
|
template <typename... Args>
|
|
|
|
static RefPtr<T> Make(Args&&... args) {
|
|
|
|
return AdoptPtr(new T(args...));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
RefPtr<T> MakeRefCounted(Args&&... args) {
|
|
|
|
return MakeRefCountedFriend<T>::Make(args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
RefPtr<T> AdoptPtr(T* ptr) {
|
|
|
|
return RefPtr(ptr);
|
|
|
|
}
|
2023-06-06 20:13:07 -07:00
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
RefPtr<T> StaticCastRefPtr(const RefPtr<U>& ref) {
|
|
|
|
return RefPtr(static_cast<T*>(ref.get()), RefPtr<T>::DontAdopt);
|
|
|
|
}
|
2023-06-21 15:07:40 -07:00
|
|
|
|
|
|
|
} // namespace glcr
|