mirror of
https://github.com/BlackMATov/enum.hpp.git
synced 2025-12-13 15:52:02 +07:00
6871fcc686a328a2803da2064ec49f797cb4f1e8
enum.hpp
Requirements
Installation
enum.hpp is a header-only library. All you need to do is copy the headers files from headers directory into your project and include them:
#include "enum.hpp/enum.hpp"
Also, you can add the root repository directory to your cmake project:
add_subdirectory(external/enum.hpp)
target_link_libraries(your_project_target enum.hpp)
Examples
Declarations
ENUM_HPP_DECL(debug_level, int,
(level_info)
(level_warning)
(level_error))
// equivalent to:
enum debug_level : int {
level_info,
level_warning,
level_error,
};
struct debug_level_traits {
using underlying_type = int;
static constexpr std::size_t size = 3;
static constexpr const debug_level values[] = {
level_info,
level_warning,
level_warning
};
static constexpr const std::string_view names[] = {
"level_info",
"level_warning",
"level_warning"
};
static constexpr underlying_type to_underlying(debug_level e) noexcept;
static constexpr std::optional<std::string_view> to_string(debug_level e) noexcept;
static constexpr std::string_view to_string_or_empty(Enum e) noexcept;
static std::string_view to_string_or_throw(Enum e);
static constexpr std::optional<debug_level> from_string(std::string_view name) noexcept;
static constexpr debug_level from_string_or_default(std::string_view name, debug_level def) noexcept;
static debug_level from_string_or_throw(std::string_view name);
static constexpr std::optional<std::size_t> to_index(debug_level e) noexcept;
static constexpr std::size_t to_index_or_invalid(debug_level e) noexcept;
static std::size_t to_index_or_throw(debug_level e);
static constexpr std::optional<debug_level> from_index(std::size_t index) noexcept;
static constexpr debug_level from_index_or_default(std::size_t index, debug_level def) noexcept;
static debug_level from_index_or_throw(std::size_t index);
};
ENUM_HPP_CLASS_DECL(color, unsigned,
(red = 1 << 0)
(green = 1 << 1)
(blue = 1 << 2)
(white = red | green | blue))
// equivalent to:
enum class color : unsigned {
red = 1 << 0,
green = 1 << 1,
blue = 1 << 2,
white = red | green | blue,
};
struct color_traits {
using underlying_type = unsigned;
static constexpr std::size_t size = 4;
static constexpr const color values[] = {
color::red,
color::green,
color::blue,
color::white
};
static constexpr const std::string_view names[] = {
"red",
"green",
"blue",
"white"
};
static constexpr underlying_type to_underlying(color e) noexcept;
static constexpr std::optional<std::string_view> to_string(color e) noexcept;
static constexpr std::string_view to_string_or_empty(Enum e) noexcept;
static std::string_view to_string_or_throw(Enum e);
static constexpr std::optional<color> from_string(std::string_view name) noexcept;
static constexpr color from_string_or_default(std::string_view name, color def) noexcept;
static color from_string_or_throw(std::string_view name);
static constexpr std::optional<std::size_t> to_index(color e) noexcept;
static constexpr std::size_t to_index_or_invalid(color e) noexcept;
static std::size_t to_index_or_throw(color e);
static constexpr std::optional<color> from_index(std::size_t index) noexcept;
static constexpr color from_index_or_default(std::size_t index, color def) noexcept;
static color from_index_or_throw(std::size_t index);
};
Traits using
namespace
{
ENUM_HPP_CLASS_DECL(color, unsigned,
(red = 0xFF0000)
(green = 0x00FF00)
(blue = 0x0000FF)
(white = red | green | blue))
}
int main() {
// size
static_assert(color_traits::size == 4);
// to_underlying
static_assert(color_traits::to_underlying(color::white) == 0xFFFFFF);
// to_string
static_assert(color_traits::to_string(color::red) == "red");
static_assert(color_traits::to_string(color(42)) == std::nullopt);
// from_string
static_assert(color_traits::from_string("green") == color::green);
static_assert(color_traits::from_string("error") == std::nullopt);
// to_index
static_assert(color_traits::to_index(color::blue) == 2);
static_assert(color_traits::to_index(color(42)) == std::nullopt);
// from_index
static_assert(color_traits::from_index(2) == color::blue);
static_assert(color_traits::from_index(42) == std::nullopt);
// names
for ( auto n : color_traits::names ) {
std::cout << n << ",";
} // stdout: red,green,blue,
return 0;
}
Alternatives
- Low variadic macros limit (64)
- Replaces C++ enum semantics to custom class
- Limited by variadic macros (msvs: 127)
- Amazing but requires at least GCC 9.0
License (MIT)
Description
C++17 compile-time enum reflection library
c-plus-plusc-plus-plus-17cplusplus-17cppcpp17enumenum-to-stringheader-onlyno-dependenciesreflectionstring-to-enum
Readme
528 KiB
Languages
C++
96.1%
CMake
3.9%