state_map_base.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2017 by George Todoran <george.todoran@tuwien.ac.at *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
33 #ifndef STATE_MAP_BASE_HPP
34 #define STATE_MAP_BASE_HPP
35 
36 #include <iostream>
37 #include <stdexcept>
38 #include <vector>
39 #include <memory>
40 #include <eigen3/Eigen/Eigen>
41 #include <stdexcept>
42 
43 
44 #ifdef USE_MAP_ALIGNMENT
45 #include <boost/align/aligned_allocator.hpp>
46 
47 namespace tuw
48 {
49 constexpr const int MapAlignment = Eigen::Aligned16;
50 template <typename T, size_t N>
51 struct __attribute__((aligned(16))) aligned_array : public std::array<T, N>
52 {
53 };
54 };
55 #endif
56 
57 namespace tuw
58 {
59 namespace
60 {
61 template <class TDerived>
62 struct StateMapBaseCRTPTraits;
63 
64 class EmptyLeafType
65 {
66 };
67 
68 template <class ThisStateTraits>
69 constexpr inline typename std::enable_if<
71 getMapSize();
72 template <class ThisStateTraits>
73 constexpr inline typename std::enable_if<
74  (!std::is_same<typename ThisStateTraits::NumericType, typename ThisStateTraits::LeafType>::value) &&
76  int>::type
77 getMapSize();
78 template <class ThisStateTraits>
79 constexpr inline typename std::enable_if<
80  (!std::is_same<typename ThisStateTraits::NumericType, typename ThisStateTraits::LeafType>::value) &&
82  int>::type
83 getMapSize();
84 
85 template <typename Tuple, std::size_t II = 0>
86 constexpr inline typename std::enable_if<II == std::tuple_size<Tuple>::value, int>::type get_tuple_map_size()
87 {
88  return 0;
89 }
90 template <typename Tuple, std::size_t II = 0>
91  constexpr inline typename std::enable_if < II<std::tuple_size<Tuple>::value, int>::type get_tuple_map_size()
92 {
93  constexpr const int sizeSubThis = getMapSize<StateMapBaseCRTPTraits<typename std::tuple_element<II, Tuple>::type>>();
94  constexpr const int sizeSubNext = get_tuple_map_size<Tuple, II + 1>();
95  return +((sizeSubThis == -1) || (sizeSubNext == -1)) * (-1) +
96  !((sizeSubThis == -1) || (sizeSubNext == -1)) * (sizeSubThis + sizeSubNext);
97 }
98 
99 template <class ThisStateTraits>
100 constexpr inline typename std::enable_if<
101  std::is_same<typename ThisStateTraits::NumericType, typename ThisStateTraits::LeafType>::value, int>::type
102 getMapSize()
103 {
104  return ThisStateTraits::subSize;
105 }
106 template <class ThisStateTraits>
107 constexpr inline typename std::enable_if<
108  (!std::is_same<typename ThisStateTraits::NumericType, typename ThisStateTraits::LeafType>::value) &&
110  int>::type
111 getMapSize()
112 {
113  constexpr const int sizeSubThis = ThisStateTraits::subSize;
114  constexpr const int sizeSubLeaf = getMapSize<StateMapBaseCRTPTraits<typename ThisStateTraits::LeafType>>();
115  return ((sizeSubThis == -1) || (sizeSubLeaf == -1)) * (-1) +
116  !((sizeSubThis == -1) || (sizeSubLeaf == -1)) * (sizeSubThis * sizeSubLeaf);
117 }
118 template <class ThisStateTraits>
119 constexpr inline typename std::enable_if<
120  (!std::is_same<typename ThisStateTraits::NumericType, typename ThisStateTraits::LeafType>::value) &&
122  int>::type
123 getMapSize()
124 {
125  return get_tuple_map_size<typename ThisStateTraits::LeafsTupleType>();
126 }
127 
128 template <class TNumericType>
129 struct LeafTypeContNum
130 {
131  using LeafType = TNumericType;
132 };
133 template <class TLeafType>
134 struct LeafTypeContClass
135 {
136  using LeafType = typename TLeafType::ImplType;
137 };
138 
139 } // namespace <anonymous>
140 
141 template <class TNumericType>
143 {
144 public:
145 #ifdef USE_MAP_ALIGNMENT
146  using ContainerType = std::vector<TNumericType, boost::alignment::aligned_allocator<TNumericType, MapAlignment>>;
147 #else
148  using ContainerType = std::vector<TNumericType>;
149 #endif
150  // special class member functions
151 public:
152  DataBufferVector(std::shared_ptr<ContainerType> _dataBuffer) : dataBuffer_(_dataBuffer)
153  {
154  }
155 
156 public:
157  DataBufferVector() = default;
158 
159 public:
160  ~DataBufferVector() = default;
161 
162 private:
163  DataBufferVector(const DataBufferVector& _other) = default;
164 
165 private:
166  DataBufferVector& operator=(const DataBufferVector& _other) = default;
167 
168 public:
169  DataBufferVector(DataBufferVector&& _other) = default;
170 
171 public:
172  DataBufferVector& operator=(DataBufferVector&& _other) = default;
173 
174 private:
175  std::shared_ptr<ContainerType> dataBuffer_;
176 
177  // friends
178  template <class Derived2>
179  friend class StateMapBaseCRTP;
180  template <class Derived2>
181  friend struct StateMapBaseCRTPTraits;
182  template <class TNumericType2>
183  friend class StateMapBaseVirt;
184  template <class TNumericType2, class... TLeafTypes2>
185  friend class StateMapTuple;
186  template <class TNumericType2, class TLeafType2>
187  friend class StateMapVector;
188  template <class TNumericType2, class TLeafType2, size_t TN2>
189  friend class StateMapArray;
190  template <class TLeafType2>
191  friend struct LeafTypeContClass;
192  template <class TNumericType2, int TMapSize2>
193  friend class DataBuffer;
194 };
195 template <class TNumericType, int TMapSize>
197 {
198 private:
199 #ifdef USE_MAP_ALIGNMENT
200  using ContainerType = aligned_array<TNumericType, TMapSize>;
201 #else
202  using ContainerType = std::array<TNumericType, TMapSize>;
203 #endif
204 
205  // special class member functions
206 public:
207  DataBufferArray(std::shared_ptr<ContainerType> _dataBuffer) : dataBuffer_(_dataBuffer)
208  {
209  }
210 
211 public:
212  DataBufferArray() = default;
213 
214 public:
215  ~DataBufferArray() = default;
216 
217 private:
218  DataBufferArray(const DataBufferArray& _other) = default;
219 
220 private:
221  DataBufferArray& operator=(const DataBufferArray& _other) = default;
222 
223 public:
224  DataBufferArray(DataBufferArray&& _other) = default;
225 
226 public:
227  DataBufferArray& operator=(DataBufferArray&& _other) = default;
228 
229 private:
230  std::shared_ptr<ContainerType> dataBuffer_;
231 
232  // friends
233  template <class Derived2>
234  friend class StateMapBaseCRTP;
235  template <class Derived2>
236  friend struct StateMapBaseCRTPTraits;
237  template <class TNumericType2>
238  friend class StateMapBaseVirt;
239  template <class TNumericType2, class... TLeafTypes2>
240  friend class StateMapTuple;
241  template <class TNumericType2, class TLeafType2>
242  friend class StateMapVector;
243  template <class TNumericType2, class TLeafType2, size_t TN2>
244  friend class StateMapArray;
245  template <class TLeafType2>
246  friend struct LeafTypeContClass;
247  template <class TNumericType2, int TMapSize2>
248  friend class DataBuffer;
249 };
250 
251 template <class TNumericType, int TMapSize>
252 class DataBuffer : public std::conditional<TMapSize == Eigen::Dynamic, DataBufferVector<TNumericType>,
253  DataBufferArray<TNumericType, TMapSize>>::type
254 {
255 private:
256  using DataBufferContainerClass = typename std::conditional<TMapSize == Eigen::Dynamic, DataBufferVector<TNumericType>,
258 
259 private:
260  using DataBufferContainerType = typename DataBufferContainerClass::ContainerType;
261 
262  // special class member functions
263 public:
264  DataBuffer(std::shared_ptr<DataBufferContainerType> _dataBuffer) : DataBufferContainerClass(_dataBuffer)
265  {
266  }
267 
268 public:
269  DataBuffer() = default;
270 
271 public:
272  ~DataBuffer() = default;
273 
274 private:
275  DataBuffer(const DataBuffer& _other) = default;
276 
277 private:
278  DataBuffer& operator=(const DataBuffer& _other) = default;
279 
280 public:
281  DataBuffer(DataBuffer&& _other) = default;
282 
283 public:
284  DataBuffer& operator=(DataBuffer&& _other) = default;
285 
286  // friends
287  template <class Derived2>
288  friend class StateMapBaseCRTP;
289  template <class Derived2>
290  friend struct StateMapBaseCRTPTraits;
291  template <class TNumericType2>
292  friend class StateMapBaseVirt;
293  template <class TNumericType2, class... TLeafTypes2>
294  friend class StateMapTuple;
295  template <class TNumericType2, class TLeafType2>
296  friend class StateMapVector;
297  template <class TNumericType2, class TLeafType2, size_t TN2>
298  friend class StateMapArray;
299  template <class TLeafType2>
300  friend struct LeafTypeContClass;
301 };
302 
303 template <class TDerived>
305 {
306 private:
307  using NumericType = typename StateMapBaseCRTPTraits<TDerived>::NumericType;
308 
309 private:
310  using LeafType = typename StateMapBaseCRTPTraits<TDerived>::LeafTypeExt;
311 
312 private:
313  using RootType = typename StateMapBaseCRTPTraits<TDerived>::RootType;
314 
315 public:
316  static constexpr const int MapSize = getMapSize<StateMapBaseCRTPTraits<TDerived>>();
317 
318 public:
319  using MatrixTypeCRTP = Eigen::Matrix<NumericType, MapSize, 1>;
320 
321 public:
322 #ifdef USE_MAP_ALIGNMENT
323  using MapTypeCRTP = Eigen::Map<MatrixTypeCRTP, MapAlignment>;
324 #else
325  using MapTypeCRTP = Eigen::Map<MatrixTypeCRTP>;
326 #endif
327 
328 private:
329  static constexpr const bool IsDynamic = StateMapBaseCRTPTraits<TDerived>::isDynamic;
330 
331  // special class member functions
332 public:
333  StateMapBaseCRTP() = default;
334 
335 public:
336  ~StateMapBaseCRTP() = default;
337 
338 private:
339  StateMapBaseCRTP(const StateMapBaseCRTP&) = default;
340 
341 private:
342  StateMapBaseCRTP& operator=(const StateMapBaseCRTP&) = default;
343 
344 public:
345  StateMapBaseCRTP(StateMapBaseCRTP&&) = default;
346 
347 public:
348  StateMapBaseCRTP& operator=(StateMapBaseCRTP&&) = default;
349 
350 public:
352  {
353  return thisDerived().dataImplCRTP();
354  }
355 
356 public:
357  const MapTypeCRTP& data() const
358  {
359  return thisDerived().dataImplCRTP();
360  }
361 
362 public:
363  template <typename leafType = LeafType,
365  LeafType& sub(const size_t& _i)
366  {
367  return thisDerived().subImplCRTP(_i);
368  }
369 
370 public:
371  template <typename leafType = LeafType,
373  const LeafType& sub(const size_t& _i) const
374  {
375  return thisDerived().subImplCRTP(_i);
376  }
377 
378 public:
379  template <size_t _i, typename leafType = LeafType,
382  {
383  return thisDerived().template subImplCRTP<_i>();
384  }
385 
386 public:
387  template <size_t _i, typename leafType = LeafType,
389  const LeafType& sub() const
390  {
391  return thisDerived().template subImplCRTP<_i>();
392  }
393 
394 public:
395  template <size_t _i, typename leafType = LeafType,
397  typename std::tuple_element<_i, typename StateMapBaseCRTPTraits<TDerived>::LeafsTupleTypeExt>::type& sub()
398  {
399  return thisDerived().template subImplCRTP<_i>();
400  }
401 
402 public:
403  template <size_t _i, typename leafType = LeafType,
405  const typename std::tuple_element<_i, typename StateMapBaseCRTPTraits<TDerived>::LeafsTupleTypeExt>::type& sub() const
406  {
407  return thisDerived().template subImplCRTP<_i>();
408  }
409 
410 public:
411  template <bool isDynamic = IsDynamic, typename std::enable_if<(isDynamic)>::type* = nullptr>
412  void subResize(const size_t& _size)
413  {
414  thisDerived().subResizeImplCRTP(_size);
415  }
416 
417 public:
418  void bindToMemory(NumericType* _memRef)
419  {
420  thisDerived().bindToMemoryImplCRTP(_memRef);
421  }
422 
423 public:
424  constexpr const size_t subSize() const
425  {
426  return thisDerived().subSizeImplCRTP();
427  }
428 
429 public:
430  NumericType* const memStartRef() const
431  {
432  return thisDerived().memStartRefImplCRTP();
433  }
434 
435 private:
436  TDerived& thisDerived()
437  {
438  return static_cast<TDerived&>(*this);
439  }
440 
441 private:
442  const TDerived& thisDerived() const
443  {
444  return static_cast<const TDerived&>(*this);
445  }
446 
447  // friends
448  template <class Derived2>
449  friend class StateMapBaseCRTP;
450  template <class Derived2>
451  friend struct StateMapBaseCRTPTraits;
452  template <class TNumericType2>
453  friend class StateMapBaseVirt;
454  template <class TNumericType2, class... TLeafTypes2>
455  friend class StateMapTuple;
456  template <class TNumericType2, class TLeafType2>
457  friend class StateMapVector;
458  template <class TNumericType2, class TLeafType2, size_t TN2>
459  friend class StateMapArray;
460  template <class TLeafType2>
461  friend struct LeafTypeContClass;
462 };
463 
464 template <class TDerived>
465 constexpr const int StateMapBaseCRTP<TDerived>::MapSize;
466 
467 template <class TNumericType>
469 {
470 private:
471 #ifdef USE_MAP_ALIGNMENT
472  using MapTypeVirt = Eigen::Map<Eigen::Matrix<TNumericType, Eigen::Dynamic, 1>, MapAlignment>;
473 #else
474  using MapTypeVirt = Eigen::Map<Eigen::Matrix<TNumericType, Eigen::Dynamic, 1> >;
475 #endif
476 
477 private:
479 
480  // special class member functions
481 public:
482  StateMapBaseVirt() : internalCopy_(false)
483  {
484  }
485 
486 public:
487  virtual ~StateMapBaseVirt() = default;
488 
489 private:
490  StateMapBaseVirt(const StateMapBaseVirt&) = default;
491 
492 private:
493  StateMapBaseVirt& operator=(const StateMapBaseVirt&) = default;
494 
495 public:
496  StateMapBaseVirt(StateMapBaseVirt&&) = default;
497 
498 public:
499  StateMapBaseVirt& operator=(StateMapBaseVirt&&) = default;
500 
501 public:
503  {
504  return dataImplVirt();
505  }
506 
507 public:
508  const MapTypeVirt data() const
509  {
510  return dataImplVirt();
511  }
512 
513 public:
514  StateBaseVirtualType& sub(const size_t& _i)
515  {
516  return subImplVirt(_i);
517  }
518 
519 public:
520  const StateBaseVirtualType& sub(const size_t& _i) const
521  {
522  return subImplVirt(_i);
523  }
524 
525 public:
526  void subResize(const size_t& _size)
527  {
528  subResizeImplVirt(_size);
529  }
530 
531 public:
532  const size_t& subSize() const
533  {
534  return subSizeImplVirt();
535  }
536 
537 public:
538  TNumericType* const memStartRef() const
539  {
540  return memStartRefImplVirt();
541  }
542 
543 private:
544  void bindToMemory(TNumericType* _memRef)
545  {
546  bindToMemoryImplVirt(_memRef);
547  }
548 
549 private:
550  virtual MapTypeVirt dataImplVirt() = 0;
551 
552 private:
553  virtual const MapTypeVirt dataImplVirt() const = 0;
554 
555 private:
556  virtual StateBaseVirtualType& subImplVirt(const size_t& _i) = 0;
557 
558 private:
559  virtual const StateBaseVirtualType& subImplVirt(const size_t& _i) const = 0;
560 
561 private:
562  virtual void subResizeImplVirt(const size_t& _size) = 0;
563 
564 private:
565  virtual const size_t subSizeImplVirt() const = 0;
566 
567 private:
568  virtual TNumericType* const memStartRefImplVirt() const = 0;
569 
570 private:
571  virtual void bindToMemoryImplVirt(TNumericType* _memRef) = 0;
572 
573 private:
575 
576  // friends
577  template <class Derived2>
578  friend class StateMapBaseCRTP;
579  template <class Derived2>
580  friend struct StateMapBaseCRTPTraits;
581  template <class TNumericType2>
582  friend class StateMapBaseVirt;
583  template <class TNumericType2, class... TLeafTypes2>
584  friend class StateMapTuple;
585  template <class TNumericType2, class TLeafType2>
586  friend class StateMapVector;
587  template <class TNumericType2, class TLeafType2, size_t TN2>
588  friend class StateMapArray;
589  template <class TLeafType2>
590  friend struct LeafTypeContClass;
591 };
592 
593 } // namespace tuw
594 
595 #endif // STATE_MAP_BASE_HPP
const StateBaseVirtualType & sub(const size_t &_i) const
const MapTypeCRTP & data() const
Eigen::Matrix< NumericType, MapSize, 1 > MatrixTypeCRTP
typename StateMapBaseCRTPTraits< StateMapTuple< TNum, TLeafTypes... > >::NumericType NumericType
StateBaseVirtualType & sub(const size_t &_i)
std::array< TNumericType, TMapSize > ContainerType
typename StateMapBaseCRTPTraits< StateMapTuple< TNum, TLeafTypes... > >::LeafTypeExt LeafType
void subResize(const size_t &_size)
const LeafType & sub() const
std::shared_ptr< ContainerType > dataBuffer_
const TDerived & thisDerived() const
NumericType *const memStartRef() const
std::tuple_element< _i, typename StateMapBaseCRTPTraits< TDerived >::LeafsTupleTypeExt >::type & sub()
void bindToMemory(NumericType *_memRef)
typename StateMapBaseCRTPTraits< StateMapTuple< TNum, TLeafTypes... > >::RootType RootType
Eigen::Map< Eigen::Matrix< TNumericType, Eigen::Dynamic, 1 > > MapTypeVirt
static constexpr auto value
Definition: utils.h:205
std::vector< TNumericType > ContainerType
DataBufferArray(std::shared_ptr< ContainerType > _dataBuffer)
void bindToMemory(TNumericType *_memRef)
std::shared_ptr< ContainerType > dataBuffer_
typename std::conditional< TMapSize==Eigen::Dynamic, DataBufferVector< TNumericType >, DataBufferArray< TNumericType, TMapSize >>::type DataBufferContainerClass
const LeafType & sub(const size_t &_i) const
LeafType & sub(const size_t &_i)
TNumericType *const memStartRef() const
void subResize(const size_t &_size)
constexpr const size_t subSize() const
DataBuffer(std::shared_ptr< DataBufferContainerType > _dataBuffer)
const std::tuple_element< _i, typename StateMapBaseCRTPTraits< TDerived >::LeafsTupleTypeExt >::type & sub() const
const MapTypeVirt data() const
const size_t & subSize() const
DataBufferVector(std::shared_ptr< ContainerType > _dataBuffer)


tuw_control
Author(s): George Todoran
autogenerated on Mon Jun 10 2019 15:27:21