Last active
July 11, 2017 11:55
-
-
Save aqzlpm11/12dbb734a1b70b2fdd1205a80b7fd721 to your computer and use it in GitHub Desktop.
C++: string enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /////////// self defined ENUM_STR, have a string val | |
| #define ENUM_STR_BEGIN(type_name) \ | |
| class type_name { \ | |
| std::string v; \ | |
| public: \ | |
| static std::set<std::string>* vals() { static std::set<std::string> s; return &s; }\ | |
| type_name(const std::string& a): v(a){} \ | |
| std::string toString() { return v; } \ | |
| bool isValid() { return vals()->find(v) != vals()->end(); } | |
| #define ENUM_STR_VAR(name) \ | |
| class run_cmd_##name {public: run_cmd_##name(){ \ | |
| vals()->insert(#name); \ | |
| }} tmpvar##name; \ | |
| bool is##name() { return v == #name; } \ | |
| static std::string name() { return #name; } | |
| #define ENUM_STR_END(type_name) } | |
| /////////// ENUM_STRING END |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
在C++中,用Trick,定义了一个string类型的enum。