convert Enum to string and conversely

2 posts / 0 new
Last post
Offline
Last seen: 6 days 49 min ago
Joined: 03/13/2022
Posts: 4
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: 3 hours 52 min ago
Joined: 11/29/2012
Posts: 381

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 ruing to use 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;
}