RobotUnitDataStreamingReceiver.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::ArmarXObjects::RobotUnitDataStreamingReceiver
17 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * @date 2020
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#pragma once
24
25#include <mutex>
26#include <type_traits>
27
29
30#include <RobotAPI/interface/units/RobotUnit/RobotUnitInterface.h>
31
33{
35}
36
37namespace armarx
38{
40
41 /**
42 * @defgroup Library-RobotUnitDataStreamingReceiver RobotUnitDataStreamingReceiver
43 * @ingroup RobotAPI
44 * A description of the library RobotUnitDataStreamingReceiver.
45 *
46 * @class RobotUnitDataStreamingReceiver
47 * @ingroup Library-RobotUnitDataStreamingReceiver
48 * @brief Brief description of class RobotUnitDataStreamingReceiver.
49 *
50 * Detailed description of class RobotUnitDataStreamingReceiver.
51 */
53 {
54 public:
55 using clock_t = std::chrono::high_resolution_clock;
56 using timestep_t = RobotUnitDataStreaming::TimeStep;
57 using entry_t = RobotUnitDataStreaming::DataEntry;
58
59 template <class T>
61 {
62 DataEntryReader() = default;
67
68 DataEntryReader(const RobotUnitDataStreaming::DataEntry& k);
69
70 DataEntryReader& operator=(const RobotUnitDataStreaming::DataEntry& k);
71
72 void set(const RobotUnitDataStreaming::DataEntry& k);
73 T get(const RobotUnitDataStreaming::TimeStep& t) const;
74 T operator()(const RobotUnitDataStreaming::TimeStep& t) const;
75
76 private:
77 RobotUnitDataStreaming::DataEntry _key;
78 };
79
81 const RobotUnitInterfacePrx& ru,
82 const RobotUnitDataStreaming::Config& cfg);
84
85 std::deque<timestep_t>& getDataBuffer();
86 const RobotUnitDataStreaming::DataStreamingDescription& getDataDescription() const;
87 std::string getDataDescriptionString() const;
88
89 template <class T>
90 DataEntryReader<T> getDataEntryReader(const std::string& name) const;
91
92 template <class T>
93 void getDataEntryReader(DataEntryReader<T>& e, const std::string& name) const;
94 //statics
95 static void VisitEntry(auto&& f, const timestep_t& st, const entry_t& e);
96 template <class T>
97 static T GetAs(const timestep_t& st, const entry_t& e);
98
99 template <class T>
100 static RobotUnitDataStreaming::DataEntryType ExpectedDataEntryType();
101 static void VisitEntries(auto&& f, const timestep_t& st, const auto& cont);
102
103 private:
106 detail::RobotUnitDataStreamingReceiver::ReceiverPtr _receiver;
107 RobotUnitDataStreaming::ReceiverPrx _proxy;
108 RobotUnitDataStreaming::DataStreamingDescription _description;
109 std::map<std::uint64_t, RobotUnitDataStreaming::TimeStepSeq> _tmp_data_buffer;
110 std::uint64_t _tmp_data_buffer_seq_id = 0;
111 std::deque<RobotUnitDataStreaming::TimeStep> _data_buffer;
112 long _last_iteration_id = -1;
113 };
114} // namespace armarx
115
116//impl
117namespace armarx
118{
119 template <class T>
121 const RobotUnitDataStreaming::DataEntry& k)
122 {
123 set(k);
124 }
125
126 template <class T>
127 inline void
129 const RobotUnitDataStreaming::DataEntry& k)
130 {
132 << "the key references a value of the wrong type!";
133 _key = k;
134 }
135
136 template <class T>
139 const RobotUnitDataStreaming::DataEntry& k)
140 {
141 set(k);
142 return *this;
143 }
144
145 template <class T>
146 inline T
148 const RobotUnitDataStreaming::TimeStep& t) const
149 {
151 }
152
153 template <class T>
154 inline T
156 const RobotUnitDataStreaming::TimeStep& t) const
157 {
158 return get(t);
159 }
160
161 template <class T>
162 inline void
164 const std::string& name) const
165 {
166 e = _description.entries.at(name);
167 }
168
169 template <class T>
172 {
175 return r;
176 }
177
178 inline void
180 {
181 using enum_t = RobotUnitDataStreaming::DataEntryType;
182 // *INDENT-OFF*
183 switch (e.type)
184 {
185 case enum_t::NodeTypeBool:
186 f(st.bools.at(e.index));
187 break;
188 case enum_t::NodeTypeByte:
189 f(st.bytes.at(e.index));
190 break;
191 case enum_t::NodeTypeShort:
192 f(st.shorts.at(e.index));
193 break;
194 case enum_t::NodeTypeInt:
195 f(st.ints.at(e.index));
196 break;
197 case enum_t::NodeTypeLong:
198 f(st.longs.at(e.index));
199 break;
200 case enum_t::NodeTypeFloat:
201 f(st.floats.at(e.index));
202 break;
203 case enum_t::NodeTypeDouble:
204 f(st.doubles.at(e.index));
205 break;
206 };
207 // *INDENT-ON*
208 }
209
210 template <class T>
211 inline T
213 {
214 using enum_t = RobotUnitDataStreaming::DataEntryType;
215 if constexpr (std::is_same_v<bool, T>)
216 {
217 ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeBool);
218 return st.bools.at(e.index);
219 }
220 else if constexpr (std::is_same_v<Ice::Byte, T>)
221 {
222 ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeByte);
223 return st.bytes.at(e.index);
224 }
225 else if constexpr (std::is_same_v<Ice::Short, T>)
226 {
227 ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeShort);
228 return st.shorts.at(e.index);
229 }
230 else if constexpr (std::is_same_v<Ice::Int, T>)
231 {
232 ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeInt);
233 return st.ints.at(e.index);
234 }
235 else if constexpr (std::is_same_v<Ice::Long, T>)
236 {
237 ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeLong);
238 return st.longs.at(e.index);
239 }
240 else if constexpr (std::is_same_v<Ice::Float, T>)
241 {
242 ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeFloat);
243 return st.floats.at(e.index);
244 }
245 else if constexpr (std::is_same_v<Ice::Double, T>)
246 {
247 ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeDouble);
248 return st.doubles.at(e.index);
249 }
250 else
251 {
252 static_assert(!std::is_same_v<T, T>, "Type not supported!");
253 }
254 }
255
256 template <class T>
257 inline RobotUnitDataStreaming::DataEntryType
259 {
260 using enum_t = RobotUnitDataStreaming::DataEntryType;
261 if constexpr (std::is_same_v<bool, T>)
262 {
263 return enum_t::NodeTypeBool;
264 }
265 else if constexpr (std::is_same_v<Ice::Byte, T>)
266 {
267 return enum_t::NodeTypeByte;
268 }
269 else if constexpr (std::is_same_v<Ice::Short, T>)
270 {
271 return enum_t::NodeTypeShort;
272 }
273 else if constexpr (std::is_same_v<Ice::Int, T>)
274 {
275 return enum_t::NodeTypeInt;
276 }
277 else if constexpr (std::is_same_v<Ice::Long, T>)
278 {
279 return enum_t::NodeTypeLong;
280 }
281 else if constexpr (std::is_same_v<Ice::Float, T>)
282 {
283 return enum_t::NodeTypeFloat;
284 }
285 else if constexpr (std::is_same_v<Ice::Double, T>)
286 {
287 return enum_t::NodeTypeDouble;
288 }
289 else
290 {
291 static_assert(!std::is_same_v<T, T>, "Type not supported!");
292 }
293 }
294
295 inline void
296 RobotUnitDataStreamingReceiver::VisitEntries(auto&& f, const timestep_t& st, const auto& cont)
297 {
298 for (const entry_t& e : cont)
299 {
300 VisitEntry(f, st, e);
301 }
302 }
303} // namespace armarx
#define TYPEDEF_PTRS_SHARED(T)
#define TYPEDEF_PTRS_HANDLE(T)
Brief description of class RobotUnitDataStreamingReceiver.
static RobotUnitDataStreaming::DataEntryType ExpectedDataEntryType()
const RobotUnitDataStreaming::DataStreamingDescription & getDataDescription() const
static void VisitEntries(auto &&f, const timestep_t &st, const auto &cont)
RobotUnitDataStreamingReceiver(const ManagedIceObjectPtr &obj, const RobotUnitInterfacePrx &ru, const RobotUnitDataStreaming::Config &cfg)
static void VisitEntry(auto &&f, const timestep_t &st, const entry_t &e)
DataEntryReader< T > getDataEntryReader(const std::string &name) const
static T GetAs(const timestep_t &st, const entry_t &e)
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotUnitInterface > RobotUnitInterfacePrx
IceInternal::Handle< ManagedIceObject > ManagedIceObjectPtr
Definition ArmarXFwd.h:42
DataEntryReader & operator=(DataEntryReader &&)=default
DataEntryReader(const DataEntryReader &)=default
DataEntryReader & operator=(const DataEntryReader &)=default
T get(const RobotUnitDataStreaming::TimeStep &t) const
T operator()(const RobotUnitDataStreaming::TimeStep &t) const