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); } }