From 58dd5bb850143cd6d0b55a99c1d7197e6153fea7 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 13 Feb 2023 15:52:47 +0800 Subject: [PATCH] 1 --- a8/magicenum.h | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/a8/magicenum.h b/a8/magicenum.h index 11a4538..e57b178 100644 --- a/a8/magicenum.h +++ b/a8/magicenum.h @@ -11,14 +11,31 @@ namespace a8 static std::vector>* fields = nullptr; if (!fields) { fields = new std::vector>(); + int last_id = -1; const char* p = ::GetEnumString(); while (*p) { if (*p == '_' || isalpha(*p)) { const char* p_key_begin = p++; while (*p == '_' || isalpha(*p) || isalnum(*p)) { ++p; } const char* p_key_end = p - 1; + std::string key(p_key_begin, p_key_end + 1); - while (*p != '=') { ++p; } + while (*p && *p != '=' && *p != ',') { ++p; } + if (!*p) { + fields->push_back(std::make_pair + (key, + ++last_id + ) + ); + break; + } else if (*p == ',') { + fields->push_back(std::make_pair + (key, + ++last_id + ) + ); + continue; + } ++p; while (!isalnum(*p)) { ++p; } @@ -26,11 +43,11 @@ namespace a8 while (isalnum(*p)) { ++p; } const char* p_val_end = p - 1; - std::string key(p_key_begin, p_key_end + 1); std::string val(p_val_begin, p_val_end + 1); + last_id = a8::XValue(val); fields->push_back(std::make_pair (key, - a8::XValue(val).GetInt())); + last_id)); //printf("%s=%s\n", key.c_str(), val.c_str()); } else { ++p; @@ -40,4 +57,16 @@ namespace a8 return fields; } + template + std::string GetEnumName(int id, T* t = nullptr) + { + auto fields = GetEnumFields(); + for (auto& pair : *fields) { + if (pair.second == id) { + return pair.first; + } + } + return ""; + } + }