2023-06-15 16:20:29 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2023-06-21 14:48:29 -07:00
|
|
|
namespace glcr {
|
2023-06-15 16:20:29 -07:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class SharedPtr {
|
|
|
|
public:
|
|
|
|
SharedPtr() : init_(false), ptr_(0), ref_cnt_(0) {}
|
|
|
|
// Takes ownership.
|
|
|
|
SharedPtr(T* ptr) {
|
|
|
|
ptr_ = ptr;
|
|
|
|
ref_cnt_ = new uint64_t(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedPtr(const SharedPtr<T>& other)
|
|
|
|
: init_(other.init_), ptr_(other.ptr_), ref_cnt_(other.ref_cnt_) {
|
|
|
|
(*ref_cnt_)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedPtr& operator=(const SharedPtr<T>& other) {
|
|
|
|
Cleanup();
|
|
|
|
init_ = other.init_;
|
|
|
|
ptr_ = other.ptr_;
|
|
|
|
ref_cnt_ = other.ref_cnt_;
|
|
|
|
(*ref_cnt_)++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~SharedPtr() { Cleanup(); }
|
|
|
|
|
2023-06-21 14:48:29 -07:00
|
|
|
T& operator*() { return *ptr_; }
|
|
|
|
const T& operator*() const { return *ptr_; }
|
|
|
|
T* operator->() { return ptr_; }
|
|
|
|
const T* operator->() const { return ptr_; }
|
2023-06-15 16:20:29 -07:00
|
|
|
|
2023-06-21 14:48:29 -07:00
|
|
|
T* ptr() { return ptr_; }
|
2023-06-15 16:20:29 -07:00
|
|
|
|
2023-06-21 14:48:29 -07:00
|
|
|
bool operator==(const SharedPtr<T>& other) { return ptr_ == other.ptr_; }
|
2023-06-15 16:20:29 -07:00
|
|
|
|
|
|
|
bool empty() { return !init_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool init_ = true;
|
|
|
|
T* ptr_;
|
|
|
|
uint64_t* ref_cnt_;
|
|
|
|
|
|
|
|
void Cleanup() {
|
|
|
|
if (!init_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (--(*ref_cnt_) == 0) {
|
|
|
|
delete ptr_;
|
|
|
|
delete ref_cnt_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, class... A>
|
|
|
|
SharedPtr<T> MakeShared(A... args) {
|
|
|
|
return {new T(args...)};
|
|
|
|
}
|
2023-06-21 14:48:29 -07:00
|
|
|
|
|
|
|
} // namespace glcr
|