All Projects → Neargye → Magic_enum

Neargye / Magic_enum

Licence: mit
Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to Magic enum

Nameof
Nameof operator for modern C++, simply obtain the name of a variable, type, function, macro, and enum
Stars: ✭ 1,017 (-56.54%)
Mutual labels:  serialization, no-dependencies
Configuration
A module to help other modules have settings
Stars: ✭ 135 (-94.23%)
Mutual labels:  serialization
Awesome Python Models
A curated list of awesome Python libraries, which implement models, schemas, serializers/deserializers, ODM's/ORM's, Active Records or similar patterns.
Stars: ✭ 124 (-94.7%)
Mutual labels:  serialization
Pyjson tricks
Extra features for Python's JSON: comments, order, numpy, pandas, datetimes, and many more! Simple but customizable.
Stars: ✭ 131 (-94.4%)
Mutual labels:  serialization
Eneida
Experimental demoscene project using C99 and Direct3D 12. Fully standalone with custom windows, and d3d12 headers. No Windows SDK, libc or Visual Studio dependency. C and HLSL compilers included.
Stars: ✭ 125 (-94.66%)
Mutual labels:  no-dependencies
Hprose Js
Hprose is a cross-language RPC. This project is Hprose 2.0 RPC for JavaScript
Stars: ✭ 133 (-94.32%)
Mutual labels:  serialization
Rust Protobuf
Rust implementation of Google protocol buffers
Stars: ✭ 1,797 (-23.21%)
Mutual labels:  serialization
Climake
The simplistic, dependency-free cli library ✨
Stars: ✭ 139 (-94.06%)
Mutual labels:  no-dependencies
Kotlinmultiplatform mvvm
Android & iOS App using MVVM pattern and LiveData on the presentation layer + Clean Arch on the common shared code.
Stars: ✭ 135 (-94.23%)
Mutual labels:  serialization
Borer
Efficient CBOR and JSON (de)serialization in Scala
Stars: ✭ 131 (-94.4%)
Mutual labels:  serialization
Ocaml Protoc
A Protobuf Compiler for OCaml
Stars: ✭ 129 (-94.49%)
Mutual labels:  serialization
Json
Lighter and Faster Json Serialization tool.
Stars: ✭ 128 (-94.53%)
Mutual labels:  serialization
Flatsharp
Fast, idiomatic C# implementation of Flatbuffers
Stars: ✭ 133 (-94.32%)
Mutual labels:  serialization
Yaxlib
Yet Another XML Serialization Library for the .NET Framework and .NET Core
Stars: ✭ 124 (-94.7%)
Mutual labels:  serialization
Deku
Declarative binary reading and writing: bit-level, symmetric, serialization/deserialization
Stars: ✭ 136 (-94.19%)
Mutual labels:  serialization
Yyjson
The fastest JSON library in C
Stars: ✭ 1,894 (-19.06%)
Mutual labels:  serialization
Protostuff
Java serialization library, proto compiler, code generator
Stars: ✭ 1,730 (-26.07%)
Mutual labels:  serialization
Jstp
Fast RPC for browser and Node.js based on TCP, WebSocket, and MDSF
Stars: ✭ 132 (-94.36%)
Mutual labels:  serialization
Noproto
Flexible, Fast & Compact Serialization with RPC
Stars: ✭ 138 (-94.1%)
Mutual labels:  serialization
Azos
A to Z Sky Operating System / Microservice Chassis Framework
Stars: ✭ 137 (-94.15%)
Mutual labels:  serialization
 __  __             _        ______                          _____
