xxx_as functions for enum types

This commit is contained in:
BlackMATov
2023-01-13 03:33:22 +07:00
parent 8bf8666a15
commit 7fcfef63f7
7 changed files with 66 additions and 0 deletions

View File

@@ -1760,6 +1760,9 @@ namespace meta_hpp
template < detail::enum_kind Enum >
[[nodiscard]] std::string_view value_to_name(Enum value) const noexcept;
[[nodiscard]] uvalue name_to_value(std::string_view name) const noexcept;
template < typename T >
[[nodiscard]] T name_to_value_as(std::string_view name) const;
private:
detail::enum_type_data* data_{};
friend auto detail::type_access<enum_type>(const enum_type&);
@@ -2623,6 +2626,12 @@ namespace meta_hpp
[[nodiscard]] const uvalue& get_value() const noexcept;
[[nodiscard]] const uvalue& get_underlying_value() const noexcept;
template < typename T >
[[nodiscard]] T get_value_as() const;
template < typename T >
[[nodiscard]] T get_underlying_value_as() const;
private:
detail::evalue_state_ptr state_;
friend auto detail::state_access<evalue>(const evalue&);
@@ -5699,6 +5708,11 @@ namespace meta_hpp
return uvalue{};
}
template < typename T >
T enum_type::name_to_value_as(std::string_view name) const {
return name_to_value(name).get_as<T>();
}
}
namespace meta_hpp::detail
@@ -5755,6 +5769,16 @@ namespace meta_hpp
inline const uvalue& evalue::get_underlying_value() const noexcept {
return state_->underlying_value;
}
template < typename T >
T evalue::get_value_as() const {
return get_value().get_as<T>();
}
template < typename T >
T evalue::get_underlying_value_as() const {
return get_underlying_value().get_as<T>();
}
}
namespace meta_hpp::detail

View File

@@ -54,9 +54,11 @@ TEST_CASE("meta/meta_states/evalue") {
CHECK(evalue.get_name() == "green");
CHECK(evalue.get_value().get_as<color>() == color::green);
CHECK(evalue.get_value_as<color>() == color::green);
CHECK(evalue.get_value().get_type() == color_type);
CHECK(evalue.get_underlying_value().get_as<unsigned>() == meta::detail::to_underlying(color::green));
CHECK(evalue.get_underlying_value_as<unsigned>() == meta::detail::to_underlying(color::green));
CHECK(evalue.get_underlying_value().get_type() == color_type.get_underlying_type());
}
}

View File

@@ -89,8 +89,12 @@ TEST_CASE("meta/meta_types/enum_type") {
{
const meta::evalue green_value = color_type.get_evalue("green");
REQUIRE(green_value);
CHECK(green_value.get_value().get_as<color>() == color::green);
CHECK(green_value.get_value_as<color>() == color::green);
CHECK(green_value.get_underlying_value().get_as<unsigned>() == meta::detail::to_underlying(color::green));
CHECK(green_value.get_underlying_value_as<unsigned>() == meta::detail::to_underlying(color::green));
}
{
@@ -106,8 +110,12 @@ TEST_CASE("meta/meta_types/enum_type") {
{
const meta::evalue green_value = ecolor_type.get_evalue("green");
REQUIRE(green_value);
CHECK(green_value.get_value().get_as<ecolor>() == ecolor_green);
CHECK(green_value.get_value_as<ecolor>() == ecolor_green);
CHECK(green_value.get_underlying_value().get_as<int>() == meta::detail::to_underlying(ecolor_green));
CHECK(green_value.get_underlying_value_as<int>() == meta::detail::to_underlying(ecolor_green));
}
{
@@ -141,10 +149,14 @@ TEST_CASE("meta/meta_types/enum_type") {
{
REQUIRE(color_type.name_to_value("blue"));
CHECK(color_type.name_to_value("blue").get_as<color>() == color::blue);
CHECK(color_type.name_to_value_as<color>("blue") == color::blue);
CHECK_THROWS(std::ignore = color_type.name_to_value_as<double>("blue"));
}
{
REQUIRE_FALSE(color_type.name_to_value("yellow"));
CHECK_THROWS(std::ignore = color_type.name_to_value_as<color>("yellow"));
CHECK_THROWS(std::ignore = color_type.name_to_value_as<double>("yellow"));
}
}
@@ -155,10 +167,14 @@ TEST_CASE("meta/meta_types/enum_type") {
{
REQUIRE(ecolor_type.name_to_value("blue"));
CHECK(ecolor_type.name_to_value("blue").get_as<ecolor>() == ecolor_blue);
CHECK(ecolor_type.name_to_value_as<ecolor>("blue") == ecolor_blue);
CHECK_THROWS(std::ignore = ecolor_type.name_to_value_as<double>("blue"));
}
{
REQUIRE_FALSE(ecolor_type.name_to_value("yellow"));
CHECK_THROWS(std::ignore = ecolor_type.name_to_value_as<color>("yellow"));
CHECK_THROWS(std::ignore = ecolor_type.name_to_value_as<double>("yellow"));
}
}
}