From String to Enum

Started by
5 comments, last by JohnBolton 18 years, 3 months ago
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]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
What language?
You can set up a map, but there's no straightforward automatic way to do it as C++ lacks reflection.
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
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
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....
Deep Blue Wave - Brian's Dev Blog.
Quote:Original post by BTownTKD
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....

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

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

(please forgive the crappy psedo-code)

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.
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"    }; 

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement