example_containers_array.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 
27 
28 enum class Color { RED = 1, GREEN = 2, BLUE = 4 };
29 template <>
31  static constexpr bool is_flags = true;
32 };
33 
34 struct RGB {
35 
36  std::uint8_t r {};
37  std::uint8_t g {};
38  std::uint8_t b {};
39 
40  [[nodiscard]] constexpr bool empty() { return std::equal_to{}(r, g) && std::equal_to{}(g, b) && std::equal_to{}(b, 0); }
41 
42  [[nodiscard]] constexpr bool operator==(RGB rgb) const noexcept { return std::equal_to{}(r, rgb.r) && std::equal_to{}(g, rgb.g) && std::equal_to{}(b, rgb.b); }
43 
44  friend std::ostream& operator<<(std::ostream& ostream, RGB rgb) {
45  ostream << "R=" << static_cast<std::uint32_t>(rgb.r) << " G=" << static_cast<std::uint32_t>(rgb.g) << " B=" << static_cast<std::uint32_t>(rgb.b);
46  return ostream;
47  }
48 };
49 
50 constexpr std::uint8_t color_max = std::numeric_limits<std::uint8_t>::max();
51 
52 int main() {
53 
54  constexpr magic_enum::containers::array<Color, RGB> color_rgb_initializer {{{{color_max, 0, 0}, {0, color_max, 0}, {0, 0, color_max}}}};
55 
56  std::cout << magic_enum::containers::get<0>(color_rgb_initializer) << std::endl; // R=255 G=0 B=0
57  std::cout << magic_enum::containers::get<1>(color_rgb_initializer) << std::endl; // R=0 G=255 B=0
58  std::cout << magic_enum::containers::get<2>(color_rgb_initializer) << std::endl; // R=0 G=0 B=255
59 
60  std::cout << magic_enum::containers::get<Color::RED>(color_rgb_initializer) << std::endl; // R=255 G=0 B=0
61  std::cout << magic_enum::containers::get<Color::GREEN>(color_rgb_initializer) << std::endl; // R=0 G=255 B=0
62  std::cout << magic_enum::containers::get<Color::BLUE>(color_rgb_initializer) << std::endl; // R=0 G=0 B=255
63 
64  return 0;
65 }
RGB
Definition: example_containers_array.cpp:34
Color::GREEN
@ GREEN
magic_enum::containers::array
Definition: magic_enum_containers.hpp:335
RGB::operator==
constexpr bool operator==(RGB rgb) const noexcept
Definition: example_containers_array.cpp:42
RGB::operator<<
friend std::ostream & operator<<(std::ostream &ostream, RGB rgb)
Definition: example_containers_array.cpp:44
RGB::r
std::uint8_t r
Definition: example_containers_array.cpp:36
magic_enum_containers.hpp
RGB::b
std::uint8_t b
Definition: example_containers_array.cpp:38
Color::RED
@ RED
magic_enum::customize::enum_range
Definition: magic_enum.hpp:172
color_max
constexpr std::uint8_t color_max
Definition: example_containers_array.cpp:50
Color
Color
Definition: example.cpp:28
RGB::empty
constexpr bool empty()
Definition: example_containers_array.cpp:40
main
int main()
Definition: example_containers_array.cpp:52
Color::BLUE
@ BLUE
RGB::g
std::uint8_t g
Definition: example_containers_array.cpp:37


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