Public Group

From String to Enum

This topic is 4535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I was just wondering if there was a simple way to convert the content of a string into an enumerated const name (not the value, the index name) without having to go through a system of 'if' s ( I was thinking something along the lines of a cast). C++ thanks [smile]

What language?

Share on other sites
You can set up a map, but there's no straightforward automatic way to do it as C++ lacks reflection.

Share on other sites
Ok thanks. I only have like 5 enumerations so Using the 'if's is no problem, I will keep "maps" in mind for larger sets

Share on other sites
It seems like id seen it done before by my c++ prof, but i dont remember. Do a little more research; seemed like it had something to do with an array of strings....

Share on other sites
Quote:
 Original post by BTownTKDIt seems like id seen it done before by my c++ prof, but i dont remember. Do a little more research; seemed like it had something to do with an array of strings....

One way is a parallel array of strings to the enum:

enum SomeEnum{  eFoo = 0,  eBar,  eBaz}char* someEnumNames[] = { "eFoo", "eBar", "eBaz" };

Then conversion involves linearly scanning though the enum names array until a match is found, at which point the array index can just be cast to the enum.

Unfortunately:
- It's fragile, enums and names must always be kept manually in sync, whereas a map does all of this for you
- Time is O(n), whereas a map would be O(log n)
- If needed the map can be sparse (ie. only a few enum strings map to an enum value). The array method requires your array to be as long as you have enums.
- You enums all need to be sequential (non-zero based is ok, but again adds fragility). A map can handle any enum values.

Share on other sites
This creates an enum and a corresponding array of strings that are automatically kept in synch.
    #define COLOR_LIST              \         ENUM_OR_STRING( Black ),    \         ENUM_OR_STRING( Red ),      \         ENUM_OR_STRING( Green ),    \         ENUM_OR_STRING( Blue ),     \         ENUM_OR_STRING( Cyan ),     \         ENUM_OR_STRING( Magenta ),  \         ENUM_OR_STRING( Yellow ),   \         ENUM_OR_STRING( White )         #define ENUM_OR_STRING( x ) x    enum Colors    {        COLOR_LIST    };        #undef ENUM_OR_STRING    #define ENUM_OR_STRING( x ) #x    char const * colorsNames[] =    {        COLOR_LIST    };
The multi-line macro makes managing the list difficult. An alternative is to put the list in a header file:

colors.h
        ENUM_OR_STRING( Black ),        ENUM_OR_STRING( Red ),        ENUM_OR_STRING( Green ),        ENUM_OR_STRING( Blue ),        ENUM_OR_STRING( Cyan ),        ENUM_OR_STRING( Magenta ),        ENUM_OR_STRING( Yellow ),        ENUM_OR_STRING( White )

and use it like this:
    #define ENUM_OR_STRING( x ) x    enum Colors    {        #include "colors.h"    };        #undef ENUM_OR_STRING    #define ENUM_OR_STRING( x ) #x    char const * colorsNames[] =    {        #include "colors.h"    };

• 17
• 11
• 12
• 9
• 49
• Forum Statistics

• Total Topics
631395
• Total Posts
2999757
×