SCENE C++ API  2.0.22
lspod.h
1 #pragma once
2 
3 #include <core_api/lsglobaldefinitions.h>
4 
5 #include <vector>
6 #include <list>
7 #include <set>
8 #include <map>
9 
10 namespace SCENE_API {
11 
16 template<class T>
17 class LSPod
18 {
19 public:
22  LSPod(const T& from);
23 
24  // Construction and assignment from existing LSPod instances.
26  LSPod(const LSPod<T>& from);
27  LSPod<T>& operator=(const LSPod<T>& from);
28 
31  virtual ~LSPod();
32 
35  T get();
36 
37  // https://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
38  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
39 
40 private:
41  T data;
42 };
43 
44 //-----------------------------------------------------------------------------
45 template <class T>
46 LSPod<T>::LSPod(const T& from)
47  : data(from)
48 {}
49 
50 //-----------------------------------------------------------------------------
51 template <class T>
53  : data(from.data)
54 {}
55 
56 //-----------------------------------------------------------------------------
57 template <class T>
59 {
60  data = from.data;
61  return *this;
62 }
63 
64 //-----------------------------------------------------------------------------
65 template <class T>
67 {}
68 
69 //-----------------------------------------------------------------------------
70 template <class T>
72 {
73  return data;
74 }
75 
76 //-----------------------------------------------------------------------------
77 // L S P o d D a t a A r r a y
78 //-----------------------------------------------------------------------------
79 template <class T>
80 class LSPodDataArray
81 {
82 public:
83  LSPodDataArray(size_t size)
84  {
85  if (size > 0)
86  dataArray = new T[size];
87  else
88  dataArray = 0;
89  }
90 
91  virtual ~LSPodDataArray()
92  {
93  if (dataArray)
94  delete[] dataArray;
95  }
96 
97  T* dataArray;
98 };
99 
100 //-----------------------------------------------------------------------------
101 // L S P o d < s t d : : v e c t o r >
102 //-----------------------------------------------------------------------------
103 template<class _Ty, class _Alloc>
104 class LSPod<std::vector<_Ty, _Alloc> >
105 {
106 public:
108  LSPod(const std::vector<_Ty, _Alloc>& from);
109 
110  // \brief Construction and assignment from existing LSPod instances.
111  LSPod(const LSPod<std::vector<_Ty, _Alloc> >& from);
112  LSPod<std::vector<_Ty, _Alloc> >& operator=(const LSPod<std::vector<_Ty, _Alloc> >& from);
113 
115  virtual ~LSPod();
116 
118  std::vector<_Ty, _Alloc> get() const;
119 
120 private:
121  size_t size;
122  LSPodDataArray<LSPod<_Ty>*>* data;
123 };
124 
125 //-----------------------------------------------------------------------------
126 template<class _Ty, class _Alloc>
127 LSPod<std::vector<_Ty, _Alloc> >::LSPod(const std::vector<_Ty, _Alloc>& from)
128  : size(from.size())
129  , data(new LSPodDataArray<LSPod<_Ty>*>(from.size()))
130 {
131  for (size_t i = 0; i < size; ++i)
132  data->dataArray[i] = new LSPod<_Ty>(from[i]);
133 }
134 
135 //-----------------------------------------------------------------------------
136 template<class _Ty, class _Alloc>
137 LSPod<std::vector<_Ty, _Alloc> >::LSPod(const LSPod<std::vector<_Ty, _Alloc> >& from)
138  : size(from.size)
139  , data(new LSPodDataArray<LSPod<_Ty>*>(from.size))
140 {
141  for (size_t i = 0; i < size; ++i)
142  data->dataArray[i] = new LSPod<_Ty>(*(from.data->dataArray[i]));
143 }
144 
145 //-----------------------------------------------------------------------------
146 template<class _Ty, class _Alloc>
147 LSPod<std::vector<_Ty, _Alloc> >& LSPod<std::vector<_Ty, _Alloc> >::operator=(const LSPod<std::vector<_Ty, _Alloc> >& from)
148 {
149  for (size_t i = 0; i < size; ++i)
150  delete data->dataArray[i];
151  delete data;
152 
153  size = from.size;
154  data = new LSPodDataArray<LSPod<_Ty>*>(from.size);
155  for (size_t i = 0; i < size; ++i)
156  data->dataArray[i] = new LSPod<_Ty>(*(from.data->dataArray[i]));
157 
158  return *this;
159 }
160 
161 //-----------------------------------------------------------------------------
162 template<class _Ty, class _Alloc>
163 LSPod<std::vector<_Ty, _Alloc> >::~LSPod()
164 {
165  for (size_t i = 0; i < size; ++i)
166  delete data->dataArray[i];
167  delete data;
168 }
169 
170 //-----------------------------------------------------------------------------
171 template<class _Ty, class _Alloc>
172 std::vector<_Ty, _Alloc> LSPod<std::vector<_Ty, _Alloc> >::get() const
173 {
174  std::vector<_Ty, _Alloc> v;
175  v.reserve(size);
176  for (size_t i = 0; i < size; ++i)
177  v.push_back(data->dataArray[i]->get());
178  return v;
179 }
180 
181 //-----------------------------------------------------------------------------
182 // L S P o d < s t d : : l i s t >
183 //-----------------------------------------------------------------------------
184 template<class _Ty, class _Alloc>
185 class LSPod<std::list<_Ty, _Alloc> >
186 {
187 public:
189  LSPod(const std::list<_Ty, _Alloc>& from);
190 
191  // \brief Construction and assignment from existing LSPod instances.
192  LSPod(const LSPod<std::list<_Ty, _Alloc> >& from);
193  LSPod<std::list<_Ty, _Alloc> >& operator=(const LSPod<std::list<_Ty, _Alloc> >& from);
194 
196  virtual ~LSPod();
197 
199  std::list<_Ty, _Alloc> get() const;
200 
201 private:
202  size_t size;
203  LSPodDataArray<LSPod<_Ty>*>* data;
204 };
205 
206 //-----------------------------------------------------------------------------
207 template<class _Ty, class _Alloc>
208 LSPod<std::list<_Ty, _Alloc> >::LSPod(const std::list<_Ty, _Alloc>& from)
209  : size(from.size())
210  , data(new LSPodDataArray<LSPod<_Ty>*>(from.size()))
211 {
212  size_t i = 0;
213  typename std::list<_Ty, _Alloc>::const_iterator iter = from.begin();
214  for (; iter != from.end(); ++iter, ++i)
215  data->dataArray[i] = new LSPod<_Ty>(*iter);
216 }
217 
218 //-----------------------------------------------------------------------------
219 template<class _Ty, class _Alloc>
220 LSPod<std::list<_Ty, _Alloc> >::LSPod(const LSPod<std::list<_Ty, _Alloc> >& from)
221  : size(from.size)
222  , data(new LSPodDataArray<LSPod<_Ty>*>(from.size))
223 {
224  for (size_t i = 0; i < size; ++i)
225  data->dataArray[i] = new LSPod<_Ty>(*(from.data->dataArray[i]));
226 }
227 
228 //-----------------------------------------------------------------------------
229 template<class _Ty, class _Alloc>
230 LSPod<std::list<_Ty, _Alloc> >& LSPod<std::list<_Ty, _Alloc> >::operator=(const LSPod<std::list<_Ty, _Alloc> >& from)
231 {
232 
233  for (size_t i = 0; i < size; ++i)
234  delete data->dataArray[i];
235  delete data;
236 
237  size = from.size;
238  data = new LSPodDataArray<LSPod<_Ty>*>(from.size);
239  for (size_t i = 0; i < size; ++i)
240  data->dataArray[i] = new LSPod<_Ty>(*(from.data->dataArray[i]));
241 
242  return *this;
243 }
244 
245 //-----------------------------------------------------------------------------
246 template<class _Ty, class _Alloc>
247 LSPod<std::list<_Ty, _Alloc> >::~LSPod()
248 {
249  for (size_t i = 0; i < size; ++i)
250  delete data->dataArray[i];
251 
252  delete data;
253 }
254 
255 //-----------------------------------------------------------------------------
256 template<class _Ty, class _Alloc>
257 std::list<_Ty, _Alloc> LSPod<std::list<_Ty, _Alloc> >::get() const
258 {
259  std::list<_Ty, _Alloc> l;
260  for (size_t i = 0; i < size; ++i)
261  l.push_back(data->dataArray[i]->get());
262  return l;
263 }
264 
265 //-----------------------------------------------------------------------------
266 // L S P o d < s t d : : s e t >
267 //-----------------------------------------------------------------------------
268 template<class _Kty, class _Pr, class _Alloc>
269 class LSPod<std::set<_Kty, _Pr, _Alloc> >
270 {
271 public:
273  LSPod(const std::set<_Kty, _Pr, _Alloc>& from);
274 
275  // \brief Construction and assignment from existing LSPod instances.
276  LSPod(const LSPod<std::set<_Kty, _Pr, _Alloc> >& from);
277  LSPod<std::set<_Kty, _Pr, _Alloc> >& operator=(const LSPod<std::set<_Kty, _Pr, _Alloc> >& from);
278 
280  virtual ~LSPod();
281 
283  std::set<_Kty, _Pr, _Alloc> get() const;
284 
285 private:
286  size_t size;
287  LSPodDataArray<LSPod<_Kty>*>* data;
288 };
289 
290 //-----------------------------------------------------------------------------
291 template<class _Kty, class _Pr, class _Alloc>
292 LSPod<std::set<_Kty, _Pr, _Alloc> >::LSPod(const std::set<_Kty, _Pr, _Alloc>& from)
293  : size(from.size())
294  , data(new LSPodDataArray<LSPod<_Kty>*>(from.size()))
295 {
296  size_t i = 0;
297  typename std::set<_Kty, _Pr, _Alloc>::const_iterator iter = from.begin();
298  for (; iter != from.end(); ++iter, ++i)
299  data->dataArray[i] = new LSPod<_Kty>(*iter);
300 }
301 
302 //-----------------------------------------------------------------------------
303 template<class _Kty, class _Pr, class _Alloc>
304 LSPod<std::set<_Kty, _Pr, _Alloc> >::LSPod(const LSPod<std::set<_Kty, _Pr, _Alloc> >& from)
305  : size(from.size)
306  , data(new LSPodDataArray<LSPod<_Kty>*>(from.size))
307 {
308  for (size_t i = 0; i < size; ++i)
309  data->dataArray[i] = new LSPod<_Kty>(*(from.data->dataArray[i]));
310 }
311 
312 //-----------------------------------------------------------------------------
313 template<class _Kty, class _Pr, class _Alloc>
314 LSPod<std::set<_Kty, _Pr, _Alloc> >& LSPod<std::set<_Kty, _Pr, _Alloc> >::operator=(const LSPod<std::set<_Kty, _Pr, _Alloc> >& from)
315 {
316  for (size_t i = 0; i < size; ++i)
317  delete data->dataArray[i];
318  delete data;
319 
320  data = new LSPodDataArray<LSPod<_Kty>*>(from.size);
321  size = from.size;
322  for (size_t i = 0; i < size; ++i)
323  data->dataArray[i] = new LSPod<_Kty>(*(from.data->dataArray[i]));
324 
325  return *this;
326 }
327 
328 //-----------------------------------------------------------------------------
329 template<class _Kty, class _Pr, class _Alloc>
330 LSPod<std::set<_Kty, _Pr, _Alloc> >::~LSPod()
331 {
332  for (size_t i = 0; i < size; ++i)
333  delete data->dataArray[i];
334 
335  delete data;
336 }
337 
338 //-----------------------------------------------------------------------------
339 template<class _Kty, class _Pr, class _Alloc>
340 std::set<_Kty, _Pr, _Alloc> LSPod<std::set<_Kty, _Pr, _Alloc> >::get() const
341 {
342  std::set<_Kty, _Pr, _Alloc> s;
343  for (size_t i = 0; i < size; ++i)
344  s.insert(data->dataArray[i]->get());
345  return s;
346 }
347 
348 //-----------------------------------------------------------------------------
349 // L S P o d < s t d : : m a p >
350 //-----------------------------------------------------------------------------
351 template<class _Kty, class _Ty, class _Pr, class _Alloc>
352 class LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >
353 {
354 public:
356  LSPod(const std::map<_Kty, _Ty, _Pr, _Alloc>& from);
357 
358  // \brief Construction and assignment from existing LSPod instances.
359  LSPod(const LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >& from);
360  LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >& operator=(const LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >& from);
361 
363  virtual ~LSPod();
364 
366  std::map<_Kty, _Ty, _Pr, _Alloc> get() const;
367 
368 private:
369  size_t size;
370  LSPodDataArray<LSPod<_Kty>*>* keys;
371  LSPodDataArray<LSPod<_Ty>*>* values;
372 };
373 
374 //-----------------------------------------------------------------------------
375 template<class _Kty, class _Ty, class _Pr, class _Alloc>
376 LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >::LSPod(const std::map<_Kty, _Ty, _Pr, _Alloc>& from)
377  : size(from.size())
378  , keys(new LSPodDataArray<LSPod<_Kty>*>(from.size()))
379  , values(new LSPodDataArray<LSPod<_Ty>*>(from.size()))
380 {
381  size_t i = 0;
382  typename std::map<_Kty, _Ty, _Pr, _Alloc>::const_iterator iter = from.begin();
383  for (; iter != from.end(); ++iter, ++i)
384  {
385  keys->dataArray[i] = new LSPod<_Kty>(iter->first);
386  values->dataArray[i] = new LSPod<_Ty>(iter->second);
387  }
388 }
389 
390 //-----------------------------------------------------------------------------
391 template<class _Kty, class _Ty, class _Pr, class _Alloc>
392 LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >::LSPod(const LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >& from)
393  : size(from.size)
394  , keys(new LSPodDataArray<LSPod<_Kty>*>(from.size))
395  , values(new LSPodDataArray<LSPod<_Ty>*>(from.size))
396 {
397  for (size_t i = 0; i < size; ++i)
398  {
399  keys->dataArray[i] = new LSPod<_Kty>(*(from.keys->dataArray[i]));
400  values->dataArray[i] = new LSPod<_Ty>(*(from.values->dataArray[i]));
401  }
402 }
403 
404 //-----------------------------------------------------------------------------
405 template<class _Kty, class _Ty, class _Pr, class _Alloc>
406 LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >& LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >::operator=(const LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >& from)
407 {
408  for (size_t i = 0; i < size; ++i)
409  {
410  delete keys->dataArray[i];
411  delete values->dataArray[i];
412  }
413  delete keys;
414  keys = nullptr;
415  delete values;
416  values = nullptr;
417 
418  keys = new LSPodDataArray<LSPod<_Kty>*>(from.size);
419  values = new LSPodDataArray<LSPod<_Ty>*>(from.size);
420  size = from.size;
421  for (size_t i = 0; i < size; ++i)
422  {
423  keys->dataArray[i] = new LSPod<_Kty>(*(from.keys->dataArray[i]));
424  values->dataArray[i] = new LSPod<_Ty>(*(from.values->dataArray[i]));
425  }
426 
427  return *this;
428 }
429 
430 //-----------------------------------------------------------------------------
431 template<class _Kty, class _Ty, class _Pr, class _Alloc>
432 LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >::~LSPod()
433 {
434  for (size_t i = 0; i < size; ++i)
435  {
436  delete keys->dataArray[i];
437  delete values->dataArray[i];
438  }
439 
440  delete keys;
441  delete values;
442 }
443 
444 //-----------------------------------------------------------------------------
445 template<class _Kty, class _Ty, class _Pr, class _Alloc>
446 std::map<_Kty, _Ty, _Pr, _Alloc> LSPod<std::map<_Kty, _Ty, _Pr, _Alloc> >::get() const
447 {
448  std::map<_Kty, _Ty, _Pr, _Alloc> m;
449  for (size_t i = 0; i < size; ++i)
450  m[keys->dataArray[i]->get()] = values->dataArray[i]->get();
451  return m;
452 }
453 
454 }