1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-10 23:44:29 +00:00

remove strict enum_name

This commit is contained in:
neargye 2020-08-16 15:16:15 +05:00
parent 9c40cae91a
commit 971a548693
2 changed files with 14 additions and 64 deletions

View file

@ -876,34 +876,26 @@ template <typename E>
// Returns string name from enum-flags value.
// If enum-flags value does not have name or value out of range, returns empty string.
template <typename E, bool Strict = false>
[[nodiscard]] constexpr auto enum_name(E value) -> detail::enable_if_enum_flags_t<E, std::conditional_t<Strict, std::string_view, std::string>> {
template <typename E>
[[nodiscard]] auto enum_name(E value) -> detail::enable_if_enum_flags_t<E, std::string> {
using D = std::decay_t<E>;
using U = underlying_type_t<D>;
if constexpr (Strict) {
for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
if (enum_value<D>(i) == value) {
return detail::names_v<D, true>[i];
}
}
} else {
std::string name;
auto check_value = U{0};
for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
if (const auto v = enum_value<D>(i); (static_cast<U>(value) & static_cast<U>(v)) != 0) {
check_value |= static_cast<U>(v);
const auto n = detail::names_v<D, true>[i];
if (!name.empty()) {
name.append(1, '|');
}
name.append(n.data(), n.size());
std::string name;
auto check_value = U{0};
for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
if (const auto v = enum_value<D>(i); (static_cast<U>(value) & static_cast<U>(v)) != 0) {
check_value |= static_cast<U>(v);
const auto n = detail::names_v<D, true>[i];
if (!name.empty()) {
name.append(1, '|');
}
name.append(n.data(), n.size());
}
}
if (check_value == static_cast<U>(value)) {
return name;
}
if (check_value == static_cast<U>(value)) {
return name;
}
return {}; // Invalid value or out of range.

View file

@ -556,48 +556,6 @@ TEST_CASE("enum_name") {
REQUIRE(nto_name == "one|three");
REQUIRE(enum_name(static_cast<number>(0)).empty());
}
SECTION("strict automatic storage") {
constexpr Color cr = Color::RED;
constexpr auto cr_name = enum_name<Color, true>(cr);
Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
Color cb = Color::BLUE;
REQUIRE(cr_name == "RED");
REQUIRE(enum_name<Color&, true>(cb) == "BLUE");
REQUIRE(enum_name<Color, true>(cm[1]) == "GREEN");
REQUIRE(enum_name<Color, true>(Color::RED | Color{0}) == "RED");
REQUIRE(enum_name<Color, true>(Color::RED | Color::GREEN).empty());
REQUIRE(enum_name<Color, true>(Color::RED | Color{8}).empty());
REQUIRE(enum_name<Color, true>(static_cast<Color>(0)).empty());
constexpr Numbers no = Numbers::one;
constexpr auto no_name = enum_name<Numbers, true>(no);
REQUIRE(no_name == "one");
REQUIRE(enum_name<Numbers, true>(Numbers::two) == "two");
REQUIRE(enum_name<Numbers, true>(Numbers::three) == "three");
REQUIRE(enum_name<Numbers, true>(Numbers::many) == "many");
REQUIRE(enum_name<Numbers, true>(Numbers::many | Numbers::two).empty());
REQUIRE(enum_name<Numbers, true>(static_cast<Numbers>(0)).empty());
constexpr Directions dr = Directions::Right;
constexpr auto dr_name = enum_name<Directions, true>(dr);
Directions du = Directions::Up;
REQUIRE(enum_name<Directions&, true>(du) == "Up");
REQUIRE(enum_name<const Directions, true>(Directions::Down) == "Down");
REQUIRE(dr_name == "Right");
REQUIRE(enum_name<Directions, true>(Directions::Left) == "Left");
REQUIRE(enum_name<Directions, true>(Directions::Right | Directions::Up | Directions::Left | Directions::Down).empty());
REQUIRE(enum_name<Directions, true>(static_cast<Directions>(0)).empty());
constexpr number nto = number::three | number::one;
auto nto_name = enum_name<number, true>(nto);
REQUIRE(enum_name<number, true>(number::one) == "one");
REQUIRE(enum_name<number, true>(number::two) == "two");
REQUIRE(enum_name<number, true>(number::three) == "three");
REQUIRE(enum_name<number, true>(number::four) == "four");
REQUIRE(nto_name.empty());
REQUIRE(enum_name<number, true>(static_cast<number>(0)).empty());
}
}
TEST_CASE("enum_names") {