example.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) 2019 - 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 
27 
28 enum class Color : int { RED = -10, BLUE = 0, GREEN = 10 };
29 
30 template <typename E>
32  // magic_enum::Enum<E> - C++17 Concept for enum type.
33  return static_cast<magic_enum::underlying_type_t<E>>(value);
34 }
35 
36 int main() {
37  // Enum variable to string name.
38  Color c1 = Color::RED;
39  auto c1_name = magic_enum::enum_name(c1);
40  std::cout << c1_name << std::endl; // RED
41 
42  // String enum name sequence.
43  constexpr auto names = magic_enum::enum_names<Color>();
44  std::cout << "Color names:";
45  for (const auto& n : names) {
46  std::cout << " " << n;
47  }
48  std::cout << std::endl;
49  // Color names: RED BLUE GREEN
50 
51  // String name to enum value.
52  auto c2 = magic_enum::enum_cast<Color>("BLUE");
53  if (c2.has_value()) {
54  std::cout << "BLUE = " << to_integer(c2.value()) << std::endl; // BLUE = 0
55  }
56 
57  // Case insensitive enum_cast.
58  c2 = magic_enum::enum_cast<Color>("blue", magic_enum::case_insensitive);
59  if (c2.has_value()) {
60  std::cout << "BLUE = " << to_integer(c2.value()) << std::endl; // BLUE = 0
61  }
62 
63  // Integer value to enum value.
64  auto c3 = magic_enum::enum_cast<Color>(10);
65  if (c3.has_value()) {
66  std::cout << "GREEN = " << magic_enum::enum_integer(c3.value()) << std::endl; // GREEN = 10
67  }
68 
69  // Enum value to integer value.
70  auto c4_integer = magic_enum::enum_integer(Color::RED);
71  std::cout << "RED = " << c4_integer << std::endl; // RED = -10
72 
73  using magic_enum::iostream_operators::operator<<; // out-of-the-box ostream operator for all enums.
74  // Ostream operator for enum.
75  std::cout << "Color: " << c1 << " " << c2 << " " << c3 << std::endl; // Color: RED BLUE GREEN
76 
77  // Number of enum values.
78  std::cout << "Color enum size: " << magic_enum::enum_count<Color>() << std::endl; // Color size: 3
79 
80  // Indexed access to enum value.
81  std::cout << "Color[0] = " << magic_enum::enum_value<Color>(0) << std::endl; // Color[0] = RED
82 
83  // Enum value sequence.
84  constexpr auto values = magic_enum::enum_values<Color>();
85  std::cout << "Colors values:";
86  for (const auto c : values) {
87  std::cout << " " << c; // Ostream operator for enum.
88  }
89  std::cout << std::endl;
90  // Color values: RED BLUE GREEN
91 
92  enum class Flags { A = 1, B = 2, C = 4, D = 8 };
93  using namespace magic_enum::bitwise_operators; // out-of-the-box bitwise operators for all enums.
94  // Support operators: ~, |, &, ^, |=, &=, ^=.
95  Flags flag = Flags::A | Flags::C;
96  std::cout << flag << std::endl; // 5
97 
98  enum color { red, green, blue };
99 
100  // Checks whether type is an Unscoped enumeration.
101  static_assert(magic_enum::is_unscoped_enum_v<color>);
102  static_assert(!magic_enum::is_unscoped_enum_v<Color>);
103  static_assert(!magic_enum::is_unscoped_enum_v<Flags>);
104 
105  // Checks whether type is an Scoped enumeration.
106  static_assert(!magic_enum::is_scoped_enum_v<color>);
107  static_assert(magic_enum::is_scoped_enum_v<Color>);
108  static_assert(magic_enum::is_scoped_enum_v<Flags>);
109 
110  // Enum pair (value enum, string enum name) sequence.
111  constexpr auto entries = magic_enum::enum_entries<Color>();
112  std::cout << "Colors entries:";
113  for (const auto& e : entries) {
114  std::cout << " " << e.second << " = " << static_cast<int>(e.first);
115  }
116  std::cout << std::endl;
117  // Color entries: RED = -10 BLUE = 0 GREEN = 10
118 
119  return 0;
120 }
Color::GREEN
@ GREEN
magic_enum::Enum
detail::enum_concept< T > Enum
Definition: magic_enum.hpp:1144
magic_enum::bitwise_operators
Definition: magic_enum.hpp:1464
magic_enum::case_insensitive
constexpr auto case_insensitive
Definition: magic_enum.hpp:1339
magic_enum.hpp
main
int main()
Definition: example.cpp:36
magic_enum::detail::names
constexpr auto names(std::index_sequence< I... >) noexcept
Definition: magic_enum.hpp:842
magic_enum_iostream.hpp
magic_enum::detail::value
constexpr E value(std::size_t i) noexcept
Definition: magic_enum.hpp:679
magic_enum::detail::values
constexpr auto values() noexcept
Definition: magic_enum.hpp:757
Catch::cout
std::ostream & cout()
magic_enum::detail::entries
constexpr auto entries(std::index_sequence< I... >) noexcept
Definition: magic_enum.hpp:854
magic_enum::underlying_type_t
typename underlying_type< T >::type underlying_type_t
Definition: magic_enum.hpp:1168
magic_enum::detail::n
constexpr auto n() noexcept
Definition: magic_enum.hpp:421
to_integer
auto to_integer(magic_enum::Enum< E > value)
Definition: example.cpp:31
Color::RED
@ RED
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
Color::BLUE
@ BLUE
magic_enum::enum_integer
constexpr auto enum_integer(E value) noexcept -> detail::enable_if_t< E, underlying_type_t< E >>
Definition: magic_enum.hpp:1225


magic_enum
Author(s):
autogenerated on Fri Feb 21 2025 03:20:18