1#pragma once
2
3#include <mbgl/util/optional.hpp>
4
5#include <algorithm>
6#include <cassert>
7#include <string>
8
9namespace mbgl {
10
11template <typename T>
12class Enum {
13public:
14 using Type = T;
15 static const char * toString(T);
16 static optional<T> toEnum(const std::string&);
17};
18
19#define MBGL_DEFINE_ENUM(T, values...) \
20 \
21static const constexpr std::pair<const T, const char *> T##_names[] = values; \
22 \
23template <> \
24const char * Enum<T>::toString(T t) { \
25 auto it = std::find_if(std::begin(T##_names), std::end(T##_names), \
26 [&] (const auto& v) { return t == v.first; }); \
27 assert(it != std::end(T##_names)); return it->second; \
28} \
29 \
30template <> \
31optional<T> Enum<T>::toEnum(const std::string& s) { \
32 auto it = std::find_if(std::begin(T##_names), std::end(T##_names), \
33 [&] (const auto& v) { return s == v.second; }); \
34 return it == std::end(T##_names) ? optional<T>() : it->first; \
35}
36
37} // namespace mbgl
38

source code of qtlocation/src/3rdparty/mapbox-gl-native/include/mbgl/util/enum.hpp