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