Skip to content

Commit e446ca9

Browse files
committed
cras_topic_tools: Added support for ros::message_traits operations on cras::ShapeShifter on Melodic.
1 parent 1792942 commit e446ca9

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

cras_topic_tools/include/cras_topic_tools/shape_shifter.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,74 @@ class ShapeShifter : public ::topic_tools::ShapeShifter
131131

132132
}
133133

134+
#if !ROS_VERSION_MINIMUM(1, 15, 0)
135+
namespace ros {
136+
namespace message_traits {
137+
138+
template <> struct IsMessage<cras::ShapeShifter> : TrueType { };
139+
template <> struct IsMessage<const cras::ShapeShifter> : TrueType { };
140+
141+
template<>
142+
struct MD5Sum<cras::ShapeShifter>
143+
{
144+
static const char* value(const cras::ShapeShifter& m) { return m.getMD5Sum().c_str(); }
145+
static const char* value() { return "*"; }
146+
};
147+
148+
template<>
149+
struct DataType<cras::ShapeShifter>
150+
{
151+
static const char* value(const cras::ShapeShifter& m) { return m.getDataType().c_str(); }
152+
static const char* value() { return "*"; }
153+
};
154+
155+
template<>
156+
struct Definition<cras::ShapeShifter>
157+
{
158+
static const char* value(const cras::ShapeShifter& m) { return m.getMessageDefinition().c_str(); }
159+
};
160+
161+
}
162+
163+
namespace serialization
164+
{
165+
166+
template<>
167+
struct Serializer<cras::ShapeShifter>
168+
{
169+
template<typename Stream>
170+
inline static void write(Stream& stream, const cras::ShapeShifter& m) {
171+
m.write(stream);
172+
}
173+
174+
template<typename Stream>
175+
inline static void read(Stream& stream, cras::ShapeShifter& m)
176+
{
177+
m.read(stream);
178+
}
179+
180+
inline static uint32_t serializedLength(const cras::ShapeShifter& m) {
181+
return m.size();
182+
}
183+
};
184+
185+
186+
template<>
187+
struct PreDeserialize<cras::ShapeShifter>
188+
{
189+
static void notify(const PreDeserializeParams<cras::ShapeShifter>& params)
190+
{
191+
std::string md5 = (*params.connection_header)["md5sum"];
192+
std::string datatype = (*params.connection_header)["type"];
193+
std::string msg_def = (*params.connection_header)["message_definition"];
194+
std::string latching = (*params.connection_header)["latching"];
195+
196+
params.message->morph(md5, datatype, msg_def, latching);
197+
}
198+
};
199+
200+
}
201+
}
202+
#endif
203+
134204
#include "impl/shape_shifter.hpp"

cras_topic_tools/test/test_shape_shifter.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,34 @@ TEST(ShapeShifter, CopyCrasShapeShifter) // NOLINT
340340
EXPECT_NE(cras::getBuffer(shifter), cras::getBuffer(shifter3));
341341
}
342342

343+
TEST(ShapeShifter, MessageTraits) // NOLINT
344+
{
345+
// Create a message
346+
geometry_msgs::PointStamped msg;
347+
msg.header.stamp.sec = 1;
348+
msg.header.stamp.nsec = 2;
349+
msg.header.frame_id = "test";
350+
msg.point.x = 1;
351+
msg.point.y = 2;
352+
msg.point.z = 3;
353+
354+
// Load the message into the shape shifter object
355+
topic_tools::ShapeShifter shifter;
356+
cras::msgToShapeShifter(msg, shifter);
357+
358+
namespace mt = ros::message_traits;
359+
360+
EXPECT_STREQ(mt::datatype(msg), mt::datatype(shifter));
361+
EXPECT_STREQ(mt::definition(msg), mt::definition(shifter));
362+
EXPECT_STREQ(mt::md5sum(msg), mt::md5sum(shifter));
363+
// We need to use the + trick so that we don't get undefined reference errors
364+
// https://stackoverflow.com/a/46296720/1076564
365+
EXPECT_EQ(true, +mt::IsMessage<cras::ShapeShifter>::value);
366+
EXPECT_EQ(false, +mt::IsSimple<cras::ShapeShifter>::value);
367+
EXPECT_EQ(false, +mt::IsFixedSize<cras::ShapeShifter>::value);
368+
EXPECT_EQ(false, +mt::HasHeader<cras::ShapeShifter>::value);
369+
}
370+
343371
int main(int argc, char **argv)
344372
{
345373
testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)