DynamicData: get name of optional members modern C++

4 posts / 0 new
Last post
Offline
Last seen: 2 years 8 months ago
Joined: 07/26/2021
Posts: 2
DynamicData: get name of optional members modern C++

we're developing an test interface to test some DDS applications, which parses JSON-Topics to IDL-Topics. To achief this, we wrote a function which iterates recursive over the members of a dds::core::xtypes::DynamicData-object – representing a IDL topic – and searches the member names in the JSON object to extract and set the values similar to this example: https://community.rti.com/kb/dynamic-data-how-access-multi-level-nesting-elements-using-c11.

Now the problem:
It seems to be impossible to get the member_name of unset optional members. Access by  member_info(uint32_t index) throws dds::core::error "member does not exists".  First we tried to get the member throug the DynamicData.type().native()._data._members[x], aside the official API. But the order of the type code members is different from DynamicData member order.
So basically we're missing the method "member_name()" for optional members, like it exists for the index: member_index (const std::string &name), which btw. also throws "member does not exists" for unset optional members.

Is there any way to get the name of unset optional members by index?

Best regards

Christian

Offline
Last seen: 1 week 10 hours ago
Joined: 04/02/2013
Posts: 195

This incorrect behavior of member_info was fixed in 6.1.0.

If you can't upgrade, try using the information in the dynamic type (DynamicData::type())

Marc.Chiesa's picture
Offline
Last seen: 2 years 1 month ago
Joined: 07/24/2017
Posts: 32

You can also try falling back to the C API as a workaround in versions prior to 6.1.0. See this example from the Python API implementation.

Offline
Last seen: 2 years 8 months ago
Joined: 07/26/2021
Posts: 2

Thank you very much for your both replies!

I could solve the problem by using Marcs solution:

You can also try falling back to the C API as a workaround in versions prior to 6.1.0. See this example from the Python API implementation.

implemented this, to get the member info:

rti::core::xtypes::DynamicDataMemberInfo CJsonParser::get_member_info( const dds::core::xtypes::DynamicData& dd, uint32_t index)
{
   rti::core::xtypes::DynamicDataMemberInfo mi;
   if (!dd.member_exists_in_type(index))
   {
      throw dds::core::InvalidArgumentError("member id " + std::to_string(index) + " does not exist in type");
   }  

   auto rc = DDS_DynamicData_get_member_info(
      &dd.native(),
      &mi.native(),
      nullptr,
      index);
   rti::core::check_return_code(rc, "DynamicData member info error (index)");
   return mi;
}