diff --git a/src/include/conversions.hpp b/src/include/conversions.hpp index a162e4a..770052a 100644 --- a/src/include/conversions.hpp +++ b/src/include/conversions.hpp @@ -12,48 +12,62 @@ #include #include +// for tuple magic +#include +#include + #include #include #include #include // Including the messages +#include "xtensor_ros/f32.h" #include "xtensor_ros/f64.h" #include "xtensor_ros/u8.h" +#include "xtensor_ros/u16.h" +#include "xtensor_ros/u32.h" +#include "xtensor_ros/u64.h" +#include "xtensor_ros/i8.h" +#include "xtensor_ros/i16.h" +#include "xtensor_ros/i32.h" +#include "xtensor_ros/i64.h" -xtensor_ros::f64 as_msg(xt::xarray& arr) +namespace xmsg { - using msg_type = xtensor_ros::f64; - using _data_type = typename msg_type::_data_type; - - xtensor_ros::f64 msg; + using namespace xtensor_ros; - msg.strides = arr.strides(); - msg.shape = arr.shape(); - msg.data = _data_type(arr.data().begin(), arr.data().end()); - return msg; -} + template + struct has_type; -auto from_msg(const xtensor_ros::f64& arr) -{ - using msg_type = xtensor_ros::f64; - using _data_type = typename msg_type::_data_type; + template + static constexpr bool has_type_v = has_type::value; - xtensor_ros::f64 msg; + template + struct has_type> : std::false_type {}; - return xt::xadapt(arr.data, arr.shape, arr.strides); -} + template + struct has_type> : has_type> {}; + template + struct has_type> : std::true_type {}; -namespace xmsg -{ - using namespace xtensor_ros; + template + using is_xtensor_msg = std::enable_if_t>; } template struct get_xmsg_type; +template +using xmsg_t = typename get_xmsg_type::type; +template <> +struct get_xmsg_type +{ + using type = xmsg::f32; +}; template <> struct get_xmsg_type { @@ -65,16 +79,71 @@ struct get_xmsg_type { using type = xmsg::u8; }; +template <> +struct get_xmsg_type +{ + using type = xmsg::u16; +}; +template <> +struct get_xmsg_type +{ + using type = xmsg::u32; +}; +template <> +struct get_xmsg_type +{ + using type = xmsg::u64; +}; -namespace ros +template <> +struct get_xmsg_type { - namespace message_traits - { + using type = xmsg::i8; +}; +template <> +struct get_xmsg_type +{ + using type = xmsg::i16; +}; +template <> +struct get_xmsg_type +{ + using type = xmsg::i32; +}; +template <> +struct get_xmsg_type +{ + using type = xmsg::i64; +}; +template +xmsg_t as_msg(xt::xarray& arr) +{ + using msg_type = xmsg_t; + using _data_type = typename msg_type::_data_type; + + msg_type msg; + + msg.strides = arr.strides(); + msg.shape = arr.shape(); + msg.data = _data_type(arr.data().begin(), arr.data().end()); + return msg; +} + +template > +auto from_msg(const MsgT& arr) +{ + return xt::xadapt(arr.data, arr.shape, arr.strides); +} + +namespace ros +{ +namespace message_traits +{ template struct MD5Sum> { - using msg_type = typename get_xmsg_type::type; + using msg_type = xmsg_t; using msg_md5 = MD5Sum; static const char* value() @@ -92,12 +161,12 @@ namespace ros { static const char* value() { - return DataType::value(); + return DataType>::value(); } static const char* value(const xt::xarray& m) { - return DataType::value(); + return DataType>::value(); } }; @@ -106,12 +175,12 @@ namespace ros { static const char* value() { - return Definition::value(); + return Definition>::value(); } static const char* value(const xt::xarray& m) { - return Definition::value(); + return Definition>::value(); } }; @@ -209,4 +278,4 @@ namespace serialization } -#endif \ No newline at end of file +#endif