Make Enum Helper parse functions constexpr, added Enum Helper Try Parse functions.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2020-02-13 13:19:59 +01:00
parent 0ef909cf55
commit 02aa6f252e
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 57 additions and 8 deletions

View File

@ -1,5 +1,5 @@
#include <algorithm> #include <algorithm>
#include <string> #include <sstream>
#include <vector> #include <vector>
#include "Exceptions/NotReachableException.hpp" #include "Exceptions/NotReachableException.hpp"
#include "MacroUtils.hpp" #include "MacroUtils.hpp"
@ -12,10 +12,20 @@
case ConstHash(#x): \ case ConstHash(#x): \
return name::x; return name::x;
# #
#define ENUM_TRY_PARSE_CASE(x, name) \
case ConstHash(#x): \
out = name::x; \
return true;
#
#define ENUM_PARSE_CASE_INSENSITIVE(x, name) \ #define ENUM_PARSE_CASE_INSENSITIVE(x, name) \
case ConstHashCI(#x): \ case ConstHashCI(#x): \
return name::x; return name::x;
# #
#define ENUM_TRY_PARSE_CASE_INSENSITIVE(x, name) \
case ConstHashCI(#x): \
out = name::x; \
return true;
#
#define ARRAY_NAME(x, name) name::x, #define ARRAY_NAME(x, name) name::x,
#define ENUM(name, type, values...) \ #define ENUM(name, type, values...) \
@ -33,21 +43,31 @@
} \ } \
\ \
public: \ public: \
static std::string ToString(name value) { \ constexpr static const char* ToString(name value) { \
switch (value) { FOR_EACH(ENUM_CASE, name, values) } \ switch (value) { FOR_EACH(ENUM_CASE, name, values) } \
throw NotReachableException(); \ throw NotReachableException(); \
} \ } \
static name Parse(const std::string& input, bool caseInsensitive = false) { \ constexpr static name Parse(const char* input, bool caseInsensitive = false) { \
if (caseInsensitive) \ if (caseInsensitive) \
return ParseCaseInsensitive(input); \ return ParseCaseInsensitive(input); \
switch (ConstHash(input.c_str())) { FOR_EACH(ENUM_PARSE_CASE, name, values) } \ switch (ConstHash(input)) { FOR_EACH(ENUM_PARSE_CASE, name, values) } \
throw CreatureException("String '" + input + "' could not be parsed as " #name); \ throw CreatureException("Invalid AttackCategory string."); \
} \
constexpr static bool TryParse(const char* input, name& out, bool caseInsensitive = false) { \
if (caseInsensitive) \
return TryParseCaseInsensitive(input, out); \
switch (ConstHash(input)) { FOR_EACH(ENUM_TRY_PARSE_CASE, name, values) } \
return false; \
} \ } \
static std::vector<name> GetValues() { return {FOR_EACH(ARRAY_NAME, name, values)}; } \ static std::vector<name> GetValues() { return {FOR_EACH(ARRAY_NAME, name, values)}; } \
\ \
private: \ private: \
static name ParseCaseInsensitive(const std::string& input) { \ static name ParseCaseInsensitive(const char* input) { \
switch (ConstHashCI(input.c_str())) { FOR_EACH(ENUM_PARSE_CASE_INSENSITIVE, name, values) } \ switch (ConstHashCI(input)) { FOR_EACH(ENUM_PARSE_CASE_INSENSITIVE, name, values) } \
throw CreatureException("String '" + input + "' could not be parsed as " #name); \ throw CreatureException("Invalid AttackCategory string."); \
} \
static bool TryParseCaseInsensitive(const char* input, name& out) { \
switch (ConstHashCI(input)) { FOR_EACH(ENUM_TRY_PARSE_CASE_INSENSITIVE, name, values) } \
return false; \
} \ } \
}; };

View File

@ -13,6 +13,18 @@ TEST_CASE("Parse Enum case sensitive", "[Utilities]") {
CHECK_THROWS(TestEnumHelper::Parse("val1")); CHECK_THROWS(TestEnumHelper::Parse("val1"));
} }
TEST_CASE("Try Parse Enum case sensitive", "[Utilities]") {
TestEnum v = static_cast<TestEnum>(255);
REQUIRE(TestEnumHelper::TryParse("Val1", v));
CHECK(v == TestEnum::Val1);
REQUIRE(TestEnumHelper::TryParse("Val2", v));
CHECK(v == TestEnum::Val2);
REQUIRE(TestEnumHelper::TryParse("Val3", v));
CHECK(v == TestEnum::Val3);
CHECK_FALSE(TestEnumHelper::TryParse("Val4", v));
CHECK_FALSE(TestEnumHelper::TryParse("val1", v));
}
TEST_CASE("Parse Enum case insensitive", "[Utilities]") { TEST_CASE("Parse Enum case insensitive", "[Utilities]") {
CHECK(TestEnumHelper::Parse("Val1", true) == TestEnum::Val1); CHECK(TestEnumHelper::Parse("Val1", true) == TestEnum::Val1);
CHECK(TestEnumHelper::Parse("Val2", true) == TestEnum::Val2); CHECK(TestEnumHelper::Parse("Val2", true) == TestEnum::Val2);
@ -23,6 +35,23 @@ TEST_CASE("Parse Enum case insensitive", "[Utilities]") {
CHECK_THROWS(TestEnumHelper::Parse("Val4", true)); CHECK_THROWS(TestEnumHelper::Parse("Val4", true));
} }
TEST_CASE("Try Parse Enum case insensitive", "[Utilities]") {
TestEnum v = static_cast<TestEnum>(255);
REQUIRE(TestEnumHelper::TryParse("Val1", v, true));
CHECK(v == TestEnum::Val1);
REQUIRE(TestEnumHelper::TryParse("Val2", v, true));
CHECK(v == TestEnum::Val2);
REQUIRE(TestEnumHelper::TryParse("Val3", v, true));
CHECK(v == TestEnum::Val3);
REQUIRE(TestEnumHelper::TryParse("val1", v, true));
CHECK(v == TestEnum::Val1);
REQUIRE(TestEnumHelper::TryParse("vAL2", v, true));
CHECK(v == TestEnum::Val2);
REQUIRE(TestEnumHelper::TryParse("VaL3", v, true));
CHECK(v == TestEnum::Val3);
CHECK_FALSE(TestEnumHelper::TryParse("Val4", v, true));
}
TEST_CASE("Enum To String", "[Utilities]") { TEST_CASE("Enum To String", "[Utilities]") {
CHECK(TestEnumHelper::ToString(TestEnum::Val1) == "Val1"); CHECK(TestEnumHelper::ToString(TestEnum::Val1) == "Val1");
CHECK(TestEnumHelper::ToString(TestEnum::Val2) == "Val2"); CHECK(TestEnumHelper::ToString(TestEnum::Val2) == "Val2");