From 80c82b10272e0c03d96bb327d7a3c8cd9ecc67e9 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 30 Dec 2022 15:10:44 +0700 Subject: [PATCH] remove fully dynamic less and equal uvalue's operators --- .../value_traits/equals_traits.hpp | 34 ----- .../meta_detail/value_traits/less_traits.hpp | 34 ----- headers/meta.hpp/meta_types.hpp | 4 +- headers/meta.hpp/meta_types/enum_type.hpp | 8 +- headers/meta.hpp/meta_uvalue/uvalue.hpp | 59 +-------- manuals/meta_examples/enum_example.cpp | 3 - singles/headers/meta.hpp/meta_all.hpp | 117 ++---------------- untests/meta_types/enum_type_tests.cpp | 4 +- untests/meta_utilities/value4_tests.cpp | 6 +- untests/meta_utilities/value_tests.cpp | 42 ------- 10 files changed, 24 insertions(+), 287 deletions(-) delete mode 100644 headers/meta.hpp/meta_detail/value_traits/equals_traits.hpp delete mode 100644 headers/meta.hpp/meta_detail/value_traits/less_traits.hpp diff --git a/headers/meta.hpp/meta_detail/value_traits/equals_traits.hpp b/headers/meta.hpp/meta_detail/value_traits/equals_traits.hpp deleted file mode 100644 index 9a65bea..0000000 --- a/headers/meta.hpp/meta_detail/value_traits/equals_traits.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - * This file is part of the "https://github.com/blackmatov/meta.hpp" - * For conditions of distribution and use, see copyright notice in LICENSE.md - * Copyright (C) 2021-2022, by Matvey Cherevko (blackmatov@gmail.com) - ******************************************************************************/ - -#pragma once - -#include "../../meta_base.hpp" -#include "../../meta_uvalue.hpp" - -namespace meta_hpp::detail -{ - template < typename T > - struct equals_traits; - - template < typename T > - concept has_equals_traits = requires(const T& l, const T& r) { - { equals_traits{}(l, r) } -> std::convertible_to; - }; -} - -namespace meta_hpp::detail -{ - template < typename T > - requires requires(const T& l, const T& r) { - { std::equal_to<>{}(l, r) } -> std::convertible_to; - } - struct equals_traits { - bool operator()(const T& l, const T& r) const { - return std::equal_to<>{}(l, r); - } - }; -} diff --git a/headers/meta.hpp/meta_detail/value_traits/less_traits.hpp b/headers/meta.hpp/meta_detail/value_traits/less_traits.hpp deleted file mode 100644 index 04bd8b3..0000000 --- a/headers/meta.hpp/meta_detail/value_traits/less_traits.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - * This file is part of the "https://github.com/blackmatov/meta.hpp" - * For conditions of distribution and use, see copyright notice in LICENSE.md - * Copyright (C) 2021-2022, by Matvey Cherevko (blackmatov@gmail.com) - ******************************************************************************/ - -#pragma once - -#include "../../meta_base.hpp" -#include "../../meta_uvalue.hpp" - -namespace meta_hpp::detail -{ - template < typename T > - struct less_traits; - - template < typename T > - concept has_less_traits = requires(const T& l, const T& r) { - { less_traits{}(l, r) } -> std::convertible_to; - }; -} - -namespace meta_hpp::detail -{ - template < typename T > - requires requires(const T& l, const T& r) { - { std::less<>{}(l, r) } -> std::convertible_to; - } - struct less_traits { - bool operator()(const T& l, const T& r) const { - return std::less<>{}(l, r); - } - }; -} diff --git a/headers/meta.hpp/meta_types.hpp b/headers/meta.hpp/meta_types.hpp index 187be35..4186992 100644 --- a/headers/meta.hpp/meta_types.hpp +++ b/headers/meta.hpp/meta_types.hpp @@ -270,8 +270,8 @@ namespace meta_hpp [[nodiscard]] evalue get_evalue(std::string_view name) const noexcept; - template < typename Value > - [[nodiscard]] std::string_view value_to_name(Value&& value) const noexcept; + 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; private: detail::enum_type_data* data_{}; diff --git a/headers/meta.hpp/meta_types/enum_type.hpp b/headers/meta.hpp/meta_types/enum_type.hpp index 732de12..92c003e 100644 --- a/headers/meta.hpp/meta_types/enum_type.hpp +++ b/headers/meta.hpp/meta_types/enum_type.hpp @@ -69,11 +69,9 @@ namespace meta_hpp return evalue{}; } - template < typename Value > - std::string_view enum_type::value_to_name(Value&& value) const noexcept { - const detail::uarg value_arg{std::forward(value)}; - - if ( value_arg.get_raw_type() != *this ) { + template < detail::enum_kind Enum > + std::string_view enum_type::value_to_name(Enum value) const noexcept { + if ( resolve_type() != *this ) { return std::string_view{}; } diff --git a/headers/meta.hpp/meta_uvalue/uvalue.hpp b/headers/meta.hpp/meta_uvalue/uvalue.hpp index b44d145..53e8b43 100644 --- a/headers/meta.hpp/meta_uvalue/uvalue.hpp +++ b/headers/meta.hpp/meta_uvalue/uvalue.hpp @@ -11,10 +11,8 @@ #include "../meta_uvalue.hpp" #include "../meta_detail/value_traits/deref_traits.hpp" -#include "../meta_detail/value_traits/equals_traits.hpp" #include "../meta_detail/value_traits/index_traits.hpp" #include "../meta_detail/value_traits/istream_traits.hpp" -#include "../meta_detail/value_traits/less_traits.hpp" #include "../meta_detail/value_traits/ostream_traits.hpp" #include "../meta_detail/value_utilities/utraits.hpp" @@ -34,9 +32,6 @@ namespace meta_hpp uvalue (*const deref)(const uvalue& from); uvalue (*const index)(const uvalue& from, std::size_t); - bool (*const less)(const uvalue&, const uvalue&); - bool (*const equals)(const uvalue&, const uvalue&); - std::istream& (*const istream)(std::istream&, uvalue&); std::ostream& (*const ostream)(std::ostream&, const uvalue&); @@ -200,22 +195,6 @@ namespace meta_hpp } }, - .less = +[]([[maybe_unused]] const uvalue& l, [[maybe_unused]] const uvalue& r) -> bool { - if constexpr ( detail::has_less_traits ) { - return detail::less_traits{}(l.get_as(), r.get_as()); - } else { - detail::throw_exception_with("value type doesn't have value less traits"); - } - }, - - .equals = +[]([[maybe_unused]] const uvalue& l, [[maybe_unused]] const uvalue& r) -> bool { - if constexpr ( detail::has_equals_traits ) { - return detail::equals_traits{}(l.get_as(), r.get_as()); - } else { - detail::throw_exception_with("value type doesn't have value equals traits"); - } - }, - .istream = +[]([[maybe_unused]] std::istream& is, [[maybe_unused]] uvalue& v) -> std::istream& { if constexpr ( detail::has_istream_traits && !detail::pointer_kind ) { return detail::istream_traits{}(is, v.get_as()); @@ -494,7 +473,7 @@ namespace meta_hpp namespace meta_hpp { - template < typename T > + template < detail::decay_non_value_kind T > [[nodiscard]] bool operator<(const uvalue& l, const T& r) { if ( !static_cast(l) ) { return true; @@ -506,7 +485,7 @@ namespace meta_hpp return (l_type < r_type) || (l_type == r_type && l.get_as() < r); } - template < typename T > + template < detail::decay_non_value_kind T > [[nodiscard]] bool operator<(const T& l, const uvalue& r) { if ( !static_cast(r) ) { return false; @@ -517,26 +496,11 @@ namespace meta_hpp return (l_type < r_type) || (l_type == r_type && l < r.get_as()); } - - [[nodiscard]] inline bool operator<(const uvalue& l, const uvalue& r) { - if ( !static_cast(r) ) { - return false; - } - - if ( !static_cast(l) ) { - return true; - } - - const any_type& l_type = l.get_type(); - const any_type& r_type = r.get_type(); - - return (l_type < r_type) || (l_type == r_type && l.vtable_->less(l, r)); - } } namespace meta_hpp { - template < typename T > + template < detail::decay_non_value_kind T > [[nodiscard]] bool operator==(const uvalue& l, const T& r) { if ( !static_cast(l) ) { return false; @@ -548,7 +512,7 @@ namespace meta_hpp return l_type == r_type && l.get_as() == r; } - template < typename T > + template < detail::decay_non_value_kind T > [[nodiscard]] bool operator==(const T& l, const uvalue& r) { if ( !static_cast(r) ) { return false; @@ -559,21 +523,6 @@ namespace meta_hpp return l_type == r_type && l == r.get_as(); } - - [[nodiscard]] inline bool operator==(const uvalue& l, const uvalue& r) { - if ( static_cast(l) != static_cast(r) ) { - return false; - } - - if ( !static_cast(l) ) { - return true; - } - - const any_type& l_type = l.get_type(); - const any_type& r_type = r.get_type(); - - return l_type == r_type && l.vtable_->equals(l, r); - } } namespace meta_hpp diff --git a/manuals/meta_examples/enum_example.cpp b/manuals/meta_examples/enum_example.cpp index 3771932..cc928e2 100644 --- a/manuals/meta_examples/enum_example.cpp +++ b/manuals/meta_examples/enum_example.cpp @@ -51,9 +51,6 @@ TEST_CASE("meta/meta_examples/enum/usage") { // converts the enumerator to its name CHECK(align_type.value_to_name(e) == "center"); - // also, it works with dynamic value types - CHECK(align_type.value_to_name(meta::uvalue{e}) == "center"); - // ... and back again CHECK(align_type.name_to_value("center") == e); } diff --git a/singles/headers/meta.hpp/meta_all.hpp b/singles/headers/meta.hpp/meta_all.hpp index df7c733..7d9a5b5 100644 --- a/singles/headers/meta.hpp/meta_all.hpp +++ b/singles/headers/meta.hpp/meta_all.hpp @@ -1648,8 +1648,8 @@ namespace meta_hpp [[nodiscard]] evalue get_evalue(std::string_view name) const noexcept; - template < typename Value > - [[nodiscard]] std::string_view value_to_name(Value&& value) const noexcept; + 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; private: detail::enum_type_data* data_{}; @@ -5495,11 +5495,9 @@ namespace meta_hpp return evalue{}; } - template < typename Value > - std::string_view enum_type::value_to_name(Value&& value) const noexcept { - const detail::uarg value_arg{std::forward(value)}; - - if ( value_arg.get_raw_type() != *this ) { + template < detail::enum_kind Enum > + std::string_view enum_type::value_to_name(Enum value) const noexcept { + if ( resolve_type() != *this ) { return std::string_view{}; } @@ -7864,30 +7862,6 @@ namespace meta_hpp::detail }; } -namespace meta_hpp::detail -{ - template < typename T > - struct equals_traits; - - template < typename T > - concept has_equals_traits = requires(const T& l, const T& r) { - { equals_traits{}(l, r) } -> std::convertible_to; - }; -} - -namespace meta_hpp::detail -{ - template < typename T > - requires requires(const T& l, const T& r) { - { std::equal_to<>{}(l, r) } -> std::convertible_to; - } - struct equals_traits { - bool operator()(const T& l, const T& r) const { - return std::equal_to<>{}(l, r); - } - }; -} - namespace meta_hpp::detail { template < typename T > @@ -7976,30 +7950,6 @@ namespace meta_hpp::detail }; } -namespace meta_hpp::detail -{ - template < typename T > - struct less_traits; - - template < typename T > - concept has_less_traits = requires(const T& l, const T& r) { - { less_traits{}(l, r) } -> std::convertible_to; - }; -} - -namespace meta_hpp::detail -{ - template < typename T > - requires requires(const T& l, const T& r) { - { std::less<>{}(l, r) } -> std::convertible_to; - } - struct less_traits { - bool operator()(const T& l, const T& r) const { - return std::less<>{}(l, r); - } - }; -} - namespace meta_hpp::detail { template < typename T > @@ -8039,9 +7989,6 @@ namespace meta_hpp uvalue (*const deref)(const uvalue& from); uvalue (*const index)(const uvalue& from, std::size_t); - bool (*const less)(const uvalue&, const uvalue&); - bool (*const equals)(const uvalue&, const uvalue&); - std::istream& (*const istream)(std::istream&, uvalue&); std::ostream& (*const ostream)(std::ostream&, const uvalue&); @@ -8205,22 +8152,6 @@ namespace meta_hpp } }, - .less = +[]([[maybe_unused]] const uvalue& l, [[maybe_unused]] const uvalue& r) -> bool { - if constexpr ( detail::has_less_traits ) { - return detail::less_traits{}(l.get_as(), r.get_as()); - } else { - detail::throw_exception_with("value type doesn't have value less traits"); - } - }, - - .equals = +[]([[maybe_unused]] const uvalue& l, [[maybe_unused]] const uvalue& r) -> bool { - if constexpr ( detail::has_equals_traits ) { - return detail::equals_traits{}(l.get_as(), r.get_as()); - } else { - detail::throw_exception_with("value type doesn't have value equals traits"); - } - }, - .istream = +[]([[maybe_unused]] std::istream& is, [[maybe_unused]] uvalue& v) -> std::istream& { if constexpr ( detail::has_istream_traits && !detail::pointer_kind ) { return detail::istream_traits{}(is, v.get_as()); @@ -8499,7 +8430,7 @@ namespace meta_hpp namespace meta_hpp { - template < typename T > + template < detail::decay_non_value_kind T > [[nodiscard]] bool operator<(const uvalue& l, const T& r) { if ( !static_cast(l) ) { return true; @@ -8511,7 +8442,7 @@ namespace meta_hpp return (l_type < r_type) || (l_type == r_type && l.get_as() < r); } - template < typename T > + template < detail::decay_non_value_kind T > [[nodiscard]] bool operator<(const T& l, const uvalue& r) { if ( !static_cast(r) ) { return false; @@ -8522,26 +8453,11 @@ namespace meta_hpp return (l_type < r_type) || (l_type == r_type && l < r.get_as()); } - - [[nodiscard]] inline bool operator<(const uvalue& l, const uvalue& r) { - if ( !static_cast(r) ) { - return false; - } - - if ( !static_cast(l) ) { - return true; - } - - const any_type& l_type = l.get_type(); - const any_type& r_type = r.get_type(); - - return (l_type < r_type) || (l_type == r_type && l.vtable_->less(l, r)); - } } namespace meta_hpp { - template < typename T > + template < detail::decay_non_value_kind T > [[nodiscard]] bool operator==(const uvalue& l, const T& r) { if ( !static_cast(l) ) { return false; @@ -8553,7 +8469,7 @@ namespace meta_hpp return l_type == r_type && l.get_as() == r; } - template < typename T > + template < detail::decay_non_value_kind T > [[nodiscard]] bool operator==(const T& l, const uvalue& r) { if ( !static_cast(r) ) { return false; @@ -8564,21 +8480,6 @@ namespace meta_hpp return l_type == r_type && l == r.get_as(); } - - [[nodiscard]] inline bool operator==(const uvalue& l, const uvalue& r) { - if ( static_cast(l) != static_cast(r) ) { - return false; - } - - if ( !static_cast(l) ) { - return true; - } - - const any_type& l_type = l.get_type(); - const any_type& r_type = r.get_type(); - - return l_type == r_type && l.vtable_->equals(l, r); - } } namespace meta_hpp diff --git a/untests/meta_types/enum_type_tests.cpp b/untests/meta_types/enum_type_tests.cpp index 37a5522..e312a6f 100644 --- a/untests/meta_types/enum_type_tests.cpp +++ b/untests/meta_types/enum_type_tests.cpp @@ -122,7 +122,6 @@ TEST_CASE("meta/meta_types/enum_type") { CHECK(color_type.value_to_name(color::red) == "red"); CHECK(color_type.value_to_name(color::blue) == "blue"); - CHECK(color_type.value_to_name(100500).empty()); CHECK(color_type.value_to_name(color{100500}).empty()); } @@ -131,8 +130,7 @@ TEST_CASE("meta/meta_types/enum_type") { REQUIRE(ecolor_type); CHECK(ecolor_type.value_to_name(ecolor_red) == "red"); - CHECK(ecolor_type.value_to_name(meta::uvalue{ecolor_blue}) == "blue"); - CHECK(ecolor_type.value_to_name(100500).empty()); + CHECK(ecolor_type.value_to_name(ecolor_blue) == "blue"); CHECK(ecolor_type.value_to_name(ecolor{100500}).empty()); } diff --git a/untests/meta_utilities/value4_tests.cpp b/untests/meta_utilities/value4_tests.cpp index 5e3a280..2c7c14c 100644 --- a/untests/meta_utilities/value4_tests.cpp +++ b/untests/meta_utilities/value4_tests.cpp @@ -22,11 +22,15 @@ namespace }; } -TEST_CASE("meta/meta_utilities/value5/throw_dtor") { +TEST_CASE("meta/meta_utilities/value5") { namespace meta = meta_hpp; meta::class_() .function_("make", &clazz_throw_dtor::make); +} + +TEST_CASE("meta/meta_utilities/value5/throw_dtor") { + namespace meta = meta_hpp; SUBCASE("value") { meta::uvalue v{clazz_throw_dtor{42}}; diff --git a/untests/meta_utilities/value_tests.cpp b/untests/meta_utilities/value_tests.cpp index 05d272f..8d33b76 100644 --- a/untests/meta_utilities/value_tests.cpp +++ b/untests/meta_utilities/value_tests.cpp @@ -143,33 +143,18 @@ TEST_CASE("meta/meta_utilities/value") { } { - CHECK_FALSE(meta::uvalue{1} < meta::uvalue{}); - CHECK(meta::uvalue{} < meta::uvalue{1}); - CHECK_FALSE(meta::uvalue{} < meta::uvalue{}); - CHECK_FALSE(1 < meta::uvalue{}); CHECK(meta::uvalue{} < 1); - CHECK_FALSE(meta::uvalue{} < meta::uvalue{}); } { - CHECK_FALSE(meta::uvalue{1} == meta::uvalue{}); - CHECK_FALSE(meta::uvalue{} == meta::uvalue{1}); - CHECK(meta::uvalue{} == meta::uvalue{}); - CHECK_FALSE(1 == meta::uvalue{}); CHECK_FALSE(meta::uvalue{} == 1); - CHECK(meta::uvalue{} == meta::uvalue{}); } { - CHECK(meta::uvalue{1} != meta::uvalue{}); - CHECK(meta::uvalue{} != meta::uvalue{1}); - CHECK_FALSE(meta::uvalue{} != meta::uvalue{}); - CHECK(1 != meta::uvalue{}); CHECK(meta::uvalue{} != 1); - CHECK_FALSE(meta::uvalue{} != meta::uvalue{}); } CHECK_FALSE(meta::uvalue{} == 0); @@ -193,7 +178,6 @@ TEST_CASE("meta/meta_utilities/value") { CHECK(*static_cast(std::as_const(val).cdata()) == vr); CHECK(val == ivec2{1,2}); - CHECK(val == meta::uvalue{ivec2{1,2}}); CHECK(val.get_as() == ivec2{1,2}); CHECK(std::as_const(val).get_as() == ivec2{1,2}); @@ -227,7 +211,6 @@ TEST_CASE("meta/meta_utilities/value") { CHECK(*static_cast(std::as_const(val).cdata()) == vr); CHECK(val == ivec2{1,2}); - CHECK(val == meta::uvalue{ivec2{1,2}}); CHECK(val.get_as() == ivec2{1,2}); CHECK(std::as_const(val).get_as() == ivec2{1,2}); @@ -255,7 +238,6 @@ TEST_CASE("meta/meta_utilities/value") { CHECK(val.get_type() == meta::resolve_type()); CHECK(val == ivec2{1,2}); - CHECK(val == meta::uvalue{ivec2{1,2}}); CHECK(val.get_as() == ivec2{1,2}); CHECK(std::as_const(val).get_as() == ivec2{1,2}); @@ -283,7 +265,6 @@ TEST_CASE("meta/meta_utilities/value") { CHECK(val.get_type() == meta::resolve_type()); CHECK(val == ivec2{1,2}); - CHECK(val == meta::uvalue{ivec2{1,2}}); CHECK(val.get_as() == ivec2{1,2}); CHECK(std::as_const(val).get_as() == ivec2{1,2}); @@ -422,18 +403,6 @@ TEST_CASE("meta/meta_utilities/value") { CHECK(ivec2{1,2} < meta::uvalue{ivec2{1,3}}); CHECK_FALSE(ivec2{1,3} < meta::uvalue{ivec2{1,2}}); - - CHECK(meta::uvalue{ivec2{1,2}} < meta::uvalue{ivec2{1,3}}); - CHECK_FALSE(meta::uvalue{ivec2{1,3}} < meta::uvalue{ivec2{1,2}}); - - { - class empty_class1 {}; - class empty_class2 {}; - - CHECK((operator<(meta::uvalue{empty_class1{}}, meta::uvalue{empty_class2{}}) - || operator<(meta::uvalue{empty_class2{}}, meta::uvalue{empty_class1{}}))); - CHECK_THROWS(std::ignore = operator<(meta::uvalue{empty_class1{}}, meta::uvalue{empty_class1{}})); - } } SUBCASE("operator==") { @@ -442,17 +411,6 @@ TEST_CASE("meta/meta_utilities/value") { CHECK(ivec2{1,2} == meta::uvalue{ivec2{1,2}}); CHECK_FALSE(ivec2{1,3} == meta::uvalue{ivec2{1,2}}); - - CHECK(meta::uvalue{ivec2{1,2}} == meta::uvalue{ivec2{1,2}}); - CHECK_FALSE(meta::uvalue{ivec2{1,2}} == meta::uvalue{ivec2{1,3}}); - - { - class empty_class1 {}; - class empty_class2 {}; - - CHECK_FALSE(operator==(meta::uvalue{empty_class1{}}, meta::uvalue{empty_class2{}})); - CHECK_THROWS(std::ignore = operator==(meta::uvalue{empty_class1{}}, meta::uvalue{empty_class1{}})); - } } SUBCASE("deref") {