example
example_custom_name.cpp
Go to the documentation of this file.
1
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
2
// SPDX-License-Identifier: MIT
3
// Copyright (c) 2020 - 2024 Daniil Goncharov <neargye@gmail.com>.
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included in all
13
// copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
// SOFTWARE.
22
23
#include <iostream>
24
25
#include <
magic_enum/magic_enum.hpp
>
26
27
enum class
Color
: int {
RED
= -10,
BLUE
= 0,
GREEN
= 10 };
28
29
// Сustom definitions of names for enum.
30
// Specialization of `enum_name` must be injected in `namespace magic_enum::customize`.
31
template
<>
32
constexpr
magic_enum::customize::customize_t
magic_enum::customize::enum_name<Color>(
Color
value
) noexcept {
33
switch
(
value
) {
34
case
Color::RED
:
35
return
"the red color"
;
36
case
Color::BLUE
:
37
return
"The BLUE"
;
38
case
Color::GREEN
:
39
return
invalid_tag
;
40
}
41
return
default_tag
;
42
}
43
44
enum class
Numbers
: int {
One
,
Two
,
Three
};
45
46
// Сustom definitions of names for enum.
47
// Specialization of `enum_name` must be injected in `namespace magic_enum::customize`.
48
template
<>
49
constexpr
magic_enum::customize::customize_t
magic_enum::customize::enum_name<Numbers>(
Numbers
value
) noexcept {
50
switch
(
value
) {
51
case
Numbers::One
:
52
return
"the one"
;
53
default
:
54
return
default_tag
;
55
}
56
}
57
58
int
main
() {
59
std::cout
<<
magic_enum::enum_name
(
Color::RED
) << std::endl;
// 'the red color'
60
std::cout
<<
magic_enum::enum_name
(
Color::BLUE
) << std::endl;
// 'The BLUE'
61
std::cout
<<
magic_enum::enum_name
(
Color::GREEN
) << std::endl;
// ''
62
63
std::cout
<< std::boolalpha;
64
std::cout << (magic_enum::enum_cast<Color>(
"the red color"
).value() ==
Color::RED
) << std::endl;
// true
65
66
std::cout
<<
magic_enum::enum_name
(
Numbers::One
) << std::endl;
// 'the one'
67
std::cout
<<
magic_enum::enum_name
(
Numbers::Two
) << std::endl;
// 'Two'
68
std::cout
<<
magic_enum::enum_name
(
Numbers::Three
) << std::endl;
// 'Three'
69
70
return
0;
71
}
Color::GREEN
@ GREEN
magic_enum.hpp
magic_enum::customize::invalid_tag
constexpr auto invalid_tag
Definition:
magic_enum.hpp:201
Numbers::Three
@ Three
magic_enum::detail::value
constexpr E value(std::size_t i) noexcept
Definition:
magic_enum.hpp:679
Numbers::One
@ One
Numbers
Numbers
Definition:
example_custom_name.cpp:44
Catch::cout
std::ostream & cout()
Color::RED
@ RED
Numbers::Two
@ Two
magic_enum::customize::customize_t
Definition:
magic_enum.hpp:189
magic_enum::enum_name
constexpr auto enum_name() noexcept -> detail::enable_if_t< decltype(V), string_view >
Definition:
magic_enum.hpp:1290
Color
Color
Definition:
example.cpp:28
magic_enum::customize::default_tag
constexpr auto default_tag
Definition:
magic_enum.hpp:199
Color::BLUE
@ BLUE
main
int main()
Definition:
example_custom_name.cpp:58
magic_enum
Author(s):
autogenerated on Fri Feb 21 2025 03:20:18