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