From 080acf36114cdd7e1e6ce7fc3279ee52ef72596b Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 22 Nov 2019 05:00:36 +0700 Subject: [PATCH 1/4] add to_string_or_empty and to_index_or_invalid traits functions --- headers/enum.hpp/enum.hpp | 19 +++++++++++++++++++ untests/enum_tests.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/headers/enum.hpp/enum.hpp b/headers/enum.hpp/enum.hpp index 40b8731..f3bc598 100644 --- a/headers/enum.hpp/enum.hpp +++ b/headers/enum.hpp/enum.hpp @@ -10,6 +10,12 @@ #include #include +namespace enum_hpp +{ + constexpr std::size_t invalid_index = std::size_t(-1); + constexpr std::string_view empty_string = std::string_view(); +} + namespace enum_hpp::detail { template < typename Enum > @@ -124,6 +130,12 @@ namespace enum_hpp::detail }\ return std::nullopt;\ }\ + static constexpr std::string_view to_string_or_empty(Enum e) noexcept {\ + if ( auto s = to_string(e) ) {\ + return *s;\ + }\ + return ::enum_hpp::empty_string;\ + }\ \ static constexpr std::optional from_string(std::string_view name) noexcept {\ for ( std::size_t i = 0; i < size; ++i) {\ @@ -143,6 +155,13 @@ namespace enum_hpp::detail return std::nullopt;\ }\ \ + static constexpr std::size_t to_index_or_invalid(Enum e) noexcept {\ + if ( auto i = to_index(e) ) {\ + return *i;\ + }\ + return ::enum_hpp::invalid_index;\ + }\ + \ static constexpr std::optional from_index(std::size_t index) noexcept {\ if ( index < size ) {\ return values[index];\ diff --git a/untests/enum_tests.cpp b/untests/enum_tests.cpp index 348693e..f63d48f 100644 --- a/untests/enum_tests.cpp +++ b/untests/enum_tests.cpp @@ -133,19 +133,36 @@ 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_or_empty(sn::color::red) == "red"); + STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color::green) == "green"); + STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color::blue) == "blue"); + STATIC_REQUIRE_FALSE(sn::color_traits::to_string(sn::color(42))); + STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color(42)) == ""); } { STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::none) == "none"); STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::color) == "color"); STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::alpha) == "alpha"); STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::all) == "all"); + + STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::none) == "none"); + STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::color) == "color"); + STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::alpha) == "alpha"); + STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::all) == "all"); } { 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_or_empty(sn::_0) == "_0"); + STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::_180) == "_180"); + STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::_240) == "_240"); + STATIC_REQUIRE_FALSE(sn::numbers_traits::to_string(sn::numbers(100500))); + STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::numbers(100500)) == ""); } } @@ -175,19 +192,36 @@ TEST_CASE("enum") { 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(sn::color_traits::to_index_or_invalid(sn::color::red) == 0u); + STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color::green) == 1u); + STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color::blue) == 2u); + STATIC_REQUIRE_FALSE(sn::color_traits::to_index(sn::color(42))); + STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color(42)) == enum_hpp::invalid_index); } { 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::render::mask_traits::to_index_or_invalid(sn::render::mask::none) == 0u); + STATIC_REQUIRE(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::color) == 1u); + STATIC_REQUIRE(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::alpha) == 2u); + STATIC_REQUIRE(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::all) == 3u); } { 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(sn::numbers_traits::to_index_or_invalid(sn::_0) == 0u); + STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::_180) == 180u); + STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::_240) == 240u); + STATIC_REQUIRE_FALSE(sn::numbers_traits::to_index(sn::numbers(100500))); + STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::numbers(100500)) == enum_hpp::invalid_index); } } From c0497c2e5d3e33a1aea2c728a94beb0cdc8942e4 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 22 Nov 2019 05:08:42 +0700 Subject: [PATCH 2/4] add to_string_or_throw and to_index_or_throw traits functions --- headers/enum.hpp/enum.hpp | 19 +++++++++++++++++++ untests/enum_tests.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/headers/enum.hpp/enum.hpp b/headers/enum.hpp/enum.hpp index f3bc598..05c08eb 100644 --- a/headers/enum.hpp/enum.hpp +++ b/headers/enum.hpp/enum.hpp @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include @@ -14,6 +15,12 @@ namespace enum_hpp { constexpr std::size_t invalid_index = std::size_t(-1); constexpr std::string_view empty_string = std::string_view(); + + class exception final : public std::runtime_error { + public: + explicit exception(const char* what) + : std::runtime_error(what) {} + }; } namespace enum_hpp::detail @@ -136,6 +143,12 @@ namespace enum_hpp::detail }\ return ::enum_hpp::empty_string;\ }\ + static std::string_view to_string_or_throw(Enum e) {\ + if ( auto s = to_string(e) ) {\ + return *s;\ + }\ + throw ::enum_hpp::exception(#Enum "_traits::to_string_or_throw(): invalid argument");\ + }\ \ static constexpr std::optional from_string(std::string_view name) noexcept {\ for ( std::size_t i = 0; i < size; ++i) {\ @@ -161,6 +174,12 @@ namespace enum_hpp::detail }\ return ::enum_hpp::invalid_index;\ }\ + static std::size_t to_index_or_throw(Enum e) {\ + if ( auto i = to_index(e) ) {\ + return *i;\ + }\ + throw ::enum_hpp::exception(#Enum "_traits::to_index_or_throw(): invalid argument");\ + }\ \ static constexpr std::optional from_index(std::size_t index) noexcept {\ if ( index < size ) {\ diff --git a/untests/enum_tests.cpp b/untests/enum_tests.cpp index f63d48f..52919a9 100644 --- a/untests/enum_tests.cpp +++ b/untests/enum_tests.cpp @@ -138,8 +138,13 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color::green) == "green"); STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color::blue) == "blue"); + REQUIRE(sn::color_traits::to_string_or_throw(sn::color::red) == "red"); + REQUIRE(sn::color_traits::to_string_or_throw(sn::color::green) == "green"); + REQUIRE(sn::color_traits::to_string_or_throw(sn::color::blue) == "blue"); + STATIC_REQUIRE_FALSE(sn::color_traits::to_string(sn::color(42))); STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color(42)) == ""); + REQUIRE_THROWS_AS(sn::color_traits::to_string_or_throw(sn::color(42)), enum_hpp::exception); } { STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::none) == "none"); @@ -151,6 +156,11 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::color) == "color"); STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::alpha) == "alpha"); STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::all) == "all"); + + REQUIRE(sn::render::mask_traits::to_string_or_throw(sn::render::mask::none) == "none"); + REQUIRE(sn::render::mask_traits::to_string_or_throw(sn::render::mask::color) == "color"); + REQUIRE(sn::render::mask_traits::to_string_or_throw(sn::render::mask::alpha) == "alpha"); + REQUIRE(sn::render::mask_traits::to_string_or_throw(sn::render::mask::all) == "all"); } { STATIC_REQUIRE(sn::numbers_traits::to_string(sn::_0) == "_0"); @@ -161,8 +171,13 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::_180) == "_180"); STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::_240) == "_240"); + REQUIRE(sn::numbers_traits::to_string_or_throw(sn::_0) == "_0"); + REQUIRE(sn::numbers_traits::to_string_or_throw(sn::_180) == "_180"); + REQUIRE(sn::numbers_traits::to_string_or_throw(sn::_240) == "_240"); + STATIC_REQUIRE_FALSE(sn::numbers_traits::to_string(sn::numbers(100500))); STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::numbers(100500)) == ""); + REQUIRE_THROWS_AS(sn::numbers_traits::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception); } } @@ -197,8 +212,13 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color::green) == 1u); STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color::blue) == 2u); + REQUIRE(sn::color_traits::to_index_or_throw(sn::color::red) == 0u); + REQUIRE(sn::color_traits::to_index_or_throw(sn::color::green) == 1u); + REQUIRE(sn::color_traits::to_index_or_throw(sn::color::blue) == 2u); + STATIC_REQUIRE_FALSE(sn::color_traits::to_index(sn::color(42))); STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color(42)) == enum_hpp::invalid_index); + REQUIRE_THROWS_AS(sn::color_traits::to_index_or_throw(sn::color(42)), enum_hpp::exception); } { STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::none) == 0u); @@ -210,6 +230,11 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::color) == 1u); STATIC_REQUIRE(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::alpha) == 2u); STATIC_REQUIRE(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::all) == 3u); + + REQUIRE(sn::render::mask_traits::to_index_or_throw(sn::render::mask::none) == 0u); + REQUIRE(sn::render::mask_traits::to_index_or_throw(sn::render::mask::color) == 1u); + REQUIRE(sn::render::mask_traits::to_index_or_throw(sn::render::mask::alpha) == 2u); + REQUIRE(sn::render::mask_traits::to_index_or_throw(sn::render::mask::all) == 3u); } { STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_0) == 0u); @@ -220,8 +245,13 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::_180) == 180u); STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::_240) == 240u); + REQUIRE(sn::numbers_traits::to_index_or_throw(sn::_0) == 0u); + REQUIRE(sn::numbers_traits::to_index_or_throw(sn::_180) == 180u); + REQUIRE(sn::numbers_traits::to_index_or_throw(sn::_240) == 240u); + STATIC_REQUIRE_FALSE(sn::numbers_traits::to_index(sn::numbers(100500))); STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::numbers(100500)) == enum_hpp::invalid_index); + REQUIRE_THROWS_AS(sn::numbers_traits::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception); } } From c783f22be30e24f8de385042cd1316a0c97f91e6 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 22 Nov 2019 05:21:16 +0700 Subject: [PATCH 3/4] add from_XXX_or_default, from_XXX_or_throw traits functions --- headers/enum.hpp/enum.hpp | 24 ++++++++++++++++++++++++ untests/enum_tests.cpp | 22 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/headers/enum.hpp/enum.hpp b/headers/enum.hpp/enum.hpp index 05c08eb..c3318e1 100644 --- a/headers/enum.hpp/enum.hpp +++ b/headers/enum.hpp/enum.hpp @@ -158,6 +158,18 @@ namespace enum_hpp::detail }\ return std::nullopt;\ }\ + static constexpr Enum from_string_or_default(std::string_view name, Enum def) noexcept {\ + if ( auto e = from_string(name) ) {\ + return *e;\ + }\ + return def;\ + }\ + static Enum from_string_or_throw(std::string_view name) {\ + if ( auto e = from_string(name) ) {\ + return *e;\ + }\ + throw ::enum_hpp::exception(#Enum "_traits::from_string_or_throw(): invalid argument");\ + }\ \ static constexpr std::optional to_index(Enum e) noexcept {\ for ( std::size_t i = 0; i < size; ++i ) {\ @@ -187,6 +199,18 @@ namespace enum_hpp::detail }\ return std::nullopt;\ }\ + static constexpr Enum from_index_or_default(std::size_t index, Enum def) noexcept {\ + if ( auto e = from_index(index) ) {\ + return *e;\ + }\ + return def;\ + }\ + static Enum from_index_or_throw(std::size_t index) {\ + if ( auto e = from_index(index) ) {\ + return *e;\ + }\ + throw ::enum_hpp::exception(#Enum "_traits::from_index_or_throw(): invalid argument");\ + }\ }; // ----------------------------------------------------------------------------- diff --git a/untests/enum_tests.cpp b/untests/enum_tests.cpp index 52919a9..d3756bf 100644 --- a/untests/enum_tests.cpp +++ b/untests/enum_tests.cpp @@ -186,7 +186,18 @@ TEST_CASE("enum") { 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(sn::color_traits::from_string_or_default("red", sn::color::green) == sn::color::red); + STATIC_REQUIRE(sn::color_traits::from_string_or_default("green", sn::color::red) == sn::color::green); + STATIC_REQUIRE(sn::color_traits::from_string_or_default("blue", sn::color::red) == sn::color::blue); + + REQUIRE(sn::color_traits::from_string_or_throw("red") == sn::color::red); + REQUIRE(sn::color_traits::from_string_or_throw("green") == sn::color::green); + REQUIRE(sn::color_traits::from_string_or_throw("blue") == sn::color::blue); + STATIC_REQUIRE_FALSE(sn::color_traits::from_string("42")); + STATIC_REQUIRE(sn::color_traits::from_string_or_default("42", sn::color::red) == sn::color::red); + REQUIRE_THROWS_AS(sn::color_traits::from_string_or_throw("42"), enum_hpp::exception); } { STATIC_REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none); @@ -260,7 +271,18 @@ TEST_CASE("enum") { 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(sn::color_traits::from_index_or_default(0, sn::color::green) == sn::color::red); + STATIC_REQUIRE(sn::color_traits::from_index_or_default(1, sn::color::red) == sn::color::green); + STATIC_REQUIRE(sn::color_traits::from_index_or_default(2, sn::color::red) == sn::color::blue); + + REQUIRE(sn::color_traits::from_index_or_throw(0) == sn::color::red); + REQUIRE(sn::color_traits::from_index_or_throw(1) == sn::color::green); + REQUIRE(sn::color_traits::from_index_or_throw(2) == sn::color::blue); + STATIC_REQUIRE_FALSE(sn::color_traits::from_index(42)); + STATIC_REQUIRE(sn::color_traits::from_index_or_default(42, sn::color::red) == sn::color::red); + REQUIRE_THROWS_AS(sn::color_traits::from_index_or_throw(42), enum_hpp::exception); } { STATIC_REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none); From ab80704d5408c6ccecdc0d314363325aea5de43e Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 22 Nov 2019 05:25:55 +0700 Subject: [PATCH 4/4] update readme to new api --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 2dd0812..0af12db 100644 --- a/README.md +++ b/README.md @@ -81,10 +81,20 @@ struct debug_level_traits { static constexpr underlying_type to_underlying(debug_level e) noexcept; static constexpr std::optional 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 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 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 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); }; ``` @@ -125,10 +135,20 @@ struct color_traits { static constexpr underlying_type to_underlying(color e) noexcept; static constexpr std::optional 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 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 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 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); }; ```