This commit is contained in:
aozhiwei 2023-02-10 22:22:03 +08:00
parent 1083101984
commit cdab528313
3 changed files with 63 additions and 0 deletions

14
a8/a8.h
View File

@ -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_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<E>() { return #__VA_ARGS__; };
#define A8_DECLARE_CLASS_ENUM(E, T, ...) \
enum class E : T \
{ \
__VA_ARGS__ \
}; \
template<> constexpr const char* GetEnumString<E>() { return #__VA_ARGS__; };
const float A8_PI = 3.1415926f; const float A8_PI = 3.1415926f;

43
a8/magicenum.h Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include <vector>
namespace a8
{
template<typename T>
std::vector<std::pair<std::string, int>>* GetEnumFields(T* t = nullptr)
{
static std::vector<std::pair<std::string, int>>* fields = nullptr;
if (!fields) {
fields = new std::vector<std::pair<std::string, int>>();
const char* p = ::GetEnumString<T>();
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;
}
}

View File

@ -45,3 +45,9 @@ namespace a8
class Class; class Class;
} }
}; };
template<typename T>
const char* GetEnumString()
{
return nullptr;
};