效果
ENUM_DEFINE ( Color,
Red,
Blue,
)
EnumHelper(Color::Red) -> "Red"
EnumHelper(Color::Red, std::toupper) -> "RED"
關鍵技術
-
__VA_ARGS__
__VA_ARGS__
實現了可變參數的宏。#define XXX(type, ...) enum class type { __VA_ARGS__ };
XXX(Color, Red, Blue)
等價於:enum class Color { Red, Blue };
-
#__VA_ARGS__
#__VA_ARGS__
可將宏的可變參數轉爲字符串。#define XXX(type, ...) #__VA_ARGS__
XXX(Color, Red, Blue)
等價於:"Red, Blue"
-
在函數外執行代碼的能力
在函數體外,可以通過定義全局變量來執行一個函數。需要注意的是,頭文件中正常是不能進行變量初始化的,除非加上
static
或者const
。const int temp = initialize();
另外,如果多個代碼文件
#include
了該頭文件,會產生多個變量,即在不同代碼文件取得的temp
變量不是同一個。與之對應,initialize
函數也會調用多次。 -
模板函數的靜態變量
函數的靜態變量可以用於存放枚舉值到枚舉字符串的映射,而將枚舉類型作爲模板參數的模板函數,則可以直接爲每種枚舉提供了一個映射容器。
-
關鍵代碼
template<typename T> string EnumHelper(T key, const std::function<char(char)> processor = nullptr, const char* pszName = NULL) { static_assert(std::is_enum_v<T>, __FUNCTION__ "'s key need a enum"); static map<T, string> s_mapName; if (nullptr != pszName) { s_mapName[key] = pszName; } std::string res = ""; auto it = s_mapName.find(key); if (it != s_mapName.end()) { res = it->second; } if (nullptr != processor) { std::transform(res.begin(), res.end(), res.begin(), processor); } return res; } template <class T> size_t analystEnum(T enumClass, const char* pszNames) { static_assert(std::is_enum_v<T>, __FUNCTION__ "'s enumClass need a enum"); cout << "analystEnum: " << pszNames << endl; if (nullptr != pszNames) { const vector<string>& vecName = split(pszNames, ","); for (int i = 0; i < vecName.size(); ++i) { if (vecName.at(i).size() > 0) { EnumHelper((T)(i + 1), nullptr, vecName.at(i).c_str() + (i == 0 ? 0 : 1) ); } } return rand(); } return rand(); } #define ENUM_DEFINE(type, ...) enum class type { placeholder, __VA_ARGS__ }; static const size_t g_uEnumSizeOf##type = analystEnum(type::placeholder, #__VA_ARGS__);
-
源碼地址