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