From d65393b4e2dfdb05509da6d06aed34642b38d0b2 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 8 Apr 2023 19:49:51 +0200 Subject: [PATCH] refactor: `quantity_spec` casts functions renamed --- .../include/mp_units/bits/quantity_cast.h | 2 +- .../include/mp_units/bits/quantity_concepts.h | 2 +- .../mp_units/bits/quantity_point_concepts.h | 4 +- src/core/include/mp_units/bits/sudo_cast.h | 2 +- src/core/include/mp_units/quantity.h | 2 +- src/core/include/mp_units/quantity_spec.h | 35 +- src/core/include/mp_units/reference.h | 6 +- test/unit_test/static/quantity_spec_test.cpp | 338 +++++++++--------- 8 files changed, 196 insertions(+), 195 deletions(-) diff --git a/src/core/include/mp_units/bits/quantity_cast.h b/src/core/include/mp_units/bits/quantity_cast.h index 8ad945fd..f6bec7f0 100644 --- a/src/core/include/mp_units/bits/quantity_cast.h +++ b/src/core/include/mp_units/bits/quantity_cast.h @@ -56,7 +56,7 @@ class quantity; * @tparam ToQS a quantity specification to use for a target quantity */ template - requires Quantity> && (castable_to(std::remove_cvref_t::quantity_spec, ToQS)) + requires Quantity> && (castable(std::remove_cvref_t::quantity_spec, ToQS)) [[nodiscard]] constexpr Quantity auto quantity_cast(Q&& q) { constexpr reference::unit> r; diff --git a/src/core/include/mp_units/bits/quantity_concepts.h b/src/core/include/mp_units/bits/quantity_concepts.h index 726aa87c..64949d05 100644 --- a/src/core/include/mp_units/bits/quantity_concepts.h +++ b/src/core/include/mp_units/bits/quantity_concepts.h @@ -56,7 +56,7 @@ concept Quantity = requires(T* t) { detail::to_base_specialization_of_quantity(t template concept QuantityOf = Quantity && ((Dimension> && Q::dimension == V) || - (QuantitySpec> && implicitly_convertible_to(Q::quantity_spec, V))); + (QuantitySpec> && implicitly_convertible(Q::quantity_spec, V))); /** * @brief A concept matching all external quantities like types diff --git a/src/core/include/mp_units/bits/quantity_point_concepts.h b/src/core/include/mp_units/bits/quantity_point_concepts.h index 9dd36192..85d2fc3f 100644 --- a/src/core/include/mp_units/bits/quantity_point_concepts.h +++ b/src/core/include/mp_units/bits/quantity_point_concepts.h @@ -68,7 +68,7 @@ concept PointOrigin = QuantityPoint || detail::is_derived_from_specialization * Satisfied by all quantity point origins that are defined using a provided quantity specification. */ template -concept PointOriginFor = PointOrigin && QuantitySpec> && Q == T::quantity_spec; +concept PointOriginFor = PointOrigin && QuantitySpec> && implicitly_convertible(Q, T::quantity_spec); template auto PO, RepresentationOf Rep> @@ -95,7 +95,7 @@ template concept QuantityPointOf = QuantityPoint && ((Dimension> && QP::dimension == V) || - (QuantitySpec> && implicitly_convertible_to(QP::quantity_spec, V)) || + (QuantitySpec> && implicitly_convertible(QP::quantity_spec, V)) || (PointOrigin> && std::same_as, std::remove_const_t>)); diff --git a/src/core/include/mp_units/bits/sudo_cast.h b/src/core/include/mp_units/bits/sudo_cast.h index 2bc3bee0..1d4b9bf1 100644 --- a/src/core/include/mp_units/bits/sudo_cast.h +++ b/src/core/include/mp_units/bits/sudo_cast.h @@ -43,7 +43,7 @@ namespace detail { * @tparam To a target quantity type to cast to */ template - requires(castable_to(get_quantity_spec(R), To::quantity_spec)) && + requires(castable(get_quantity_spec(R), To::quantity_spec)) && ((get_unit(R) == To::unit && std::constructible_from) || (get_unit(R) != To::unit)) // && scalable_with_)) // TODO how to constrain the second part here? diff --git a/src/core/include/mp_units/quantity.h b/src/core/include/mp_units/quantity.h index b340812f..b61a3a85 100644 --- a/src/core/include/mp_units/quantity.h +++ b/src/core/include/mp_units/quantity.h @@ -58,7 +58,7 @@ concept Harmonic = // exposition only template concept QuantityConvertibleTo = // exposition only - Quantity && Quantity && implicitly_convertible_to(QFrom::quantity_spec, QTo::quantity_spec) && + Quantity && Quantity && implicitly_convertible(QFrom::quantity_spec, QTo::quantity_spec) && convertible_to(QFrom::unit, QTo::unit) && requires(QFrom q) { detail::sudo_cast(q); } && (treat_as_floating_point || (!treat_as_floating_point && Harmonic)); diff --git a/src/core/include/mp_units/quantity_spec.h b/src/core/include/mp_units/quantity_spec.h index 1829ac5e..8170b51d 100644 --- a/src/core/include/mp_units/quantity_spec.h +++ b/src/core/include/mp_units/quantity_spec.h @@ -323,12 +323,12 @@ struct quantity_spec : std::remove_const_t { #ifdef __cpp_explicit_this_parameter template auto... Args> - requires requires { QS._equation_; } && (implicitly_convertible_to(Eq, QS._equation_)) + requires requires { QS._equation_; } && (implicitly_convertible(Eq, QS._equation_)) struct quantity_spec : quantity_spec { #else template - requires requires { QS._equation_; } && (implicitly_convertible_to(Eq, QS._equation_)) + requires requires { QS._equation_; } && (implicitly_convertible(Eq, QS._equation_)) struct quantity_spec : quantity_spec { #endif static constexpr auto _equation_ = Eq; @@ -684,7 +684,7 @@ template } template -[[nodiscard]] consteval convertible_to_result convertible_to_impl(Q1 q1, Q2 q2) +[[nodiscard]] consteval convertible_to_result convertible_impl(Q1 q1, Q2 q2) { using enum convertible_to_result; @@ -695,9 +695,9 @@ template else if constexpr (QuantityKindSpec || QuantityKindSpec) { if constexpr (IntermediateDerivedQuantitySpec && NamedQuantitySpec>) - return convertible_to_impl(get_kind(q1), remove_kind(q2)); + return convertible_impl(get_kind(q1), remove_kind(q2)); else - return convertible_to_impl(get_kind(q1), get_kind(q2)) != no ? yes : no; + return convertible_impl(get_kind(q1), get_kind(q2)) != no ? yes : no; } else if constexpr (IntermediateDerivedQuantitySpec && IntermediateDerivedQuantitySpec) return are_ingredients_convertible_to(q1, q2); else if constexpr (NamedQuantitySpec && NamedQuantitySpec) { @@ -710,15 +710,15 @@ template } else if constexpr (IntermediateDerivedQuantitySpec) { auto q1_exploded = explode(q1); if constexpr (NamedQuantitySpec>) - return convertible_to_impl(q1_exploded, q2); + return convertible_impl(q1_exploded, q2); else if constexpr (requires { q2._equation_; }) - return convertible_to_impl(q1_exploded, q2._equation_); + return convertible_impl(q1_exploded, q2._equation_); } else if constexpr (IntermediateDerivedQuantitySpec) { auto q2_exploded = explode(q2); if constexpr (NamedQuantitySpec>) - return convertible_to_impl(q2_exploded, q1); + return convertible_impl(q1, q2_exploded); else if constexpr (requires { q1._equation_; }) - return std::min(explicit_conversion, convertible_to_impl(q1._equation_, q2_exploded)); + return std::min(explicit_conversion, convertible_impl(q1._equation_, q2_exploded)); } return no; } @@ -726,21 +726,21 @@ template } // namespace detail template -[[nodiscard]] consteval bool implicitly_convertible_to(Q1 q1, Q2 q2) +[[nodiscard]] consteval bool implicitly_convertible(Q1 q1, Q2 q2) { - return detail::convertible_to_impl(q1, q2) == detail::convertible_to_result::yes; + return detail::convertible_impl(q1, q2) == detail::convertible_to_result::yes; } template -[[nodiscard]] consteval bool explicitly_convertible_to(Q1 q1, Q2 q2) +[[nodiscard]] consteval bool explicitly_convertible(Q1 q1, Q2 q2) { - return detail::convertible_to_impl(q1, q2) >= detail::convertible_to_result::explicit_conversion; + return detail::convertible_impl(q1, q2) >= detail::convertible_to_result::explicit_conversion; } template -[[nodiscard]] consteval bool castable_to(Q1 q1, Q2 q2) +[[nodiscard]] consteval bool castable(Q1 q1, Q2 q2) { - return detail::convertible_to_impl(q1, q2) >= detail::convertible_to_result::cast; + return detail::convertible_impl(q1, q2) >= detail::convertible_to_result::cast; } namespace detail { @@ -783,14 +783,13 @@ template template [[nodiscard]] consteval QuantitySpec auto common_quantity_spec(Q1 q1, Q2 q2) - requires(implicitly_convertible_to(get_kind(q1), get_kind(q2)) || - implicitly_convertible_to(get_kind(q2), get_kind(q1))) + requires(implicitly_convertible(get_kind(q1), get_kind(q2)) || implicitly_convertible(get_kind(q2), get_kind(q1))) { if constexpr (q1 == q2) return q1; else if constexpr (detail::have_common_base(q1, q2)) return detail::get_common_base(q1, q2); - else if constexpr (implicitly_convertible_to(get_kind(q1), get_kind(q2))) + else if constexpr (implicitly_convertible(get_kind(q1), get_kind(q2))) return get_kind(q2); else return get_kind(q1); diff --git a/src/core/include/mp_units/reference.h b/src/core/include/mp_units/reference.h index 84821698..9de50612 100644 --- a/src/core/include/mp_units/reference.h +++ b/src/core/include/mp_units/reference.h @@ -122,19 +122,19 @@ struct reference { template [[nodiscard]] friend consteval bool convertible_to(reference, reference) { - return implicitly_convertible_to(Q, Q2) && convertible_to(U, U2); + return implicitly_convertible(Q, Q2) && convertible_to(U, U2); } template [[nodiscard]] friend consteval bool convertible_to(reference, U2 u2) { - return implicitly_convertible_to(Q, get_quantity_spec(u2)) && convertible_to(U, u2); + return implicitly_convertible(Q, get_quantity_spec(u2)) && convertible_to(U, u2); } template [[nodiscard]] friend consteval bool convertible_to(U1 u1, reference) { - return implicitly_convertible_to(get_quantity_spec(u1), Q) && convertible_to(u1, U); + return implicitly_convertible(get_quantity_spec(u1), Q) && convertible_to(u1, U); } }; diff --git a/test/unit_test/static/quantity_spec_test.cpp b/test/unit_test/static/quantity_spec_test.cpp index c76ff154..b4d83440 100644 --- a/test/unit_test/static/quantity_spec_test.cpp +++ b/test/unit_test/static/quantity_spec_test.cpp @@ -435,233 +435,235 @@ using namespace detail; using enum convertible_to_result; // different dimensions -static_assert(convertible_to_impl(mass, length) == no); -static_assert(convertible_to_impl(speed, length) == no); -static_assert(convertible_to_impl(length, speed) == no); -static_assert(convertible_to_impl(energy, speed) == no); -static_assert(convertible_to_impl(length, kind_of