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

improve customize

This commit is contained in:
Daniil Goncharov 2020-09-07 12:55:59 +03:00 committed by GitHub
parent 000551a655
commit 690486e7f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 107 additions and 49 deletions

View file

@ -26,17 +26,46 @@
enum class Color : int { RED = -10, BLUE = 0, GREEN = 10 };
// Сustom definitions of name for enum value.
// Сustom definitions of names for enum.
// Specialization of `enum_name` must be injected in `namespace magic_enum::customize`.
template <>
constexpr auto magic_enum::detail::n<Color, Color::RED>() noexcept {
return std::string_view{"the red color"};
constexpr std::string_view magic_enum::customize::enum_name<Color>(Color value) noexcept {
switch (value) {
case Color::RED:
return "the red color";
case Color::BLUE:
return "The BLUE";
case Color::GREEN:
return {}; // "Empty string for default value."
}
return {}; // "Empty string for unknow value."
}
enum class Numbers : int { One, Two, Three };
// Сustom definitions of names for enum.
// Specialization of `enum_name` must be injected in `namespace magic_enum::customize`.
template <>
constexpr std::string_view magic_enum::customize::enum_name<Numbers>(Numbers value) noexcept {
switch (value) {
case Numbers::One:
return "the one";
default:
return {}; // "Empty string for default or unknow value."
}
}
int main() {
std::cout << magic_enum::enum_name(Color::RED) << std::endl; // "the red color"
std::cout << magic_enum::enum_name(Color::BLUE) << std::endl; // "The BLUE"
std::cout << magic_enum::enum_name(Color::GREEN) << std::endl; // "GREEN"
std::cout << std::boolalpha;
std::cout << (magic_enum::enum_cast<Color>("the red color").value() == Color::RED) << std::endl; // "true"
std::cout << magic_enum::enum_name(Numbers::One) << std::endl; // "the one"
std::cout << magic_enum::enum_name(Numbers::Two) << std::endl; // "Two"
std::cout << magic_enum::enum_name(Numbers::Three) << std::endl; // "Three"
return 0;
}