diff --git a/cras_topic_tools/include/cras_topic_tools/shape_shifter.h b/cras_topic_tools/include/cras_topic_tools/shape_shifter.h index dd3ba8f..c7db28b 100644 --- a/cras_topic_tools/include/cras_topic_tools/shape_shifter.h +++ b/cras_topic_tools/include/cras_topic_tools/shape_shifter.h @@ -9,6 +9,7 @@ * \author Martin Pecka */ +#include #include #include @@ -131,4 +132,74 @@ class ShapeShifter : public ::topic_tools::ShapeShifter } +#if !ROS_VERSION_MINIMUM(1, 15, 0) +namespace ros { +namespace message_traits { + +template <> struct IsMessage : TrueType { }; +template <> struct IsMessage : TrueType { }; + +template<> +struct MD5Sum +{ + static const char* value(const cras::ShapeShifter& m) { return m.getMD5Sum().c_str(); } + static const char* value() { return "*"; } +}; + +template<> +struct DataType +{ + static const char* value(const cras::ShapeShifter& m) { return m.getDataType().c_str(); } + static const char* value() { return "*"; } +}; + +template<> +struct Definition +{ + static const char* value(const cras::ShapeShifter& m) { return m.getMessageDefinition().c_str(); } +}; + +} + +namespace serialization +{ + +template<> +struct Serializer +{ + template + inline static void write(Stream& stream, const cras::ShapeShifter& m) { + m.write(stream); + } + + template + inline static void read(Stream& stream, cras::ShapeShifter& m) + { + m.read(stream); + } + + inline static uint32_t serializedLength(const cras::ShapeShifter& m) { + return m.size(); + } +}; + + +template<> +struct PreDeserialize +{ + static void notify(const PreDeserializeParams& params) + { + std::string md5 = (*params.connection_header)["md5sum"]; + std::string datatype = (*params.connection_header)["type"]; + std::string msg_def = (*params.connection_header)["message_definition"]; + std::string latching = (*params.connection_header)["latching"]; + + params.message->morph(md5, datatype, msg_def, latching); + } +}; + +} +} +#endif + #include "impl/shape_shifter.hpp" diff --git a/cras_topic_tools/src/shape_shifter.cpp b/cras_topic_tools/src/shape_shifter.cpp index 91ab4c2..8efeb9e 100644 --- a/cras_topic_tools/src/shape_shifter.cpp +++ b/cras_topic_tools/src/shape_shifter.cpp @@ -200,8 +200,8 @@ bool setHeader(topic_tools::ShapeShifter& msg, std_msgs::Header& header) #if ROS_VERSION_MINIMUM(1, 15, 0) #else -ShapeShifter::ShapeShifter() = default; -ShapeShifter::~ShapeShifter() = default; +ShapeShifter::ShapeShifter() {} +ShapeShifter::~ShapeShifter() {} ShapeShifter::ShapeShifter(const topic_tools::ShapeShifter& other) : ShapeShifter(reinterpret_cast(other)) diff --git a/cras_topic_tools/test/test_shape_shifter.cpp b/cras_topic_tools/test/test_shape_shifter.cpp index 65208b1..3d25c63 100644 --- a/cras_topic_tools/test/test_shape_shifter.cpp +++ b/cras_topic_tools/test/test_shape_shifter.cpp @@ -340,6 +340,34 @@ TEST(ShapeShifter, CopyCrasShapeShifter) // NOLINT EXPECT_NE(cras::getBuffer(shifter), cras::getBuffer(shifter3)); } +TEST(ShapeShifter, MessageTraits) // NOLINT +{ + // Create a message + geometry_msgs::PointStamped msg; + msg.header.stamp.sec = 1; + msg.header.stamp.nsec = 2; + msg.header.frame_id = "test"; + msg.point.x = 1; + msg.point.y = 2; + msg.point.z = 3; + + // Load the message into the shape shifter object + topic_tools::ShapeShifter shifter; + cras::msgToShapeShifter(msg, shifter); + + namespace mt = ros::message_traits; + + EXPECT_STREQ(mt::datatype(msg), mt::datatype(shifter)); + EXPECT_STREQ(mt::definition(msg), mt::definition(shifter)); + EXPECT_STREQ(mt::md5sum(msg), mt::md5sum(shifter)); + // We need to use the + trick so that we don't get undefined reference errors + // https://stackoverflow.com/a/46296720/1076564 + EXPECT_EQ(true, +mt::IsMessage::value); + EXPECT_EQ(false, +mt::IsSimple::value); + EXPECT_EQ(false, +mt::IsFixedSize::value); + EXPECT_EQ(false, +mt::HasHeader::value); +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv);