v01
This commit is contained in:
22
thirdparty/magic_enum/example/CMakeLists.txt
vendored
Normal file
22
thirdparty/magic_enum/example/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
include(CheckCXXCompilerFlag)
|
||||
|
||||
if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
|
||||
set(OPTIONS -Wall -Wextra -pedantic-errors -Werror)
|
||||
elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
|
||||
set(OPTIONS /W4 /WX)
|
||||
if(HAS_PERMISSIVE_FLAG)
|
||||
set(OPTIONS ${OPTIONS} /permissive-)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
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)
|
||||
make_example(example_custom_name)
|
||||
95
thirdparty/magic_enum/example/enum_flag_example.cpp
vendored
Normal file
95
thirdparty/magic_enum/example/enum_flag_example.cpp
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2019 - 2021 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 <string>
|
||||
|
||||
#include <magic_enum.hpp>
|
||||
|
||||
enum class AnimalFlags : std::uint64_t { HasClaws = 1 << 10, CanFly = 1 << 20, EatsFish = 1 << 30, Endangered = std::uint64_t{1} << 40 };
|
||||
|
||||
int main() {
|
||||
// Enum-flags variable to string name.
|
||||
AnimalFlags f1 = AnimalFlags::Endangered;
|
||||
auto f1_name = magic_enum::flags::enum_name(f1);
|
||||
std::cout << f1_name << std::endl; // Endangered
|
||||
|
||||
// String enum-flags name sequence.
|
||||
constexpr auto& names = magic_enum::flags::enum_names<AnimalFlags>();
|
||||
std::cout << "AnimalFlags names:";
|
||||
for (const auto& n : names) {
|
||||
std::cout << " " << n;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
// AnimalFlags names: HasClaws CanFly EatsFish Endangered
|
||||
|
||||
// String name to enum-flags value.
|
||||
auto f2 = magic_enum::flags::enum_cast<AnimalFlags>("EatsFish|CanFly");
|
||||
if (f2.has_value()) {
|
||||
std::cout << "EatsFish = " << magic_enum::flags::enum_integer(f2.value()) << std::endl; // CanFly|EatsFish = 1074790400
|
||||
}
|
||||
|
||||
// Integer value to enum-flags value.
|
||||
auto f3 = magic_enum::flags::enum_cast<AnimalFlags>(1073742848);
|
||||
if (f3.has_value()) {
|
||||
std::cout << magic_enum::flags::enum_name(f3.value()) << " = " << magic_enum::flags::enum_integer(f3.value()) << std::endl; // HasClaws|EatsFish = 1073742848
|
||||
}
|
||||
|
||||
// Enum-flags value to integer value.
|
||||
auto f4_integer = magic_enum::flags::enum_integer(AnimalFlags::HasClaws);
|
||||
std::cout << "HasClaws = " << f4_integer << std::endl; // HasClaws = 1024
|
||||
|
||||
using namespace magic_enum::flags::ostream_operators; // out-of-the-box ostream operator for enum-flags.
|
||||
// Ostream operator for enum-flags.
|
||||
std::cout << f1 << " " << f2 << " " << f3 << std::endl; // Endangered CanFly|EatsFish HasClaws|EatsFish
|
||||
|
||||
// Number of enum-flags values.
|
||||
std::cout << "AnimalFlags enum size: " << magic_enum::flags::enum_count<AnimalFlags>() << std::endl; // AnimalFlags enum size: 4
|
||||
|
||||
// Indexed access to enum-flags value.
|
||||
std::cout << "AnimalFlags[0] = " << magic_enum::flags::enum_value<AnimalFlags>(0) << std::endl; // AnimalFlags[0] = HasClaws
|
||||
|
||||
// Enum-flags value sequence.
|
||||
constexpr auto& values = magic_enum::flags::enum_values<AnimalFlags>();
|
||||
std::cout << "AnimalFlags values:";
|
||||
for (const auto& f : values) {
|
||||
std::cout << " " << f; // Ostream operator for enum-flags.
|
||||
}
|
||||
std::cout << std::endl;
|
||||
// AnimalFlags sequence: HasClaws CanFly EatsFish Endangered
|
||||
|
||||
using namespace magic_enum::flags::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-flags pair (value, string name) sequence.
|
||||
constexpr auto& entries = magic_enum::flags::enum_entries<AnimalFlags>();
|
||||
std::cout << "AnimalFlags entries:";
|
||||
for (const auto& e : entries) {
|
||||
std::cout << " " << e.second << " = " << magic_enum::flags::enum_integer(e.first);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
// AnimalFlags entries: AnimalFlags entries: HasClaws = 1024 CanFly = 1048576 EatsFish = 1073741824 Endangered = 1099511627776
|
||||
|
||||
return 0;
|
||||
}
|
||||
113
thirdparty/magic_enum/example/example.cpp
vendored
Normal file
113
thirdparty/magic_enum/example/example.cpp
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2019 - 2021 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 Color : int { RED = -10, BLUE = 0, GREEN = 10 };
|
||||
|
||||
template <typename E>
|
||||
auto to_integer(magic_enum::Enum<E> value) {
|
||||
// magic_enum::Enum<E> - C++17 Concept for enum type.
|
||||
return static_cast<magic_enum::underlying_type_t<E>>(value);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Enum variable to string name.
|
||||
Color c1 = Color::RED;
|
||||
auto c1_name = magic_enum::enum_name(c1);
|
||||
std::cout << c1_name << std::endl; // RED
|
||||
|
||||
// String enum name sequence.
|
||||
constexpr auto& names = magic_enum::enum_names<Color>();
|
||||
std::cout << "Color names:";
|
||||
for (const auto& n : names) {
|
||||
std::cout << " " << n;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
// Color names: RED BLUE GREEN
|
||||
|
||||
// String name to enum value.
|
||||
auto c2 = magic_enum::enum_cast<Color>("BLUE");
|
||||
if (c2.has_value()) {
|
||||
std::cout << "BLUE = " << to_integer(c2.value()) << std::endl; // BLUE = 0
|
||||
}
|
||||
|
||||
// Integer value to enum value.
|
||||
auto c3 = magic_enum::enum_cast<Color>(10);
|
||||
if (c3.has_value()) {
|
||||
std::cout << "GREEN = " << magic_enum::enum_integer(c3.value()) << std::endl; // GREEN = 10
|
||||
}
|
||||
|
||||
// Enum value to integer value.
|
||||
auto c4_integer = magic_enum::enum_integer(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.
|
||||
// Ostream operator for enum.
|
||||
std::cout << "Color: " << c1 << " " << c2 << " " << c3 << std::endl; // Color: RED BLUE GREEN
|
||||
|
||||
// Number of enum values.
|
||||
std::cout << "Color enum size: " << magic_enum::enum_count<Color>() << std::endl; // Color size: 3
|
||||
|
||||
// Indexed access to enum value.
|
||||
std::cout << "Color[0] = " << magic_enum::enum_value<Color>(0) << std::endl; // Color[0] = RED
|
||||
|
||||
// Enum value sequence.
|
||||
constexpr auto& values = magic_enum::enum_values<Color>();
|
||||
std::cout << "Colors values:";
|
||||
for (const auto& c : values) {
|
||||
std::cout << " " << c; // Ostream operator for enum.
|
||||
}
|
||||
std::cout << std::endl;
|
||||
// Color sequence: RED BLUE GREEN
|
||||
|
||||
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 flag = Flags::A | Flags::C;
|
||||
std::cout << flag << std::endl; // 5
|
||||
|
||||
enum color { red, green, blue };
|
||||
|
||||
// Checks whether type is an Unscoped enumeration.
|
||||
static_assert(magic_enum::is_unscoped_enum_v<color>);
|
||||
static_assert(!magic_enum::is_unscoped_enum_v<Color>);
|
||||
static_assert(!magic_enum::is_unscoped_enum_v<Flags>);
|
||||
|
||||
// Checks whether type is an Scoped enumeration.
|
||||
static_assert(!magic_enum::is_scoped_enum_v<color>);
|
||||
static_assert(magic_enum::is_scoped_enum_v<Color>);
|
||||
static_assert(magic_enum::is_scoped_enum_v<Flags>);
|
||||
|
||||
// Enum pair (value enum, string enum name) sequence.
|
||||
constexpr auto& entries = magic_enum::enum_entries<Color>();
|
||||
std::cout << "Colors entries:";
|
||||
for (const auto& e : entries) {
|
||||
std::cout << " " << e.second << " = " << static_cast<int>(e.first);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
// Color entries: RED = -10 BLUE = 0 GREEN = 10
|
||||
|
||||
return 0;
|
||||
}
|
||||
71
thirdparty/magic_enum/example/example_custom_name.cpp
vendored
Normal file
71
thirdparty/magic_enum/example/example_custom_name.cpp
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2020 - 2021 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 Color : int { RED = -10, BLUE = 0, GREEN = 10 };
|
||||
|
||||
// Сustom definitions of names for enum.
|
||||
// Specialization of `enum_name` must be injected in `namespace magic_enum::customize`.
|
||||
template <>
|
||||
constexpr std::string_view magic_enum::customize::enum_name<Color>(Color value) noexcept {
|
||||
switch (value) {
|
||||
case Color::RED:
|
||||
return "the red color";
|
||||
case Color::BLUE:
|
||||
return "The BLUE";
|
||||
case Color::GREEN:
|
||||
return {}; // Empty string for default value.
|
||||
}
|
||||
return {}; // Empty string for unknow value.
|
||||
}
|
||||
|
||||
enum class Numbers : int { One, Two, Three };
|
||||
|
||||
// Сustom definitions of names for enum.
|
||||
// Specialization of `enum_name` must be injected in `namespace magic_enum::customize`.
|
||||
template <>
|
||||
constexpr std::string_view magic_enum::customize::enum_name<Numbers>(Numbers value) noexcept {
|
||||
switch (value) {
|
||||
case Numbers::One:
|
||||
return "the one";
|
||||
default:
|
||||
return {}; // Empty string for default or unknow value.
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << magic_enum::enum_name(Color::RED) << std::endl; // the red color
|
||||
std::cout << magic_enum::enum_name(Color::BLUE) << std::endl; // The BLUE
|
||||
std::cout << magic_enum::enum_name(Color::GREEN) << std::endl; // GREEN
|
||||
|
||||
std::cout << std::boolalpha;
|
||||
std::cout << (magic_enum::enum_cast<Color>("the red color").value() == Color::RED) << std::endl; // true
|
||||
|
||||
std::cout << magic_enum::enum_name(Numbers::One) << std::endl; // the one
|
||||
std::cout << magic_enum::enum_name(Numbers::Two) << std::endl; // Two
|
||||
std::cout << magic_enum::enum_name(Numbers::Three) << std::endl; // Three
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user