remove fully dynamic less and equal uvalue's operators

This commit is contained in:
BlackMATov
2022-12-30 15:10:44 +07:00
parent 398d4012f9
commit 80c82b1027
10 changed files with 24 additions and 287 deletions

View File

@@ -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<T>{}(l, r) } -> std::convertible_to<bool>;
};
}
namespace meta_hpp::detail
{
template < typename T >
requires requires(const T& l, const T& r) {
{ std::equal_to<>{}(l, r) } -> std::convertible_to<bool>;
}
struct equals_traits<T> {
bool operator()(const T& l, const T& r) const {
return std::equal_to<>{}(l, r);
}
};
}

View File

@@ -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<T>{}(l, r) } -> std::convertible_to<bool>;
};
}
namespace meta_hpp::detail
{
template < typename T >
requires requires(const T& l, const T& r) {
{ std::less<>{}(l, r) } -> std::convertible_to<bool>;
}
struct less_traits<T> {
bool operator()(const T& l, const T& r) const {
return std::less<>{}(l, r);
}
};
}

View File

@@ -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_{};

View File

@@ -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>(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<Enum>() != *this ) {
return std::string_view{};
}

View File

@@ -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<Tp> ) {
return detail::less_traits<Tp>{}(l.get_as<Tp>(), r.get_as<Tp>());
} 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<Tp> ) {
return detail::equals_traits<Tp>{}(l.get_as<Tp>(), r.get_as<Tp>());
} 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<Tp> && !detail::pointer_kind<Tp> ) {
return detail::istream_traits<Tp>{}(is, v.get_as<Tp>());
@@ -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<bool>(l) ) {
return true;
@@ -506,7 +485,7 @@ namespace meta_hpp
return (l_type < r_type) || (l_type == r_type && l.get_as<T>() < r);
}
template < typename T >
template < detail::decay_non_value_kind T >
[[nodiscard]] bool operator<(const T& l, const uvalue& r) {
if ( !static_cast<bool>(r) ) {
return false;
@@ -517,26 +496,11 @@ namespace meta_hpp
return (l_type < r_type) || (l_type == r_type && l < r.get_as<T>());
}
[[nodiscard]] inline bool operator<(const uvalue& l, const uvalue& r) {
if ( !static_cast<bool>(r) ) {
return false;
}
if ( !static_cast<bool>(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<bool>(l) ) {
return false;
@@ -548,7 +512,7 @@ namespace meta_hpp
return l_type == r_type && l.get_as<T>() == r;
}
template < typename T >
template < detail::decay_non_value_kind T >
[[nodiscard]] bool operator==(const T& l, const uvalue& r) {
if ( !static_cast<bool>(r) ) {
return false;
@@ -559,21 +523,6 @@ namespace meta_hpp
return l_type == r_type && l == r.get_as<T>();
}
[[nodiscard]] inline bool operator==(const uvalue& l, const uvalue& r) {
if ( static_cast<bool>(l) != static_cast<bool>(r) ) {
return false;
}
if ( !static_cast<bool>(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

View File

@@ -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);
}

View File

@@ -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>(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<Enum>() != *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<T>{}(l, r) } -> std::convertible_to<bool>;
};
}
namespace meta_hpp::detail
{
template < typename T >
requires requires(const T& l, const T& r) {
{ std::equal_to<>{}(l, r) } -> std::convertible_to<bool>;
}
struct equals_traits<T> {
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<T>{}(l, r) } -> std::convertible_to<bool>;
};
}
namespace meta_hpp::detail
{
template < typename T >
requires requires(const T& l, const T& r) {
{ std::less<>{}(l, r) } -> std::convertible_to<bool>;
}
struct less_traits<T> {
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<Tp> ) {
return detail::less_traits<Tp>{}(l.get_as<Tp>(), r.get_as<Tp>());
} 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<Tp> ) {
return detail::equals_traits<Tp>{}(l.get_as<Tp>(), r.get_as<Tp>());
} 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<Tp> && !detail::pointer_kind<Tp> ) {
return detail::istream_traits<Tp>{}(is, v.get_as<Tp>());
@@ -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<bool>(l) ) {
return true;
@@ -8511,7 +8442,7 @@ namespace meta_hpp
return (l_type < r_type) || (l_type == r_type && l.get_as<T>() < r);
}
template < typename T >
template < detail::decay_non_value_kind T >
[[nodiscard]] bool operator<(const T& l, const uvalue& r) {
if ( !static_cast<bool>(r) ) {
return false;
@@ -8522,26 +8453,11 @@ namespace meta_hpp
return (l_type < r_type) || (l_type == r_type && l < r.get_as<T>());
}
[[nodiscard]] inline bool operator<(const uvalue& l, const uvalue& r) {
if ( !static_cast<bool>(r) ) {
return false;
}
if ( !static_cast<bool>(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<bool>(l) ) {
return false;
@@ -8553,7 +8469,7 @@ namespace meta_hpp
return l_type == r_type && l.get_as<T>() == r;
}
template < typename T >
template < detail::decay_non_value_kind T >
[[nodiscard]] bool operator==(const T& l, const uvalue& r) {
if ( !static_cast<bool>(r) ) {
return false;
@@ -8564,21 +8480,6 @@ namespace meta_hpp
return l_type == r_type && l == r.get_as<T>();
}
[[nodiscard]] inline bool operator==(const uvalue& l, const uvalue& r) {
if ( static_cast<bool>(l) != static_cast<bool>(r) ) {
return false;
}
if ( !static_cast<bool>(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

View File

@@ -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());
}

View File

@@ -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_<clazz_throw_dtor>()
.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}};

View File

@@ -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<const ivec2*>(std::as_const(val).cdata()) == vr);
CHECK(val == ivec2{1,2});
CHECK(val == meta::uvalue{ivec2{1,2}});
CHECK(val.get_as<ivec2>() == ivec2{1,2});
CHECK(std::as_const(val).get_as<ivec2>() == ivec2{1,2});
@@ -227,7 +211,6 @@ TEST_CASE("meta/meta_utilities/value") {
CHECK(*static_cast<const ivec2*>(std::as_const(val).cdata()) == vr);
CHECK(val == ivec2{1,2});
CHECK(val == meta::uvalue{ivec2{1,2}});
CHECK(val.get_as<ivec2>() == ivec2{1,2});
CHECK(std::as_const(val).get_as<ivec2>() == ivec2{1,2});
@@ -255,7 +238,6 @@ TEST_CASE("meta/meta_utilities/value") {
CHECK(val.get_type() == meta::resolve_type<ivec2>());
CHECK(val == ivec2{1,2});
CHECK(val == meta::uvalue{ivec2{1,2}});
CHECK(val.get_as<ivec2>() == ivec2{1,2});
CHECK(std::as_const(val).get_as<ivec2>() == ivec2{1,2});
@@ -283,7 +265,6 @@ TEST_CASE("meta/meta_utilities/value") {
CHECK(val.get_type() == meta::resolve_type<ivec2>());
CHECK(val == ivec2{1,2});
CHECK(val == meta::uvalue{ivec2{1,2}});
CHECK(val.get_as<ivec2>() == ivec2{1,2});
CHECK(std::as_const(val).get_as<ivec2>() == 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") {