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
34namespace 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*
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 {
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,
124 class = typename std::enable_if<std::is_base_of<SensorValueBase, T>::value>::type>
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
203namespace armarx
204{
215} // namespace armarx
std::string timestamp()
#define ARMARX_PLACEMENT_CONSTRUCTION_HELPER_BASE(CommonBaseType)
#define DETAIL_SensorValueBase_DEFAULT_METHOD_IMPLEMENTATION
The SensorValueBase class.
virtual void getDataFieldAs(std::size_t i, Ice::Double &out) const =0
virtual void getDataFieldAs(std::size_t i, Ice::Long &out) const =0
virtual const std::type_info & getDataFieldType(std::size_t i) const =0
virtual void getDataFieldAs(std::size_t i, std::string &out) const =0
introspection::ClassMemberInfo< SensorValueBase, DerivedClass > SensorValueInfo
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_META_MAKE_HAS_MEMBER_FNC_CHECK(SensorValueHasGetClassMemberInfo, GetClassMemberInfo, SensorValueInfo< T >(*)(void))
T getDataFieldAs(std::size_t i) const
virtual std::size_t getNumberOfDataFields() const =0
virtual std::string getSensorValueType(bool withoutNamespaceSpecifier) const =0
virtual void getDataFieldAs(std::size_t i, bool &out) const =0
virtual void getDataFieldAs(std::size_t i, Ice::Byte &out) const =0
const T * asA() const
virtual void getDataFieldAs(std::size_t i, Ice::Float &out) const =0
virtual std::vector< std::string > getDataFieldNames() const =0
virtual void getDataFieldAs(std::size_t i, Ice::Short &out) const =0
virtual void getDataFieldAs(std::size_t i, Ice::Int &out) const =0
void _copyTo(std::unique_ptr< T > &target) const
virtual ~SensorValueBase()=default
static DETAIL_SensorValueBase_DEFAULT_METHOD_IMPLEMENTATION SensorValueInfo< SensorValueDummy > GetClassMemberInfo()
This file offers overloads of toIce() and fromIce() functions for STL container types.
#define ARMARX_TRACE
Definition trace.h:77