add to_string_or_throw and to_index_or_throw traits functions

This commit is contained in:
2019-11-22 05:08:42 +07:00
parent 080acf3611
commit c0497c2e5d
2 changed files with 49 additions and 0 deletions

View File

@@ -7,6 +7,7 @@
#pragma once
#include <optional>
#include <stdexcept>
#include <string_view>
#include <type_traits>
@@ -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<Enum> 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<Enum> from_index(std::size_t index) noexcept {\
if ( index < size ) {\

View File

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