1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00
Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code
Find a file
2019-04-06 18:16:22 +05:00
example v0.1.2 2019-04-06 18:15:25 +05:00
include add doc 2019-04-06 18:16:22 +05:00
test update test 2019-04-06 18:16:14 +05:00
.appveyor.yml v0.1.0 2019-03-31 02:44:47 +05:00
.gitignore v0.1.0 2019-03-31 02:44:47 +05:00
.travis.yml gcc-9 yeat unstable 2019-04-02 12:38:30 +05:00
CMakeLists.txt v0.1.1 2019-04-02 17:33:51 +05:00
LICENSE v0.1.0 2019-03-31 02:44:47 +05:00
README.md update doc 2019-04-04 11:01:17 +05:00

Magic Enum C++

 __  __             _        ______                          _____
|  \/  |           (_)      |  ____|                        / ____|_     _
| \  / | __ _  __ _ _  ___  | |__   _ __  _   _ _ __ ___   | |   _| |_ _| |_
| |\/| |/ _` |/ _` | |/ __| |  __| | '_ \| | | | '_ ` _ \  | |  |_   _|_   _|
| |  | | (_| | (_| | | (__  | |____| | | | |_| | | | | | | | |____|_|   |_|
|_|  |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_|  \_____|
               __/ |
              |___/

Github Releases License Build Status Build status Codacy Badge Try online

What is Magic Enum?

Header-only C++17 library provides Enum-to-String and String-to-Enum functions.

  • magic_enum::enum_to_string obtains string enum name from enum variable.
  • magic_enum::enum_from_string obtains enum variable from enum string name.

Features

  • C++17
  • Header-only
  • Dependency-free
  • Compile-time
  • Enum to string
  • String to enum

Examples

  • Enum variable to string enum name

    auto color = Color::RED;
    auto color_name = magic_enum::enum_to_string(color);
    if (color_name.has_value()) {
      // color_name.value() -> "RED"
    }
    
  • Static storage enum variable to string enum name

    constexpr auto color = Color::BLUE;
    constexpr auto color_name = magic_enum::enum_to_string<color>();
    if (color_name.has_value()) {
      // color_name.value() -> "BLUE"
    }
    
  • String enum name to enum variable

    constexpr auto color = magic_enum::enum_from_string<Color>("GREEN");
    if (color.has_value()) {
      // color.value() -> Color::GREEN
    }
    

Remarks

  • magic_enum::enum_to_string returns std::optional<std::string_view>, using has_value() to check contains enum name and value() to get the enum name. If enum variable does not have name or out of range MAGIC_ENUM_RANGE, returns std::nullopt.

  • magic_enum::enum_from_string returns std::optional<E>, using has_value() to check contains enum variable and value() to get the enum variable. If enum variable does not have name or out of range MAGIC_ENUM_RANGE, returns std::nullopt.

  • Enum variable must be in range (-MAGIC_ENUM_RANGE, MAGIC_ENUM_RANGE). By default MAGIC_ENUM_RANGE = 128. If you need larger range, redefine the macro MAGIC_ENUM_RANGE.

    #define MAGIC_ENUM_RANGE 1028 // Redefine MAGIC_ENUM_RANGE for larger range.
    #include <magic_enum.hpp>
    

Integration

You should add the required file magic_enum.hpp.

Compiler compatibility

  • Clang/LLVM >= 5
  • Visual C++ >= 15.3 / Visual Studio >= 2017
  • Xcode >= 10.2
  • GCC >= 9

Licensed under the MIT License