example_switch.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 // Copyright (c) 2022 - 2023 Bela Schaum <schaumb@gmail.com>.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 
24 #include <iostream>
25 
26 #define MAGIC_ENUM_ENABLE_HASH
28 
29 enum class Color { RED, BLUE, GREEN };
30 
31 template <Color C>
32 constexpr std::string_view DoWork() {
33  return "default";
34 }
35 
36 template <>
37 constexpr std::string_view DoWork<Color::GREEN>() {
38  return "override";
39 }
40 
41 // Helper type for the visitor pattern.
42 template <typename... Ts> struct overloaded : Ts... { using Ts::operator()...; };
43 template <typename... Ts> overloaded(Ts...) -> overloaded<Ts...>;
44 
45 int main() {
46  Color c = Color::RED;
47 
48  auto lambda = [] (auto value) {
49  std::cout << DoWork<value>() << std::endl;
50  };
51 
52  magic_enum::enum_switch(lambda, c); // prints "default"
53 
54  c = Color::GREEN;
55 
56  magic_enum::enum_switch(lambda, c); // prints "override"
57 
58  // with object, explicit enum type
59  auto switcher1 = overloaded{
61  std::cout << "Blue" << std::endl;
62  },
64  std::cout << "Red" << std::endl;
65  }
66  };
67 
68  magic_enum::enum_switch(switcher1, Color::GREEN); // prints nothing
69  magic_enum::enum_switch(switcher1, Color::BLUE); // prints "Blue"
70  magic_enum::enum_switch(switcher1, Color::RED); // prints "Red"
71 
72  // explicit result type
73  auto switcher2 = overloaded{
75  return "called with green argument";
76  },
77  [] (Color other) { // default case
78  auto name = magic_enum::enum_name(other); // not empty
79  return "default: " + std::string{name};
80  }
81  };
82 
83  std::cout << magic_enum::enum_switch<std::string>(switcher2, Color::GREEN) << std::endl; // prints "called with green argument"
84  std::cout << magic_enum::enum_switch<std::string>(switcher2, Color::RED) << std::endl; // prints "default: RED"
85 
86  auto empty = magic_enum::enum_switch<std::string>(switcher2, static_cast<Color>(-3)); // returns an empty string
87  assert(empty.empty());
88 
89  // result with default object
90  std::cout << magic_enum::enum_switch<std::string>(switcher2, static_cast<Color>(-3), "unrecognized") << std::endl; // prints "unrecognized"
91 
92  auto switcher3 = overloaded{
93  [] (magic_enum::enum_constant<Color::RED>) -> std::optional<std::string> {
94  return "red result";
95  },
96  [] (magic_enum::enum_constant<Color::BLUE>) -> std::optional<std::string> {
97  return std::nullopt;
98  }
99  };
100 
101  std::cout << std::boolalpha;
102  std::cout << magic_enum::enum_switch(switcher3, Color::GREEN, std::make_optional("cica")).value() << std::endl; // prints default: "cica"
103  std::cout << magic_enum::enum_switch(switcher3, Color::RED, std::make_optional("cica")).value() << std::endl; // prints: "red result"
104  std::cout << magic_enum::enum_switch(switcher3, Color::BLUE, std::make_optional("cica")).has_value() << std::endl; // prints: false
105 
106  return 0;
107 }
Color::GREEN
@ GREEN
magic_enum::detail::value
constexpr E value(std::size_t i) noexcept
Definition: magic_enum.hpp:679
magic_enum_switch.hpp
Catch::cout
std::ostream & cout()
DoWork
constexpr std::string_view DoWork()
Definition: example_switch.cpp:32
Color::RED
@ RED
magic_enum::enum_switch
constexpr decltype(auto) enum_switch(F &&f, E value)
Definition: magic_enum_switch.hpp:137
overloaded
overloaded(Ts...) -> overloaded< Ts... >
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
main
int main()
Definition: example_switch.cpp:45
Color::BLUE
@ BLUE
overloaded
Definition: example_switch.cpp:42
magic_enum::enum_constant
detail::enum_constant< V > enum_constant
Definition: magic_enum.hpp:1171


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