码迷,mamicode.com
首页 > 其他好文 > 详细

mapbox中 变长参数的宏函数

时间:2021-02-16 12:24:36      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:using   test   int   color   col   temp   optional   public   fir   

 

//---------------------------------------------------------------------
// 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(Tvalues...)                                          \
                                                                                \
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<TEnum<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;
    }
}

 

mapbox中 变长参数的宏函数

标签:using   test   int   color   col   temp   optional   public   fir   

原文地址:https://www.cnblogs.com/3d-gis/p/14399395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!