Suppose we have some named enums:
enum MyEnum { FOO, BAR = 0x50 };
What I googled for is a script (any language) that scans all
#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<