From 4ea281c750c5afcc68f2832bb42d98a1cbce6735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 16 Jul 2014 18:53:56 -0700 Subject: rename llmr => mbgl --- include/mbgl/util/enum.hpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 include/mbgl/util/enum.hpp (limited to 'include/mbgl/util/enum.hpp') diff --git a/include/mbgl/util/enum.hpp b/include/mbgl/util/enum.hpp new file mode 100644 index 0000000000..195aaab5a5 --- /dev/null +++ b/include/mbgl/util/enum.hpp @@ -0,0 +1,55 @@ +#ifndef MBGL_UTIL_ENUM +#define MBGL_UTIL_ENUM + +#include +#include + +namespace mbgl { + +template +struct EnumValue { + const Type value; + const char *name; +}; + +template *names, const size_t length> +struct Enum { + using Type = EnumName; + Type value; + static const constexpr size_t l = length; +private: + static constexpr inline bool compare(const char *a, const char *b) { + return *a == *b && (*a == '\0' || compare(a + 1, b + 1)); + } + static constexpr inline const char *lookup_type(Type e, EnumValue const * const l, size_t r) { + return r == 0 ? "" : l->value == e ? l->name : lookup_type(e, l + 1, r - 1); + } + static constexpr inline Type lookup_name(const char *n, EnumValue const * const l, size_t r) { + return r == 0 ? Type(-1) : compare(l->name, n) ? l->value : lookup_name(n, l + 1, r - 1); + } +public: + inline constexpr Enum(const char *n) : value(lookup_name(n, names, length)) {} + inline constexpr Enum(const std::string &n) : value(lookup_name(n.c_str(), names, length)) {} + inline constexpr Enum(Type t) : value(t) {} + + inline void operator=(const char *n) { value = lookup_name(n, names, length); } + inline void operator=(const std::string &n) { *this = n.c_str(); } + inline void operator=(Type t) { value = t; } + + inline constexpr bool valid() const { return value != Type(-1); } + + inline constexpr const char *c_str() const { return lookup_type(value, names, length); } + inline std::string str() const { return c_str(); } + + inline constexpr operator Type() const { return value; } +}; + +#define MBGL_DEFINE_ENUM_CLASS(name, type, strings...) \ + const constexpr ::mbgl::EnumValue type##_names[] = strings; \ + using name = ::mbgl::Enum)>; \ + inline std::ostream& operator<<(std::ostream& os, type t) { return os << name(t).str(); } + +} + +#endif + -- cgit v1.2.1