🛰️航天仿真算法库 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 "AstUtil/Referenced.hpp"
28#include <type_traits> // for std::decay, std::remove_pointer, std::remove_reference
29#include <string> // for std::string
30#include <stdint.h> // for uint32_t
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<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
80enum class EObjectFlags: uint32_t
81{
82 eNone = 0,
83 eReadOnly = 0x01,
84 eInActive = 0x02,
85 eComponent = 0x04,
86 eEntity = 0x08,
87};
88
89
90A_ENUM_CLASS_FLAGS(EObjectFlags)
91
92
94class AST_UTIL_API Object: public Referenced
95{
96public:
100 static Object* Resolve(StringView value);
101
102 // friend uint32_t aObject_IncRef(Object* obj);
103 // friend uint32_t aObject_DecRef(Object* obj);
104 // friend uint32_t aObject_IncWeakRef(Object* obj);
105 // friend uint32_t aObject_DecWeakRef(Object* obj);
106 // friend bool aObject_IsDestructed(const Object* obj);
107
108 using Referenced::Referenced;
109 Object() = default;
110 explicit Object(Object* parentScope);
111 Object(std::nullptr_t);
112
113
114public:
115 static Class staticType;
116 static void ClassInit(Class* cls);
117 static inline Class* StaticType(){return &staticType;}
118
121 virtual Class* getType() const;
122
125 virtual std::string getRepresentation() const;
126
128 virtual const std::string& getName() const;
129
132 virtual void setName(StringView name) = 0;
133
135 const std::string& name() const {return this->getName();}
136
138 std::string displayName() const;
139public: // 编辑属性
140
143 errc_t showEditDialog();
144
145public: // 通用属性访问
149 Attribute attr(StringView path);
150
151public: // 获取属性
156 errc_t getAttrBool(StringView path, bool& value) const;
157
162 errc_t getAttrInt(StringView path, int& value) const;
163
168 errc_t getAttrDouble(StringView path, double& value) const;
169
174 errc_t getAttrString(StringView path, std::string& value) const;
175
176
181 errc_t getAttrObject(StringView path, Object*& value) const;
182
183public: // 获取属性值
187 double getAttrDouble(StringView path) const;
188
192 int getAttrInt(StringView path) const;
193
197 bool getAttrBool(StringView path) const;
198
202 std::string getAttrString(StringView path) const;
203
204
208 Object* getAttrObject(StringView path) const;
209
210public: // 设置属性值
214 errc_t setAttrBool(StringView path, bool value);
215
220 errc_t setAttrInt(StringView path, int value);
221
226 errc_t setAttrDouble(StringView path, double value);
227
232 errc_t setAttrString(StringView path, StringView value);
233
234
239 errc_t setAttrObject(StringView path, Object* value);
240
241public: // 类型与字段属性
244 Class* type() const{return getType();} // 转发到新接口getType
245
246 const std::string& typeName() const;
247
251 Property* getProperty(StringView fieldName) const;
252public: // 对象ID
253
256 ObjectId getID() const;
257
261 errc_t setParentScope(Object* parentScope);
262
265 Object* getParentScope() const;
266
267 template<typename T>
268 T getParent() const;
269
273 bool isOfType(const Class* type) const;
274
278 bool isOfType(StringView typeName) const;
279
282 template<typename T>
283 bool isOfType() const;
284public: // 延迟链接
289 template<typename Func>
290 inline void addDelayedLink(Func &&link);
291
292
297 template<typename Func>
298 inline void addDelayedLinkIfFailed(Func &&link);
299
304 void resolveLinks();
305public: // 对象标志位
306 bool readOnly() const {return !!(flags_ & EObjectFlags::eReadOnly);}
307 void setReadOnly(bool readOnly);
308
309 bool active() const {return !(flags_ & EObjectFlags::eInActive);}
310 void setActive(bool active);
311
312 bool isComponent() const {return !!(flags_ & EObjectFlags::eComponent);}
313 void setIsComponent(bool isComponent);
314
315 bool isEntity() const {return !!(flags_ & EObjectFlags::eEntity);}
316 void setIsEntity(bool isEntity);
317
318public: // 国际化
319 const char* tr(const char* msg) const;
320protected:
321 friend class ObjectManager;
322 virtual ~Object();
323 Object(const Object& obj)
324 : Object()
325 {}
326 Object& operator=(const Object&)
327 {
328 return *this;
329 }
330private:
331 // Class* type_; ///< 类型元信息,同时用于标识对象是否被析构(废弃,采用虚函数`getType()`实现)
332 uint32_t index_{static_cast<uint32_t>(INVALID_ID)};
333 EObjectFlags flags_{};
334};
335
336// 定义:检测 T 是否拥有 “Class* (T::*)() const” 签名的成员函数 getType
337template <typename T>
339private:
340 typedef char yes;
341 typedef long no;
342
343 // 仅当 U 具有 Class* (U::*)() const 的成员时,此辅助模板才能实例化
344 template <typename U, Class* (U::*)() const>
345 struct Check;
346
347 // 如果能匹配 &U::getType,则返回 yes
348 template <typename U>
349 static yes test(Check<U, &U::getType>*);
350
351 // 否则走 ...
352 template <typename>
353 static no test(...);
354
355public:
356 static constexpr bool value = (sizeof(test<T>(0)) == sizeof(yes));
357};
358
359
360template<typename T, typename U>
361A_ALWAYS_INLINE T aobject_cast(U* obj)
362{
363 // obj 可以是 Object* 或 const Object*
364 using ObjectType = typename std::decay<
365 typename std::remove_pointer<
366 typename std::remove_pointer<T>::type
367 >::type
368 >::type;
370 "aobject_cast requires the type to has a AST_OBJECT macro");
371 return static_cast<T>(ObjectType::StaticType()->cast(obj));
372}
373
374
375template<typename T>
376bool Object::isOfType() const
377{
378 using ObjectType = typename std::decay<typename std::remove_pointer<T>::type>::type;
379 static_assert(has_own_getType<ObjectType>::value, "isOfType requires the type to has a AST_OBJECT macro");
380 return isOfType(ObjectType::StaticType());
381}
382
383template<typename T>
384T Object::getParent() const
385{
386 return aobject_cast<T>(getParentScope());
387}
388
389template<typename T>
390SharedPtr<T> aMakeShared()
391{
392 return new T();
393}
394
395template<typename T>
396SharedPtr<T> aMakeShared(Object* parentScope)
397{
398 T* obj = new T();
399 aSetParentScope(obj, parentScope);
400 return obj;
401}
402
403template<typename T>
404SharedPtr<T> aMakeShared(StringView objectName)
405{
406 T* obj = new T();
407 obj->setName(objectName);
408 return obj;
409}
410
411template<typename T>
412SharedPtr<T> aMakeShared(Object* parentScope, StringView typeName)
413{
414 T* obj = new T();
415 obj->setParentScope(parentScope);
416 obj->setName(typeName);
417 return obj;
418}
419
420
421template<typename T>
422T* aNewObject(Object* parentScope=nullptr)
423{
424 T* obj = new T();
425 obj->setParentScope(parentScope);
426 return obj;
427}
428
429template<typename T>
430T* aNewObject(StringView objectName)
431{
432 T* obj = new T();
433 obj->setName(objectName);
434 return obj;
435}
436
437template<typename T>
438T* aNewObject(Object* parentScope, StringView objectName)
439{
440 T* obj = new T();
441 obj->setParentScope(parentScope);
442 obj->setName(objectName);
443 return obj;
444}
445
450AST_NAMESPACE_END
451
452AST_DECL_TYPE_ALIAS(Object)
453
454#include "AstUtil/Attribute.hpp"
455#include "AstUtil/Class.hpp"
456#include "AstUtil/WeakPtr.hpp"
类元信息
定义 Class.hpp:40
对象基类,继承自该类的对象可以使用运行时类型信息相关功能,实现强弱引用计数、运行时元信息(属性访问、序列化等)等基础功能
定义 Object.hpp:95
const std::string & name() const
获取对象的名称
定义 Object.hpp:135
virtual void setName(StringView name)=0
设置对象的名称
Class * type() const
获取对象类型
定义 Object.hpp:244
反射属性类
定义 Property.hpp:67
定义 Referenced.hpp:42
EObjectFlags
对象标志位
定义 Object.hpp:81
errc_t aSetParentScope(Object *obj, Object *parentScope)
设置对象的父作用域
定义 RTTIAPI.cpp:211
@ eComponent
组件对象(和eEntity互斥)
@ eEntity
实体对象(和eComponent互斥)
@ eInActive
不活跃
@ INVALID_ID
无效对象ID
定义 Object.hpp:76
A_ALWAYS_INLINE const char * tr(const char *msgid)
获取翻译文本
定义 I18n.hpp:49
定义 Object.hpp:338