🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
Object.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstUtil/SharedPtr.hpp"
25#include "AstUtil/ScopedPtr.hpp"
26#include "AstUtil/WeakPtr.hpp"
27#include "AstUtil/ReflectAPI.hpp"
28#include <string> // for std::string
29#include <stdint.h> // for uint32_t
30#include <assert.h> // for assert
31#include <atomic> // for std::atomic
32
33AST_NAMESPACE_BEGIN
34
35
47class Class; // 类元信息
48class Property; // 属性元信息
49
50template<typename ObjectPtrType, typename PropertyType>
51class AttributeBasic;
52
53typedef AttributeBasic<WeakPtr<Object>, Property> Attribute;
54
55// AST 对象运行时元信息
56#define AST_OBJECT(TYPE) \
57 static Class staticType;\
58 static inline Class* getStaticType(){return &staticType;}\
59 Class* getType() const override{return &staticType;} \
60 static void ClassInit(Class* cls);\
61
62// 定义属性
63#define AST_PROPERT(NAME) static constexpr const char* _prop_##NAME = #NAME;
64
65// 获取属性名称
66#define AST_PROPERT_NAME(CLASS, NAME) CLASS::_prop_##NAME
67#define aPropertyName(CLASS, NAME) AST_PROPERT_NAME(CLASS, NAME)
68
69#define _AST_IMPL_OBJECT(TYPE) \
70 Class TYPE::staticType;\
71
72
73enum {
75};
76
77
80class AST_UTIL_API Object
81{
82public:
83 Object()
84 : refcnt_{0}
85 , weakrefcnt_{1}
86 {}
87 Object(Object* parentScope);
88 Object(std::nullptr_t);
89public:
90 static Class staticType;
91 static inline Class* getStaticType(){return &staticType;}
92
95 virtual Class* getType() const;
96
99 virtual const std::string& getName() const;
100
101
102public: // 编辑属性
103
106 errc_t openEditDialog();
107
108public: // 通用属性访问
112 Attribute attr(StringView path);
113
114public: // 获取属性
119 errc_t getAttrBool(StringView path, bool& value) const;
120
125 errc_t getAttrInt(StringView path, int& value) const;
126
131 errc_t getAttrDouble(StringView path, double& value) const;
132
137 errc_t getAttrString(StringView path, std::string& value) const;
138
139public: // 获取属性值
143 double getAttrDouble(StringView path) const;
144
148 int getAttrInt(StringView path) const;
149
153 bool getAttrBool(StringView path) const;
154
158 std::string getAttrString(StringView path) const;
159
160public: // 设置属性值
164 errc_t setAttrBool(StringView path, bool value);
165
170 errc_t setAttrInt(StringView path, int value);
171
176 errc_t setAttrDouble(StringView path, double value);
177
182 errc_t setAttrString(StringView path, StringView value);
183
184public: // 类型与字段属性
187 Class* type() const{return getType();} // 转发到新接口getType
188
192 Property* getProperty(StringView fieldName) const;
193public: // 对象ID
194
197 uint32_t getID() const;
198
202 errc_t setParentScope(Object* parentScope);
203
206 Object* getParentScope() const;
207public: // 引用计数
210 uint32_t refCount() const{return refcnt_;}
211
214 uint32_t weakRefCount() const{return weakrefcnt_;}
215
218 bool isDestructed() const{return refcnt_ == static_cast<uint32_t>(-1);}
219
222 void destruct()
223 {
224 assert(refcnt_ == 0); // 只能直接删除不采用共享引用计数管理的对象
225 this->_destruct();
226 }
227
230 uint32_t incWeakRef()
231 {
232 return ++weakrefcnt_;
233 }
234
237 uint32_t decWeakRef()
238 {
239 if (weakrefcnt_ == 1) {
240 operator delete(this);
241 return 0;
242 }
243 else {
244 return --weakrefcnt_;
245 }
246 }
247
250 uint32_t incRef()
251 {
252 return ++refcnt_;
253 }
254
257 uint32_t decRef()
258 {
259 if (refcnt_ == 1) {
260 this->_destruct();
261 return 0;
262 }
263 return --refcnt_;
264 }
265
268 uint32_t decRefNoDelete()
269 {
270 return --refcnt_;
271 }
272private:
275 void _destruct()
276 {
277 this->~Object();
278 this->refcnt_ = static_cast<uint32_t>(-1); // 标识对象是否被析构. bit mask indicate whether object is destructed.
279 this->decWeakRef();
280 }
281
282protected:
283 friend class ObjectManager;
284 virtual ~Object();
285 Object(const Object& obj)
286 : refcnt_(0)
287 , weakrefcnt_(1)
288 {}
289 Object& operator=(const Object& obj)
290 {
291 A_UNUSED(obj);
292 return *this;
293 }
294private:
295 // Class* type_; ///< 类型元信息,同时用于标识对象是否被析构(废弃)
296 std::atomic<uint32_t> refcnt_{0};
297 std::atomic<uint32_t> weakrefcnt_{1};
298 uint32_t index_{static_cast<uint32_t>(INVALID_ID)};
299 uint32_t flags_{0};
300};
301
302
307AST_NAMESPACE_END
308
309AST_DECL_TYPE_ALIAS(Object)
310
311#include "AstUtil/Attribute.hpp"
312#include "AstUtil/Class.hpp"
对象的特定属性
定义 Attribute.hpp:42
类元信息
定义 Class.hpp:37
对象基类,继承自该类的对象可以使用运行时类型信息相关功能,实现强弱引用计数、运行时元信息(属性访问、序列化等)等基础功能
定义 Object.hpp:81
uint32_t weakRefCount() const
获取弱引用计数
定义 Object.hpp:214
uint32_t incRef()
增加强引用计数
定义 Object.hpp:250
uint32_t incWeakRef()
增加弱引用计数
定义 Object.hpp:230
bool isDestructed() const
判断对象是否被析构
定义 Object.hpp:218
uint32_t decWeakRef()
减少弱引用计数
定义 Object.hpp:237
uint32_t refCount() const
获取强引用计数
定义 Object.hpp:210
uint32_t decRef()
减少强引用计数
定义 Object.hpp:257
void destruct()
析构对象,仅当强引用计数为0时才会被调用
定义 Object.hpp:222
Class * type() const
获取对象类型
定义 Object.hpp:187
uint32_t decRefNoDelete()
减少强引用计数,不删除对象
定义 Object.hpp:268
反射属性类
定义 Property.hpp:58
@ INVALID_ID
无效对象ID
定义 Object.hpp:74