summaryrefslogtreecommitdiff
path: root/include/mbgl/util/enum.hpp
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-07-16 18:53:56 -0700
committerKonstantin Käfer <mail@kkaefer.com>2014-07-16 18:53:56 -0700
commit4ea281c750c5afcc68f2832bb42d98a1cbce6735 (patch)
tree60bc7d3ccba2c54859e2e023997cc027cc67aea7 /include/mbgl/util/enum.hpp
parentc1a64dc5fa73b54cc5de77629781dfc74302a1e7 (diff)
downloadqtlocation-mapboxgl-4ea281c750c5afcc68f2832bb42d98a1cbce6735.tar.gz
rename llmr => mbgl
Diffstat (limited to 'include/mbgl/util/enum.hpp')
-rw-r--r--include/mbgl/util/enum.hpp55
1 files changed, 55 insertions, 0 deletions
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 <iosfwd>
+#include <string>
+
+namespace mbgl {
+
+template <typename Type>
+struct EnumValue {
+ const Type value;
+ const char *name;
+};
+
+template <typename EnumName, const EnumValue<EnumName> *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<Type> 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<Type> 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> type##_names[] = strings; \
+ using name = ::mbgl::Enum<type, type##_names, sizeof(type##_names) / sizeof(::mbgl::EnumValue<type>)>; \
+ inline std::ostream& operator<<(std::ostream& os, type t) { return os << name(t).str(); }
+
+}
+
+#endif
+