HeterogenousContinuousContainerMacros.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::RobotUnit
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <memory>
26 #include <type_traits>
27 
28 #define ARMARX_MINIMAL_PLACEMENT_CONSTRUCTION_HELPER_BASE(CommonBaseType) \
29  using _typeBase = CommonBaseType; \
30  virtual std::size_t _alignof() const = 0; \
31  virtual std::size_t _sizeof() const = 0; \
32  virtual _typeBase* _placementCopyConstruct(void* place) const = 0; \
33  std::size_t _accumulateSize(std::size_t offset = 0) const \
34  { \
35  return offset + (_alignof() - (offset % _alignof())) % _alignof() + _sizeof(); \
36  }
37 
38 #define ARMARX_PLACEMENT_CONSTRUCTION_HELPER_BASE(CommonBaseType) \
39  ARMARX_MINIMAL_PLACEMENT_CONSTRUCTION_HELPER_BASE(CommonBaseType) \
40  virtual void _copyTo(_typeBase* target) const = 0; \
41  virtual std::unique_ptr<_typeBase> _clone() const = 0; \
42  virtual _typeBase* _placementConstruct(void* place) const = 0;
43 
44 #define ARMARX_MINIMAL_PLACEMENT_CONSTRUCTION_HELPER \
45  std::size_t _alignof() const override \
46  { \
47  using _type = typename std::decay<decltype(*this)>::type; \
48  return alignof(_type); \
49  } \
50  std::size_t _sizeof() const override \
51  { \
52  using _type = typename std::decay<decltype(*this)>::type; \
53  return sizeof(_type); \
54  } \
55  _typeBase* _placementCopyConstruct(void* place) const override \
56  { \
57  using _type = typename std::decay<decltype(*this)>::type; \
58  return new (place) _type(*this); \
59  } \
60  void _checkBaseType() \
61  { \
62  using _type = typename std::decay<decltype(*this)>::type; \
63  static_assert(std::is_base_of<_typeBase, _type>::value, \
64  "This class has to derive the common base class"); \
65  }
66 
67 #define ARMARX_PLACEMENT_CONSTRUCTION_HELPER \
68  ARMARX_MINIMAL_PLACEMENT_CONSTRUCTION_HELPER \
69  void _copyTo(_typeBase* target) const override \
70  { \
71  using _type = typename std::decay<decltype(*this)>::type; \
72  _type* const castedTarget = dynamic_cast<_type*>(target); \
73  ARMARX_CHECK_NOT_NULL(castedTarget); \
74  *castedTarget = *this; \
75  } \
76  std::unique_ptr<_typeBase> _clone() const override \
77  { \
78  using _type = typename std::decay<decltype(*this)>::type; \
79  return std::unique_ptr<_typeBase>{new _type(*this)}; \
80  } \
81  _typeBase* _placementConstruct(void* place) const override \
82  { \
83  using _type = typename std::decay<decltype(*this)>::type; \
84  return new (place) _type; \
85  }