Skip to content
Snippets Groups Projects
Commit dd4cd59b authored by Raphael Grimm's avatar Raphael Grimm
Browse files

Improve and clean up RobotUnitDataStreamingReceiver

* Capitalize static functions
* move template implementations out of class definition
* Add DataEntryReader
parent 09d426fa
Branches try_to_fix_observers
No related tags found
No related merge requests found
......@@ -222,9 +222,9 @@ namespace armarx
{
_logfile << s.iterationId << ';' << s.timestampUSec;
//logging
RobotUnitDataStreamingReceiver::visitEntries(log, s, _adc);
RobotUnitDataStreamingReceiver::visitEntries(log, s, _adc_temp);
RobotUnitDataStreamingReceiver::visitEntries(log, s, _joints);
RobotUnitDataStreamingReceiver::VisitEntries(log, s, _adc);
RobotUnitDataStreamingReceiver::VisitEntries(log, s, _adc_temp);
RobotUnitDataStreamingReceiver::VisitEntries(log, s, _joints);
_logfile << '\n';
}
std::vector<float> vals;
......@@ -234,8 +234,8 @@ namespace armarx
{
vals.emplace_back(n);
};
RobotUnitDataStreamingReceiver::visitEntries(pback, last, _adc);
RobotUnitDataStreamingReceiver::visitEntries(pback, last, _adc_temp);
RobotUnitDataStreamingReceiver::VisitEntries(pback, last, _adc);
RobotUnitDataStreamingReceiver::VisitEntries(pback, last, _adc_temp);
}
//calc + compensate
......
......@@ -54,6 +54,26 @@ namespace armarx
using timestep_t = RobotUnitDataStreaming::TimeStep;
using entry_t = RobotUnitDataStreaming::DataEntry;
template<class T>
struct DataEntryReader
{
DataEntryReader() = default;
DataEntryReader(DataEntryReader&&) = default;
DataEntryReader(const DataEntryReader&) = default;
DataEntryReader& operator=(DataEntryReader&&) = default;
DataEntryReader& operator=(const DataEntryReader&) = default;
DataEntryReader(const RobotUnitDataStreaming::DataEntry& k);
DataEntryReader& operator=(const RobotUnitDataStreaming::DataEntry& k);
void set(const RobotUnitDataStreaming::DataEntry& k);
T get(const RobotUnitDataStreaming::TimeStep& t) const;
T operator()(const RobotUnitDataStreaming::TimeStep& t) const;
private:
RobotUnitDataStreaming::DataEntry _key;
};
RobotUnitDataStreamingReceiver(
const ManagedIceObjectPtr& obj,
const RobotUnitInterfacePrx& ru,
......@@ -63,76 +83,19 @@ namespace armarx
std::deque<timestep_t>& getDataBuffer();
const RobotUnitDataStreaming::DataStreamingDescription& getDataDescription() const;
static void visitEntry(auto&& f, const timestep_t& st, const entry_t& e)
{
using enum_t = RobotUnitDataStreaming::DataEntryType;
// *INDENT-OFF*
switch (e.type)
{
case enum_t::NodeTypeBool : f(st.bools .at(e.index)); break;
case enum_t::NodeTypeByte : f(st.bytes .at(e.index)); break;
case enum_t::NodeTypeShort : f(st.shorts .at(e.index)); break;
case enum_t::NodeTypeInt : f(st.ints .at(e.index)); break;
case enum_t::NodeTypeLong : f(st.longs .at(e.index)); break;
case enum_t::NodeTypeFloat : f(st.floats .at(e.index)); break;
case enum_t::NodeTypeDouble: f(st.doubles.at(e.index)); break;
};
// *INDENT-ON*
}
template<class T>
DataEntryReader<T> getDataEntryReader(const std::string& name) const;
template<class T>
static T getAs(const timestep_t& st, const entry_t& e)
{
using enum_t = RobotUnitDataStreaming::DataEntryType;
if constexpr(std::is_same_v<bool, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeBool);
return st.bools .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Byte, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeByte);
return st.bytes .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Short, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeShort);
return st.shorts .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Int, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeInt);
return st.ints .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Long, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeLong);
return st.longs .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Float, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeFloat);
return st.floats .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Double, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeDouble);
return st.doubles.at(e.index);
}
else
{
static_assert(!std::is_same_v<T, T>, "Type not supported!");
}
}
static void visitEntries(auto&& f, const timestep_t& st, const auto& cont)
{
for (const entry_t& e : cont)
{
visitEntry(f, st, e);
}
}
void getDataEntryReader(DataEntryReader<T>& e, const std::string& name) const;
//statics
static void VisitEntry(auto&& f, const timestep_t& st, const entry_t& e);
template<class T>
static T GetAs(const timestep_t& st, const entry_t& e);
template<class T>
static RobotUnitDataStreaming::DataEntryType ExpectedDataEntryType();
static void VisitEntries(auto&& f, const timestep_t& st, const auto& cont);
private:
ManagedIceObjectPtr _obj;
RobotUnitInterfacePrx _ru;
......@@ -143,3 +106,170 @@ namespace armarx
long _last_iteration_id = -1;
};
}
//impl
namespace armarx
{
template<class T> inline
RobotUnitDataStreamingReceiver::DataEntryReader<T>::DataEntryReader(const RobotUnitDataStreaming::DataEntry& k)
{
set(k);
}
template<class T> inline
void
RobotUnitDataStreamingReceiver::DataEntryReader<T>::set(const RobotUnitDataStreaming::DataEntry& k)
{
ARMARX_CHECK_EQUAL(RobotUnitDataStreamingReceiver::ExpectedDataEntryType<T>(),
k.type) << "the key references a value of the wrong type!";
_key = k;
}
template<class T> inline
RobotUnitDataStreamingReceiver::DataEntryReader<T>&
RobotUnitDataStreamingReceiver::DataEntryReader<T>::operator=(const RobotUnitDataStreaming::DataEntry& k)
{
set(k);
return *this;
}
template<class T> inline
T
RobotUnitDataStreamingReceiver::DataEntryReader<T>::get(const RobotUnitDataStreaming::TimeStep& t) const
{
return RobotUnitDataStreamingReceiver::GetAs<T>(t, _key);
}
template<class T> inline
T
RobotUnitDataStreamingReceiver::DataEntryReader<T>::operator()(const RobotUnitDataStreaming::TimeStep& t) const
{
return get(t);
}
template<class T> inline
void
RobotUnitDataStreamingReceiver::getDataEntryReader(DataEntryReader<T>& e, const std::string& name) const
{
e = _description.entries.at(name);
}
template<class T> inline
RobotUnitDataStreamingReceiver::DataEntryReader<T>
RobotUnitDataStreamingReceiver::getDataEntryReader(const std::string& name) const
{
DataEntryReader<T> r;
getDataEntryReader(r);
return r;
}
inline void
RobotUnitDataStreamingReceiver::VisitEntry(auto&& f, const timestep_t& st, const entry_t& e)
{
using enum_t = RobotUnitDataStreaming::DataEntryType;
// *INDENT-OFF*
switch (e.type)
{
case enum_t::NodeTypeBool : f(st.bools .at(e.index)); break;
case enum_t::NodeTypeByte : f(st.bytes .at(e.index)); break;
case enum_t::NodeTypeShort : f(st.shorts .at(e.index)); break;
case enum_t::NodeTypeInt : f(st.ints .at(e.index)); break;
case enum_t::NodeTypeLong : f(st.longs .at(e.index)); break;
case enum_t::NodeTypeFloat : f(st.floats .at(e.index)); break;
case enum_t::NodeTypeDouble: f(st.doubles.at(e.index)); break;
};
// *INDENT-ON*
}
template<class T> inline
T
RobotUnitDataStreamingReceiver::GetAs(const timestep_t& st, const entry_t& e)
{
using enum_t = RobotUnitDataStreaming::DataEntryType;
if constexpr(std::is_same_v<bool, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeBool);
return st.bools .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Byte, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeByte);
return st.bytes .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Short, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeShort);
return st.shorts .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Int, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeInt);
return st.ints .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Long, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeLong);
return st.longs .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Float, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeFloat);
return st.floats .at(e.index);
}
else if constexpr(std::is_same_v<Ice::Double, T>)
{
ARMARX_CHECK_EQUAL(e.type, enum_t::NodeTypeDouble);
return st.doubles.at(e.index);
}
else
{
static_assert(!std::is_same_v<T, T>, "Type not supported!");
}
}
template<class T> inline
RobotUnitDataStreaming::DataEntryType
RobotUnitDataStreamingReceiver::ExpectedDataEntryType()
{
using enum_t = RobotUnitDataStreaming::DataEntryType;
if constexpr(std::is_same_v<bool, T>)
{
return enum_t::NodeTypeBool;
}
else if constexpr(std::is_same_v<Ice::Byte, T>)
{
return enum_t::NodeTypeByte;
}
else if constexpr(std::is_same_v<Ice::Short, T>)
{
return enum_t::NodeTypeShort;
}
else if constexpr(std::is_same_v<Ice::Int, T>)
{
return enum_t::NodeTypeInt;
}
else if constexpr(std::is_same_v<Ice::Long, T>)
{
return enum_t::NodeTypeLong;
}
else if constexpr(std::is_same_v<Ice::Float, T>)
{
return enum_t::NodeTypeFloat;
}
else if constexpr(std::is_same_v<Ice::Double, T>)
{
return enum_t::NodeTypeDouble;
}
else
{
static_assert(!std::is_same_v<T, T>, "Type not supported!");
}
}
inline void
RobotUnitDataStreamingReceiver::VisitEntries(auto&& f, const timestep_t& st, const auto& cont)
{
for (const entry_t& e : cont)
{
VisitEntry(f, st, e);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment