c++11 實現列舉值到列舉名的轉換

hchlqlz發表於2022-03-11

效果

```
ENUM_DEFINE ( Color,
    Red,
    Blue,
)

EnumHelper(Color::Red) -> "Red"
EnumHelper(Color::Red, std::toupper) -> "RED"
```

關鍵技術

  1. __VA_ARGS__

    __VA_ARGS__ 實現了可變引數的巨集。

    #define XXX(type, ...) enum class type { __VA_ARGS__ };
    

    XXX(Color, Red, Blue) 等價於:

    enum class Color
    {
        Red,
        Blue
    };
    
  2. #__VA_ARGS__

    #__VA_ARGS__ 可將巨集的可變引數轉為字串。

    #define XXX(type, ...) #__VA_ARGS__
    

    XXX(Color, Red, Blue) 等價於:"Red, Blue"

  3. 在函式外執行程式碼的能力

    在函式體外,可以通過定義全域性變數來執行一個函式。需要注意的是,標頭檔案中正常是不能進行變數初始化的,除非加上 static 或者 const

    const int temp = initialize();
    

    另外,如果多個程式碼檔案 #include 了該標頭檔案,會產生多個變數,即在不同程式碼檔案取得的 temp 變數不是同一個。與之對應,initialize 函式也會呼叫多次。

  4. 模板函式的靜態變數

    函式的靜態變數可以用於存放列舉值到列舉字串的對映,而將列舉型別作為模板引數的模板函式,則可以直接為每種列舉提供了一個對映容器。

  5. 關鍵程式碼

    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__);
    
  6. 原始碼地址

相關文章