🛰️航天仿真算法库 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
37class Property;
38class Object;
40template<typename ObjectPtrType=WeakPtr<Object>, typename PropertyType=Property>
42{
43public:
44
45 AttributeBasic(ObjectPtrType object, PropertyType* property)
46 : object_(object)
47 , property_(property)
48 {
49 }
50 errc_t getValueDouble(double& value) const
51 {
52 auto object = getObject();
53 if(!property_ || !object) return eErrorInvalidParam;
54 return property_->getValueDouble(object, value);
55 }
56 errc_t setValueDouble(double value)
57 {
58 auto object = getObject();
59 if(!property_ || !object) return eErrorInvalidParam;
60 return property_->setValueDouble(object, value);
61 }
62 errc_t getValueInt(int& value) const
63 {
64 auto object = getObject();
65 if(!property_ || !object) return eErrorInvalidParam;
66 return property_->getValueInt(object, value);
67 }
68 errc_t setValueInt(int value)
69 {
70 auto object = getObject();
71 if(!property_ || !object) return eErrorInvalidParam;
72 return property_->setValueInt(object, value);
73 }
74 errc_t getValueBool(bool& value) const
75 {
76 auto object = getObject();
77 if(!property_ || !object) return eErrorInvalidParam;
78 return property_->getValueBool(object, value);
79 }
80 errc_t setValueBool(bool value)
81 {
82 auto object = getObject();
83 if(!property_ || !object) return eErrorInvalidParam;
84 return property_->setValueBool(object, value);
85 }
86 errc_t getValueString(std::string& value) const
87 {
88 auto object = getObject();
89 if(!property_ || !object) return eErrorInvalidParam;
90 return property_->getValueString(object, value);
91 }
92 errc_t setValueString(StringView value)
93 {
94 auto object = getObject();
95 if(!property_ || !object) return eErrorInvalidParam;
96 return property_->setValueString(object, value);
97 }
98public:
99 double getValueDouble() const
100 {
101 double value = 0.0;
102 getValueDouble(value);
103 return value;
104 }
105 int getValueInt() const
106 {
107 int value = 0;
108 getValueInt(value);
109 return value;
110 }
111 bool getValueBool() const
112 {
113 bool value = false;
114 getValueBool(value);
115 return value;
116 }
117 std::string getValueString() const
118 {
119 std::string value;
120 getValueString(value);
121 return value;
122 }
123public:
124 operator double() const
125 {
126 return getValueDouble();
127 }
128 operator int() const
129 {
130 return getValueInt();
131 }
132 operator bool() const
133 {
134 return getValueBool();
135 }
136 operator std::string() const
137 {
138 return getValueString();
139 }
140public:
141 AttributeBasic& operator=(double value)
142 {
143 setValueDouble(value);
144 return *this;
145 }
146 AttributeBasic& operator=(int value)
147 {
148 setValueInt(value);
149 return *this;
150 }
151 // 避免对bool类型的隐式转换
152 template<typename T, typename = typename std::enable_if<std::is_same<T, bool>::value>::type>
153 AttributeBasic& operator=(T value)
154 {
155 setValueBool(value);
156 return *this;
157 }
158 AttributeBasic& operator=(StringView value)
159 {
160 setValueString(value);
161 return *this;
162 }
163protected:
164 void* getObject() const;
165protected:
166 ObjectPtrType object_{};
167 PropertyType* property_{nullptr};
168};
169
170template<>
171inline void* AttributeBasic<WeakPtr<Object>, Property>::getObject() const
172{
173 return object_.get();
174}
175
176template<typename ObjectPtrType, typename PropertyType>
177inline void* AttributeBasic<ObjectPtrType, PropertyType>::getObject() const
178{
179 return object_;
180}
181
182typedef AttributeBasic<> Attribute;
183
186AST_NAMESPACE_END
对象的特定属性
定义 Attribute.hpp:42
反射属性类
定义 Property.hpp:58