state_map_tuple.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_TUPLE_HPP
34 #define STATE_MAP_TUPLE_HPP
35 
37 #include <tuw_control/utils.h>
38 
39 #include <stdexcept>
40 #include <vector>
41 #include <memory>
42 #include <eigen3/Eigen/Eigen>
43 
44 namespace tuw
45 {
46 template <class TNumericType, typename... TLeafTypes>
48  : public StateMapBaseCRTP<StateMapTuple<TNumericType, TLeafTypes...>>,
49  public StateMapBaseVirt<TNumericType>,
50  public DataBuffer<TNumericType, StateMapBaseCRTP<StateMapTuple<TNumericType, TLeafTypes...>>::MapSize>
51 {
52 private:
53  using ImplType = StateMapTuple<TNumericType, TLeafTypes...>;
54 
55 private:
56  using NumericType = TNumericType;
57 
58 private:
59  using LeafType = EmptyLeafType;
60 
61 private:
63 
64 private:
65  using RootType = typename StateMapBaseCRTPTraits<ImplType>::RootType;
66 
67 public:
69 
70 private:
72 
73 private:
75 
76 private:
79 
80 private:
82 
83 private:
85 
86 private:
87  TNumericType* memStartRef_;
88 
89 private:
91 
92 private:
93  std::tuple<TLeafTypes...> subs_;
94 
95 private:
96  std::array<StateBaseVirtualType*, sizeof...(TLeafTypes)> subsBase_;
97 
98 private:
99  StateBaseVirtualType* root_;
100 
101 public:
102  template <bool numericLeaf = HasNumericLeaf, int mapSize = StateBaseCRTPType::MapSize,
103  typename std::enable_if<!numericLeaf && (mapSize != Eigen::Dynamic)>::type* = nullptr>
106  , StateMapBaseVirt<NumericType>()
108  , map_(nullptr)
109  , memStartRef_(nullptr)
110  , mapElementSize_(StateBaseCRTPType::MapSize)
111  , subs_(std::make_tuple(TLeafTypes(this, this->dataBuffer_)...))
112  , root_(this)
113  {
114  size_t i = 0;
115  for_each_tuple(subs_, [this, &i](auto& subI)
116  {
117  subsBase_[i++] = &subI;
118  });
119  this->bindToMemory(&this->dataBuffer_->at(0));
120  }
121 
122 public:
123  template <bool numericLeaf = HasNumericLeaf, int mapSize = StateBaseCRTPType::MapSize,
124  typename std::enable_if<!numericLeaf && (mapSize == Eigen::Dynamic)>::type* = nullptr>
127  , StateMapBaseVirt<NumericType>()
129  , map_(nullptr, 0)
130  , memStartRef_(nullptr)
131  , mapElementSize_(0)
132  , subs_(std::make_tuple(TLeafTypes(this, this->dataBuffer_)...))
133  , root_(this)
134  {
135  size_t i = 0;
136  for_each_tuple(subs_, [this, &i](auto& subI)
137  {
138  subsBase_[i++] = &subI;
139  mapElementSize_ += subI.mapElementSize_;
140  });
141  if (mapElementSize_ > 0)
142  {
143  this->dataBuffer_->resize(mapElementSize_);
144  this->bindToMemory(&this->dataBuffer_->at(0));
145  }
146  }
147 
148 public:
149  template <bool numericLeaf = HasNumericLeaf, int mapSize = StateBaseCRTPType::MapSize, typename TDataBuffer,
150  typename std::enable_if<!numericLeaf && (mapSize != Eigen::Dynamic)>::type* = nullptr>
151  StateMapTuple(RootType* _root, std::shared_ptr<TDataBuffer>& _dataBuffer)
153  , StateMapBaseVirt<NumericType>()
154  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>()
155  , map_(nullptr)
156  , memStartRef_(nullptr)
157  , mapElementSize_(StateBaseCRTPType::MapSize)
158  , subs_(std::make_tuple(TLeafTypes(_root, _dataBuffer)...))
159  , root_(_root)
160  {
161  size_t i = 0;
162  for_each_tuple(subs_, [this, &i](auto& subI)
163  {
164  subsBase_[i++] = &subI;
165  });
166  }
167 
168 public:
169  template <bool numericLeaf = HasNumericLeaf, int mapSize = StateBaseCRTPType::MapSize, typename TDataBuffer,
170  typename std::enable_if<!numericLeaf && (mapSize == Eigen::Dynamic)>::type* = nullptr>
171  StateMapTuple(RootType* _root, std::shared_ptr<TDataBuffer>& _dataBuffer)
173  , StateMapBaseVirt<NumericType>()
174  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>()
175  , map_(nullptr, 0)
176  , memStartRef_(nullptr)
177  , mapElementSize_(0)
178  , subs_(std::make_tuple(TLeafTypes(_root, _dataBuffer)...))
179  , root_(_root)
180  {
181  size_t i = 0;
182  for_each_tuple(subs_, [this, &i](auto& subI)
183  {
184  subsBase_[i++] = &subI;
185  mapElementSize_ += subI.mapElementSize_;
186  });
187  }
188 
189 public:
190  virtual ~StateMapTuple() = default;
191 
192 public:
194  : DataBuffer<TNumericType, StateBaseCRTPType::MapSize>(_rhs.dataBuffer_)
195  , map_(_rhs.map_)
196  , memStartRef_(nullptr)
197  , mapElementSize_(_rhs.mapElementSize_)
198  , subs_(_rhs.subs_)
199  , root_(_rhs.root_)
200  {
201  if (!_rhs.root_->internalCopy_)
202  {
203  throw std::runtime_error("Copy-constructor not allowed");
204  }
205  size_t i = 0;
206  for_each_tuple(subs_, [this, &i](auto& subI)
207  {
208  subsBase_[i++] = &subI;
209  });
210  }
211 
212 private:
213  void copyRhsData(const StateMapTuple& _rhs)
214  {
215  for_each_2_tuples(_rhs.subs_, subs_, [](auto& _subIRhs, auto& _subI)
216  {
217  _subI.copyRhsData(_subIRhs);
218  });
219  }
220 
221 public:
223  {
224  if (this != &_rhs)
225  {
226  if (!root_->internalCopy_)
227  {
228  copyRhsData(_rhs);
229  this->data() = _rhs.data();
230  }
231  else
232  {
233  mapElementSize_ = _rhs.mapElementSize_;
234  subs_ = _rhs.subs_;
235  size_t i = 0;
236  for_each_tuple(subs_, [this, &i](auto& subI)
237  {
238  subsBase_[i++] = &subI;
239  });
240  }
241  }
242  return *this;
243  }
244 
245 public:
246  StateMapTuple(StateMapTuple&&) = default;
247 
248 public:
249  StateMapTuple& operator=(StateMapTuple&&) = default;
250 
251 public:
252  using StateMapBaseCRTP<StateMapTuple<TNumericType, TLeafTypes...>>::data;
253 
254 public:
255  using StateMapBaseCRTP<StateMapTuple<TNumericType, TLeafTypes...>>::sub;
256 
257 public:
258  using StateMapBaseCRTP<StateMapTuple<TNumericType, TLeafTypes...>>::subResize;
259 
260 public:
261  using StateMapBaseCRTP<StateMapTuple<TNumericType, TLeafTypes...>>::subSize;
262 
263 public:
264  using StateMapBaseCRTP<StateMapTuple<TNumericType, TLeafTypes...>>::memStartRef;
265 
266 public:
267  using StateMapBaseCRTP<StateMapTuple<TNumericType, TLeafTypes...>>::bindToMemory;
268 
269 private:
270  MapTypeVirt dataImplVirt() override final
271  {
272  return MapTypeVirt(memStartRef_, mapElementSize_);
273  }
274 
275 private:
276  const MapTypeVirt dataImplVirt() const override final
277  {
278  return MapTypeVirt(memStartRef_, mapElementSize_);
279  }
280 
281 private:
282  StateBaseVirtualType& subImplVirt(const size_t& _i) override final
283  {
284  return subImplVirtDispatch(_i);
285  }
286 
287 private:
288  const StateBaseVirtualType& subImplVirt(const size_t& _i) const override final
289  {
290  return subImplVirtDispatch(_i);
291  }
292 
293 private:
294  void subResizeImplVirt(const size_t& _size) override final
295  {
296  throw std::runtime_error("Cannot resize a tuple");
297  }
298 
299 private:
300  void bindToMemoryImplVirt(TNumericType* _memRef) override final
301  {
302  bindToMemoryImplCRTP(_memRef);
303  }
304 
305 private:
306  const size_t subSizeImplVirt() const override final
307  {
308  return subSizeImplCRTP();
309  }
310 
311 private:
312  NumericType* const memStartRefImplVirt() const override final
313  {
314  return memStartRefImplCRTP();
315  }
316 
317 private:
318  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
319  StateBaseVirtualType& subImplVirtDispatch(const size_t& _i)
320  {
321  return *subsBase_[_i];
322  }
323 
324 private:
325  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
326  const StateBaseVirtualType& subImplVirtDispatch(const size_t& _i) const
327  {
328  return *subsBase_[_i];
329  }
330 
331 private:
332  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
333  StateBaseVirtualType& subImplVirtDispatch(const size_t& _i)
334  {
335  throw std::runtime_error("Access of numeric Leaf using sub function not allowed");
336  return *root_;
337  }
338 
339 private:
340  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
341  const StateBaseVirtualType& subImplVirtDispatch(const size_t& _i) const
342  {
343  throw std::runtime_error("Access of numeric Leaf using sub function not allowed");
344  return *root_;
345  }
346 
347 private:
348  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
349  void bindToMemoryImplCRTP(TNumericType* _memRef)
350  {
351  size_t mapMemBeginShift(0);
352  memStartRef_ = _memRef;
353  for_each_tuple(subs_, [this, &mapMemBeginShift](auto& subI)
354  {
355  subI.bindToMemory(memStartRef_ + mapMemBeginShift);
356  mapMemBeginShift += subI.mapElementSize_;
357  });
358  mapElementSize_ = mapMemBeginShift;
359  bindMap();
360  }
361 
362 private:
363  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
364  void bindToMemoryImplCRTP(TNumericType* _memRef)
365  {
366  memStartRef_ = _memRef;
367  bindMap();
368  }
369 
370 private:
371  template <bool dynamicMap = StateBaseCRTPType::MapSize == -1, typename std::enable_if<(dynamicMap)>::type* = nullptr>
372  void bindMap()
373  {
374  new (&map_) MapTypeCRTP(memStartRef_, mapElementSize_);
375  }
376 
377 private:
378  template <bool dynamicMap = StateBaseCRTPType::MapSize == -1, typename std::enable_if<(!dynamicMap)>::type* = nullptr>
379  void bindMap()
380  {
381  new (&map_) MapTypeCRTP(memStartRef_);
382  }
383 
384 private:
386  {
387  return map_;
388  }
389 
390 private:
391  const MapTypeCRTP& dataImplCRTP() const
392  {
393  return map_;
394  }
395 
396 private:
397  template <size_t _i>
398  typename std::tuple_element<_i, std::tuple<TLeafTypes...>>::type& subImplCRTP()
399  {
400  return std::get<_i>(this->subs_);
401  }
402 
403 private:
404  template <size_t _i>
405  const typename std::tuple_element<_i, std::tuple<TLeafTypes...>>::type& subImplCRTP() const
406  {
407  return std::get<_i>(this->subs_);
408  }
409 
410 private:
411  constexpr const size_t subSizeImplCRTP() const
412  {
413  return sizeof...(TLeafTypes);
414  }
415 
416 private:
417  NumericType* const memStartRefImplCRTP() const
418  {
419  return memStartRef_;
420  }
421 
422  // friends
423  template <class Derived2>
424  friend class StateMapBaseCRTP;
425  template <class Derived2>
426  friend struct StateMapBaseCRTPTraits;
427  template <class TNumericType2>
428  friend class StateMapBaseVirt;
429  template <class TNumericType2, class... TLeafTypes2>
430  friend class StateMapTuple;
431  template <class TNumericType2, class TLeafType2>
432  friend class StateMapVector;
433  template <class TNumericType2, class TLeafType2, size_t TN2>
434  friend class StateMapArray;
435  template <class TLeafType2>
436  friend struct LeafTypeContClass;
437 };
438 
439 namespace
440 {
441 template <class TNumericType, class... TLeafTypes>
442 struct StateMapBaseCRTPTraits<StateMapTuple<TNumericType, TLeafTypes...>>
443 {
444  using NumericType = TNumericType;
445  using LeafType = EmptyLeafType;
446  using LeafTypeExt = EmptyLeafType;
447  using LeafsTupleType = std::tuple<typename TLeafTypes::ImplType...>;
448  using LeafsTupleTypeExt = std::tuple<TLeafTypes...>;
450  static constexpr const int subSize = sizeof...(TLeafTypes);
451  static constexpr const bool isDynamic = false;
452 };
453 
454 } // namespace <anonymous>
455 }
456 
457 #endif // STATE_MAP_TUPLE_HPP
std::tuple< TLeafTypes... > subs_
friend struct StateMapBaseCRTPTraits
StateMapTuple & operator=(const StateMapTuple &_rhs)
void bindToMemoryImplVirt(TNumericType *_memRef) overridefinal
typename StateMapBaseCRTPTraits< StateMapTuple< TNum, TLeafTypes... > >::NumericType NumericType
typename StateMapBaseCRTP< StateMapTuple< NumericType, TLeafTypes... >>::MapTypeCRTP MapTypeCRTP
void copyRhsData(const StateMapTuple &_rhs)
typename StateMapBaseCRTPTraits< StateMapTuple< TNum, TLeafTypes... > >::LeafTypeExt LeafType
Helper function needed to upgrade c++ 2011.
Definition: utils.h:193
const StateBaseVirtualType & subImplVirt(const size_t &_i) const overridefinal
void bindToMemoryImplCRTP(TNumericType *_memRef)
TNumericType * memStartRef_
virtual ~StateMapTuple()=default
static constexpr const int MapSize
friend struct LeafTypeContClass
std::tuple_element< _i, std::tuple< TLeafTypes... > >::type & subImplCRTP()
typename StateMapBaseVirt< NumericType >::MapTypeVirt MapTypeVirt
constexpr std::enable_if< II==sizeof...(Tp), void >::type for_each_tuple(std::tuple< Tp... > &, FuncT)
Definition: utils.h:101
typename DataBufferContainerClass::ContainerType DataBufferContainerType
StateBaseVirtualType & subImplVirt(const size_t &_i) overridefinal
MapTypeVirt dataImplVirt() overridefinal
const StateBaseVirtualType & subImplVirtDispatch(const size_t &_i) const
MapTypeCRTP & dataImplCRTP()
NumericType *const memStartRefImplVirt() const overridefinal
typename StateMapBaseVirt< NumericType >::StateBaseVirtualType StateBaseVirtualType
StateMapTuple(RootType *_root, std::shared_ptr< TDataBuffer > &_dataBuffer)
typename StateMapBaseCRTPTraits< StateMapTuple< TNum, TLeafTypes... > >::RootType RootType
Eigen::Map< Eigen::Matrix< TNum, Eigen::Dynamic, 1 > > MapTypeVirt
static constexpr auto value
Definition: utils.h:205
void bindToMemory(TNumericType *_memRef)
NumericType *const memStartRefImplCRTP() const
static constexpr const bool HasNumericLeaf
StateMapTuple(const StateMapTuple &_rhs)
TNumericType *const memStartRef() const
const MapTypeVirt dataImplVirt() const overridefinal
std::array< StateBaseVirtualType *, sizeof...(TLeafTypes)> subsBase_
constexpr const size_t subSizeImplCRTP() const
const MapTypeCRTP & dataImplCRTP() const
void subResize(const size_t &_size)
StateBaseVirtualType & subImplVirtDispatch(const size_t &_i)
const size_t subSizeImplVirt() const overridefinal
TNumericType NumericType
const std::tuple_element< _i, std::tuple< TLeafTypes... > >::type & subImplCRTP() const
StateBaseVirtualType * root_
void subResizeImplVirt(const size_t &_size) overridefinal
const size_t & subSize() const


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