mapbox中 变长参数的宏函数

 

//---------------------------------------------------------------------
// variadic_template.h
//---------------------------------------------------------------------
template <typename T>
class Enum {
public:
    using Type = T;
    static const char * toString(T);
    static T  toEnum(const std::string&);
};

#ifdef  _WINDOWS
#define MBGL_DEFINE_ENUM(T, ...)                                          \
                                                                                \
static const constexpr std::pair<const T, const char *> T##_names[] = ##__VA_ARGS__;   \
                                                                                \
template <>                                                                     \
const char * Enum<T>::toString(T t) {                                           \
    auto it = std::find_if(std::begin(T##_names), std::end(T##_names),          \
        [&] (const auto& v) { return t == v.first; });                          \
    assert(it != std::end(T##_names)); return it->second;                       \
}                                                                               \
                                                                                \
template <>                                                                     \
T  Enum<T>::toEnum(const std::string& s) {                             \
    auto it = std::find_if(std::begin(T##_names), std::end(T##_names),          \
        [&] (const auto& v) { return s == v.second; });                         \
    return it == std::end(T##_names) ? T() : it->first;               \
}
#else // ios

#define MBGL_DEFINE_ENUM(T, values...)                                          \                                                                                 \ static const constexpr std::pair<const T, const char *> T##_names[] = values;   \                                                                                 \ template <>                                                                     \ const char * Enum<T>::toString(T t) {                                           \     auto it = std::find_if(std::begin(T##_names), std::end(T##_names),          \         [&] (const auto& v) { return t == v.first; });                          \     assert(it != std::end(T##_names)); return it->second;                       \ }                                                                               \                                                                                 \ template <>                                                                     \ optional<T> Enum<T>::toEnum(const std::string& s) {                             \     auto it = std::find_if(std::begin(T##_names), std::end(T##_names),          \         [&] (const auto& v) { return s == v.second; });                         \     return it == std::end(T##_names) ? optional<T>() : it->first;               \ }

#endif  // #ifdef  _WINDOWS

 

//---------------------------------------------------------------------
// variadic_template.cpp
//---------------------------------------------------------------------
MBGL_DEFINE_ENUM(int, {
{ 1, "vector" },
{2, "raster" },
{ 3, "geojson" },
{ 4, "video" },
{ 5, "annotations" },
{ 6, "image" },
{ 7, "customvector" }
    });

void testMBGL_DEFINE_ENUM()
{
    for (auto i = std::begin(int_names); i != std::end(int_names); i++)
    {
         i->first;
         i->second;
    }
}

 

上一篇:Linux学习记录(五):Shell脚本流程控制


下一篇:python 列表操作