mirror of
https://github.com/BlackMATov/enum.hpp.git
synced 2025-12-13 15:52:02 +07:00
Merge branch 'refs/heads/dev'
This commit is contained in:
20
README.md
20
README.md
@@ -81,10 +81,20 @@ struct debug_level_traits {
|
||||
static constexpr underlying_type to_underlying(debug_level e) noexcept;
|
||||
|
||||
static constexpr std::optional<std::string_view> to_string(debug_level e) noexcept;
|
||||
static constexpr std::string_view to_string_or_empty(Enum e) noexcept;
|
||||
static std::string_view to_string_or_throw(Enum e);
|
||||
|
||||
static constexpr std::optional<debug_level> from_string(std::string_view name) noexcept;
|
||||
static constexpr debug_level from_string_or_default(std::string_view name, debug_level def) noexcept;
|
||||
static debug_level from_string_or_throw(std::string_view name);
|
||||
|
||||
static constexpr std::optional<std::size_t> to_index(debug_level e) noexcept;
|
||||
static constexpr std::size_t to_index_or_invalid(debug_level e) noexcept;
|
||||
static std::size_t to_index_or_throw(debug_level e);
|
||||
|
||||
static constexpr std::optional<debug_level> from_index(std::size_t index) noexcept;
|
||||
static constexpr debug_level from_index_or_default(std::size_t index, debug_level def) noexcept;
|
||||
static debug_level from_index_or_throw(std::size_t index);
|
||||
};
|
||||
```
|
||||
|
||||
@@ -125,10 +135,20 @@ struct color_traits {
|
||||
static constexpr underlying_type to_underlying(color e) noexcept;
|
||||
|
||||
static constexpr std::optional<std::string_view> to_string(color e) noexcept;
|
||||
static constexpr std::string_view to_string_or_empty(Enum e) noexcept;
|
||||
static std::string_view to_string_or_throw(Enum e);
|
||||
|
||||
static constexpr std::optional<color> from_string(std::string_view name) noexcept;
|
||||
static constexpr color from_string_or_default(std::string_view name, color def) noexcept;
|
||||
static color from_string_or_throw(std::string_view name);
|
||||
|
||||
static constexpr std::optional<std::size_t> to_index(color e) noexcept;
|
||||
static constexpr std::size_t to_index_or_invalid(color e) noexcept;
|
||||
static std::size_t to_index_or_throw(color e);
|
||||
|
||||
static constexpr std::optional<color> from_index(std::size_t index) noexcept;
|
||||
static constexpr color from_index_or_default(std::size_t index, color def) noexcept;
|
||||
static color from_index_or_throw(std::size_t index);
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
@@ -7,9 +7,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
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
|
||||
{
|
||||
template < typename Enum >
|
||||
@@ -124,6 +137,18 @@ 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 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) {\
|
||||
@@ -133,6 +158,18 @@ namespace enum_hpp::detail
|
||||
}\
|
||||
return std::nullopt;\
|
||||
}\
|
||||
static constexpr Enum from_string_or_default(std::string_view name, Enum def) noexcept {\
|
||||
if ( auto e = from_string(name) ) {\
|
||||
return *e;\
|
||||
}\
|
||||
return def;\
|
||||
}\
|
||||
static Enum from_string_or_throw(std::string_view name) {\
|
||||
if ( auto e = from_string(name) ) {\
|
||||
return *e;\
|
||||
}\
|
||||
throw ::enum_hpp::exception(#Enum "_traits::from_string_or_throw(): invalid argument");\
|
||||
}\
|
||||
\
|
||||
static constexpr std::optional<std::size_t> to_index(Enum e) noexcept {\
|
||||
for ( std::size_t i = 0; i < size; ++i ) {\
|
||||
@@ -143,12 +180,37 @@ 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 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 ) {\
|
||||
return values[index];\
|
||||
}\
|
||||
return std::nullopt;\
|
||||
}\
|
||||
static constexpr Enum from_index_or_default(std::size_t index, Enum def) noexcept {\
|
||||
if ( auto e = from_index(index) ) {\
|
||||
return *e;\
|
||||
}\
|
||||
return def;\
|
||||
}\
|
||||
static Enum from_index_or_throw(std::size_t index) {\
|
||||
if ( auto e = from_index(index) ) {\
|
||||
return *e;\
|
||||
}\
|
||||
throw ::enum_hpp::exception(#Enum "_traits::from_index_or_throw(): invalid argument");\
|
||||
}\
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -133,19 +133,51 @@ 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");
|
||||
|
||||
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");
|
||||
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");
|
||||
|
||||
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");
|
||||
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");
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +186,18 @@ TEST_CASE("enum") {
|
||||
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(sn::color_traits::from_string_or_default("red", sn::color::green) == sn::color::red);
|
||||
STATIC_REQUIRE(sn::color_traits::from_string_or_default("green", sn::color::red) == sn::color::green);
|
||||
STATIC_REQUIRE(sn::color_traits::from_string_or_default("blue", sn::color::red) == sn::color::blue);
|
||||
|
||||
REQUIRE(sn::color_traits::from_string_or_throw("red") == sn::color::red);
|
||||
REQUIRE(sn::color_traits::from_string_or_throw("green") == sn::color::green);
|
||||
REQUIRE(sn::color_traits::from_string_or_throw("blue") == sn::color::blue);
|
||||
|
||||
STATIC_REQUIRE_FALSE(sn::color_traits::from_string("42"));
|
||||
STATIC_REQUIRE(sn::color_traits::from_string_or_default("42", sn::color::red) == sn::color::red);
|
||||
REQUIRE_THROWS_AS(sn::color_traits::from_string_or_throw("42"), enum_hpp::exception);
|
||||
}
|
||||
{
|
||||
STATIC_REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none);
|
||||
@@ -175,19 +218,51 @@ 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);
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,7 +271,18 @@ TEST_CASE("enum") {
|
||||
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(sn::color_traits::from_index_or_default(0, sn::color::green) == sn::color::red);
|
||||
STATIC_REQUIRE(sn::color_traits::from_index_or_default(1, sn::color::red) == sn::color::green);
|
||||
STATIC_REQUIRE(sn::color_traits::from_index_or_default(2, sn::color::red) == sn::color::blue);
|
||||
|
||||
REQUIRE(sn::color_traits::from_index_or_throw(0) == sn::color::red);
|
||||
REQUIRE(sn::color_traits::from_index_or_throw(1) == sn::color::green);
|
||||
REQUIRE(sn::color_traits::from_index_or_throw(2) == sn::color::blue);
|
||||
|
||||
STATIC_REQUIRE_FALSE(sn::color_traits::from_index(42));
|
||||
STATIC_REQUIRE(sn::color_traits::from_index_or_default(42, sn::color::red) == sn::color::red);
|
||||
REQUIRE_THROWS_AS(sn::color_traits::from_index_or_throw(42), enum_hpp::exception);
|
||||
}
|
||||
{
|
||||
STATIC_REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none);
|
||||
|
||||
Reference in New Issue
Block a user