1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00
This commit is contained in:
neargye 2020-07-05 01:29:21 +05:00
parent 25912781ee
commit 81f2888125
4 changed files with 315 additions and 34 deletions

View file

@ -9,8 +9,13 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
endif()
endif()
add_executable(example example.cpp)
set_target_properties(example PROPERTIES CXX_EXTENSIONS OFF)
target_compile_features(example PRIVATE cxx_std_17)
target_compile_options(example PRIVATE ${OPTIONS})
target_link_libraries(example PRIVATE ${CMAKE_PROJECT_NAME})
function(make_example target)
add_executable(${target} ${target}.cpp)
set_target_properties(${target} PROPERTIES CXX_EXTENSIONS OFF)
target_compile_features(${target} PRIVATE cxx_std_17)
target_compile_options(${target} PRIVATE ${OPTIONS})
target_link_libraries(${target} PRIVATE ${CMAKE_PROJECT_NAME})
endfunction()
make_example(example)
make_example(enum_flag_example)

View file

@ -0,0 +1,100 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2020 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <iostream>
#include <magic_enum.hpp>
enum class AnimalFlags : std::uint64_t { HasClaws = 1 << 1, CanFly = 1 << 2, EatsFish = 1 << 20, Endangered = std::uint64_t{1} << 40 };
int main() {
// Enum variable to string name.
AnimalFlags f1 = AnimalFlags::CanFly;
auto f1_name = magic_enum::flag::enum_name(f1);
std::cout << f1_name << std::endl; // CanFly
// String enum name sequence.
constexpr auto names = magic_enum::flag::enum_names<AnimalFlags>();
std::cout << "AnimalFlags names:";
for (auto n : names) {
std::cout << " " << n;
}
std::cout << std::endl;
// AnimalFlags names: HasClaws CanFly EatsFish Endangered
#if 0
// String name to enum value.
auto f2 = magic_enum::flag::enum_cast<AnimalFlags>("EatsFish");
if (f2.has_value() && f2.value() == AnimalFlags::EatsFish) {
std::cout << "EatsFish = " << magic_enum::flag::enum_integer(f2.value()) << std::endl; // EatsFish = 4
}
// Integer value to enum value.
auto f3 = magic_enum::flag::enum_cast<AnimalFlags>(8);
if (f3.has_value() && f3.value() == AnimalFlags::Endangered) {
std::cout << "Endangered = " << magic_enum::flag::enum_integer(f3.value()) << std::endl; // Endangered = 8
}
#endif
// Enum value to integer value.
auto f4_integer = magic_enum::flag::enum_integer(AnimalFlags::HasClaws);
if (f4_integer == static_cast<std::underlying_type_t<AnimalFlags>>(AnimalFlags::HasClaws)) {
std::cout << "HasClaws = " << f4_integer << std::endl; // HasClaws = 2
}
using namespace magic_enum::flag::ostream_operators; // out-of-the-box ostream operator for all enums.
#if 0
// ostream operator for enum.
std::cout << f1 << " " << f2 << " " << f3 << std::endl; // CanFly EatsFish Endangered
#endif
// Number of enum values.
std::cout << "AnimalFlags enum size: " << magic_enum::flag::enum_count<AnimalFlags>() << std::endl; // AnimalFlags enum size: 4
// Indexed access to enum value.
std::cout << "AnimalFlags[0] = " << magic_enum::flag::enum_value<AnimalFlags>(0) << std::endl; // AnimalFlags[0] = HasClaws
// Enum value sequence.
constexpr auto values = magic_enum::flag::enum_values<AnimalFlags>();
std::cout << "AnimalFlags values:";
for (AnimalFlags f : values) {
std::cout << " " << f; // ostream operator for enum.
}
std::cout << std::endl;
// AnimalFlags sequence: HasClaws CanFly EatsFish Endangered
using namespace magic_enum::flag::bitwise_operators; // out-of-the-box bitwise operators for all enums.
// Support operators: ~, |, &, ^, |=, &=, ^=.
AnimalFlags flag = AnimalFlags::HasClaws | AnimalFlags::CanFly;
std::cout << flag << std::endl; // HasClaws | CanFly
// Enum pair (value enum, string enum name) sequence.
constexpr auto entries = magic_enum::flag::enum_entries<AnimalFlags>();
std::cout << "AnimalFlags entries:";
for (auto e : entries) {
std::cout << " " << e.second << " = " << magic_enum::flag::enum_integer(e.first);
}
std::cout << std::endl;
// AnimalFlags entries: AnimalFlags entries: HasClaws = 2 CanFly = 4 EatsFish = 1048576 Endangered = 1099511627776
return 0;
}

View file

@ -39,9 +39,9 @@ int main() {
std::cout << c1_name << std::endl; // RED
// String enum name sequence.
constexpr auto color_names = magic_enum::enum_names<Color>();
constexpr auto names = magic_enum::enum_names<Color>();
std::cout << "Color names:";
for (auto n : color_names) {
for (auto n : names) {
std::cout << " " << n;
}
std::cout << std::endl;
@ -60,9 +60,9 @@ int main() {
}
// Enum value to integer value.
auto color_integer = magic_enum::enum_integer(c1);
if (color_integer == to_integer(Color::RED)) {
std::cout << "RED = " << color_integer << std::endl; // RED = -10
auto c4_integer = magic_enum::enum_integer(Color::RED);
if (c4_integer == static_cast<std::underlying_type_t<Color>>(Color::RED)) {
std::cout << "RED = " << c4_integer << std::endl; // RED = -10
}
using namespace magic_enum::ostream_operators; // out-of-the-box ostream operator for all enums.
@ -76,9 +76,9 @@ int main() {
std::cout << "Color[0] = " << magic_enum::enum_value<Color>(0) << std::endl; // Color[0] = RED
// Enum value sequence.
constexpr auto colors = magic_enum::enum_values<Color>();
std::cout << "Colors sequence:";
for (Color c : colors) {
constexpr auto values = magic_enum::enum_values<Color>();
std::cout << "Colors values:";
for (Color c : values) {
std::cout << " " << c; // ostream operator for enum.
}
std::cout << std::endl;
@ -87,8 +87,8 @@ int main() {
enum class Flags { A = 1, B = 2, C = 4, D = 8 };
using namespace magic_enum::bitwise_operators; // out-of-the-box bitwise operators for all enums.
// Support operators: ~, |, &, ^, |=, &=, ^=.
Flags flags = Flags::A | Flags::C;
std::cout << flags << std::endl;
Flags flag = Flags::A | Flags::C;
std::cout << flag << std::endl;
enum color { red, green, blue };
@ -103,9 +103,9 @@ int main() {
static_assert(magic_enum::is_scoped_enum_v<Flags>);
// Enum pair (value enum, string enum name) sequence.
constexpr auto color_entries = magic_enum::enum_entries<Color>();
constexpr auto entries = magic_enum::enum_entries<Color>();
std::cout << "Colors entries:";
for (auto e : color_entries) {
for (auto e : entries) {
std::cout << " " << e.second << " = " << static_cast<int>(e.first);
}
std::cout << std::endl;