From fae022c8489089a1c97eb4c496bdf572349fa9c0 Mon Sep 17 00:00:00 2001 From: terik23 Date: Mon, 8 Apr 2019 02:23:51 +0500 Subject: [PATCH] clean-up --- README.md | 54 +++++++++++++++++++++++++++++++++++---------- example/example.cpp | 1 - test/CMakeLists.txt | 4 ---- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ce40fb3..abbef54 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,11 @@ [![Build Status](https://travis-ci.org/Neargye/magic_enum.svg?branch=master)](https://travis-ci.org/Neargye/magic_enum) [![Build status](https://ci.appveyor.com/api/projects/status/0rpr966p9ssrvwu3/branch/master?svg=true)](https://ci.appveyor.com/project/Neargye/magic-enum-hf8vk/branch/master) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/64d04f150af14c3e8bd1090057b68538)](https://www.codacy.com/app/Neargye/magic_enum?utm_source=github.com&utm_medium=referral&utm_content=Neargye/magic_enum&utm_campaign=Badge_Grade) -[![Try online](https://img.shields.io/badge/try-online-blue.svg)](https://wandbox.org/permlink/uCF1Op6ZgSI6cDJS) +[![Try online](https://img.shields.io/badge/try-online-blue.svg)](https://wandbox.org/permlink/XfV2tDCzFdk36RSG) ## What is Magic Enum? -Header-only C++17 library provides Enum-to-String and String-to-Enum functions. +Header-only C++17 library provides Enum-to-String and String-to-Enum and other useful functions, without any macro or boilerplate code. * `magic_enum::enum_cast` obtains enum value from string or integer. * `magic_enum::enum_value` obtains indexed access to enum value. * `magic_enum::enum_values` obtains enum value sequence. @@ -40,6 +40,11 @@ Header-only C++17 library provides Enum-to-String and String-to-Enum functions. ## [Examples](example/example.cpp) +```cpp +// For example color enum. +enum Color { RED = 2, BLUE = 4, GREEN = 8 }; +``` + * Enum value to string ```cpp Color color = Color::RED; @@ -60,24 +65,49 @@ Header-only C++17 library provides Enum-to-String and String-to-Enum functions. * String to enum value ```cpp - constexpr auto color = magic_enum::enum_cast("GREEN"); + std::string color_name{"GREEN"}; + auto color = magic_enum::enum_cast(color_name); if (color.has_value()) { // color.value() -> Color::GREEN } ``` +* Static storage string to enum value + ```cpp + constexpr auto color = magic_enum::enum_cast("BLUE"); + if (color.has_value()) { + // color.value() -> Color::BLUE + } + ``` + * Integer to enum value ```cpp - constexpr auto color = magic_enum::enum_cast(0); - if (color.has_value()) { + int color_value = 2; + auto color = magic_enum::enum_cast(color_value); + if (colo.has_value()) { // color.value() -> Color::RED } ``` +* Static storage integer to enum value + ```cpp + constexpr auto color = magic_enum::enum_cast(4); + if (color.has_value()) { + // color.value() -> Color::BLUE + } + ``` + * Indexed access to enum value ```cpp - constexpr Color color_value = magic_enum::enum_value(0); - // color_element -> Color::RED + int i = 1; + Color colo = magic_enum::enum_value(i); + // color -> Color::BLUE + ``` + +* Compile-time indexed access. + ```cpp + constexpr Color color = magic_enum::enum_value(0); + // color -> Color::RED ``` * Enum value sequence @@ -88,11 +118,11 @@ Header-only C++17 library provides Enum-to-String and String-to-Enum functions. * Number of enum elements ```cpp - constexpr std::size_t color_size = magic_enum::enum_count(); - // color_size -> 3 + constexpr std::size_t color_count = magic_enum::enum_count(); + // color_count -> 3 ``` -* Enum string sequence +* Enum names sequence ```cpp constexpr auto color_names = magic_enum::enum_names(); // color_names -> {"RED", "BLUE", "GREEN"} @@ -109,11 +139,11 @@ Header-only C++17 library provides Enum-to-String and String-to-Enum functions. * `magic_enum::enum_cast` returns `std::optional`, using `has_value()` to check contains enum value and `value()` to get the enum value. -* `magic_enum::enum_values` returns `std::array` with all enum value, sorted by enum value. +* `magic_enum::enum_values` returns `std::array` with all enum value where `N = number of enum values`, sorted by enum value. * `magic_enum::enum_name` returns `std::optional`, using `has_value()` to check contains enum name and `value()` to get the enum name. -* `magic_enum::enum_names` returns `std::array` with all string enum name, sorted by enum value. +* `magic_enum::enum_names` returns `std::array` with all string enum name where `N = number of enum values`, sorted by enum value. * Enum value 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`. ```cpp diff --git a/example/example.cpp b/example/example.cpp index f63a930..07bf4ad 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -21,7 +21,6 @@ // SOFTWARE. #include -#include #include diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8bfcde3..51840d9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,8 +14,6 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") set(OPTIONS ${OPTIONS} /permissive-) endif() - set(HAS_CPP11_FLAG TRUE) - check_cxx_compiler_flag(/std:c++14 HAS_CPP14_FLAG) check_cxx_compiler_flag(/std:c++17 HAS_CPP17_FLAG) check_cxx_compiler_flag(/std:c++20 HAS_CPP20_FLAG) check_cxx_compiler_flag(/std:c++latest HAS_CPPLATEST_FLAG) @@ -23,8 +21,6 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") set(CMAKE_VERBOSE_MAKEFILE ON) set(OPTIONS -Wall -Wextra -pedantic-errors) - check_cxx_compiler_flag(-std=c++11 HAS_CPP11_FLAG) - check_cxx_compiler_flag(-std=c++14 HAS_CPP14_FLAG) check_cxx_compiler_flag(-std=c++17 HAS_CPP17_FLAG) check_cxx_compiler_flag(-std=c++20 HAS_CPP20_FLAG) endif()