enum_flag_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 #include <string>
25 
28 
29 enum class AnimalFlags : std::uint64_t { HasClaws = 1 << 10, CanFly = 1 << 20, EatsFish = 1 << 30, Endangered = std::uint64_t{1} << 40 };
30 // Add specialization `is_flags` to define that enum are flags.
31 template <>
33  static constexpr bool is_flags = true;
34 };
35 
36 int main() {
37  // Enum-flags variable to string name.
39  auto f1_name = magic_enum::enum_name(f1);
40  std::cout << f1_name << std::endl; // Endangered
41 
42  // String enum-flags name sequence.
43  constexpr auto names = magic_enum::enum_names<AnimalFlags>();
44  std::cout << "AnimalFlags names:";
45  for (const auto& n : names) {
46  std::cout << " " << n;
47  }
48  std::cout << std::endl;
49  // AnimalFlags names: HasClaws CanFly EatsFish Endangered
50 
51  // String name to enum-flags value.
52  auto f2 = magic_enum::enum_flags_cast<AnimalFlags>("EatsFish|CanFly");
53  if (f2.has_value()) {
54  std::cout << "EatsFish|CanFly = " << magic_enum::enum_integer(f2.value()) << std::endl; // CanFly|EatsFish = 1074790400
55  }
56 
57  // Integer value to enum-flags value.
58  auto f3 = magic_enum::enum_cast<AnimalFlags>(1073742848);
59  if (f3.has_value()) {
60  std::cout << magic_enum::enum_flags_name(f3.value()) << " = " << magic_enum::enum_integer(f3.value()) << std::endl; // HasClaws|EatsFish = 1073742848
61  }
62 
63  // Enum-flags value to integer value.
65  std::cout << "HasClaws = " << f4_integer << std::endl; // HasClaws = 1024
66 
67  using magic_enum::iostream_operators::operator<<; // out-of-the-box ostream operator for enum-flags.
68  // Ostream operator for enum-flags.
69  std::cout << f1 << " " << f2 << " " << f3 << std::endl; // Endangered CanFly|EatsFish HasClaws|EatsFish
70 
71  // Number of enum-flags values.
72  std::cout << "AnimalFlags enum size: " << magic_enum::enum_count<AnimalFlags>() << std::endl; // AnimalFlags enum size: 4
73 
74  // Indexed access to enum-flags value.
75  std::cout << "AnimalFlags[0] = " << magic_enum::enum_value<AnimalFlags>(0) << std::endl; // AnimalFlags[0] = HasClaws
76 
77  // Enum-flags value sequence.
78  constexpr auto values = magic_enum::enum_values<AnimalFlags>();
79  std::cout << "AnimalFlags values:";
80  for (const auto f : values) {
81  std::cout << " " << f; // Ostream operator for enum-flags.
82  }
83  std::cout << std::endl;
84  // AnimalFlags values: HasClaws CanFly EatsFish Endangered
85 
86  using namespace magic_enum::bitwise_operators; // out-of-the-box bitwise operators for all enums.
87  // Support operators: ~, |, &, ^, |=, &=, ^=.
89  std::cout << flag << std::endl; // HasClaws|CanFly
90 
91  // Enum-flags pair (value, string name) sequence.
92  constexpr auto entries = magic_enum::enum_entries<AnimalFlags>();
93  std::cout << "AnimalFlags entries:";
94  for (const auto& e : entries) {
95  std::cout << " " << e.second << " = " << magic_enum::enum_integer(e.first);
96  }
97  std::cout << std::endl;
98  // AnimalFlags entries: AnimalFlags entries: HasClaws = 1024 CanFly = 1048576 EatsFish = 1073741824 Endangered = 1099511627776
99 
100  return 0;
101 }
magic_enum::bitwise_operators
Definition: magic_enum.hpp:1464
main
int main()
Definition: enum_flag_example.cpp:36
magic_enum.hpp
magic_enum::enum_flags_name
auto enum_flags_name(E value, char_type sep=static_cast< char_type >('|')) -> detail::enable_if_t< E, string >
Definition: magic_enum_flags.hpp:67
magic_enum::detail::names
constexpr auto names(std::index_sequence< I... >) noexcept
Definition: magic_enum.hpp:842
magic_enum_iostream.hpp
AnimalFlags::HasClaws
@ HasClaws
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
AnimalFlags::EatsFish
@ EatsFish
AnimalFlags
AnimalFlags
Definition: enum_flag_example.cpp:29
magic_enum::detail::n
constexpr auto n() noexcept
Definition: magic_enum.hpp:421
magic_enum::customize::enum_range
Definition: magic_enum.hpp:172
AnimalFlags::CanFly
@ CanFly
AnimalFlags::Endangered
@ Endangered
magic_enum::enum_name
constexpr auto enum_name() noexcept -> detail::enable_if_t< decltype(V), string_view >
Definition: magic_enum.hpp:1290
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