facontidavide / ros_type_introspection

Deserialize ROS messages that are unknown at compilation time
MIT License
61 stars 30 forks source link

Partially deserialize the message? #31

Closed ZHOUYue67 closed 5 years ago

ZHOUYue67 commented 5 years ago

I have a use case that I want to check a message if it has Header field, then deserialize only this part to get the timestamp in it. Can I use this library to implement this efficiently?

facontidavide commented 5 years ago

Yes, but I am away from the office right now and it is not the most documented feature. I will give you more details the next week

facontidavide commented 5 years ago

I created an example for you here: https://github.com/facontidavide/type_introspection_tests/blob/master/example/selective_deserialization.cpp

Note that the raw_buffer that I create manually here, is usually obtained from ShapeShifter or Rosbag Messages.

ZHOUYue67 commented 5 years ago

@facontidavide Thank you very much. The example works like a charm.

I also find a way to check if a connection/type has any header.

Parser parser;
const ROSType header_type(datatype<std_msgs::Header>());
std::map<std::string, bool> topics_has_header;

for (auto &conn : view.getConnections()) {
  parser.registerMessageDefinition(conn->topic, ROSType(conn->datatype),
                                   conn->msg_def);
  if (topics_has_header.find(conn->topic) != topics_has_header.end())
    continue;
  auto msg_info = parser.getMessageInfo(conn->topic);
  bool has_header =
      nullptr != parser.getMessageByType(header_type, *msg_info);
  topics_has_header.emplace(conn->topic, has_header);
}