c++11 枚举值到枚举名的转换

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

c++11 枚举值到枚举名的转换

hchlqlz   2022-05-28 我要评论

效果

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__);

源码地址

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们