Enum field names to strings in XML

3 posts / 0 new
Last post
ilya_1725's picture
Offline
Last seen: 2 years 4 months ago
Joined: 04/20/2020
Posts: 24
Enum field names to strings in XML

I have the source XML file that contains all the interface information.

One particular segement is the enum:

        <enum name="InsId">
          <enumerator name="kInsA" value="0"/>
          <enumerator name="kInsB" value="1"/>
          <enumerator name="kInsC" value="2"/>
          <enumerator name="kNumIns" value="3"/>
        </enum>
 
 
Passing this XML file throught he RTI generator creates a nice enum (not enum class, sadly).
 
        typedef enum InsId
        {
            kInsA  = 0,      
            kInsB  = 1,      
            kInsC  = 2,      
            kNumIns  = 3     
        } InsId;

 
What I need is a way to convert some string representation to enum values. For example, if I have some configuration file saying that the `InsId` is "kInsA", then the code will set it to `InsId::kInsA`. I will have to create a local custom map to map strings to `InsId` values. If there are any changes in the XML file, the said map will have to be manually changed.
Is there any way to automate the creation of this map? For example some tool, already existing, that can process the XML file and output mapping from strings to enum values.
 
Thank you.
 
 

 
 
Keywords:
Offline
Last seen: 1 month 5 days ago
Joined: 04/02/2013
Posts: 196

You can use the DynamicType/TypeCode (depending on which API you're using) to reflectively look up those names.

In modern C++ you would do something like this:

 

using namespace dds::core::xtypes;

const auto& type = static_cast<const EnumType&>(rti::topic::dynamic_type<InsId>::get());

for (const EnumMember& member : type.members()) {

    std::cout << member.name() << std::endl; // "kInsA", "kInsB", etc.

}

 

 See dynamic_type and EnumType.

 

If you're using the traditional C++ API you can do something similar using InsId_get_typecode().

ilya_1725's picture
Offline
Last seen: 2 years 4 months ago
Joined: 04/20/2020
Posts: 24

Great suggestion, thank you.