From 46cb781d127b377b0c77c11441b4a804475e2134 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 9 Jun 2023 16:03:19 +0200 Subject: derivation_clause: remove all operator --- source/lib/include/newtype/newtype.hpp | 150 ++++++++++++++++----------------- source/tests/src/derivation_clause.cpp | 16 ++-- 2 files changed, 79 insertions(+), 87 deletions(-) diff --git a/source/lib/include/newtype/newtype.hpp b/source/lib/include/newtype/newtype.hpp index b1199b3..8e7729b 100644 --- a/source/lib/include/newtype/newtype.hpp +++ b/source/lib/include/newtype/newtype.hpp @@ -1,6 +1,7 @@ #ifndef NEWTYPE_NEWTYPE_HPP #define NEWTYPE_NEWTYPE_HPP +#include #include #include #include @@ -752,28 +753,19 @@ namespace nt template struct derivation_clause { + template + using derives_one = std::disjunction...>; + template - using contains = std::disjunction...>; + using derives = std::conjunction...>; constexpr derivation_clause(derivable...) noexcept { } - - template - auto constexpr operator()(derivable) const noexcept -> bool - { - return (std::is_same_v || ...); - } - - template - auto constexpr operator()(derivable, derivable...) const noexcept -> bool - { - return (*this)(derivable{}) && (*this)(derivable{}...); - } }; template - concept contains = requires(DerivationClause clause) { requires DerivationClause::template contains::value; }; + concept derives = requires(DerivationClause clause) { requires DerivationClause::template derives::value; }; template auto constexpr deriving(derivable... features) noexcept -> derivation_clause @@ -784,7 +776,7 @@ namespace nt template class new_type : impl::new_type_move_assignment - , public impl::new_type_iterator_types + , public impl::new_type_iterator_types> { static_assert(!std::is_reference_v, "The base type must not be a reference type"); static_assert(!std::is_void_v>, "The base type must not be possibly cv-qualified void"); @@ -793,79 +785,79 @@ namespace nt typename StreamTraits, nt::concepts::input_streamable BaseTypeT, typename TagTypeT, - nt::contains auto DerivationClauseV> + nt::derives auto DerivationClauseV> auto friend operator>>(std::basic_istream &, new_type &) noexcept( nt::concepts::nothrow_input_streamable) -> std::basic_istream &; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend operator+=(new_type & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_compound_addable) -> new_type &; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend operator-=(new_type & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_compound_subtractable) -> new_type &; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend operator*=(new_type & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_compound_multipliable) -> new_type &; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend operator/=(new_type & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_compound_divisible) -> new_type &; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend begin(new_type & obj) -> typename new_type::iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend begin(new_type const & obj) -> typename new_type::const_iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend cbegin(new_type const & obj) -> typename new_type::const_iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend rbegin(new_type & obj) -> typename new_type::reverse_iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend rbegin(new_type const & obj) -> typename new_type::const_reverse_iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend crbegin(new_type const & obj) -> typename new_type::const_reverse_iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend end(new_type & obj) -> typename new_type::iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend end(new_type const & obj) -> typename new_type::const_iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend cend(new_type const & obj) -> typename new_type::const_iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend rend(new_type & obj) -> typename new_type::reverse_iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend rend(new_type const & obj) -> typename new_type::const_reverse_iterator; - template auto DerivationClauseV> + template auto DerivationClauseV> auto constexpr friend crend(new_type const & obj) -> typename new_type::const_reverse_iterator; @@ -894,99 +886,99 @@ namespace nt template constexpr operator base_type() const noexcept(std::is_nothrow_copy_constructible_v) - requires(nt::contains) + requires(nt::derives) { return decay(); } template explicit constexpr operator base_type() const noexcept(std::is_nothrow_copy_constructible_v) - requires(!nt::contains) + requires(!nt::derives) { return decay(); } template - requires(nt::contains) + requires(nt::derives) auto constexpr operator->() noexcept -> BaseType * { return std::addressof(this->m_value); } template - requires(nt::contains) + requires(nt::derives) auto constexpr operator->() const noexcept -> BaseType const * { return std::addressof(this->m_value); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr begin() -> typename new_type::iterator { return this->m_value.begin(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr begin() const -> typename new_type::const_iterator { return this->m_value.begin(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr cbegin() const -> typename new_type::const_iterator { return this->m_value.cbegin(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr rbegin() -> typename new_type::reverse_iterator { return this->m_value.rbegin(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr rbegin() const -> typename new_type::const_reverse_iterator { return this->m_value.rbegin(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr crbegin() const -> typename new_type::const_reverse_iterator { return this->m_value.crbegin(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr end() -> typename new_type::iterator { return this->m_value.end(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr end() const -> typename new_type::const_iterator { return this->m_value.end(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr cend() const -> typename new_type::const_iterator { return this->m_value.cend(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr rend() -> typename new_type::reverse_iterator { return this->m_value.rend(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr rend() const -> typename new_type::const_reverse_iterator { return this->m_value.rend(); } - template auto DerivationClauseV = DerivationClause> + template auto DerivationClauseV = DerivationClause> auto constexpr crend() const -> typename new_type::const_reverse_iterator { return this->m_value.crend(); @@ -1001,14 +993,14 @@ namespace nt return lhs.decay() == rhs.decay(); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator==(new_type const & lhs, BaseType const & rhs) noexcept(nt::concepts::nothrow_equality_comparable) -> bool { return lhs.decay() == rhs; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator==(BaseType const & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_equality_comparable) -> bool @@ -1024,14 +1016,14 @@ namespace nt return lhs.decay() != rhs.decay(); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator!=(new_type const & lhs, BaseType const & rhs) noexcept(nt::concepts::nothrow_inequality_comparable) -> bool { return lhs.decay() != rhs; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator!=(BaseType const & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_inequality_comparable) -> bool @@ -1039,7 +1031,7 @@ namespace nt return lhs != rhs.decay(); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator<(new_type const & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_less_than_comparable) @@ -1047,7 +1039,7 @@ namespace nt return lhs.decay() < rhs.decay(); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator>(new_type const & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_greater_than_comparable) @@ -1055,7 +1047,7 @@ namespace nt return lhs.decay() > rhs.decay(); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator<=(new_type const & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_less_than_equal_comparable) @@ -1063,7 +1055,7 @@ namespace nt return lhs.decay() <= rhs.decay(); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator>=(new_type const & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_greater_than_equal_comparable) @@ -1075,7 +1067,7 @@ namespace nt typename StreamTraits, nt::concepts::output_streamable BaseType, typename TagType, - nt::contains auto DerivationClause> + nt::derives auto DerivationClause> auto operator<<(std::basic_ostream & output, new_type const & source) noexcept( nt::concepts::nothrow_output_streamable) -> std::basic_ostream & { @@ -1086,14 +1078,14 @@ namespace nt typename StreamTraits, nt::concepts::input_streamable BaseType, typename TagType, - nt::contains auto DerivationClause> + nt::derives auto DerivationClause> auto operator>>(std::basic_istream & input, new_type & target) noexcept( nt::concepts::nothrow_input_streamable) -> std::basic_istream & { return input >> target.m_value; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator+(new_type const & lhs, new_type const & rhs) noexcept( nt::concepts::nothrow_addable && std::is_nothrow_copy_constructible_v) @@ -1102,7 +1094,7 @@ namespace nt return {lhs.decay() + rhs.decay()}; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator+=(new_type & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_compound_addable) @@ -1112,7 +1104,7 @@ namespace nt return lhs; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator-(new_type const & lhs, new_type const & rhs) noexcept( nt::concepts::nothrow_subtractable && std::is_nothrow_copy_constructible_v) @@ -1121,7 +1113,7 @@ namespace nt return {lhs.decay() - rhs.decay()}; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator-=(new_type & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_compound_subtractable) @@ -1131,7 +1123,7 @@ namespace nt return lhs; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator*(new_type const & lhs, new_type const & rhs) noexcept( nt::concepts::nothrow_multipliable && std::is_nothrow_copy_constructible_v) @@ -1140,7 +1132,7 @@ namespace nt return {lhs.decay() * rhs.decay()}; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator*=(new_type & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_compound_multipliable) @@ -1150,7 +1142,7 @@ namespace nt return lhs; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator/(new_type const & lhs, new_type const & rhs) noexcept( nt::concepts::nothrow_divisible && std::is_nothrow_copy_constructible_v) @@ -1159,7 +1151,7 @@ namespace nt return {lhs.decay() / rhs.decay()}; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr operator/=(new_type & lhs, new_type const & rhs) noexcept(nt::concepts::nothrow_compound_divisible) @@ -1169,82 +1161,82 @@ namespace nt return lhs; } - template auto DerivationClause> + template auto DerivationClause> auto constexpr begin(new_type & obj) -> typename new_type::iterator { return begin(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr begin(new_type const & obj) -> typename new_type::const_iterator { return begin(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr cbegin(new_type const & obj) -> typename new_type::const_iterator { return cbegin(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr rbegin(new_type & obj) -> typename new_type::reverse_iterator { return rbegin(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr rbegin(new_type const & obj) -> typename new_type::const_reverse_iterator { return rbegin(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr crbegin(new_type const & obj) -> typename new_type::const_reverse_iterator { return crbegin(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr end(new_type & obj) -> typename new_type::iterator { return end(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr end(new_type const & obj) -> typename new_type::const_iterator { return end(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr cend(new_type const & obj) -> typename new_type::const_iterator { return cend(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr rend(new_type & obj) -> typename new_type::reverse_iterator { return rend(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr rend(new_type const & obj) -> typename new_type::const_reverse_iterator { return rend(obj.m_value); } - template auto DerivationClause> + template auto DerivationClause> auto constexpr crend(new_type const & obj) -> typename new_type::const_reverse_iterator { @@ -1255,7 +1247,7 @@ namespace nt namespace std { - template auto DerivationClause> + template auto DerivationClause> struct hash> { auto constexpr operator()(nt::new_type const & object) const diff --git a/source/tests/src/derivation_clause.cpp b/source/tests/src/derivation_clause.cpp index 4cda87f..78bd3d4 100644 --- a/source/tests/src/derivation_clause.cpp +++ b/source/tests/src/derivation_clause.cpp @@ -12,7 +12,7 @@ SCENARIO("Derivation Clause", "[infrastructure]") THEN("it doesn't contain any derivable") { - STATIC_REQUIRE_FALSE(clause(nt::Show)); + STATIC_REQUIRE_FALSE(nt::derives); } } @@ -22,12 +22,12 @@ SCENARIO("Derivation Clause", "[infrastructure]") THEN("it doesn't contain nt::EqBase") { - STATIC_REQUIRE_FALSE(clause(nt::EqBase)); + STATIC_REQUIRE_FALSE(nt::derives); } THEN("it contains nt::Show") { - STATIC_REQUIRE(clause(nt::Show)); + STATIC_REQUIRE(nt::derives); } } @@ -37,27 +37,27 @@ SCENARIO("Derivation Clause", "[infrastructure]") THEN("it contains nt::EqBase") { - STATIC_REQUIRE(clause(nt::EqBase)); + STATIC_REQUIRE(nt::derives); } THEN("it contains nt::Show") { - STATIC_REQUIRE(clause(nt::Show)); + STATIC_REQUIRE(nt::derives); } THEN("it contains both nt::Show and nt::EqBase") { - STATIC_REQUIRE(clause(nt::Show, nt::EqBase)); + STATIC_REQUIRE(nt::derives); } THEN("it does not contain nt::Arithmetic") { - STATIC_REQUIRE_FALSE(clause(nt::Arithmetic)); + STATIC_REQUIRE_FALSE(nt::derives); } THEN("it does not contain both nt::Arithmetic and nt::Show") { - STATIC_REQUIRE_FALSE(clause(nt::Arithmetic, nt::Show)); + STATIC_REQUIRE_FALSE(nt::derives); } } } -- cgit v1.2.3