🛰️航天仿真算法库 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/ReflectAPI.hpp"
27#include <string> // for std::string
28#include <stdint.h> // for uint32_t
29#include <assert.h> // for assert
30#include <atomic> // for std::atomic
31
32AST_NAMESPACE_BEGIN
33
34
46class Class; // 类元信息
47class Property; // 属性元信息
48
49template<typename _Object>
50class WeakPtr;
51
52template<typename ObjectPtrType, typename PropertyType>
53class AttributeBasic;
54
55typedef AttributeBasic<WeakPtr<Object>, Property> Attribute;
56
57// AST 对象运行时元信息
58#define AST_OBJECT(TYPE) \
59 static Class staticType;\
60 static inline Class* StaticType(){return &staticType;}\
61 Class* getType() const override{return &staticType;} \
62 static void ClassInit(Class* cls);\
63
64// 定义属性
65#define AST_PROPERT(NAME) static constexpr const char* _prop_##NAME = #NAME;
66
67// 获取属性名称
68#define AST_PROPERT_NAME(CLASS, NAME) CLASS::_prop_##NAME
69#define aPropertyName(CLASS, NAME) AST_PROPERT_NAME(CLASS, NAME)
70
71#define _AST_IMPL_OBJECT(TYPE) \
72 Class TYPE::staticType;\
73
74
75enum {
77};
78
81
82
85class AST_UTIL_API Object
86{
87public:
91 static Object* Resolve(StringView value);
92
93 // friend uint32_t aObject_IncRef(Object* obj);
94 // friend uint32_t aObject_DecRef(Object* obj);
95 // friend uint32_t aObject_IncWeakRef(Object* obj);
96 // friend uint32_t aObject_DecWeakRef(Object* obj);
97 // friend bool aObject_IsDestructed(const Object* obj);
98
99 Object()
100 : refcnt_{0}
101 , weakrefcnt_{1}
102 {}
103 explicit Object(Object* parentScope);
104 Object(std::nullptr_t);
105
109 : refcnt_{1}
110 , weakrefcnt_{1}
111 {}
112public:
113 static Class staticType;
114 static void ClassInit(Class* cls);
115 static inline Class* StaticType(){return &staticType;}
116
119 virtual Class* getType() const;
120
123 virtual std::string getRepresentation() const;
124
126 virtual const std::string& getName() const;
127
130 virtual void setName(StringView name) = 0;
131
133 const std::string& name() const {return this->getName();}
134public: // 编辑属性
135
138 errc_t showEditDialog();
139
140public: // 通用属性访问
144 Attribute attr(StringView path);
145
146public: // 获取属性
151 errc_t getAttrBool(StringView path, bool& value) const;
152
157 errc_t getAttrInt(StringView path, int& value) const;
158
163 errc_t getAttrDouble(StringView path, double& value) const;
164
169 errc_t getAttrString(StringView path, std::string& value) const;
170
171
176 errc_t getAttrObject(StringView path, Object*& value) const;
177
178public: // 获取属性值
182 double getAttrDouble(StringView path) const;
183
187 int getAttrInt(StringView path) const;
188
192 bool getAttrBool(StringView path) const;
193
197 std::string getAttrString(StringView path) const;
198
199
203 Object* getAttrObject(StringView path) const;
204
205public: // 设置属性值
209 errc_t setAttrBool(StringView path, bool value);
210
215 errc_t setAttrInt(StringView path, int value);
216
221 errc_t setAttrDouble(StringView path, double value);
222
227 errc_t setAttrString(StringView path, StringView value);
228
229
234 errc_t setAttrObject(StringView path, Object* value);
235
236public: // 类型与字段属性
239 Class* type() const{return getType();} // 转发到新接口getType
240
241 const std::string& typeName() const;
242
246 Property* getProperty(StringView fieldName) const;
247public: // 对象ID
248
251 ObjectId getID() const;
252
256 errc_t setParentScope(Object* parentScope);
257
260 Object* getParentScope() const;
261
265 bool isOfType(const Class* type) const;
266
270 bool isOfType(StringView typeName) const;
271
274 template<typename T>
275 bool isOfType() const;
276
277public: // 引用计数
280 uint32_t refCount() const{return refcnt_;}
281
284 uint32_t weakRefCount() const{return weakrefcnt_;}
285
288 bool isDestructed() const{return aObject_IsDestructed(this);}
289
293 void destruct()
294 {
295 #ifndef NDEBUG
296 if(refcnt_ != 0)
297 {
298 printf("object ref count is not 0, can not destruct");
299 }
300 #endif
301 assert(refcnt_ == 0); // 只能直接删除不采用共享引用计数管理的对象
302 this->_destruct();
303 }
304
307 uint32_t incWeakRef()
308 {
309 return aObject_IncWeakRef(this);
310 }
311
314 uint32_t decWeakRef()
315 {
316 return aObject_DecWeakRef(this);
317 }
318
321 uint32_t incRef()
322 {
323 return aObject_IncRef(this);
324 }
325
328 uint32_t decRef()
329 {
330 return aObject_DecRef(this);
331 }
332
335 uint32_t decRefNoDelete()
336 {
337 return --refcnt_;
338 }
339private:
343 void _destruct()
344 {
345 this->~Object();
346 this->decWeakRef();
347 }
348public: // 延迟链接
353 template<typename Func>
354 inline void addDelayedLink(Func &&link);
355
356
361 template<typename Func>
362 inline void addDelayedLinkIfFailed(Func &&link);
363
368 void resolveLinks();
369
370protected:
371 friend class ObjectManager;
372 virtual ~Object();
373 Object(const Object& obj)
374 : Object()
375 {}
376 Object& operator=(const Object&)
377 {
378 return *this;
379 }
380public: // 实参依赖查找(ADL)
381 A_ALWAYS_INLINE
382 friend uint32_t aObject_IncRef(Object* obj)
383 {
384 return ++(obj->refcnt_);
385 }
386 A_ALWAYS_INLINE
387 friend uint32_t aObject_DecRef(Object* obj)
388 {
389 if (obj->refcnt_ == 1) {
390 obj->_destruct();
391 return 0;
392 }
393 return --(obj->refcnt_);
394 }
395 A_ALWAYS_INLINE
396 friend uint32_t aObject_IncWeakRef(Object* obj)
397 {
398 return ++(obj->weakrefcnt_);
399 }
400 A_ALWAYS_INLINE
401 friend uint32_t aObject_DecWeakRef(Object* obj)
402 {
403 if (obj->weakrefcnt_ == 1) {
404 operator delete(obj);
405 return 0;
406 }
407 else {
408 return --(obj->weakrefcnt_);
409 }
410 }
411 A_ALWAYS_INLINE
412 friend bool aObject_IsDestructed(const Object *obj)
413 {
414 return obj->refcnt_ == static_cast<uint32_t>(-1);
415 }
416
417private:
418 // Class* type_; ///< 类型元信息,同时用于标识对象是否被析构(废弃,采用虚函数`getType()`实现)
419 std::atomic<uint32_t> refcnt_{0};
420 std::atomic<uint32_t> weakrefcnt_{1};
421 uint32_t index_{static_cast<uint32_t>(INVALID_ID)};
422 // uint32_t flags_{0}; ///< 对象标志位,用于存储对象的额外信息
423};
424
425// 定义:检测 T 是否拥有 “Class* (T::*)() const” 签名的成员函数 getType
426template <typename T>
428private:
429 typedef char yes;
430 typedef long no;
431
432 // 仅当 U 具有 Class* (U::*)() const 的成员时,此辅助模板才能实例化
433 template <typename U, Class* (U::*)() const>
434 struct Check;
435
436 // 如果能匹配 &U::getType,则返回 yes
437 template <typename U>
438 static yes test(Check<U, &U::getType>*);
439
440 // 否则走 ...
441 template <typename>
442 static no test(...);
443
444public:
445 static constexpr bool value = (sizeof(test<T>(0)) == sizeof(yes));
446};
447
448
449template<typename T>
450T aobject_cast(Object* obj)
451{
452 using ObjectType = typename std::decay<typename std::remove_pointer<T>::type>::type;
453 static_assert(has_own_getType<ObjectType>::value, "aobject_cast requires the type to has a AST_OBJECT macro");
454 return static_cast<T>(ObjectType::StaticType()->cast(obj));
455}
456
457template<typename T>
458bool Object::isOfType() const
459{
460 using ObjectType = typename std::decay<typename std::remove_pointer<T>::type>::type;
461 static_assert(has_own_getType<ObjectType>::value, "isOfType requires the type to has a AST_OBJECT macro");
462 return isOfType(ObjectType::StaticType());
463}
464
469AST_NAMESPACE_END
470
471AST_DECL_TYPE_ALIAS(Object)
472
473#include "AstUtil/Attribute.hpp"
474#include "AstUtil/Class.hpp"
475#include "AstUtil/WeakPtr.hpp"
类元信息
定义 Class.hpp:40
对象基类,继承自该类的对象可以使用运行时类型信息相关功能,实现强弱引用计数、运行时元信息(属性访问、序列化等)等基础功能
定义 Object.hpp:86
uint32_t weakRefCount() const
获取弱引用计数
定义 Object.hpp:284
uint32_t incRef()
增加强引用计数
定义 Object.hpp:321
uint32_t incWeakRef()
增加弱引用计数
定义 Object.hpp:307
const std::string & name() const
获取对象的名称
定义 Object.hpp:133
bool isDestructed() const
判断对象是否被析构
定义 Object.hpp:288
uint32_t decWeakRef()
减少弱引用计数
定义 Object.hpp:314
uint32_t refCount() const
获取强引用计数
定义 Object.hpp:280
uint32_t decRef()
减少强引用计数
定义 Object.hpp:328
Object(initial_strong_ref_t)
构造函数,用于初始化对象的强引用计数为1
定义 Object.hpp:108
void destruct()
析构对象,仅当强引用计数为0时才会被调用
定义 Object.hpp:293
virtual void setName(StringView name)=0
设置对象的名称
Class * type() const
获取对象类型
定义 Object.hpp:239
uint32_t decRefNoDelete()
减少强引用计数,不删除对象
定义 Object.hpp:335
反射属性类
定义 Property.hpp:67
constexpr initial_strong_ref_t initial_strong_ref
初始化强引用计数的标记值
定义 Object.hpp:80
@ INVALID_ID
无效对象ID
定义 Object.hpp:76
定义 Object.hpp:427
初始化强引用计数的标记
定义 Object.hpp:79