MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
enum.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <cassert>
5 #include <string>
6 #include <optional>
7 
8 namespace mbgl {
9 
10 template <typename T>
11 class Enum {
12 public:
13  using Type = T;
14  static const char * toString(T);
15  static std::optional<T> toEnum(const std::string&);
16 };
17 
18 #define MBGL_DEFINE_ENUM(T, ...) \
19  \
20 static const constexpr std::pair<const T, const char *> T##_names[] = __VA_ARGS__; \
21  \
22 template <> \
23 const char * Enum<T>::toString(T t) { \
24  auto it = std::find_if(std::begin(T##_names), std::end(T##_names), \
25  [&] (const auto& v) { return t == v.first; }); \
26  assert(it != std::end(T##_names)); return it->second; \
27 } \
28  \
29 template <> \
30 std::optional<T> Enum<T>::toEnum(const std::string& s) { \
31  auto it = std::find_if(std::begin(T##_names), std::end(T##_names), \
32  [&] (const auto& v) { return s == v.second; }); \
33  return it == std::end(T##_names) ? std::optional<T>() : it->first; \
34 }
35 
36 } // namespace mbgl
T Type
Definition: enum.hpp:13
static const char * toString(T)
static std::optional< T > toEnum(const std::string &)
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
Definition: actor.hpp:15