This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Make enum ↔ string conversion more generic-friendly
Rewrite enum.hpp in such a way that parseConstant can be defined generically for all enumerated types. While there, properly validated enumerated property values.
- Loading branch information
1 parent
a8df0fe
commit 29d336c
Showing
15 changed files
with
193 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,36 @@ | ||
#pragma once | ||
|
||
#include <iosfwd> | ||
#include <mbgl/util/optional.hpp> | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
#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 list, size_t r) { | ||
return r == 0 ? "" : list->value == e ? list->name : lookup_type(e, list + 1, r - 1); | ||
} | ||
static constexpr inline Type lookup_name(const char *n, EnumValue<Type> const * const list, size_t r) { | ||
return r == 0 ? Type(-1) : compare(list->name, n) ? list->value : lookup_name(n, list + 1, r - 1); | ||
} | ||
template <typename T> | ||
class Enum { | ||
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; } | ||
using Value = std::pair<const T, const char *>; | ||
|
||
inline constexpr bool valid() const { return value != Type(-1); } | ||
static const char * toString(T t) { | ||
auto it = std::find_if(begin, end, [&] (const auto& v) { return t == v.first; }); | ||
assert(it != end); return it->second; | ||
} | ||
|
||
inline constexpr const char *c_str() const { return lookup_type(value, names, length); } | ||
inline std::string str() const { return c_str(); } | ||
static optional<T> toEnum(const std::string& s) { | ||
auto it = std::find_if(begin, end, [&] (const auto& v) { return s == v.second; }); | ||
return it == end ? optional<T>() : it->first; | ||
} | ||
|
||
inline constexpr operator Type() const { return value; } | ||
private: | ||
static const Value* begin; | ||
static const Value* end; | ||
}; | ||
|
||
#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(); } | ||
#define MBGL_DEFINE_ENUM(type, strings...) \ | ||
const constexpr Enum<type>::Value type##_names[] = strings; \ | ||
template <> const Enum<type>::Value* Enum<type>::begin = std::begin(type##_names); \ | ||
template <> const Enum<type>::Value* Enum<type>::end = std::end(type##_names); | ||
|
||
} // namespace mbgl | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
#include <mbgl/platform/log.hpp> | ||
#include <mbgl/util/enum.hpp> | ||
|
||
#include <iostream> | ||
|
||
namespace mbgl { | ||
|
||
void Log::platformRecord(EventSeverity severity, const std::string &msg) { | ||
std::cerr << "[" << severity << "] " << msg << std::endl; | ||
std::cerr << "[" << Enum<EventSeverity>::toString(severity) << "] " << msg << std::endl; | ||
} | ||
|
||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <mbgl/platform/event.hpp> | ||
#include <mbgl/util/enum.hpp> | ||
|
||
namespace mbgl { | ||
|
||
MBGL_DEFINE_ENUM(EventSeverity, { | ||
{ EventSeverity::Debug, "DEBUG" }, | ||
{ EventSeverity::Info, "INFO" }, | ||
{ EventSeverity::Warning, "WARNING" }, | ||
{ EventSeverity::Error, "ERROR" }, | ||
{ EventSeverity(-1), "UNKNOWN" }, | ||
}); | ||
|
||
MBGL_DEFINE_ENUM(Event, { | ||
{ Event::General, "General" }, | ||
{ Event::Setup, "Setup" }, | ||
{ Event::Shader, "Shader" }, | ||
{ Event::ParseStyle, "ParseStyle" }, | ||
{ Event::ParseTile, "ParseTile" }, | ||
{ Event::Render, "Render" }, | ||
{ Event::Style, "Style" }, | ||
{ Event::Database, "Database" }, | ||
{ Event::HttpRequest, "HttpRequest" }, | ||
{ Event::Sprite, "Sprite" }, | ||
{ Event::Image, "Image" }, | ||
{ Event::OpenGL, "OpenGL" }, | ||
{ Event::JNI, "JNI" }, | ||
{ Event::Android, "Android" }, | ||
{ Event::Crash, "Crash" }, | ||
{ Event::Glyph, "Glyph" }, | ||
{ Event(-1), "Unknown" }, | ||
}); | ||
|
||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.