From aaf1099717644d29d60200045ad7d903c53480e6 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 22 Nov 2019 03:36:48 +0700 Subject: [PATCH] optional using instead throw and nothrow functions --- headers/enum.hpp/enum.hpp | 47 +++------------ untests/enum_tests.cpp | 124 +++++++++----------------------------- 2 files changed, 37 insertions(+), 134 deletions(-) diff --git a/headers/enum.hpp/enum.hpp b/headers/enum.hpp/enum.hpp index e197e40..ebb961f 100644 --- a/headers/enum.hpp/enum.hpp +++ b/headers/enum.hpp/enum.hpp @@ -6,21 +6,10 @@ #pragma once -#include +#include #include #include -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 { template < typename Enum > @@ -127,56 +116,38 @@ namespace enum_hpp::detail return static_cast(e);\ }\ \ - static constexpr std::string_view to_string(Enum e) noexcept {\ + static constexpr std::optional to_string(Enum e) noexcept {\ for ( std::size_t i = 0; i < size; ++i) {\ if ( e == values[i] ) {\ return names[i];\ }\ }\ - return std::string_view();\ + return std::nullopt;\ }\ \ - static Enum from_string(std::string_view name){\ + static constexpr std::optional from_string(std::string_view name) noexcept {\ for ( std::size_t i = 0; i < size; ++i) {\ if ( name == names[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 {\ - 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 {\ + static constexpr std::optional to_index(Enum e) noexcept {\ for ( std::size_t i = 0; i < size; ++i ) {\ if ( e == values[i] ) {\ return i;\ }\ }\ - return ::enum_hpp::invalid_index;\ + return std::nullopt;\ }\ \ - static Enum from_index(std::size_t index) {\ + static constexpr std::optional from_index(std::size_t index) noexcept {\ if ( index < size ) {\ return values[index];\ }\ - throw ::enum_hpp::exception(#Enum "_traits::from_index(): invalid argument");\ - }\ - \ - static constexpr bool from_index_nothrow(std::size_t index, Enum& result) noexcept {\ - if ( index < size ) {\ - result = values[index];\ - return true;\ - }\ - return false;\ + return std::nullopt;\ }\ }; diff --git a/untests/enum_tests.cpp b/untests/enum_tests.cpp index 72365bf..b16b000 100644 --- a/untests/enum_tests.cpp +++ b/untests/enum_tests.cpp @@ -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::green) == "green"); 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"); @@ -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::_180) == "_180"); 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") { { - REQUIRE(sn::color_traits::from_string("red") == sn::color::red); - REQUIRE(sn::color_traits::from_string("green") == sn::color::green); - REQUIRE(sn::color_traits::from_string("blue") == sn::color::blue); - REQUIRE_THROWS_AS(sn::color_traits::from_string("42"), enum_hpp::exception); + 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_FALSE(sn::color_traits::from_string("42")); } { - REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none); - REQUIRE(sn::render::mask_traits::from_string("color") == sn::render::mask::color); - REQUIRE(sn::render::mask_traits::from_string("alpha") == sn::render::mask::alpha); - 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(sn::render::mask_traits::from_string("none") == sn::render::mask::none); + STATIC_REQUIRE(sn::render::mask_traits::from_string("color") == sn::render::mask::color); + STATIC_REQUIRE(sn::render::mask_traits::from_string("alpha") == sn::render::mask::alpha); + STATIC_REQUIRE(sn::render::mask_traits::from_string("all") == sn::render::mask::all); + STATIC_REQUIRE_FALSE(sn::render::mask_traits::from_string("42")); } { - REQUIRE(sn::numbers_traits::from_string("_10") == sn::_10); - REQUIRE(sn::numbers_traits::from_string("_240") == sn::_240); - REQUIRE_THROWS_AS(sn::numbers_traits::from_string("error"), enum_hpp::exception); - } - } - - 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); + STATIC_REQUIRE(sn::numbers_traits::from_string("_10") == sn::_10); + STATIC_REQUIRE(sn::numbers_traits::from_string("_240") == sn::_240); + STATIC_REQUIRE_FALSE(sn::numbers_traits::from_string("error")); } } @@ -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::green) == 1); 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); @@ -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::_180) == 180); 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") { { - REQUIRE(sn::color_traits::from_index(0) == sn::color::red); - REQUIRE(sn::color_traits::from_index(1) == sn::color::green); - REQUIRE(sn::color_traits::from_index(2) == sn::color::blue); - REQUIRE_THROWS_AS(sn::color_traits::from_index(42), enum_hpp::exception); + 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_FALSE(sn::color_traits::from_index(42)); } { - REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none); - REQUIRE(sn::render::mask_traits::from_index(1) == sn::render::mask::color); - REQUIRE(sn::render::mask_traits::from_index(2) == sn::render::mask::alpha); - 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(sn::render::mask_traits::from_index(0) == sn::render::mask::none); + STATIC_REQUIRE(sn::render::mask_traits::from_index(1) == sn::render::mask::color); + STATIC_REQUIRE(sn::render::mask_traits::from_index(2) == sn::render::mask::alpha); + STATIC_REQUIRE(sn::render::mask_traits::from_index(3) == sn::render::mask::all); + STATIC_REQUIRE_FALSE(sn::render::mask_traits::from_index(42)); } { - REQUIRE(sn::numbers_traits::from_index(10) == sn::_10); - REQUIRE(sn::numbers_traits::from_index(240) == sn::_240); - REQUIRE_THROWS_AS(sn::numbers_traits::from_index(100500), enum_hpp::exception); - } - } - - 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); + STATIC_REQUIRE(sn::numbers_traits::from_index(10) == sn::_10); + STATIC_REQUIRE(sn::numbers_traits::from_index(240) == sn::_240); + STATIC_REQUIRE_FALSE(sn::numbers_traits::from_index(100500)); } } }