I’ve added support for C++ messages to go along support for C messages, made the message<–>YAML conversion symmetrical, and did a bunch of refactoring. The code for this is on a branch on my fork: GitHub - christophebedard/dynamic_message_introspection at add-cpp-support-and-refactor
In short, it allows you to convert a C or C++ message, like this
std_msgs::msg::Header msg; msg.frame_id = "my_frame"; builtin_interfaces::msg::Time stamp; stamp.sec = 4; stamp.nanosec = 20U; msg.stamp = stamp;
into a YAML representation, which can be converted to string:
stamp: sec: 4 nanosec: 20 frame_id: my_frame
That string can be parsed as a YAML object and then converted back to a
std_msgs/Header message that will be equivalent to the original one!
Note that you could do the first part using
std_msgs::msg::to_yaml(const Header & msg). However, that doesn’t use type support introspection, and again it only does the conversion to YAML and not the other way.
rosidl_runtime_py can do the conversion from YAML (e.g., for
ros2 topic pub), but it’s in Python.
Even starting from the implementation for C messages, writing the introspection code for C++ messages was a nice challenge. I’m sure some things could be improved, and I might have done some things wrong (although it works ). It could nonetheless serve as another example of how to do type support introspection!