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

new customize (#166)

This commit is contained in:
Daniil Goncharov 2022-03-18 13:58:07 +04:00 committed by GitHub
parent 84efdbd2bf
commit 60de6bff8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 48 deletions

View file

@ -29,16 +29,16 @@ enum class Color : int { RED = -10, BLUE = 0, GREEN = 10 };
// С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<Color>(Color value) noexcept {
constexpr magic_enum::customize::customize_t 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 invalid_tag;
}
return {}; // Empty string for unknow value.
return default_tag;
}
enum class Numbers : int { One, Two, Three };
@ -46,26 +46,26 @@ 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 {
constexpr magic_enum::customize::customize_t 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.
return default_tag;
}
}
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 << 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; // ''
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
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;
}