VTK  9.1.0
Object.h
Go to the documentation of this file.
1#pragma once
2
3#include "../RTWrapper.h"
4
5#include <VisRTX.h>
6
7#include <cstring>
8#include <cassert>
9#include <iostream>
10#include <map>
11#include <string>
12#include <vector>
13
14namespace RTW
15{
16 class Data;
17
18 class Object
19 {
20 public:
22 : dataType(type)
23 {
24 this->AddRef();
25 }
26
27 virtual ~Object()
28 {
29 // Release all objects
30 for (auto it : this->objectMap.map)
31 if (it.second)
32 it.second->Release();
33 }
34
35 virtual void Commit() = 0;
36
37 public:
38 void AddRef()
39 {
40 ++this->refCount;
41 }
42
43 void Release()
44 {
45 assert(refCount > 0);
46 if (--refCount == 0)
47 {
48 delete this;
49 }
50 }
51
52 private:
53 int64_t refCount = 0;
54
55 public:
56 inline void SetString(const std::string& id, const std::string& s)
57 {
58 this->stringMap.Set(id, s);
59 }
60
61 inline const std::string GetString(const std::vector<std::string>& ids, const std::string& defaultValue = "", bool* found = nullptr) const
62 {
63 return this->stringMap.Get(ids, defaultValue, found);
64 }
65
66 inline bool GetString(const std::vector<std::string>& ids, std::string* result, const std::string& defaultValue = nullptr)
67 {
68 bool found;
69 *result = this->GetString(ids, defaultValue, &found);
70 return found;
71 }
72
73 inline void SetBool(const std::string& id, bool b)
74 {
75 this->boolMap.Set(id, b);
76 }
77
78 virtual void SetObject(const std::string& id, Object* object)
79 {
80 // Check if already exists and release
81 Object* current = this->objectMap.Get({ id }, nullptr);
82 if (current)
83 current->Release();
84
85 // Set new object and add reference
86 if (object)
87 {
88 this->objectMap.Set(id, object);
89 object->AddRef();
90 }
91 else
92 {
93 this->objectMap.Remove(id);
94 }
95 }
96
97 template<typename T = Object>
98 inline T* GetObject(const std::vector<std::string>& ids, T* defaultValue = nullptr, bool* found = nullptr) const
99 {
100 return reinterpret_cast<T*>(this->objectMap.Get(ids, reinterpret_cast<Object*>(defaultValue), found));
101 }
102
103 template<typename T = Object>
104 inline bool GetObject(const std::vector<std::string>& ids, T** result, T* defaultValue = nullptr)
105 {
106 bool found;
107 *result = this->GetObject<T>(ids, defaultValue, &found);
108 return found;
109 }
110
111 inline void SetInt(const std::string& id, int32_t x)
112 {
113 this->int1Map.Set(id, x);
114 }
115
116 inline int32_t GetInt(const std::vector<std::string>& ids, int32_t defaultValue = 0, bool* found = nullptr) const
117 {
118 return this->int1Map.Get(ids, defaultValue, found);
119 }
120
121 inline bool GetInt(const std::vector<std::string>& ids, int32_t* result, int32_t defaultValue = 0)
122 {
123 bool found;
124 *result = this->GetInt(ids, defaultValue, &found);
125 return found;
126 }
127
128 inline void SetFloat(const std::string& id, float x)
129 {
130 this->float1Map.Set(id, x);
131 }
132
133 inline float GetFloat(const std::vector<std::string>& ids, float defaultValue = 0.0f, bool* found = nullptr) const
134 {
135 return this->float1Map.Get(ids, defaultValue, found);
136 }
137
138 inline bool GetFloat(const std::vector<std::string>& ids, float* result, float defaultValue = 0.0f)
139 {
140 bool found;
141 *result = this->GetFloat(ids, defaultValue, &found);
142 return found;
143 }
144
145 inline void SetVec2i(const std::string& id, int32_t x, int32_t y)
146 {
147 this->int2Map.Set(id, VisRTX::Vec2i(x, y));
148 }
149
150 inline VisRTX::Vec2i GetVec2i(const std::vector<std::string>& ids, const VisRTX::Vec2i& defaultValue = VisRTX::Vec2i(), bool* found = nullptr) const
151 {
152 return this->int2Map.Get(ids, defaultValue, found);
153 }
154
155 inline bool GetVec2i(const std::vector<std::string>& ids, VisRTX::Vec2i* result, const VisRTX::Vec2i& defaultValue = VisRTX::Vec2i())
156 {
157 bool found;
158 *result = this->GetVec2i(ids, defaultValue, &found);
159 return found;
160 }
161
162 inline void SetVec2f(const std::string& id, float x, float y)
163 {
164 this->float2Map.Set(id, VisRTX::Vec2f(x, y));
165 }
166
167 inline VisRTX::Vec2f GetVec2f(const std::vector<std::string>& ids, const VisRTX::Vec2f& defaultValue = VisRTX::Vec2f(), bool* found = nullptr) const
168 {
169 return this->float2Map.Get(ids, defaultValue, found);
170 }
171
172 inline bool GetVec2f(const std::vector<std::string>& ids, VisRTX::Vec2f* result, const VisRTX::Vec2f& defaultValue = VisRTX::Vec2f())
173 {
174 bool found;
175 *result = this->GetVec2f(ids, defaultValue, &found);
176 return found;
177 }
178
179 inline void SetVec3i(const std::string& id, int32_t x, int32_t y, int32_t z)
180 {
181 this->int3Map.Set(id, VisRTX::Vec3i(x, y, z));
182 }
183
184 inline VisRTX::Vec3i GetVec3i(const std::vector<std::string>& ids, const VisRTX::Vec3i& defaultValue = VisRTX::Vec3i(), bool* found = nullptr) const
185 {
186 return this->int3Map.Get(ids, defaultValue, found);
187 }
188
189 inline bool GetVec3i(const std::vector<std::string>& ids, VisRTX::Vec3i* result, const VisRTX::Vec3i& defaultValue = VisRTX::Vec3i())
190 {
191 bool found;
192 *result = this->GetVec3i(ids, defaultValue, &found);
193 return found;
194 }
195
196 inline void SetVec3f(const std::string& id, float x, float y, float z)
197 {
198 this->float3Map.Set(id, VisRTX::Vec3f(x, y, z));
199 }
200
201 inline VisRTX::Vec3f GetVec3f(const std::vector<std::string>& ids, const VisRTX::Vec3f& defaultValue = VisRTX::Vec3f(), bool* found = nullptr) const
202 {
203 return this->float3Map.Get(ids, defaultValue, found);
204 }
205
206 inline bool GetVec3f(const std::vector<std::string>& ids, VisRTX::Vec3f* result, const VisRTX::Vec3f& defaultValue = VisRTX::Vec3f())
207 {
208 bool found;
209 *result = this->GetVec3f(ids, defaultValue, &found);
210 return found;
211 }
212
213 inline void SetVec4f(const std::string& id, float x, float y, float z, float w)
214 {
215 this->float4Map.Set(id, VisRTX::Vec4f(x, y, z, w));
216 }
217
218 inline VisRTX::Vec4f GetVec4f(const std::vector<std::string>& ids, const VisRTX::Vec4f& defaultValue = VisRTX::Vec4f(), bool* found = nullptr) const
219 {
220 return this->float4Map.Get(ids, defaultValue, found);
221 }
222
223 inline bool GetVec4f(const std::vector<std::string>& ids, VisRTX::Vec4f* result, const VisRTX::Vec4f& defaultValue = VisRTX::Vec4f())
224 {
225 bool found;
226 *result = this->GetVec4f(ids, defaultValue, &found);
227 return found;
228 }
229
230 virtual void RemoveParam(const std::string& id)
231 {
232 this->stringMap.Remove(id);
233 this->objectMap.Remove(id);
234 this->int1Map.Remove(id);
235 this->float1Map.Remove(id);
236 this->float2Map.Remove(id);
237 this->int2Map.Remove(id);
238 this->int3Map.Remove(id);
239 this->float3Map.Remove(id);
240 this->float4Map.Remove(id);
241 }
242
243 public:
245 {
246 for (auto it : this->stringMap.map)
247 std::cout << "String: \"" << it.first << "\" -> \"" << it.second << "\"" << std::endl;
248
249 for (auto it : this->objectMap.map)
250 std::cout << "Object/Data: \"" << it.first << "\"" << std::endl;
251
252 for (auto it : this->int1Map.map)
253 std::cout << "int1: \"" << it.first << "\" -> " << it.second << std::endl;
254
255 for (auto it : this->float1Map.map)
256 std::cout << "float1: \"" << it.first << "\" -> " << it.second << std::endl;
257
258 for (auto it : this->int2Map.map)
259 std::cout << "int2: \"" << it.first << "\" -> (" << it.second.x << ", " << it.second.y << ")" << std::endl;
260
261 for (auto it : this->float2Map.map)
262 std::cout << "float2: \"" << it.first << "\" -> (" << it.second.x << ", " << it.second.y << ")" << std::endl;
263
264 for (auto it : this->int3Map.map)
265 std::cout << "int3: \"" << it.first << "\" -> (" << it.second.x << ", " << it.second.y << ", " << it.second.z << ")" << std::endl;
266
267 for (auto it : this->float3Map.map)
268 std::cout << "float3: \"" << it.first << "\" -> (" << it.second.x << ", " << it.second.y << ", " << it.second.z << ")" << std::endl;
269
270 for (auto it : this->float4Map.map)
271 std::cout << "float4: \"" << it.first << "\" -> (" << it.second.x << ", " << it.second.y << ", " << it.second.z << ", " << it.second.w << ")" << std::endl;
272 }
273
274 std::set<std::string> GetAllParameters() const
275 {
276 std::set<std::string> result;
277 for (auto it : this->stringMap.map)
278 result.insert("string " + it.first);
279
280 for (auto it : this->objectMap.map)
281 result.insert("object " + it.first);
282
283 for (auto it : this->int1Map.map)
284 result.insert("int1 " + it.first);
285
286 for (auto it : this->float1Map.map)
287 result.insert("float1 " + it.first);
288
289 for (auto it : this->int2Map.map)
290 result.insert("int2 " + it.first);
291
292 for (auto it : this->float2Map.map)
293 result.insert("float2 " + it.first);
294
295 for (auto it : this->int3Map.map)
296 result.insert("int3 " + it.first);
297
298 for (auto it : this->float3Map.map)
299 result.insert("float3 " + it.first);
300
301 for (auto it : this->float4Map.map)
302 result.insert("float4 " + it.first);
303 return result;
304 }
305
307 {
308 return dataType;
309 }
310
311 private:
312 template<typename T>
313 class ParameterMap
314 {
315 public:
316 inline void Set(const std::string& id, const T& value)
317 {
318 this->map[id] = value;
319 }
320
321 inline T Get(const std::vector<std::string>& ids, const T& defaultValueValue, bool* found = nullptr) const
322 {
323 for (const std::string& id : ids)
324 {
325 auto it = this->map.find(id);
326 if (it != this->map.end())
327 {
328 if (found)
329 *found = true;
330 return (*it).second;
331 }
332 }
333
334 if (found)
335 *found = false;
336 return defaultValueValue;
337 }
338
339 inline void Remove(const std::string& id)
340 {
341 auto it = this->map.find(id);
342 if (it != this->map.end())
343 this->map.erase(it);
344 }
345
346 public:
347 std::map<std::string, T> map;
348 };
349
350 private:
351 ParameterMap<std::string> stringMap;
352 ParameterMap<bool> boolMap;
353 ParameterMap<Object*> objectMap;
354
355 ParameterMap<int32_t> int1Map;
356 ParameterMap<float> float1Map;
357 ParameterMap<VisRTX::Vec2f> float2Map;
358 ParameterMap<VisRTX::Vec2i> int2Map;
359 ParameterMap<VisRTX::Vec3i> int3Map;
360 ParameterMap<VisRTX::Vec3f> float3Map;
361 ParameterMap<VisRTX::Vec4f> float4Map;
362
363 RTWDataType dataType;
364 };
365}
RTWDataType
Definition: Types.h:120
@ RTW_OBJECT
Definition: Types.h:131
bool GetVec3i(const std::vector< std::string > &ids, VisRTX::Vec3i *result, const VisRTX::Vec3i &defaultValue=VisRTX::Vec3i())
Definition: Object.h:189
void SetVec4f(const std::string &id, float x, float y, float z, float w)
Definition: Object.h:213
VisRTX::Vec2i GetVec2i(const std::vector< std::string > &ids, const VisRTX::Vec2i &defaultValue=VisRTX::Vec2i(), bool *found=nullptr) const
Definition: Object.h:150
bool GetString(const std::vector< std::string > &ids, std::string *result, const std::string &defaultValue=nullptr)
Definition: Object.h:66
bool GetVec2i(const std::vector< std::string > &ids, VisRTX::Vec2i *result, const VisRTX::Vec2i &defaultValue=VisRTX::Vec2i())
Definition: Object.h:155
RTWDataType GetDataType() const
Definition: Object.h:306
int32_t GetInt(const std::vector< std::string > &ids, int32_t defaultValue=0, bool *found=nullptr) const
Definition: Object.h:116
void SetVec3i(const std::string &id, int32_t x, int32_t y, int32_t z)
Definition: Object.h:179
void SetInt(const std::string &id, int32_t x)
Definition: Object.h:111
const std::string GetString(const std::vector< std::string > &ids, const std::string &defaultValue="", bool *found=nullptr) const
Definition: Object.h:61
void SetVec3f(const std::string &id, float x, float y, float z)
Definition: Object.h:196
void SetVec2f(const std::string &id, float x, float y)
Definition: Object.h:162
bool GetInt(const std::vector< std::string > &ids, int32_t *result, int32_t defaultValue=0)
Definition: Object.h:121
bool GetVec2f(const std::vector< std::string > &ids, VisRTX::Vec2f *result, const VisRTX::Vec2f &defaultValue=VisRTX::Vec2f())
Definition: Object.h:172
void Release()
Definition: Object.h:43
VisRTX::Vec2f GetVec2f(const std::vector< std::string > &ids, const VisRTX::Vec2f &defaultValue=VisRTX::Vec2f(), bool *found=nullptr) const
Definition: Object.h:167
std::set< std::string > GetAllParameters() const
Definition: Object.h:274
void SetBool(const std::string &id, bool b)
Definition: Object.h:73
virtual void SetObject(const std::string &id, Object *object)
Definition: Object.h:78
void SetVec2i(const std::string &id, int32_t x, int32_t y)
Definition: Object.h:145
bool GetObject(const std::vector< std::string > &ids, T **result, T *defaultValue=nullptr)
Definition: Object.h:104
void SetFloat(const std::string &id, float x)
Definition: Object.h:128
void AddRef()
Definition: Object.h:38
VisRTX::Vec3i GetVec3i(const std::vector< std::string > &ids, const VisRTX::Vec3i &defaultValue=VisRTX::Vec3i(), bool *found=nullptr) const
Definition: Object.h:184
bool GetFloat(const std::vector< std::string > &ids, float *result, float defaultValue=0.0f)
Definition: Object.h:138
T * GetObject(const std::vector< std::string > &ids, T *defaultValue=nullptr, bool *found=nullptr) const
Definition: Object.h:98
Object(RTWDataType type=RTW_OBJECT)
Definition: Object.h:21
void PrintAllParameters() const
Definition: Object.h:244
bool GetVec4f(const std::vector< std::string > &ids, VisRTX::Vec4f *result, const VisRTX::Vec4f &defaultValue=VisRTX::Vec4f())
Definition: Object.h:223
virtual void RemoveParam(const std::string &id)
Definition: Object.h:230
virtual void Commit()=0
float GetFloat(const std::vector< std::string > &ids, float defaultValue=0.0f, bool *found=nullptr) const
Definition: Object.h:133
void SetString(const std::string &id, const std::string &s)
Definition: Object.h:56
VisRTX::Vec4f GetVec4f(const std::vector< std::string > &ids, const VisRTX::Vec4f &defaultValue=VisRTX::Vec4f(), bool *found=nullptr) const
Definition: Object.h:218
virtual ~Object()
Definition: Object.h:27
bool GetVec3f(const std::vector< std::string > &ids, VisRTX::Vec3f *result, const VisRTX::Vec3f &defaultValue=VisRTX::Vec3f())
Definition: Object.h:206
VisRTX::Vec3f GetVec3f(const std::vector< std::string > &ids, const VisRTX::Vec3f &defaultValue=VisRTX::Vec3f(), bool *found=nullptr) const
Definition: Object.h:201
Definition: Backend.h:6
@ value
Definition: vtkX3D.h:226
@ type
Definition: vtkX3D.h:522
@ string
Definition: vtkX3D.h:496