🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
Attribute.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstUtil/Property.hpp"
25#include "AstUtil/SharedPtr.hpp"
26#include "AstUtil/WeakPtr.hpp"
27#include "AstUtil/Object.hpp"
28
29AST_NAMESPACE_BEGIN
30
36template<typename ObjectType>
38{
39 typedef ObjectType* type;
40};
41
42template<>
44{
45 typedef WeakPtr<Object> type;
46};
47
48
49class Property;
50class Object;
52template<typename ObjectType=Object, typename PropertyType=Property>
54{
55public:
56 typedef ObjectType object_type;
57 typedef PropertyType property_type;
58 typedef typename object_ptr_holder<object_type>::type object_ptr_holder_type;
59
60 AttributeBasic() = default;
61
62 AttributeBasic(ObjectType* object, PropertyType* property)
63 : object_(object)
64 , property_(property)
65 {
66 }
67
68 bool isValid() const {return object_ && property_;}
69
71 object_type* object() const { return getObject(); }
72
74 property_type* property() const { return property_; }
75
76 EValueType getValueType() const {
77 if(!property_) return EValueType::eInvalid;
78 return property_->getValueType();
79 }
80
81 errc_t getValueDouble(double& value) const
82 {
83 auto object = getObject();
84 if(!property_ || !object) return eErrorInvalidParam;
85 return property_->getValueDouble(object, value);
86 }
87 errc_t setValueDouble(double value)
88 {
89 auto object = getObject();
90 if(!property_ || !object) return eErrorInvalidParam;
91 return property_->setValueDouble(object, value);
92 }
93 errc_t getValueInt(int& value) const
94 {
95 auto object = getObject();
96 if(!property_ || !object) return eErrorInvalidParam;
97 return property_->getValueInt(object, value);
98 }
99 errc_t setValueInt(int value)
100 {
101 auto object = getObject();
102 if(!property_ || !object) return eErrorInvalidParam;
103 return property_->setValueInt(object, value);
104 }
105 errc_t getValueBool(bool& value) const
106 {
107 auto object = getObject();
108 if(!property_ || !object) return eErrorInvalidParam;
109 return property_->getValueBool(object, value);
110 }
111 errc_t setValueBool(bool value)
112 {
113 auto object = getObject();
114 if(!property_ || !object) return eErrorInvalidParam;
115 return property_->setValueBool(object, value);
116 }
117 errc_t getValueString(std::string& value) const
118 {
119 auto object = getObject();
120 if(!property_ || !object) return eErrorInvalidParam;
121 return property_->getValueString(object, value);
122 }
123 errc_t setValueString(StringView value)
124 {
125 auto object = getObject();
126 if(!property_ || !object) return eErrorInvalidParam;
127 return property_->setValueString(object, value);
128 }
129 errc_t getValueObject(Object*& value) const
130 {
131 auto object = getObject();
132 if(!property_ || !object) return eErrorInvalidParam;
133 return property_->getValueObject(object, value);
134 }
135 errc_t setValueObject(Object* value)
136 {
137 auto object = getObject();
138 if(!property_ || !object) return eErrorInvalidParam;
139 return property_->setValueObject(object, value);
140 }
141public:
142 double getValueDouble() const
143 {
144 double value = 0.0;
145 getValueDouble(value);
146 return value;
147 }
148 int getValueInt() const
149 {
150 int value = 0;
151 getValueInt(value);
152 return value;
153 }
154 bool getValueBool() const
155 {
156 bool value = false;
157 getValueBool(value);
158 return value;
159 }
160 std::string getValueString() const
161 {
162 std::string value;
163 getValueString(value);
164 return value;
165 }
166 Object* getValueObject() const
167 {
168 Object* value = nullptr;
169 getValueObject(value);
170 return value;
171 }
172public:
173 operator double() const
174 {
175 return getValueDouble();
176 }
177 operator int() const
178 {
179 return getValueInt();
180 }
181 operator bool() const
182 {
183 return getValueBool();
184 }
185 operator std::string() const
186 {
187 return getValueString();
188 }
189public:
190 AttributeBasic& operator=(double value)
191 {
192 setValueDouble(value);
193 return *this;
194 }
195 AttributeBasic& operator=(int value)
196 {
197 setValueInt(value);
198 return *this;
199 }
200 // 避免对bool类型的隐式转换
201 template<typename T, typename = typename std::enable_if<std::is_same<T, bool>::value>::type>
202 AttributeBasic& operator=(T value)
203 {
204 setValueBool(value);
205 return *this;
206 }
207 AttributeBasic& operator=(StringView value)
208 {
209 setValueString(value);
210 return *this;
211 }
212protected:
213 object_type* getObject() const;
214protected:
215 object_ptr_holder_type object_{};
216 property_type* property_{nullptr};
217};
218
219template<>
220inline typename AttributeBasic<Object, Property>::object_type*
221AttributeBasic<Object, Property>::getObject() const
222{
223 return object_.get();
224}
225
226template<typename ObjectPtrType, typename PropertyType>
227inline typename AttributeBasic<ObjectPtrType, PropertyType>::object_type*
228AttributeBasic<ObjectPtrType, PropertyType>::getObject() const
229{
230 return object_;
231}
232
233// typedef AttributeBasic<> Attribute;
234
237AST_NAMESPACE_END
对象的特定属性
定义 Attribute.hpp:54
object_type * object() const
获取属性所属的对象
定义 Attribute.hpp:71
property_type * property() const
获取属性对应的属性描述符
定义 Attribute.hpp:74
对象基类,继承自该类的对象可以使用运行时类型信息相关功能,实现强弱引用计数、运行时元信息(属性访问、序列化等)等基础功能
定义 Object.hpp:95
反射属性类
定义 Property.hpp:67
弱引用指针
定义 WeakPtr.hpp:35
EValueType
定义 Property.hpp:38
定义 Attribute.hpp:38