add enum class to unify parsing/stringification

This commit is contained in:
Konstantin Käfer
2014-07-14 16:33:02 -07:00
parent 37bb2ff048
commit bdc97d55d2
13 changed files with 173 additions and 133 deletions

View File

@@ -1,8 +1,9 @@
#ifndef LLMR_PLATFORM_EVENT
#define LLMR_PLATFORM_EVENT
#include <llmr/util/enum.hpp>
#include <cstdint>
#include <ostream>
namespace llmr {
@@ -14,9 +15,14 @@ enum class EventSeverity : uint8_t {
Error,
};
EventSeverity parseEventSeverity(const char *name);
const char *stringifyEventSeverity(EventSeverity eventSeverity);
::std::ostream& operator<<(::std::ostream& os, EventSeverity eventSeverity);
LLMR_DEFINE_ENUM_CLASS(EventSeverityClass, EventSeverity, {
{ EventSeverity::Debug, "DEBUG" },
{ EventSeverity::Info, "INFO" },
{ EventSeverity::Test, "TEST" },
{ EventSeverity::Warning, "WARNING" },
{ EventSeverity::Error, "ERROR" },
{ EventSeverity(-1), "UNKNOWN" },
});
enum class Event : uint8_t {
General,
@@ -28,9 +34,17 @@ enum class Event : uint8_t {
HttpRequest,
};
Event parseEvent(const char *name);
const char *stringifyEvent(Event event);
::std::ostream& operator<<(::std::ostream& os, Event event);
LLMR_DEFINE_ENUM_CLASS(EventClass, Event, {
{ Event::General, "General" },
{ Event::Setup, "Setup" },
{ Event::Shader, "Shader" },
{ Event::ParseStyle, "ParseStyle" },
{ Event::ParseTile, "ParseTile" },
{ Event::Render, "Render" },
{ Event::HttpRequest, "HttpRequest" },
{ Event(-1), "Unknown" },
});
struct EventPermutation {
const EventSeverity severity;

View File

@@ -9,7 +9,8 @@ namespace llmr {
class Source;
struct StyleSource {
class StyleSource {
public:
const SourceType type;
const std::string url;
const uint32_t tile_size;
@@ -19,16 +20,10 @@ struct StyleSource {
bool enabled = false;
std::shared_ptr<Source> source;
StyleSource(SourceType type = SourceType::Vector,
const std::string &url = "",
uint32_t tile_size = 512, uint32_t min_zoom = 0,
uint32_t max_zoom = 22)
: type(type),
url(url),
tile_size(tile_size),
min_zoom(min_zoom),
max_zoom(max_zoom) {}
};
StyleSource(SourceType type = SourceType::Vector, const std::string &url = "",
uint32_t tile_size = 512, uint32_t min_zoom = 0, uint32_t max_zoom = 22)
: type(type), url(url), tile_size(tile_size), min_zoom(min_zoom), max_zoom(max_zoom) {}
};
};
#endif

View File

@@ -1,6 +1,8 @@
#ifndef LLMR_STYLE_TYPES
#define LLMR_STYLE_TYPES
#include <llmr/util/enum.hpp>
#include <string>
#include <array>
@@ -20,9 +22,17 @@ enum class StyleLayerType : uint8_t {
Background
};
StyleLayerType parseStyleLayerType(const std::string &type);
const char *stringifyStyleLayerType(StyleLayerType type);
std::ostream& operator<<(std::ostream&, StyleLayerType type);
LLMR_DEFINE_ENUM_CLASS(StyleLayerTypeClass, StyleLayerType, {
{ StyleLayerType::Unknown, "unknown" },
{ StyleLayerType::Fill, "fill" },
{ StyleLayerType::Line, "line" },
{ StyleLayerType::Icon, "icon" },
{ StyleLayerType::Text, "text" },
{ StyleLayerType::Raster, "raster" },
{ StyleLayerType::Composite, "composite" },
{ StyleLayerType::Background, "background" },
{ StyleLayerType(-1), "unknown" },
});
enum class WindingType : uint8_t {

View File

@@ -0,0 +1,55 @@
#ifndef LLMR_UTIL_ENUM
#define LLMR_UTIL_ENUM
#include <iosfwd>
#include <string>
namespace llmr {
template <typename Type>
struct EnumValue {
const Type value;
const char *name;
};
template <typename EnumName, const constexpr 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 LLMR_DEFINE_ENUM_CLASS(name, type, strings...) \
const constexpr ::llmr::EnumValue<type> type##_names[] = strings; \
using name = ::llmr::Enum<type, type##_names, sizeof(type##_names) / sizeof(::llmr::EnumValue<type>)>; \
inline std::ostream& operator<<(std::ostream& os, type t) { return os << name(t).str(); }
}
#endif