Is there a simple way to convert C++ enum to string?

后端 未结 30 2577
我在风中等你
我在风中等你 2020-11-22 10:37

Suppose we have some named enums:

enum MyEnum {
      FOO,
      BAR = 0x50
};

What I googled for is a script (any language) that scans all

30条回答
  •  时光取名叫无心
    2020-11-22 10:48

    #include 
    #include 
    #include  
    #include 
    #include 
    #include 
    
    #define SMART_ENUM(EnumName, ...)                                   \
    class EnumName                                                      \
    {                                                                   \
    private:                                                            \
        static std::map nameMap;                      \
    public:                                                             \
        enum {__VA_ARGS__};                                             \
    private:                                                            \
        static std::map initMap()                     \
        {                                                               \
            using namespace std;                                        \
                                                                        \
            int val = 0;                                                \
            string buf_1, buf_2, str = #__VA_ARGS__;                    \
            replace(str.begin(), str.end(), '=', ' ');                  \
            stringstream stream(str);                                   \
            vector strings;                                     \
            while (getline(stream, buf_1, ','))                         \
                strings.push_back(buf_1);                               \
            map tmp;                                       \
            for(vector::iterator it = strings.begin();          \
                                                   it != strings.end(); \
                                                   ++it)                \
            {                                                           \
                buf_1.clear(); buf_2.clear();                           \
                stringstream localStream(*it);                          \
                localStream>> buf_1 >> buf_2;                           \
                if(buf_2.size() > 0)                                    \
                    val = atoi(buf_2.c_str());                          \
                tmp[val++] = buf_1;                                     \
            }                                                           \
            return tmp;                                                 \
        }                                                               \
    public:                                                             \
        static std::string toString(int aInt)                           \
        {                                                               \
            return nameMap[aInt];                                       \
        }                                                               \
    };                                                                  \
    std::map                                          \
    EnumName::nameMap = EnumName::initMap();
    

    Usage:

    SMART_ENUM(MyEnum, ONE=1, TWO, THREE, TEN=10, ELEVEN)
    cout<

提交回复
热议问题