|  \/  |           (_)      |  ____|                        / ____|_     _
| \  / | __ _  __ _ _  ___  | |__   _ __  _   _ _ __ ___   | |   _| |_ _| |_
| |\/| |/ _` |/ _` | |/ __| |  __| | '_ \| | | | '_ ` _ \  | |  |_   _|_   _|
| |  | | (_| | (_| | | (__  | |____| | | | |_| | | | | | | | |____|_|   |_|
|_|  |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_|  \_____|
               __/ |
              |___/

Github releases Conan package Vcpkg package Build2 package License Try online Compiler explorer

Magic Enum C++

Header-only C++17 library provides static reflection for enums, work with any enum type without any macro or boilerplate code.

  • enum_cast obtains enum value from string or integer.
  • enum_value returns enum value at specified index.
  • enum_values obtains enum value sequence.
  • enum_count returns number of enum values.
  • enum_integer obtains integer value from enum value.
  • enum_name returns name from enum value.
  • enum_names obtains string enum name sequence.
  • enum_entries obtains pair (value enum, string enum name) sequence.
  • enum_index obtains index in enum value sequence from enum value.
  • enum_contains checks whether enum contains enumerator with such value.
  • enum_type_name returns name of enum type.
  • is_unscoped_enum checks whether type is an Unscoped enumeration.
  • is_scoped_enum checks whether type is an Scoped enumeration.
  • underlying_type improved UB-free "SFINAE-friendly" underlying_type.
  • ostream_operators ostream operators for enums.
  • bitwise_operators bitwise operators for enums.

Documentation

Features

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

Examples

// For example color enum.
enum class Color { RED = 2, BLUE = 4, GREEN = 8 };
  • Enum value to string

    Color color = Color::RED;
    auto color_name = magic_enum::enum_name(color);
    // color_name -> "RED"
  • String to enum value

    std::string color_name{"GREEN"};
    auto color = magic_enum::enum_cast<Color>(color_name);
    if (color.has_value()) {
      // color.value() -> Color::GREEN
    }
  • Integer to enum value

    int color_integer = 2;
    auto color = magic_enum::enum_cast<Color>(color_integer);
    if (color.has_value()) {
      // color.value() -> Color::RED
    }
  • Indexed access to enum value

    std::size_t i = 1;
    Color color = magic_enum::enum_value<Color>(i);
    // color -> Color::BLUE
  • Enum value sequence

    constexpr auto colors = magic_enum::enum_values<Color>();
    // colors -> {Color::RED, Color::BLUE, Color::GREEN}
    // colors[0] -> Color::RED
  • Number of enum elements

    constexpr std::size_t color_count = magic_enum::enum_count<Color>();
    // color_count -> 3
  • Enum value to integer

    Color color = Color::RED;
    auto color_integer = magic_enum::enum_integer(color);
    // color -> 2
  • Enum names sequence

    constexpr auto color_names = magic_enum::enum_names<Color>();
    // color_names -> {"RED", "BLUE", "GREEN"}
    // color_names[0] -> "RED"
  • Enum entries sequence

    constexpr auto color_entries = magic_enum::enum_entries<Color>();
    // color_entries -> {{Color::RED, "RED"}, {Color::BLUE, "BLUE"}, {Color::GREEN, "GREEN"}}
    // color_entries[0].first -> Color::RED
    // color_entries[0].second -> "RED"
  • Ostream operator for enum

    using namespace magic_enum::ostream_operators; // out-of-the-box ostream operators for enums.
    Color color = Color::BLUE;
    std::cout << color << std::endl; // "BLUE"
  • Bitwise operator for enum

    enum class Flags { A = 1 << 0, B = 1 << 1, C = 1 << 2, D = 1 << 3 };
    using namespace magic_enum::bitwise_operators; // out-of-the-box bitwise operators for enums.
    // Support operators: ~, |, &, ^, |=, &=, ^=.
    Flags flags = Flags::A | Flags::B & ~Flags::C;
  • Checks whether type is an Unscoped enumeration.

    enum color { red, green, blue };
    enum class direction { left, right };
    
    magic_enum::is_unscoped_enum<color>::value -> true
    magic_enum::is_unscoped_enum<direction>::value -> false
    magic_enum::is_unscoped_enum<int>::value -> false
    
    // Helper variable template.
    magic_enum::is_unscoped_enum_v<color> -> true
  • Checks whether type is an Scoped enumeration.

    enum color { red, green, blue };
    enum class direction { left, right };
    
    magic_enum::is_scoped_enum<color>::value -> false
    magic_enum::is_scoped_enum<direction>::value -> true
    magic_enum::is_scoped_enum<int>::value -> false
    
    // Helper variable template.
    magic_enum::is_scoped_enum_v<direction> -> true
  • Static storage enum variable to string This version is much lighter on the compile times and is not restricted to the enum_range limitation.

    constexpr Color color = Color::BLUE;
    constexpr auto color_name = magic_enum::enum_name<color>();
    // color_name -> "BLUE"

Remarks

  • magic_enum does not pretend to be a silver bullet for reflection for enums, it was originally designed for small enum.

  • Before use, read the limitations of functionality.

  • For the small enum use the API from the namespace magic_enum, and for enum-flags use the API from the namespace magic_enum::flags.

Integration

  • You should add the required file magic_enum.hpp.

  • If you are using vcpkg on your project for external dependencies, then you can use the magic-enum package.

  • If you are using Conan to manage your dependencies, merely add magic_enum/x.y.z to your conan's requires, where x.y.z is the release version you want to use.

  • If you are using Build2 to build and manage your dependencies, add depends: magic_enum ^x.y.z to the manifest file where x.y.z is the release version you want to use. You can then import the target using magic_enum%lib{magic_enum}.

  • Alternatively, you can use something like CPM which is based on CMake's Fetch_Content module.

    CPMAddPackage(
        NAME magic_enum
        GITHUB_REPOSITORY Neargye/magic_enum
        GIT_TAG x.y.z # Where `x.y.z` is the release version you want to use.
    )
  • Bazel is also supported, simply add to your WORKSPACE file:

    http_archive(
        name = "magic_enum",
        strip_prefix = "magic_enum-<commit>",
        urls = ["https://github.com/Neargye/magic_enum/archive/<commit>.zip"],
    )
    

    To use bazel inside the repository it's possible to do:

    bazel build //...
    bazel test //...
    bazel run //:example
    

    (Note that you must use a supported compiler or specify it with export CC= <compiler>.)

Compiler compatibility

  • Clang/LLVM >= 5
  • MSVC++ >= 14.11 / Visual Studio >= 2017
  • Xcode >= 10
  • GCC >= 9
  • MinGW >= 9

Licensed under the MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].