SensorValueBase.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
17  * @author Raphael ( raphael dot grimm at kit dot edu )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 #pragma once
23 
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <typeinfo>
28 
30 
33 
34 namespace armarx
35 {
36  /**
37  * @ingroup Library-RobotUnit
38  * @brief The SensorValueBase class
39  */
41  {
42  template <class... Ts>
43  struct IsAHelper
44  {
45  // only called then sizeof...(Ts) == 0
46  static_assert(sizeof...(Ts) == 0, "called overload for empty pack with params");
47 
48  static bool
49  IsA(const SensorValueBase* sv)
50  {
51  return true;
52  }
53  };
54 
55  template <class T, class... Ts>
56  struct IsAHelper<T, Ts...>
57  {
58  static bool
59  IsA(const SensorValueBase* sv)
60  {
61  return dynamic_cast<const T*>(sv) && IsAHelper<Ts...>::IsA(sv);
62  }
63  };
64 
65  public:
66  template <class DerivedClass>
68 
69  virtual ~SensorValueBase() = default;
70 
71  virtual std::string getSensorValueType(bool withoutNamespaceSpecifier) const = 0;
72 
73  template <class... Ts>
74  bool
75  isA() const
76  {
77  return IsAHelper<Ts...>::IsA(this);
78  }
79 
80  template <class T>
81  const T*
82  asA() const
83  {
84  return dynamic_cast<const T*>(this);
85  }
86 
87  template <class T>
88  T*
89  asA()
90  {
91  return dynamic_cast<T*>(this);
92  }
93 
94  //logging functions
95  /// @brief used to send the data to the DebugObserverTopic and to other Components (e.g. GUI widgets)
96  virtual std::map<std::string, VariantBasePtr>
97  toVariants(const IceUtil::Time& timestamp) const = 0;
98 
99  virtual std::size_t getNumberOfDataFields() const = 0;
100  virtual std::vector<std::string> getDataFieldNames() const = 0;
101  virtual void getDataFieldAs(std::size_t i, bool& out) const = 0;
102  virtual void getDataFieldAs(std::size_t i, Ice::Byte& out) const = 0;
103  virtual void getDataFieldAs(std::size_t i, Ice::Short& out) const = 0;
104  virtual void getDataFieldAs(std::size_t i, Ice::Int& out) const = 0;
105  virtual void getDataFieldAs(std::size_t i, Ice::Long& out) const = 0;
106  virtual void getDataFieldAs(std::size_t i, Ice::Float& out) const = 0;
107  virtual void getDataFieldAs(std::size_t i, Ice::Double& out) const = 0;
108  virtual void getDataFieldAs(std::size_t i, std::string& out) const = 0;
109 
110  template <class T>
111  T
112  getDataFieldAs(std::size_t i) const
113  {
114  ARMARX_TRACE;
115  T t;
116  this->getDataFieldAs(i, t);
117  return t;
118  }
119 
120  virtual const std::type_info& getDataFieldType(std::size_t i) const = 0;
121 
122  //management functions
123  template <class T,
125  void
126  _copyTo(std::unique_ptr<T>& target) const
127  {
128  _copyTo(target.get());
129  }
130 
131  ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK(SensorValueHasGetClassMemberInfo,
132  GetClassMemberInfo,
133  SensorValueInfo<T> (*)(void));
134 
136  };
137 } // namespace armarx
138 
139 #define DETAIL_SensorValueBase_DEFAULT_METHOD_IMPLEMENTATION \
140  ARMARX_PLACEMENT_CONSTRUCTION_HELPER \
141  using SensorValueBase = ::armarx::SensorValueBase; \
142  using VariantBasePtr = ::armarx::VariantBasePtr; \
143  std::string getSensorValueType(bool withoutNamespaceSpecifier = false) const override \
144  { \
145  return armarx::GetTypeString(*this, withoutNamespaceSpecifier); \
146  } \
147  void _check_for_static_GetClassMemberInfo_overload() \
148  { \
149  static_assert(SensorValueHasGetClassMemberInfo<std::decay<decltype(*this)>::type>::value, \
150  "This class has to implement GetClassMemberInfo() returning " \
151  "an instance of SensorValueInfo<THIS_CLASS_TYPE>"); \
152  } \
153  std::map<std::string, VariantBasePtr> toVariants(const IceUtil::Time& timestamp) \
154  const override \
155  { \
156  return SensorValueInfo<std::decay<decltype(*this)>::type>::ToVariants(timestamp, this); \
157  } \
158  std::size_t getNumberOfDataFields() const override \
159  { \
160  return SensorValueInfo<std::decay<decltype(*this)>::type>::GetNumberOfDataFields(); \
161  } \
162  void getDataFieldAs(std::size_t i, bool& out) const override \
163  { \
164  SensorValueInfo<std::decay<decltype(*this)>::type>::GetDataFieldAs(this, i, out); \
165  } \
166  void getDataFieldAs(std::size_t i, Ice::Byte& out) const override \
167  { \
168  SensorValueInfo<std::decay<decltype(*this)>::type>::GetDataFieldAs(this, i, out); \
169  } \
170  void getDataFieldAs(std::size_t i, Ice::Short& out) const override \
171  { \
172  SensorValueInfo<std::decay<decltype(*this)>::type>::GetDataFieldAs(this, i, out); \
173  } \
174  void getDataFieldAs(std::size_t i, Ice::Int& out) const override \
175  { \
176  SensorValueInfo<std::decay<decltype(*this)>::type>::GetDataFieldAs(this, i, out); \
177  } \
178  void getDataFieldAs(std::size_t i, Ice::Long& out) const override \
179  { \
180  SensorValueInfo<std::decay<decltype(*this)>::type>::GetDataFieldAs(this, i, out); \
181  } \
182  void getDataFieldAs(std::size_t i, Ice::Float& out) const override \
183  { \
184  SensorValueInfo<std::decay<decltype(*this)>::type>::GetDataFieldAs(this, i, out); \
185  } \
186  void getDataFieldAs(std::size_t i, Ice::Double& out) const override \
187  { \
188  SensorValueInfo<std::decay<decltype(*this)>::type>::GetDataFieldAs(this, i, out); \
189  } \
190  void getDataFieldAs(std::size_t i, std::string& out) const override \
191  { \
192  SensorValueInfo<std::decay<decltype(*this)>::type>::GetDataFieldAs(this, i, out); \
193  } \
194  const std::type_info& getDataFieldType(std::size_t i) const override \
195  { \
196  return SensorValueInfo<std::decay<decltype(*this)>::type>::GetDataFieldType(i); \
197  } \
198  std::vector<std::string> getDataFieldNames() const override \
199  { \
200  return SensorValueInfo<std::decay<decltype(*this)>::type>::GetDataFieldNames(); \
201  }
202 
203 namespace armarx
204 {
206  {
207  public:
211  {
213  }
214  };
215 } // namespace armarx
armarx::SensorValueBase::~SensorValueBase
virtual ~SensorValueBase()=default
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::SensorValueBase::asA
const T * asA() const
Definition: SensorValueBase.h:82
armarx::SensorValueBase
The SensorValueBase class.
Definition: SensorValueBase.h:40
armarx::SensorValueBase::asA
T * asA()
Definition: SensorValueBase.h:89
armarx::SensorValueBase::_copyTo
void _copyTo(std::unique_ptr< T > &target) const
Definition: SensorValueBase.h:126
TemplateMetaProgramming.h
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
ClassMemberInfo.h
armarx::introspection::ClassMemberInfo
Definition: ClassMemberInfo.h:34
armarx::SensorValueBase::ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK
ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK(SensorValueHasGetClassMemberInfo, GetClassMemberInfo, SensorValueInfo< T >(*)(void))
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:919
armarx::SensorValueBase::getDataFieldType
virtual const std::type_info & getDataFieldType(std::size_t i) const =0
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::SensorValueBase::toVariants
virtual std::map< std::string, VariantBasePtr > toVariants(const IceUtil::Time &timestamp) const =0
used to send the data to the DebugObserverTopic and to other Components (e.g. GUI widgets)
armarx::SensorValueDummy
Definition: SensorValueBase.h:205
armarx::SensorValueBase::getSensorValueType
virtual std::string getSensorValueType(bool withoutNamespaceSpecifier) const =0
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::SensorValueBase::getDataFieldNames
virtual std::vector< std::string > getDataFieldNames() const =0
DETAIL_SensorValueBase_DEFAULT_METHOD_IMPLEMENTATION
#define DETAIL_SensorValueBase_DEFAULT_METHOD_IMPLEMENTATION
Definition: SensorValueBase.h:139
armarx::SensorValueDummy::GetClassMemberInfo
static DETAIL_SensorValueBase_DEFAULT_METHOD_IMPLEMENTATION SensorValueInfo< SensorValueDummy > GetClassMemberInfo()
Definition: SensorValueBase.h:210
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
ARMARX_PLACEMENT_CONSTRUCTION_HELPER_BASE
#define ARMARX_PLACEMENT_CONSTRUCTION_HELPER_BASE(CommonBaseType)
Definition: HeterogenousContinuousContainerMacros.h:38
armarx::SensorValueBase::getDataFieldAs
virtual void getDataFieldAs(std::size_t i, bool &out) const =0
armarx::SensorValueBase::getDataFieldAs
T getDataFieldAs(std::size_t i) const
Definition: SensorValueBase.h:112
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::SensorValueBase::getNumberOfDataFields
virtual std::size_t getNumberOfDataFields() const =0
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
HeterogenousContinuousContainerMacros.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::SensorValueBase::isA
bool isA() const
Definition: SensorValueBase.h:75