From cdab528313c06e9a6df5ae740a58f624b65332d3 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 10 Feb 2023 22:22:03 +0800 Subject: [PATCH] 1 --- a8/a8.h | 14 ++++++++++++++ a8/magicenum.h | 43 +++++++++++++++++++++++++++++++++++++++++++ a8/types.h | 6 ++++++ 3 files changed, 63 insertions(+) create mode 100644 a8/magicenum.h diff --git a/a8/a8.h b/a8/a8.h index 059f421..7471987 100644 --- a/a8/a8.h +++ b/a8/a8.h @@ -26,4 +26,18 @@ #define A8_ABORT() do{printf("abort file:%s line:%d func:%s\n", __FILE__, __LINE__, __func__);fflush(stdout);fflush(stderr);abort();}while(0); +#define A8_DECLARE_ENUM(E, ...) \ +enum E \ +{ \ +__VA_ARGS__ \ +}; \ +template<> constexpr const char* GetEnumString() { return #__VA_ARGS__; }; + +#define A8_DECLARE_CLASS_ENUM(E, T, ...) \ +enum class E : T \ +{ \ +__VA_ARGS__ \ +}; \ +template<> constexpr const char* GetEnumString() { return #__VA_ARGS__; }; + const float A8_PI = 3.1415926f; diff --git a/a8/magicenum.h b/a8/magicenum.h new file mode 100644 index 0000000..11a4538 --- /dev/null +++ b/a8/magicenum.h @@ -0,0 +1,43 @@ +#pragma once + +#include + +namespace a8 +{ + + template + std::vector>* GetEnumFields(T* t = nullptr) + { + static std::vector>* fields = nullptr; + if (!fields) { + fields = new std::vector>(); + 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; + + while (*p != '=') { ++p; } + + ++p; + while (!isalnum(*p)) { ++p; } + const char* p_val_begin = p++; + 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); + fields->push_back(std::make_pair + (key, + a8::XValue(val).GetInt())); + //printf("%s=%s\n", key.c_str(), val.c_str()); + } else { + ++p; + } + } + } + return fields; + } + +} diff --git a/a8/types.h b/a8/types.h index 80e33aa..81d42e5 100644 --- a/a8/types.h +++ b/a8/types.h @@ -45,3 +45,9 @@ namespace a8 class Class; } }; + +template +const char* GetEnumString() +{ + return nullptr; +};