optional using instead throw and nothrow functions

This commit is contained in:
2019-11-22 03:36:48 +07:00
parent e488de0d46
commit aaf1099717
2 changed files with 37 additions and 134 deletions

View File

@@ -6,21 +6,10 @@
#pragma once #pragma once
#include <stdexcept> #include <optional>
#include <string_view> #include <string_view>
#include <type_traits> #include <type_traits>
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 namespace enum_hpp::detail
{ {
template < typename Enum > template < typename Enum >
@@ -127,56 +116,38 @@ namespace enum_hpp::detail
return static_cast<underlying_t>(e);\ return static_cast<underlying_t>(e);\
}\ }\
\ \
static constexpr std::string_view to_string(Enum e) noexcept {\ static constexpr std::optional<std::string_view> to_string(Enum e) noexcept {\
for ( std::size_t i = 0; i < size; ++i) {\ for ( std::size_t i = 0; i < size; ++i) {\
if ( e == values[i] ) {\ if ( e == values[i] ) {\
return names[i];\ return names[i];\
}\ }\
}\ }\
return std::string_view();\ return std::nullopt;\
}\ }\
\ \
static Enum from_string(std::string_view name){\ static constexpr std::optional<Enum> from_string(std::string_view name) noexcept {\
for ( std::size_t i = 0; i < size; ++i) {\ for ( std::size_t i = 0; i < size; ++i) {\
if ( name == names[i] ) {\ if ( name == names[i] ) {\
return values[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 {\ static constexpr std::optional<std::size_t> to_index(Enum e) 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 {\
for ( std::size_t i = 0; i < size; ++i ) {\ for ( std::size_t i = 0; i < size; ++i ) {\
if ( e == values[i] ) {\ if ( e == values[i] ) {\
return i;\ return i;\
}\ }\
}\ }\
return ::enum_hpp::invalid_index;\ return std::nullopt;\
}\ }\
\ \
static Enum from_index(std::size_t index) {\ static constexpr std::optional<Enum> from_index(std::size_t index) noexcept {\
if ( index < size ) {\ if ( index < size ) {\
return values[index];\ return values[index];\
}\ }\
throw ::enum_hpp::exception(#Enum "_traits::from_index(): invalid argument");\ return std::nullopt;\
}\
\
static constexpr bool from_index_nothrow(std::size_t index, Enum& result) noexcept {\
if ( index < size ) {\
result = values[index];\
return true;\
}\
return false;\
}\ }\
}; };

View File

@@ -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::red) == "red");
STATIC_REQUIRE(sn::color_traits::to_string(sn::color::green) == "green"); 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::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"); 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::_0) == "_0");
STATIC_REQUIRE(sn::numbers_traits::to_string(sn::_180) == "_180"); 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::_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") { SECTION("from_string") {
{ {
REQUIRE(sn::color_traits::from_string("red") == sn::color::red); STATIC_REQUIRE(sn::color_traits::from_string("red") == sn::color::red);
REQUIRE(sn::color_traits::from_string("green") == sn::color::green); STATIC_REQUIRE(sn::color_traits::from_string("green") == sn::color::green);
REQUIRE(sn::color_traits::from_string("blue") == sn::color::blue); STATIC_REQUIRE(sn::color_traits::from_string("blue") == sn::color::blue);
REQUIRE_THROWS_AS(sn::color_traits::from_string("42"), enum_hpp::exception); STATIC_REQUIRE_FALSE(sn::color_traits::from_string("42"));
} }
{ {
REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none); STATIC_REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none);
REQUIRE(sn::render::mask_traits::from_string("color") == sn::render::mask::color); STATIC_REQUIRE(sn::render::mask_traits::from_string("color") == sn::render::mask::color);
REQUIRE(sn::render::mask_traits::from_string("alpha") == sn::render::mask::alpha); STATIC_REQUIRE(sn::render::mask_traits::from_string("alpha") == sn::render::mask::alpha);
REQUIRE(sn::render::mask_traits::from_string("all") == sn::render::mask::all); STATIC_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_FALSE(sn::render::mask_traits::from_string("42"));
} }
{ {
REQUIRE(sn::numbers_traits::from_string("_10") == sn::_10); STATIC_REQUIRE(sn::numbers_traits::from_string("_10") == sn::_10);
REQUIRE(sn::numbers_traits::from_string("_240") == sn::_240); STATIC_REQUIRE(sn::numbers_traits::from_string("_240") == sn::_240);
REQUIRE_THROWS_AS(sn::numbers_traits::from_string("error"), enum_hpp::exception); STATIC_REQUIRE_FALSE(sn::numbers_traits::from_string("error"));
}
}
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);
} }
} }
@@ -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::red) == 0);
STATIC_REQUIRE(sn::color_traits::to_index(sn::color::green) == 1); 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::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); 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::_0) == 0);
STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_180) == 180); 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::_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") { SECTION("from_index") {
{ {
REQUIRE(sn::color_traits::from_index(0) == sn::color::red); STATIC_REQUIRE(sn::color_traits::from_index(0) == sn::color::red);
REQUIRE(sn::color_traits::from_index(1) == sn::color::green); STATIC_REQUIRE(sn::color_traits::from_index(1) == sn::color::green);
REQUIRE(sn::color_traits::from_index(2) == sn::color::blue); STATIC_REQUIRE(sn::color_traits::from_index(2) == sn::color::blue);
REQUIRE_THROWS_AS(sn::color_traits::from_index(42), enum_hpp::exception); STATIC_REQUIRE_FALSE(sn::color_traits::from_index(42));
} }
{ {
REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none); STATIC_REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none);
REQUIRE(sn::render::mask_traits::from_index(1) == sn::render::mask::color); STATIC_REQUIRE(sn::render::mask_traits::from_index(1) == sn::render::mask::color);
REQUIRE(sn::render::mask_traits::from_index(2) == sn::render::mask::alpha); STATIC_REQUIRE(sn::render::mask_traits::from_index(2) == sn::render::mask::alpha);
REQUIRE(sn::render::mask_traits::from_index(3) == sn::render::mask::all); STATIC_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_FALSE(sn::render::mask_traits::from_index(42));
} }
{ {
REQUIRE(sn::numbers_traits::from_index(10) == sn::_10); STATIC_REQUIRE(sn::numbers_traits::from_index(10) == sn::_10);
REQUIRE(sn::numbers_traits::from_index(240) == sn::_240); STATIC_REQUIRE(sn::numbers_traits::from_index(240) == sn::_240);
REQUIRE_THROWS_AS(sn::numbers_traits::from_index(100500), enum_hpp::exception); STATIC_REQUIRE_FALSE(sn::numbers_traits::from_index(100500));
}
}
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);
} }
} }
} }