1 Star 2 Fork 0

InnovatorNZ/CppGCPtr

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
PtrGuard.h 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
#ifndef CPPGCPTR_PTRGUARD_H
#define CPPGCPTR_PTRGUARD_H
#include "GCWorker.h"
#include "GCRegion.h"
template<typename T>
class PtrGuard {
private:
T* ptr;
GCRegion* region;
bool owns;
const bool relocationEnabled;
struct DeferGuard_t {
explicit DeferGuard_t() = default;
};
public:
static constexpr DeferGuard_t DeferGuard{};
PtrGuard(T* ptr, GCRegion* region, DeferGuard_t) :
ptr(ptr), region(region), owns(false),
relocationEnabled(GCWorker::getWorker()->relocationEnabled()) {
}
PtrGuard(T* ptr, GCRegion* region) : PtrGuard(ptr, region, DeferGuard) {
if (relocationEnabled)
lock();
}
~PtrGuard() {
unlock();
}
PtrGuard(const PtrGuard&) = delete;
PtrGuard(PtrGuard&&) noexcept = delete;
void lock() {
if (!owns) {
region->inc_use_count();
owns = true;
}
}
void unlock() {
if (owns) {
region->dec_use_count();
owns = false;
}
}
T* get() const {
return ptr;
}
T* operator->() const {
return ptr;
}
T& operator*() const {
return *ptr;
}
T* value() const {
return ptr;
}
};
#endif //CPPGCPTR_PTRGUARD_H
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/innovatornz/CppGCPtr.git
git@gitee.com:innovatornz/CppGCPtr.git
innovatornz
CppGCPtr
CppGCPtr
master

搜索帮助