From 9efae8b856e630bc7dcb7b3cb7058f5b3147668d Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 22 Nov 2019 03:02:22 +0700 Subject: [PATCH 1/4] add to_index, from_index, from_index_nothrow traits functions --- headers/enum.hpp/enum.hpp | 26 ++++++++++++++ untests/enum_tests.cpp | 76 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/headers/enum.hpp/enum.hpp b/headers/enum.hpp/enum.hpp index d4a6013..5955176 100644 --- a/headers/enum.hpp/enum.hpp +++ b/headers/enum.hpp/enum.hpp @@ -16,6 +16,8 @@ namespace enum_hpp explicit exception(const char* what) : std::runtime_error(what) {} }; + + constexpr std::size_t invalid_index = std::size_t(-1); } namespace enum_hpp::detail @@ -146,6 +148,30 @@ namespace enum_hpp::detail }\ return false;\ }\ + \ + static constexpr std::size_t to_index(Enum e) noexcept {\ + for ( std::size_t i = 0; i < size; ++i ) {\ + if ( e == values[i] ) {\ + return i;\ + }\ + }\ + return ::enum_hpp::invalid_index;\ + }\ + \ + static Enum from_index(std::size_t index) {\ + if ( index < size ) {\ + return values[index];\ + }\ + throw ::enum_hpp::exception(#Enum "_traits::from_index(): invalid argument");\ + }\ + \ + static constexpr bool from_index_nothrow(std::size_t index, Enum& result) noexcept {\ + if ( index < size ) {\ + result = values[index];\ + return true;\ + }\ + return false;\ + }\ }; // ----------------------------------------------------------------------------- diff --git a/untests/enum_tests.cpp b/untests/enum_tests.cpp index 64aea69..109f2f3 100644 --- a/untests/enum_tests.cpp +++ b/untests/enum_tests.cpp @@ -182,4 +182,80 @@ TEST_CASE("enum") { REQUIRE(result == sn::_240); } } + + SECTION("to_index") { + { + STATIC_REQUIRE(sn::color_traits::to_index(sn::color::red) == 0); + STATIC_REQUIRE(sn::color_traits::to_index(sn::color::green) == 1); + STATIC_REQUIRE(sn::color_traits::to_index(sn::color::blue) == 2); + STATIC_REQUIRE(sn::color_traits::to_index(sn::color(42)) == enum_hpp::invalid_index); + } + { + STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::none) == 0); + STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::color) == 1); + STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::alpha) == 2); + STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::all) == 3); + } + { + STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_0) == 0); + STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_180) == 180); + STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_240) == 240); + STATIC_REQUIRE(sn::numbers_traits::to_index(sn::numbers(100500)) == enum_hpp::invalid_index); + } + } + + SECTION("from_index") { + { + REQUIRE(sn::color_traits::from_index(0) == sn::color::red); + REQUIRE(sn::color_traits::from_index(1) == sn::color::green); + REQUIRE(sn::color_traits::from_index(2) == sn::color::blue); + REQUIRE_THROWS_AS(sn::color_traits::from_index(42), enum_hpp::exception); + } + { + REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none); + REQUIRE(sn::render::mask_traits::from_index(1) == sn::render::mask::color); + REQUIRE(sn::render::mask_traits::from_index(2) == sn::render::mask::alpha); + REQUIRE(sn::render::mask_traits::from_index(3) == sn::render::mask::all); + REQUIRE_THROWS_AS(sn::render::mask_traits::from_index(42), enum_hpp::exception); + } + { + REQUIRE(sn::numbers_traits::from_index(10) == sn::_10); + REQUIRE(sn::numbers_traits::from_index(240) == sn::_240); + REQUIRE_THROWS_AS(sn::numbers_traits::from_index(100500), enum_hpp::exception); + } + } + + SECTION("from_index_nothrow") { + { + sn::color result{42}; + REQUIRE(sn::color_traits::from_index_nothrow(0, result)); + REQUIRE(result == sn::color::red); + REQUIRE(sn::color_traits::from_index_nothrow(1, result)); + REQUIRE(result == sn::color::green); + REQUIRE(sn::color_traits::from_index_nothrow(2, result)); + REQUIRE(result == sn::color::blue); + REQUIRE_FALSE(sn::color_traits::from_index_nothrow(42, result)); + REQUIRE(result == sn::color::blue); + } + { + sn::render::mask result{42}; + REQUIRE(sn::render::mask_traits::from_index_nothrow(0, result)); + REQUIRE(result == sn::render::mask::none); + REQUIRE(sn::render::mask_traits::from_index_nothrow(1, result)); + REQUIRE(result == sn::render::mask::color); + REQUIRE(sn::render::mask_traits::from_index_nothrow(2, result)); + REQUIRE(result == sn::render::mask::alpha); + REQUIRE(sn::render::mask_traits::from_index_nothrow(3, result)); + REQUIRE(result == sn::render::mask::all); + REQUIRE_FALSE(sn::render::mask_traits::from_index_nothrow(42, result)); + REQUIRE(result == sn::render::mask::all); + } + { + sn::numbers result{100500}; + REQUIRE(sn::numbers_traits::from_index_nothrow(240, result)); + REQUIRE(result == sn::_240); + REQUIRE_FALSE(sn::numbers_traits::from_index_nothrow(100500, result)); + REQUIRE(result == sn::_240); + } + } } From e488de0d46ae563031f06d1b256ac811e6daa837 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 22 Nov 2019 03:10:30 +0700 Subject: [PATCH 2/4] add to_underlying traits function --- headers/enum.hpp/enum.hpp | 14 ++++++++++---- untests/enum_tests.cpp | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/headers/enum.hpp/enum.hpp b/headers/enum.hpp/enum.hpp index 5955176..e197e40 100644 --- a/headers/enum.hpp/enum.hpp +++ b/headers/enum.hpp/enum.hpp @@ -8,6 +8,7 @@ #include #include +#include namespace enum_hpp { @@ -96,7 +97,7 @@ namespace enum_hpp::detail enum Enum : Type {\ ENUM_HPP_GENERATE_ENUM_FIELDS(Fields)\ };\ - ENUM_HPP_TRAITS_IMPL(Enum, Type, Fields) + ENUM_HPP_TRAITS_DECL(Enum, Fields) // // ENUM_HPP_CLASS_DECL @@ -106,21 +107,26 @@ namespace enum_hpp::detail enum class Enum : Type {\ ENUM_HPP_GENERATE_ENUM_FIELDS(Fields)\ };\ - ENUM_HPP_TRAITS_IMPL(Enum, Type, Fields) + ENUM_HPP_TRAITS_DECL(Enum, Fields) // -// ENUM_HPP_TRAITS_IMPL +// ENUM_HPP_TRAITS_DECL // -#define ENUM_HPP_TRAITS_IMPL(Enum, Type, Fields)\ +#define ENUM_HPP_TRAITS_DECL(Enum, Fields)\ struct Enum##_traits {\ private:\ enum enum_names_for_this_score_ { ENUM_HPP_GENERATE_ENUM_FIELDS(Fields) };\ public:\ + using underlying_t = std::underlying_type_t;\ static constexpr std::size_t size = ENUM_HPP_PP_SEQ_SIZE(Fields);\ static constexpr const Enum values[] = { ENUM_HPP_GENERATE_VALUES(Enum, Fields) };\ static constexpr const std::string_view names[] = { ENUM_HPP_GENERATE_NAMES(Fields) };\ public:\ + static constexpr underlying_t to_underlying(Enum e) noexcept {\ + return static_cast(e);\ + }\ + \ static constexpr std::string_view to_string(Enum e) noexcept {\ for ( std::size_t i = 0; i < size; ++i) {\ if ( e == values[i] ) {\ diff --git a/untests/enum_tests.cpp b/untests/enum_tests.cpp index 109f2f3..72365bf 100644 --- a/untests/enum_tests.cpp +++ b/untests/enum_tests.cpp @@ -107,6 +107,27 @@ TEST_CASE("enum") { } } + SECTION("to_underlying") { + { + STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::red) == enum_to_underlying(sn::color::red)); + STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::green) == enum_to_underlying(sn::color::green)); + STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::blue) == enum_to_underlying(sn::color::blue)); + STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color(42)) == 42); + } + { + STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::none) == enum_to_underlying(sn::render::mask::none)); + STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::color) == enum_to_underlying(sn::render::mask::color)); + STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::alpha) == enum_to_underlying(sn::render::mask::alpha)); + STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::all) == enum_to_underlying(sn::render::mask::all)); + } + { + STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_0) == enum_to_underlying(sn::_0)); + STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_180) == enum_to_underlying(sn::_180)); + STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_240) == enum_to_underlying(sn::_240)); + STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::numbers(100500)) == 100500); + } + } + SECTION("to_string") { { STATIC_REQUIRE(sn::color_traits::to_string(sn::color::red) == "red"); From aaf1099717644d29d60200045ad7d903c53480e6 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 22 Nov 2019 03:36:48 +0700 Subject: [PATCH 3/4] optional using instead throw and nothrow functions --- headers/enum.hpp/enum.hpp | 47 +++------------ untests/enum_tests.cpp | 124 +++++++++----------------------------- 2 files changed, 37 insertions(+), 134 deletions(-) diff --git a/headers/enum.hpp/enum.hpp b/headers/enum.hpp/enum.hpp index e197e40..ebb961f 100644 --- a/headers/enum.hpp/enum.hpp +++ b/headers/enum.hpp/enum.hpp @@ -6,21 +6,10 @@ #pragma once -#include +#include #include #include -namespace enum_hpp -{ - class exception final : public std::runtime_error { - public: - explicit exception(const char* what) - : std::runtime_error(what) {} - }; - - constexpr std::size_t invalid_index = std::size_t(-1); -} - namespace enum_hpp::detail { template < typename Enum > @@ -127,56 +116,38 @@ namespace enum_hpp::detail return static_cast(e);\ }\ \ - static constexpr std::string_view to_string(Enum e) noexcept {\ + static constexpr std::optional to_string(Enum e) noexcept {\ for ( std::size_t i = 0; i < size; ++i) {\ if ( e == values[i] ) {\ return names[i];\ }\ }\ - return std::string_view();\ + return std::nullopt;\ }\ \ - static Enum from_string(std::string_view name){\ + static constexpr std::optional from_string(std::string_view name) noexcept {\ for ( std::size_t i = 0; i < size; ++i) {\ if ( name == names[i] ) {\ return values[i];\ }\ }\ - throw ::enum_hpp::exception(#Enum "_traits::from_string(): invalid argument");\ + return std::nullopt;\ }\ \ - static constexpr bool from_string_nothrow(std::string_view name, Enum& result) noexcept {\ - for ( std::size_t i = 0; i < size; ++i) {\ - if ( name == names[i] ) {\ - result = values[i];\ - return true;\ - }\ - }\ - return false;\ - }\ - \ - static constexpr std::size_t to_index(Enum e) noexcept {\ + static constexpr std::optional to_index(Enum e) noexcept {\ for ( std::size_t i = 0; i < size; ++i ) {\ if ( e == values[i] ) {\ return i;\ }\ }\ - return ::enum_hpp::invalid_index;\ + return std::nullopt;\ }\ \ - static Enum from_index(std::size_t index) {\ + static constexpr std::optional from_index(std::size_t index) noexcept {\ if ( index < size ) {\ return values[index];\ }\ - throw ::enum_hpp::exception(#Enum "_traits::from_index(): invalid argument");\ - }\ - \ - static constexpr bool from_index_nothrow(std::size_t index, Enum& result) noexcept {\ - if ( index < size ) {\ - result = values[index];\ - return true;\ - }\ - return false;\ + return std::nullopt;\ }\ }; diff --git a/untests/enum_tests.cpp b/untests/enum_tests.cpp index 72365bf..b16b000 100644 --- a/untests/enum_tests.cpp +++ b/untests/enum_tests.cpp @@ -133,7 +133,7 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::color_traits::to_string(sn::color::red) == "red"); STATIC_REQUIRE(sn::color_traits::to_string(sn::color::green) == "green"); STATIC_REQUIRE(sn::color_traits::to_string(sn::color::blue) == "blue"); - STATIC_REQUIRE(sn::color_traits::to_string(sn::color(42)) == ""); + STATIC_REQUIRE_FALSE(sn::color_traits::to_string(sn::color(42))); } { STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::none) == "none"); @@ -145,62 +145,28 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::numbers_traits::to_string(sn::_0) == "_0"); STATIC_REQUIRE(sn::numbers_traits::to_string(sn::_180) == "_180"); STATIC_REQUIRE(sn::numbers_traits::to_string(sn::_240) == "_240"); - STATIC_REQUIRE(sn::numbers_traits::to_string(sn::numbers(100500)) == ""); + STATIC_REQUIRE_FALSE(sn::numbers_traits::to_string(sn::numbers(100500))); } } SECTION("from_string") { { - REQUIRE(sn::color_traits::from_string("red") == sn::color::red); - REQUIRE(sn::color_traits::from_string("green") == sn::color::green); - REQUIRE(sn::color_traits::from_string("blue") == sn::color::blue); - REQUIRE_THROWS_AS(sn::color_traits::from_string("42"), enum_hpp::exception); + STATIC_REQUIRE(sn::color_traits::from_string("red") == sn::color::red); + STATIC_REQUIRE(sn::color_traits::from_string("green") == sn::color::green); + STATIC_REQUIRE(sn::color_traits::from_string("blue") == sn::color::blue); + STATIC_REQUIRE_FALSE(sn::color_traits::from_string("42")); } { - REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none); - REQUIRE(sn::render::mask_traits::from_string("color") == sn::render::mask::color); - REQUIRE(sn::render::mask_traits::from_string("alpha") == sn::render::mask::alpha); - REQUIRE(sn::render::mask_traits::from_string("all") == sn::render::mask::all); - REQUIRE_THROWS_AS(sn::render::mask_traits::from_string("42"), enum_hpp::exception); + STATIC_REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none); + STATIC_REQUIRE(sn::render::mask_traits::from_string("color") == sn::render::mask::color); + STATIC_REQUIRE(sn::render::mask_traits::from_string("alpha") == sn::render::mask::alpha); + STATIC_REQUIRE(sn::render::mask_traits::from_string("all") == sn::render::mask::all); + STATIC_REQUIRE_FALSE(sn::render::mask_traits::from_string("42")); } { - REQUIRE(sn::numbers_traits::from_string("_10") == sn::_10); - REQUIRE(sn::numbers_traits::from_string("_240") == sn::_240); - REQUIRE_THROWS_AS(sn::numbers_traits::from_string("error"), enum_hpp::exception); - } - } - - SECTION("from_string_nothrow") { - { - sn::color result{42}; - REQUIRE(sn::color_traits::from_string_nothrow("red", result)); - REQUIRE(result == sn::color::red); - REQUIRE(sn::color_traits::from_string_nothrow("green", result)); - REQUIRE(result == sn::color::green); - REQUIRE(sn::color_traits::from_string_nothrow("blue", result)); - REQUIRE(result == sn::color::blue); - REQUIRE_FALSE(sn::color_traits::from_string_nothrow("42", result)); - REQUIRE(result == sn::color::blue); - } - { - sn::render::mask result{42}; - REQUIRE(sn::render::mask_traits::from_string_nothrow("none", result)); - REQUIRE(result == sn::render::mask::none); - REQUIRE(sn::render::mask_traits::from_string_nothrow("color", result)); - REQUIRE(result == sn::render::mask::color); - REQUIRE(sn::render::mask_traits::from_string_nothrow("alpha", result)); - REQUIRE(result == sn::render::mask::alpha); - REQUIRE(sn::render::mask_traits::from_string_nothrow("all", result)); - REQUIRE(result == sn::render::mask::all); - REQUIRE_FALSE(sn::render::mask_traits::from_string_nothrow("42", result)); - REQUIRE(result == sn::render::mask::all); - } - { - sn::numbers result{100500}; - REQUIRE(sn::numbers_traits::from_string_nothrow("_240", result)); - REQUIRE(result == sn::_240); - REQUIRE_FALSE(sn::numbers_traits::from_string_nothrow("error", result)); - REQUIRE(result == sn::_240); + STATIC_REQUIRE(sn::numbers_traits::from_string("_10") == sn::_10); + STATIC_REQUIRE(sn::numbers_traits::from_string("_240") == sn::_240); + STATIC_REQUIRE_FALSE(sn::numbers_traits::from_string("error")); } } @@ -209,7 +175,7 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::color_traits::to_index(sn::color::red) == 0); STATIC_REQUIRE(sn::color_traits::to_index(sn::color::green) == 1); STATIC_REQUIRE(sn::color_traits::to_index(sn::color::blue) == 2); - STATIC_REQUIRE(sn::color_traits::to_index(sn::color(42)) == enum_hpp::invalid_index); + STATIC_REQUIRE_FALSE(sn::color_traits::to_index(sn::color(42))); } { STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::none) == 0); @@ -221,62 +187,28 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_0) == 0); STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_180) == 180); STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_240) == 240); - STATIC_REQUIRE(sn::numbers_traits::to_index(sn::numbers(100500)) == enum_hpp::invalid_index); + STATIC_REQUIRE_FALSE(sn::numbers_traits::to_index(sn::numbers(100500))); } } SECTION("from_index") { { - REQUIRE(sn::color_traits::from_index(0) == sn::color::red); - REQUIRE(sn::color_traits::from_index(1) == sn::color::green); - REQUIRE(sn::color_traits::from_index(2) == sn::color::blue); - REQUIRE_THROWS_AS(sn::color_traits::from_index(42), enum_hpp::exception); + STATIC_REQUIRE(sn::color_traits::from_index(0) == sn::color::red); + STATIC_REQUIRE(sn::color_traits::from_index(1) == sn::color::green); + STATIC_REQUIRE(sn::color_traits::from_index(2) == sn::color::blue); + STATIC_REQUIRE_FALSE(sn::color_traits::from_index(42)); } { - REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none); - REQUIRE(sn::render::mask_traits::from_index(1) == sn::render::mask::color); - REQUIRE(sn::render::mask_traits::from_index(2) == sn::render::mask::alpha); - REQUIRE(sn::render::mask_traits::from_index(3) == sn::render::mask::all); - REQUIRE_THROWS_AS(sn::render::mask_traits::from_index(42), enum_hpp::exception); + STATIC_REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none); + STATIC_REQUIRE(sn::render::mask_traits::from_index(1) == sn::render::mask::color); + STATIC_REQUIRE(sn::render::mask_traits::from_index(2) == sn::render::mask::alpha); + STATIC_REQUIRE(sn::render::mask_traits::from_index(3) == sn::render::mask::all); + STATIC_REQUIRE_FALSE(sn::render::mask_traits::from_index(42)); } { - REQUIRE(sn::numbers_traits::from_index(10) == sn::_10); - REQUIRE(sn::numbers_traits::from_index(240) == sn::_240); - REQUIRE_THROWS_AS(sn::numbers_traits::from_index(100500), enum_hpp::exception); - } - } - - SECTION("from_index_nothrow") { - { - sn::color result{42}; - REQUIRE(sn::color_traits::from_index_nothrow(0, result)); - REQUIRE(result == sn::color::red); - REQUIRE(sn::color_traits::from_index_nothrow(1, result)); - REQUIRE(result == sn::color::green); - REQUIRE(sn::color_traits::from_index_nothrow(2, result)); - REQUIRE(result == sn::color::blue); - REQUIRE_FALSE(sn::color_traits::from_index_nothrow(42, result)); - REQUIRE(result == sn::color::blue); - } - { - sn::render::mask result{42}; - REQUIRE(sn::render::mask_traits::from_index_nothrow(0, result)); - REQUIRE(result == sn::render::mask::none); - REQUIRE(sn::render::mask_traits::from_index_nothrow(1, result)); - REQUIRE(result == sn::render::mask::color); - REQUIRE(sn::render::mask_traits::from_index_nothrow(2, result)); - REQUIRE(result == sn::render::mask::alpha); - REQUIRE(sn::render::mask_traits::from_index_nothrow(3, result)); - REQUIRE(result == sn::render::mask::all); - REQUIRE_FALSE(sn::render::mask_traits::from_index_nothrow(42, result)); - REQUIRE(result == sn::render::mask::all); - } - { - sn::numbers result{100500}; - REQUIRE(sn::numbers_traits::from_index_nothrow(240, result)); - REQUIRE(result == sn::_240); - REQUIRE_FALSE(sn::numbers_traits::from_index_nothrow(100500, result)); - REQUIRE(result == sn::_240); + STATIC_REQUIRE(sn::numbers_traits::from_index(10) == sn::_10); + STATIC_REQUIRE(sn::numbers_traits::from_index(240) == sn::_240); + STATIC_REQUIRE_FALSE(sn::numbers_traits::from_index(100500)); } } } From 35d63b1b37f98bd4e44f581530b089ddb88c3181 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 22 Nov 2019 03:59:49 +0700 Subject: [PATCH 4/4] fix readme to new api --- .travis.yml | 4 -- README.md | 90 +++++++++++++++++++++++---------------- headers/enum.hpp/enum.hpp | 6 +-- untests/enum_examples.cpp | 53 +++++++++++++++++++++++ untests/enum_tests.cpp | 40 ++++++++--------- 5 files changed, 130 insertions(+), 63 deletions(-) create mode 100644 untests/enum_examples.cpp diff --git a/.travis.yml b/.travis.yml index 739cd3b..2c000ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,10 +24,6 @@ matrix: after_success: ./scripts/upload_coverage.sh before_install: - eval "${MATRIX_EVAL}" - - if [ "$TRAVIS_OS_NAME" == 'osx' ]; then - brew update; - brew upgrade cmake; - fi - if [ "$TRAVIS_OS_NAME" == 'linux' ]; then mkdir $HOME/cmake; export PATH="$HOME/cmake/bin:$PATH"; diff --git a/README.md b/README.md index 4c47476..2dd0812 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,12 @@ [enum]: https://github.com/BlackMATov/enum.hpp +## Requirements + +- [gcc](https://www.gnu.org/software/gcc/) **>= 7** +- [clang](https://clang.llvm.org/) **>= 7.0** +- [msvc](https://visualstudio.microsoft.com/) **>= 2017** + ## Installation [enum.hpp][enum] is a header-only library. All you need to do is copy the headers files from `headers` directory into your project and include them: @@ -57,6 +63,7 @@ enum debug_level : int { }; struct debug_level_traits { + using underlying_type = int; static constexpr std::size_t size = 3; static constexpr const debug_level values[] = { @@ -71,15 +78,13 @@ struct debug_level_traits { "level_warning" }; - static constexpr std::string_view to_string( - debug_level e) noexcept; + static constexpr underlying_type to_underlying(debug_level e) noexcept; - static debug_level from_string( - std::string_view name); + static constexpr std::optional to_string(debug_level e) noexcept; + static constexpr std::optional from_string(std::string_view name) noexcept; - static constexpr bool from_string_nothrow( - std::string_view name, - debug_level& result) noexcept; + static constexpr std::optional to_index(debug_level e) noexcept; + static constexpr std::optional from_index(std::size_t index) noexcept; }; ``` @@ -100,6 +105,7 @@ enum class color : unsigned { }; struct color_traits { + using underlying_type = unsigned; static constexpr std::size_t size = 4; static constexpr const color values[] = { @@ -116,44 +122,56 @@ struct color_traits { "white" }; - static constexpr std::string_view to_string( - color e) noexcept; + static constexpr underlying_type to_underlying(color e) noexcept; - static color from_string( - std::string_view name); + static constexpr std::optional to_string(color e) noexcept; + static constexpr std::optional from_string(std::string_view name) noexcept; - static constexpr bool from_string_nothrow( - std::string_view name, - color& result) noexcept; + static constexpr std::optional to_index(color e) noexcept; + static constexpr std::optional from_index(std::size_t index) noexcept; }; ``` ### Traits using ```cpp -ENUM_HPP_CLASS_DECL(color, unsigned, red, green, blue) - -// size -color_traits::size; // 3 - -// to_string -color_traits::to_string(color::red); // returns "red"; -color_traits::to_string(color(42)); // returns ""; - -// from_string -color_traits::from_string("green"); // returns color::green; -color_traits::from_string("error"); // throws enum_hpp::exception - -// from_string_nothrow -color result; -bool success = color_traits::from_string_nothrow("blue", result); -// success == true, result == color::blue - -// names -for ( auto n : color_traits::names ) { - std::cout << n << ","; +namespace +{ + ENUM_HPP_CLASS_DECL(color, unsigned, + (red = 0xFF0000) + (green = 0x00FF00) + (blue = 0x0000FF) + (white = red | green | blue)) +} + +SECTION("traits_using") { + // size + STATIC_REQUIRE(color_traits::size == 4); + + // to_underlying + STATIC_REQUIRE(color_traits::to_underlying(color::white) == 0xFFFFFF); + + // to_string + STATIC_REQUIRE(color_traits::to_string(color::red) == "red"); + STATIC_REQUIRE(color_traits::to_string(color(42)) == std::nullopt); + + // from_string + STATIC_REQUIRE(color_traits::from_string("green") == color::green); + STATIC_REQUIRE(color_traits::from_string("error") == std::nullopt); + + // to_index + STATIC_REQUIRE(color_traits::to_index(color::blue) == 2); + STATIC_REQUIRE(color_traits::to_index(color(42)) == std::nullopt); + + // from_index + STATIC_REQUIRE(color_traits::from_index(2) == color::blue); + STATIC_REQUIRE(color_traits::from_index(42) == std::nullopt); + + // names + for ( auto n : color_traits::names ) { + std::cout << n << ","; + } // stdout: red,green,blue, } -// prints red,green,blue ``` ## Alternatives diff --git a/headers/enum.hpp/enum.hpp b/headers/enum.hpp/enum.hpp index ebb961f..40b8731 100644 --- a/headers/enum.hpp/enum.hpp +++ b/headers/enum.hpp/enum.hpp @@ -107,13 +107,13 @@ namespace enum_hpp::detail private:\ enum enum_names_for_this_score_ { ENUM_HPP_GENERATE_ENUM_FIELDS(Fields) };\ public:\ - using underlying_t = std::underlying_type_t;\ + using underlying_type = std::underlying_type_t;\ static constexpr std::size_t size = ENUM_HPP_PP_SEQ_SIZE(Fields);\ static constexpr const Enum values[] = { ENUM_HPP_GENERATE_VALUES(Enum, Fields) };\ static constexpr const std::string_view names[] = { ENUM_HPP_GENERATE_NAMES(Fields) };\ public:\ - static constexpr underlying_t to_underlying(Enum e) noexcept {\ - return static_cast(e);\ + static constexpr underlying_type to_underlying(Enum e) noexcept {\ + return static_cast(e);\ }\ \ static constexpr std::optional to_string(Enum e) noexcept {\ diff --git a/untests/enum_examples.cpp b/untests/enum_examples.cpp new file mode 100644 index 0000000..877f6bf --- /dev/null +++ b/untests/enum_examples.cpp @@ -0,0 +1,53 @@ +/******************************************************************************* + * This file is part of the "https://github.com/blackmatov/enum.hpp" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2019, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#define CATCH_CONFIG_FAST_COMPILE +#include + +#include + +#include +#include + +namespace +{ + ENUM_HPP_CLASS_DECL(color, unsigned, + (red = 0xFF0000) + (green = 0x00FF00) + (blue = 0x0000FF) + (white = red | green | blue)) +} + +TEST_CASE("examples") { + SECTION("traits_using") { + // size + STATIC_REQUIRE(color_traits::size == 4); + + // to_underlying + STATIC_REQUIRE(color_traits::to_underlying(color::white) == 0xFFFFFF); + + // to_string + STATIC_REQUIRE(color_traits::to_string(color::red) == "red"); + STATIC_REQUIRE(color_traits::to_string(color(42)) == std::nullopt); + + // from_string + STATIC_REQUIRE(color_traits::from_string("green") == color::green); + STATIC_REQUIRE(color_traits::from_string("error") == std::nullopt); + + // to_index + STATIC_REQUIRE(color_traits::to_index(color::blue) == 2u); + STATIC_REQUIRE(color_traits::to_index(color(42)) == std::nullopt); + + // from_index + STATIC_REQUIRE(color_traits::from_index(2) == color::blue); + STATIC_REQUIRE(color_traits::from_index(42) == std::nullopt); + + // names + for ( auto n : color_traits::names ) { + std::cout << n << ","; + } + } +} diff --git a/untests/enum_tests.cpp b/untests/enum_tests.cpp index b16b000..348693e 100644 --- a/untests/enum_tests.cpp +++ b/untests/enum_tests.cpp @@ -64,19 +64,19 @@ TEST_CASE("enum") { std::underlying_type_t, unsigned>); - REQUIRE(enum_to_underlying(sn::color::red) == 2); - REQUIRE(enum_to_underlying(sn::color::green) == 3); - REQUIRE(enum_to_underlying(sn::color::blue) == 6); + REQUIRE(enum_to_underlying(sn::color::red) == 2u); + REQUIRE(enum_to_underlying(sn::color::green) == 3u); + REQUIRE(enum_to_underlying(sn::color::blue) == 6u); } { STATIC_REQUIRE(std::is_same_v< std::underlying_type_t, unsigned char>); - REQUIRE(enum_to_underlying(sn::render::mask::none) == 0); - REQUIRE(enum_to_underlying(sn::render::mask::color) == 1); - REQUIRE(enum_to_underlying(sn::render::mask::alpha) == 2); - REQUIRE(enum_to_underlying(sn::render::mask::all) == 3); + REQUIRE(enum_to_underlying(sn::render::mask::none) == 0u); + REQUIRE(enum_to_underlying(sn::render::mask::color) == 1u); + REQUIRE(enum_to_underlying(sn::render::mask::alpha) == 2u); + REQUIRE(enum_to_underlying(sn::render::mask::all) == 3u); } { STATIC_REQUIRE(std::is_same_v< @@ -91,17 +91,17 @@ TEST_CASE("enum") { SECTION("size") { { - REQUIRE(sn::color_traits::size == 3); + REQUIRE(sn::color_traits::size == 3u); REQUIRE(sn::color_traits::size == std::size(sn::color_traits::names)); REQUIRE(sn::color_traits::size == std::size(sn::color_traits::values)); } { - REQUIRE(sn::render::mask_traits::size == 4); + REQUIRE(sn::render::mask_traits::size == 4u); REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::names)); REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::values)); } { - REQUIRE(sn::numbers_traits::size == 241); + REQUIRE(sn::numbers_traits::size == 241u); REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::names)); REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::values)); } @@ -172,21 +172,21 @@ TEST_CASE("enum") { SECTION("to_index") { { - STATIC_REQUIRE(sn::color_traits::to_index(sn::color::red) == 0); - STATIC_REQUIRE(sn::color_traits::to_index(sn::color::green) == 1); - STATIC_REQUIRE(sn::color_traits::to_index(sn::color::blue) == 2); + STATIC_REQUIRE(sn::color_traits::to_index(sn::color::red) == 0u); + STATIC_REQUIRE(sn::color_traits::to_index(sn::color::green) == 1u); + STATIC_REQUIRE(sn::color_traits::to_index(sn::color::blue) == 2u); STATIC_REQUIRE_FALSE(sn::color_traits::to_index(sn::color(42))); } { - STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::none) == 0); - STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::color) == 1); - STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::alpha) == 2); - STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::all) == 3); + STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::none) == 0u); + STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::color) == 1u); + STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::alpha) == 2u); + STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::all) == 3u); } { - STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_0) == 0); - STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_180) == 180); - STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_240) == 240); + STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_0) == 0u); + STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_180) == 180u); + STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_240) == 240u); STATIC_REQUIRE_FALSE(sn::numbers_traits::to_index(sn::numbers(100500))); } }