Skip to content

Commit

Permalink
cras_topic_tools: Added support for ros::message_traits operations on…
Browse files Browse the repository at this point in the history
… cras::ShapeShifter on Melodic.
  • Loading branch information
peci1 committed Jan 6, 2025
1 parent 1792942 commit 3cbe47c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
71 changes: 71 additions & 0 deletions cras_topic_tools/include/cras_topic_tools/shape_shifter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* \author Martin Pecka
*/

#include <string>
#include <type_traits>

#include <ros/message_traits.h>
Expand Down Expand Up @@ -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<cras::ShapeShifter> : TrueType { };
template <> struct IsMessage<const cras::ShapeShifter> : TrueType { };

template<>
struct MD5Sum<cras::ShapeShifter>
{
static const char* value(const cras::ShapeShifter& m) { return m.getMD5Sum().c_str(); }
static const char* value() { return "*"; }
};

template<>
struct DataType<cras::ShapeShifter>
{
static const char* value(const cras::ShapeShifter& m) { return m.getDataType().c_str(); }
static const char* value() { return "*"; }
};

template<>
struct Definition<cras::ShapeShifter>
{
static const char* value(const cras::ShapeShifter& m) { return m.getMessageDefinition().c_str(); }
};

}

namespace serialization
{

template<>
struct Serializer<cras::ShapeShifter>
{
template<typename Stream>
inline static void write(Stream& stream, const cras::ShapeShifter& m) {
m.write(stream);
}

template<typename Stream>
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<cras::ShapeShifter>
{
static void notify(const PreDeserializeParams<cras::ShapeShifter>& 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"
4 changes: 2 additions & 2 deletions cras_topic_tools/src/shape_shifter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const ShapeShifter&>(other))
Expand Down
28 changes: 28 additions & 0 deletions cras_topic_tools/test/test_shape_shifter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<cras::ShapeShifter>::value);
EXPECT_EQ(false, +mt::IsSimple<cras::ShapeShifter>::value);
EXPECT_EQ(false, +mt::IsFixedSize<cras::ShapeShifter>::value);
EXPECT_EQ(false, +mt::HasHeader<cras::ShapeShifter>::value);
}

int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit 3cbe47c

Please sign in to comment.