From 7fcfef63f7b71300c0fdab905e7efe3b159b6782 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 13 Jan 2023 03:33:22 +0700 Subject: [PATCH] xxx_as functions for enum types --- develop/singles/headers/meta.hpp/meta_all.hpp | 24 +++++++++++++++++++ develop/untests/meta_states/evalue_tests.cpp | 2 ++ .../untests/meta_types/enum_type_tests.cpp | 16 +++++++++++++ headers/meta.hpp/meta_states.hpp | 6 +++++ headers/meta.hpp/meta_states/evalue.hpp | 10 ++++++++ headers/meta.hpp/meta_types.hpp | 3 +++ headers/meta.hpp/meta_types/enum_type.hpp | 5 ++++ 7 files changed, 66 insertions(+) diff --git a/develop/singles/headers/meta.hpp/meta_all.hpp b/develop/singles/headers/meta.hpp/meta_all.hpp index 05fd69a..e62449a 100644 --- a/develop/singles/headers/meta.hpp/meta_all.hpp +++ b/develop/singles/headers/meta.hpp/meta_all.hpp @@ -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(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(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(); + } } 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(); + } + + template < typename T > + T evalue::get_underlying_value_as() const { + return get_underlying_value().get_as(); + } } namespace meta_hpp::detail diff --git a/develop/untests/meta_states/evalue_tests.cpp b/develop/untests/meta_states/evalue_tests.cpp index ed4a69c..f4d2d6e 100644 --- a/develop/untests/meta_states/evalue_tests.cpp +++ b/develop/untests/meta_states/evalue_tests.cpp @@ -54,9 +54,11 @@ TEST_CASE("meta/meta_states/evalue") { CHECK(evalue.get_name() == "green"); CHECK(evalue.get_value().get_as() == color::green); + CHECK(evalue.get_value_as() == color::green); CHECK(evalue.get_value().get_type() == color_type); CHECK(evalue.get_underlying_value().get_as() == meta::detail::to_underlying(color::green)); + CHECK(evalue.get_underlying_value_as() == meta::detail::to_underlying(color::green)); CHECK(evalue.get_underlying_value().get_type() == color_type.get_underlying_type()); } } diff --git a/develop/untests/meta_types/enum_type_tests.cpp b/develop/untests/meta_types/enum_type_tests.cpp index 243093d..03eb552 100644 --- a/develop/untests/meta_types/enum_type_tests.cpp +++ b/develop/untests/meta_types/enum_type_tests.cpp @@ -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::green); + CHECK(green_value.get_value_as() == color::green); + CHECK(green_value.get_underlying_value().get_as() == meta::detail::to_underlying(color::green)); + CHECK(green_value.get_underlying_value_as() == 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_green); + CHECK(green_value.get_value_as() == ecolor_green); + CHECK(green_value.get_underlying_value().get_as() == meta::detail::to_underlying(ecolor_green)); + CHECK(green_value.get_underlying_value_as() == 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::blue); + CHECK(color_type.name_to_value_as("blue") == color::blue); + CHECK_THROWS(std::ignore = color_type.name_to_value_as("blue")); } { REQUIRE_FALSE(color_type.name_to_value("yellow")); + CHECK_THROWS(std::ignore = color_type.name_to_value_as("yellow")); + CHECK_THROWS(std::ignore = color_type.name_to_value_as("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_blue); + CHECK(ecolor_type.name_to_value_as("blue") == ecolor_blue); + CHECK_THROWS(std::ignore = ecolor_type.name_to_value_as("blue")); } { REQUIRE_FALSE(ecolor_type.name_to_value("yellow")); + CHECK_THROWS(std::ignore = ecolor_type.name_to_value_as("yellow")); + CHECK_THROWS(std::ignore = ecolor_type.name_to_value_as("yellow")); } } } diff --git a/headers/meta.hpp/meta_states.hpp b/headers/meta.hpp/meta_states.hpp index 58e8cdd..eef6da1 100644 --- a/headers/meta.hpp/meta_states.hpp +++ b/headers/meta.hpp/meta_states.hpp @@ -197,6 +197,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(const evalue&); diff --git a/headers/meta.hpp/meta_states/evalue.hpp b/headers/meta.hpp/meta_states/evalue.hpp index bb061fe..6b30f19 100644 --- a/headers/meta.hpp/meta_states/evalue.hpp +++ b/headers/meta.hpp/meta_states/evalue.hpp @@ -65,4 +65,14 @@ 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(); + } + + template < typename T > + T evalue::get_underlying_value_as() const { + return get_underlying_value().get_as(); + } } diff --git a/headers/meta.hpp/meta_types.hpp b/headers/meta.hpp/meta_types.hpp index d6e3a15..880626b 100644 --- a/headers/meta.hpp/meta_types.hpp +++ b/headers/meta.hpp/meta_types.hpp @@ -275,6 +275,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(const enum_type&); diff --git a/headers/meta.hpp/meta_types/enum_type.hpp b/headers/meta.hpp/meta_types/enum_type.hpp index 0e72405..88541b7 100644 --- a/headers/meta.hpp/meta_types/enum_type.hpp +++ b/headers/meta.hpp/meta_types/enum_type.hpp @@ -91,4 +91,9 @@ 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(); + } }