C++ (Qt)class TagStatus {public: enum Status { NoDefine, Define1, Define2 }; static String convertToString(Status); static Status convertToEnum(String);private: static class HashStr: public<String,enum> { HashStr() { insert("Not define",NoDefine); ...... } } mHashStr;};
C++ (Qt)struct TagStatus{ enum Status { NoDefine, Define1, Define2 }; static std::string convertToString(Status s) { return get_hash().at(s); } static Status convertToStatus(const std::string & s) { for (auto & x : get_hash()) { if (s == x.second) return x.first; } throw std::bad_cast(); } private: typedef std::map<Status, std::string> hash_type; static const hash_type & get_hash() { static hash_type hash = { {NoDefine, "No Define"}, {Define1, "Define 1"}, {Define2, "Define 2"} }; return hash; }};
mHashStr.insert(mHashEnum[enum],enum);
C++ (Qt)struct TagStatus{#define STATUS \ X(NoDefine, "NoDefine") \ X(Define1, "Define1") \ X(Define2, "Define2") #define X(a, b) a, enum Status { STATUS };#undef X static std::string convertToString(Status s) { return get_hash().at(s); } static Status convertToStatus(const std::string & s) { for (auto & x : get_hash()) { if (s == x.second) return x.first; } throw std::bad_cast(); } private: typedef std::map<Status, std::string> hash_type; static const hash_type & get_hash() {#define X(a, b) {a, #b}, static hash_type hash = { STATUS };#undef X return hash; }};
C++ (Qt)#define X(a, b) {a, #b},
C++ (Qt)#define X(a, b) {a, b},