convert Enum to string and conversely

6 posts / 0 new
Last post
Offline
Last seen: 1 year 3 months ago
Joined: 03/13/2022
Posts: 11
convert Enum to string and conversely

Hi

I want to convert a safe enum to string and conversely. For example I have a enum as following:

struct ESocketStatus_def {
        enum type {
            ESocketStatus_Unknown,      
            ESocketStatus_Listening,      
            ESocketStatus_Connected,      
            ESocketStatus_Connecting,      
            ESocketStatus_Disconnected     
        };
        static type get_default(){ return ESocketStatus_Unknown;}
    };

    typedef dds::core::safe_enum<ESocketStatus_def> ESocketStatus;

now, we want to convert the ESocketStatus_Unknown to "ESocketStatus_Unknown" or conversely.

Is there a way for this work?

 

Thank you

Howard's picture
Offline
Last seen: 18 hours 6 min ago
Joined: 11/29/2012
Posts: 565

Hi,

No the  dds::core::safe_enum class template does not provide any ways to convert an enumeration to its string/readable value.

I'm not sure why you're using that class...it's not really designed for use by the end user directly. 

If you want to use an enumeration in a datatype that will be used by a DDS topic, then you need to define the enumeration in an IDL/XML file and then use rtiddsgen to generate the type definition in the programming language that you're using (for example C++11).

For code generated for the C++11 (aka Modern C++) programming language, the enumeration is defined with a "helper" override function that will take a value of the enumeration and print the string equivalent.

For example, if your IDL is:

    enum myenum {
        ESocketStatus_Unknown,      
        ESocketStatus_Listening,      
        ESocketStatus_Connected,      
        ESocketStatus_Connecting,      
        ESocketStatus_Disconnected
    };


    struct MyData {
        myenum foo;
    };

then in the .hpp file for the datatype, you should find this

enum class myenum {
    ESocketStatus_Unknown,
    ESocketStatus_Listening,
    ESocketStatus_Connected,
    ESocketStatus_Connecting,
    ESocketStatus_Disconnected
};

NDDSUSERDllExport std::ostream& operator << (std::ostream& o,const myenum& sample);

 

and in the related .cxx implementation file, you would find this

std::ostream& operator << (std::ostream& o,const myenum& sample)
{
    ::rti::util::StreamFlagSaver flag_saver (o);
    switch(sample){
        case myenum::ESocketStatus_Unknown:
        o << "myenum::ESocketStatus_Unknown" << " ";
        break;
        case myenum::ESocketStatus_Listening:
        o << "myenum::ESocketStatus_Listening" << " ";
        break;
        case myenum::ESocketStatus_Connected:
        o << "myenum::ESocketStatus_Connected" << " ";
        break;
        case myenum::ESocketStatus_Connecting:
        o << "myenum::ESocketStatus_Connecting" << " ";
        break;
        case myenum::ESocketStatus_Disconnected:
        o << "myenum::ESocketStatus_Disconnected" << " ";
        break;
    }
    return o;
}

 

Offline
Last seen: 1 year 3 months ago
Joined: 03/13/2022
Posts: 11

Hi

Thank you for response. That is correct, but there is exist another way, also.

Is there a way for converting the string to the enum into the rti?

If we have one Enum, this work is easy. but I have many enum in namespace.

For example we enter the "ESocketStatus_Connected" and getting the equivalent enum that is ESocketStatus_Connected. while i don't know that the corresponding enum.

Thank you

Howard's picture
Offline
Last seen: 18 hours 6 min ago
Joined: 11/29/2012
Posts: 565

Sorry, no.  The generated type support code does not offer that API.

Offline
Last seen: 1 year 3 months ago
Joined: 03/13/2022
Posts: 11

I offer you that insert this functionality.

Because as the enum to string can be used, the vice versa is useful.

Thank you

Howard's picture
Offline
Last seen: 18 hours 6 min ago
Joined: 11/29/2012
Posts: 565

Noted.