🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
Referenced.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include <atomic> // for std::atomic
25#include <cstdint> // for uint32_t
26#include <cstdio> // for printf
27#include <cassert> // for assert
28
29AST_NAMESPACE_BEGIN
30
39
40
41class AST_UTIL_API Referenced
42{
43public:
45 : refcnt_{0}
46 , weakrefcnt_{1}
47 {}
51 : refcnt_{1}
52 , weakrefcnt_{1}
53 {}
54 virtual ~Referenced()
55 {
56 setDestructed();
57 }
58public: // 引用计数
61 uint32_t refCount() const{return refcnt_;}
62
65 uint32_t weakRefCount() const{return weakrefcnt_;}
66
69 bool isDestructed() const{return aObject_IsDestructed(this);}
70
74 void destruct()
75 {
76 #ifndef NDEBUG
77 if(refcnt_ != 0)
78 {
79 std::printf("object ref count is not 0, can not destruct");
80 }
81 #endif
82 assert(refcnt_ == 0); // 只能直接删除不采用共享引用计数管理的对象
83 this->_destruct();
84 }
85
88 uint32_t incWeakRef()
89 {
90 return aObject_IncWeakRef(this);
91 }
92
95 uint32_t decWeakRef()
96 {
97 return aObject_DecWeakRef(this);
98 }
99
102 uint32_t incRef()
103 {
104 return aObject_IncRef(this);
105 }
106
109 uint32_t decRef()
110 {
111 return aObject_DecRef(this);
112 }
113
116 uint32_t decRefNoDelete()
117 {
118 return --refcnt_;
119 }
120
121public: // 实参依赖查找(ADL)
122 A_ALWAYS_INLINE
123 friend uint32_t aObject_IncRef(Referenced* obj)
124 {
125 return ++(obj->refcnt_);
126 }
127 A_ALWAYS_INLINE
128 friend uint32_t aObject_DecRef(Referenced* obj)
129 {
130 if (obj->refcnt_ == 1) {
131 obj->_destruct();
132 return 0;
133 }
134 return --(obj->refcnt_);
135 }
136 A_ALWAYS_INLINE
137 friend uint32_t aObject_IncWeakRef(Referenced* obj)
138 {
139 return ++(obj->weakrefcnt_);
140 }
141 A_ALWAYS_INLINE
142 friend uint32_t aObject_DecWeakRef(Referenced* obj)
143 {
144 if (obj->weakrefcnt_ == 1) {
145 operator delete(obj);
146 return 0;
147 }
148 else {
149 return --(obj->weakrefcnt_);
150 }
151 }
152 A_ALWAYS_INLINE
153 friend bool aObject_IsDestructed(const Referenced *obj)
154 {
155 return obj->refcnt_ == static_cast<uint32_t>(-1);
156 }
157protected:
158 void setDestructed()
159 {
160 this->refcnt_ = static_cast<uint32_t>(-1); // 标识对象是否被析构. bit mask indicate whether object is destructed.
161 }
162private:
165 void _destruct()
166 {
167 this->~Referenced();
168 this->decWeakRef();
169 }
170private:
171 std::atomic<uint32_t> refcnt_{0};
172 std::atomic<uint32_t> weakrefcnt_{1};
173};
174
175
176
179AST_NAMESPACE_END
定义 Referenced.hpp:42
uint32_t refCount() const
获取强引用计数
定义 Referenced.hpp:61
uint32_t decWeakRef()
减少弱引用计数
定义 Referenced.hpp:95
void destruct()
析构对象,仅当强引用计数为0时才会被调用
定义 Referenced.hpp:74
uint32_t incWeakRef()
增加弱引用计数
定义 Referenced.hpp:88
uint32_t weakRefCount() const
获取弱引用计数
定义 Referenced.hpp:65
uint32_t decRefNoDelete()
减少强引用计数,不删除对象
定义 Referenced.hpp:116
Referenced(initial_strong_ref_t)
构造函数,用于初始化对象的强引用计数为1
定义 Referenced.hpp:50
uint32_t decRef()
减少强引用计数
定义 Referenced.hpp:109
uint32_t incRef()
增加强引用计数
定义 Referenced.hpp:102
bool isDestructed() const
判断对象是否被析构
定义 Referenced.hpp:69
constexpr initial_strong_ref_t initial_strong_ref
初始化强引用计数的标记值
定义 Referenced.hpp:38
初始化强引用计数的标记
定义 Referenced.hpp:37