state_map_array.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_ARRAY_HPP
34 #define STATE_MAP_ARRAY_HPP
35 
37 
38 #include <stdexcept>
39 #include <vector>
40 #include <memory>
41 #include <eigen3/Eigen/Eigen>
42 
43 namespace tuw
44 {
45 template <class TLeafType, size_t TN>
47 {
48 protected:
49  std::array<std::shared_ptr<TLeafType>, TN> subs_;
50 };
52 {
53 };
54 
55 template <class TNumericType, class TLeafType, size_t TN>
57  : public StateMapBaseCRTP<StateMapArray<TNumericType, TLeafType, TN>>,
58  public StateMapBaseVirt<TNumericType>,
59  public DataBuffer<TNumericType, StateMapBaseCRTP<StateMapArray<TNumericType, TLeafType, TN>>::MapSize>,
60  public std::conditional<!std::is_same<TLeafType, TNumericType>::value, ContainerSubStateMapArray<TLeafType, TN>,
61  ContainerSubStateMapArrayEmpty>::type
62 {
63 private:
65 
66 private:
67  using NumericType = TNumericType;
68 
69 private:
70  using LeafType = TLeafType;
71 
72 private:
74 
75 private:
76  using RootType = typename StateMapBaseCRTPTraits<ImplType>::RootType;
77 
78 public:
80 
81 private:
83 
84 private:
85  using StateBaseVirtualType = typename StateMapBaseVirt<NumericType>::StateBaseVirtualType;
86 
87 private:
90 
91 private:
92  static constexpr const bool HasNumericLeaf = std::is_same<LeafType, NumericType>::value;
93 
94 private:
96 
97 private:
98  TNumericType* memStartRef_;
99 
100 private:
102 
103 private:
104  StateBaseVirtualType* root_;
105 
106  // static, not leaf
107 public:
108  template <bool numericLeaf = HasNumericLeaf, int mapSize = StateBaseCRTPType::MapSize,
109  typename std::enable_if<!numericLeaf && (mapSize != Eigen::Dynamic)>::type* = nullptr>
113  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>(std::shared_ptr<DataBufferContainterType>(new DataBufferContainterType))
114  , map_(nullptr)
115  , memStartRef_(nullptr)
116  , mapElementSize_(StateBaseCRTPType::MapSize)
117  , root_(this)
118  {
119  for (auto& subI : this->subs_)
120  {
121  subI = std::shared_ptr<LeafType>(new LeafType(this, this->dataBuffer_));
122  }
123  this->bindToMemory(this->dataBuffer_->data());
124  }
125  // dynamic, not leaf
126 public:
127  template <bool numericLeaf = HasNumericLeaf, int mapSize = StateBaseCRTPType::MapSize,
128  typename std::enable_if<!numericLeaf && (mapSize == Eigen::Dynamic)>::type* = nullptr>
132  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>(std::shared_ptr<DataBufferContainterType>(new DataBufferContainterType))
133  , map_(nullptr, 0)
134  , memStartRef_(nullptr)
135  , mapElementSize_(0)
136  , root_(this)
137  {
138  for (auto& subI : this->subs_)
139  {
140  subI = std::shared_ptr<LeafType>(new LeafType(this, this->dataBuffer_));
141  mapElementSize_ += subI->mapElementSize_;
142  }
143  if (mapElementSize_ > 0)
144  {
145  this->dataBuffer_->resize(mapElementSize_);
146  this->bindToMemory(this->dataBuffer_->data());
147  }
148  }
149  // leaf => static
150 public:
151  template <bool numericLeaf = HasNumericLeaf, int mapSize = StateBaseCRTPType::MapSize,
152  typename std::enable_if<numericLeaf && (mapSize != -1)>::type* = nullptr>
156  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>(std::shared_ptr<DataBufferContainterType>(new DataBufferContainterType))
157  , map_(nullptr)
158  , memStartRef_(nullptr)
159  , mapElementSize_(StateBaseCRTPType::MapSize)
160  , root_(this)
161  {
162  this->bindToMemory(this->dataBuffer_->data());
163  }
164 
165  // static, not leaf
166 public:
167  template <bool numericLeaf = HasNumericLeaf, int mapSize = StateBaseCRTPType::MapSize, typename TDataBuffer,
168  typename std::enable_if<!numericLeaf && (mapSize != Eigen::Dynamic)>::type* = nullptr>
169  StateMapArray(RootType* _root, std::shared_ptr<TDataBuffer>& _dataBuffer)
172  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>()
173  , map_(nullptr)
174  , memStartRef_(nullptr)
175  , mapElementSize_(StateBaseCRTPType::MapSize)
176  , root_(_root)
177  {
178  for (auto& subI : this->subs_)
179  {
180  subI = std::shared_ptr<LeafType>(new LeafType(_root, _dataBuffer));
181  }
182  }
183  // dynamic, not leaf
184 public:
185  template <bool numericLeaf = HasNumericLeaf, int mapSize = StateBaseCRTPType::MapSize, typename TDataBuffer,
186  typename std::enable_if<!numericLeaf && (mapSize == Eigen::Dynamic)>::type* = nullptr>
187  StateMapArray(RootType* _root, std::shared_ptr<TDataBuffer>& _dataBuffer)
190  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>()
191  , map_(nullptr, 0)
192  , memStartRef_(nullptr)
193  , mapElementSize_(0)
194  , root_(_root)
195  {
196  for (auto& subI : this->subs_)
197  {
198  subI = std::shared_ptr<LeafType>(new LeafType(_root, _dataBuffer));
199  mapElementSize_ += subI->mapElementSize_;
200  }
201  }
202  // leaf => static
203 public:
204  template <bool numericLeaf = HasNumericLeaf, int mapSize = StateBaseCRTPType::MapSize, typename TDataBuffer,
205  typename std::enable_if<numericLeaf && (mapSize != Eigen::Dynamic)>::type* = nullptr>
206  StateMapArray(RootType* _root, std::shared_ptr<TDataBuffer>& _dataBuffer)
209  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>()
210  , map_(nullptr)
211  , memStartRef_(nullptr)
212  , mapElementSize_(StateBaseCRTPType::MapSize)
213  , root_(_root)
214  {
215  }
216 
217 public:
218  virtual ~StateMapArray() = default;
219 
220 public:
222  : DataBuffer<TNumericType, StateBaseCRTPType::MapSize>(_rhs.dataBuffer_)
223  , map_(_rhs.map_)
224  , memStartRef_(nullptr)
225  , mapElementSize_(_rhs.mapElementSize_)
226  , root_(_rhs.root_)
227  {
228  if (!_rhs.root_->internalCopy_)
229  {
230  throw std::runtime_error("Copy-constructor not allowed");
231  }
232  else
233  {
234  copyRhsSubs(_rhs);
235  }
236  }
237 
238 private:
239  template <typename SubType = LeafType,
241  void copyRhsData(const StateMapArray& _rhs)
242  {
243  for (size_t i = 0; i < this->subs_.size(); ++i)
244  {
245  this->subs_[i]->copyRhsData(*_rhs.subs_[i]);
246  }
247  }
248 
249 private:
250  template <typename SubType = LeafType,
252  void copyRhsData(const StateMapArray& _rhs)
253  {
254  }
255 
256 private:
257  template <typename SubType = LeafType,
259  void copyRhsSubs(const StateMapArray& _rhs)
260  {
261  for (size_t i = 0; i < this->subs_.size(); ++i)
262  {
263  this->subs_[i] = std::shared_ptr<LeafType>(new LeafType(*_rhs.subs_[i].get()));
264  }
265  }
266 
267 private:
268  template <typename SubType = LeafType,
270  void copyRhsSubs(const StateMapArray& _rhs)
271  {
272  }
273 
274 private:
275  template <typename SubType = LeafType,
278  {
279  for (size_t i = 0; i < this->subs_.size(); ++i)
280  {
281  this->subs_[i] = std::shared_ptr<LeafType>(new LeafType(*_rhs.subs_[i].get()));
282  }
283  }
284 
285 private:
286  template <typename SubType = LeafType,
289  {
290  }
291 
292 public:
294  {
295  if (this != &_rhs)
296  {
297  if (!root_->internalCopy_)
298  {
299  copyRhsData(_rhs);
300  this->data() = _rhs.data();
301  }
302  else
303  {
304  mapElementSize_ = _rhs.mapElementSize_;
305  createCopyRhsSubs(_rhs);
306  }
307  }
308  return *this;
309  }
310 
311 public:
312  StateMapArray(StateMapArray&& _rhs) = default;
313 
314 public:
315  StateMapArray& operator=(StateMapArray&& _rhs) = default;
316 
317 public:
319 
320 public:
322 
323 public:
325 
326 public:
328 
329 public:
331 
332 public:
334 
335 private:
336  MapTypeVirt dataImplVirt() override final
337  {
338  return MapTypeVirt(memStartRef_, mapElementSize_);
339  }
340 
341 private:
342  const MapTypeVirt dataImplVirt() const override final
343  {
344  return MapTypeVirt(memStartRef_, mapElementSize_);
345  }
346 
347 private:
348  StateBaseVirtualType& subImplVirt(const size_t& _i) override final
349  {
350  return subImplVirtDispatch(_i);
351  }
352 
353 private:
354  const StateBaseVirtualType& subImplVirt(const size_t& _i) const override final
355  {
356  return subImplVirtDispatch(_i);
357  }
358 
359 private:
360  void subResizeImplVirt(const size_t& _size) override final
361  {
362  throw std::runtime_error("Cannot resize an array");
363  }
364 
365 private:
366  void bindToMemoryImplVirt(TNumericType* _memRef) override final
367  {
368  bindToMemoryImplCRTP(_memRef);
369  }
370 
371 private:
372  const size_t subSizeImplVirt() const override final
373  {
374  return subSizeImplCRTP();
375  }
376 
377 private:
378  NumericType* const memStartRefImplVirt() const override final
379  {
380  return memStartRefImplCRTP();
381  }
382 
383 private:
384  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
385  StateBaseVirtualType& subImplVirtDispatch(const size_t& _i)
386  {
387  return subImplCRTP(_i);
388  }
389 
390 private:
391  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
392  const StateBaseVirtualType& subImplVirtDispatch(const size_t& _i) const
393  {
394  return subImplCRTP(_i);
395  }
396 
397 private:
398  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
399  StateBaseVirtualType& subImplVirtDispatch(const size_t& _i)
400  {
401  throw std::runtime_error("Access of numeric Leaf using virtual \"sub\" function not allowed");
402  return *root_;
403  }
404 
405 private:
406  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
407  const StateBaseVirtualType& subImplVirtDispatch(const size_t& _i) const
408  {
409  throw std::runtime_error("Access of numeric Leaf using virtual \"sub\" function not allowed");
410  return *root_;
411  }
412 
413 private:
414  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
415  void bindToMemoryImplCRTP(TNumericType* _memRef)
416  {
417  size_t mapMemBeginShift(0);
418  for (auto& subI : this->subs_)
419  {
420  subI->bindToMemory(_memRef + mapMemBeginShift);
421  mapMemBeginShift += subI->mapElementSize_;
422  }
423  memStartRef_ = _memRef;
424  mapElementSize_ = mapMemBeginShift;
425  bindMap();
426  }
427 
428 private:
429  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
430  void bindToMemoryImplCRTP(TNumericType* _memRef)
431  {
432  memStartRef_ = _memRef;
433  bindMap();
434  }
435 
436 private:
437  template <bool dynamicMap = StateBaseCRTPType::MapSize == -1, typename std::enable_if<(dynamicMap)>::type* = nullptr>
438  void bindMap()
439  {
440  new (&map_) MapTypeCRTP(memStartRef_, mapElementSize_);
441  }
442 
443 private:
444  template <bool dynamicMap = StateBaseCRTPType::MapSize == -1, typename std::enable_if<(!dynamicMap)>::type* = nullptr>
445  void bindMap()
446  {
447  new (&map_) MapTypeCRTP(memStartRef_);
448  }
449 
450 private:
452  {
453  return map_;
454  }
455 
456 private:
457  const MapTypeCRTP& dataImplCRTP() const
458  {
459  return map_;
460  }
461 
462 private:
463  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
464  LeafType& subImplCRTP(const size_t& _i)
465  {
466  return *this->subs_[_i];
467  }
468 
469 private:
470  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
471  const LeafType& subImplCRTP(const size_t& _i) const
472  {
473  return *this->subs_[_i];
474  }
475 
476 private:
477  template <size_t _i, bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
479  {
480  return *this->subs_[_i];
481  }
482 
483 private:
484  template <size_t _i, bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
485  const LeafType& subImplCRTP() const
486  {
487  return *this->subs_[_i];
488  }
489 
490 private:
491  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
492  const size_t subSizeImplCRTP() const
493  {
494  return this->subs_.size();
495  }
496 
497 private:
498  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
499  LeafType& subImplCRTP(const size_t& _i)
500  {
501  return this->data()(_i);
502  }
503 
504 private:
505  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
506  const LeafType& subImplCRTP(const size_t& _i) const
507  {
508  return this->data()(_i);
509  }
510 
511 private:
512  template <size_t _i, bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
514  {
515  return this->data()(_i);
516  }
517 
518 private:
519  template <size_t _i, bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
520  const LeafType& subImplCRTP() const
521  {
522  return this->data()(_i);
523  }
524 
525 private:
526  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
527  const size_t subSizeImplCRTP() const
528  {
529  return mapElementSize_;
530  }
531 
532 private:
534  {
535  return memStartRef_;
536  }
537 
538  // friends
539  template <class Derived2>
540  friend class StateMapBaseCRTP;
541  template <class Derived2>
542  friend struct StateMapBaseCRTPTraits;
543  template <class TNumericType2>
544  friend class StateMapBaseVirt;
545  template <class TNumericType2, class... TLeafTypes2>
546  friend class StateMapTuple;
547  template <class TNumericType2, class TLeafType2>
548  friend class StateMapVector;
549  template <class TNumericType2, class TLeafType2, size_t TN2>
550  friend class StateMapArray;
551  template <class TLeafType2>
552  friend struct LeafTypeContClass;
553 };
554 
555 namespace
556 {
557 template <class TNumericType, class TLeafType, size_t TN>
558 struct StateMapBaseCRTPTraits<StateMapArray<TNumericType, TLeafType, TN>>
559  : public std::conditional<std::is_same<TNumericType, TLeafType>::value, LeafTypeContNum<TNumericType>,
560  LeafTypeContClass<TLeafType>>::type
561 {
562  using NumericType = TNumericType;
563  using LeafTypeExt = TLeafType;
564  using LeafsTupleType = EmptyLeafType;
565  using LeafsTupleTypeExt = EmptyLeafType;
567  static constexpr const int subSize = TN;
568  static constexpr const bool isDynamic = false;
569 };
570 
571 } // namespace <anonymous>
572 }
573 
574 #endif // STATE_MAP_ARRAY_HPP
void bindToMemoryImplCRTP(TNumericType *_memRef)
StateMapArray & operator=(const StateMapArray &_rhs)
StateMapArray(RootType *_root, std::shared_ptr< TDataBuffer > &_dataBuffer)
const MapTypeVirt dataImplVirt() const overridefinal
std::array< std::shared_ptr< TLeafType >, TN > subs_
Helper function needed to upgrade c++ 2011.
Definition: utils.h:193
LeafType & subImplCRTP(const size_t &_i)
const size_t subSizeImplCRTP() const
const StateBaseVirtualType & subImplVirtDispatch(const size_t &_i) const
NumericType *const memStartRefImplCRTP() const
StateBaseVirtualType & subImplVirtDispatch(const size_t &_i)
typename StateMapBaseCRTP< ImplType >::MapTypeCRTP MapTypeCRTP
typename StateMapBaseVirt< NumericType >::MapTypeVirt MapTypeVirt
typename DataBufferContainerClass::ContainerType DataBufferContainerType
void copyRhsData(const StateMapArray &_rhs)
void subResizeImplVirt(const size_t &_size) overridefinal
Eigen::Map< Eigen::Matrix< TNumericType, Eigen::Dynamic, 1 > > MapTypeVirt
static constexpr auto value
Definition: utils.h:205
typename DataBuffer< TNumType, StateBaseCRTPType::MapSize >::DataBufferContainerType DataBufferContainterType
typename StateMapBaseCRTPTraits< ImplType >::RootType RootType
void bindToMemoryImplVirt(TNumericType *_memRef) overridefinal
NumericType *const memStartRefImplVirt() const overridefinal
const LeafType & subImplCRTP() const
const StateBaseVirtualType & subImplVirt(const size_t &_i) const overridefinal
void copyRhsSubs(const StateMapArray &_rhs)
MapTypeVirt dataImplVirt() overridefinal
const size_t subSizeImplVirt() const overridefinal
TNumericType * memStartRef_
void createCopyRhsSubs(const StateMapArray &_rhs)
StateMapArray(const StateMapArray &_rhs)
StateBaseVirtualType * root_
const MapTypeCRTP & dataImplCRTP() const
const LeafType & subImplCRTP(const size_t &_i) const
LeafType & subImplCRTP()
MapTypeCRTP & dataImplCRTP()
StateBaseVirtualType & subImplVirt(const size_t &_i) overridefinal


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