refactor: 💥 op==(U1, U2) now checks for the same type (old behavior available as equivalent(U1, U2)) + convertible now verifies associated quantity_spec as well

This commit is contained in:
Mateusz Pusz committed 2024-10-09 17:26:13 +02:00
1 parent e3ce507fd3
commit 70a18fec0c
4 files changed
+91 -72

No files matched your search

+6 -5
View File
@@ -96,13 +96,13 @@ struct conversion_value_traits {
*/
template<Quantity To, typename FwdFrom, Quantity From = std::remove_cvref_t<FwdFrom>>
requires(castable(From::quantity_spec, To::quantity_spec)) &&
((From::unit == To::unit && std::constructible_from<typename To::rep, typename From::rep>) ||
(From::unit != To::unit)) // && scalable_with_<typename To::rep>))
(((equivalent(From::unit, To::unit)) && std::constructible_from<typename To::rep, typename From::rep>) ||
(!equivalent(From::unit, To::unit))) // && scalable_with_<typename To::rep>))
// TODO how to constrain the second part here?
[[nodiscard]] constexpr To sudo_cast(FwdFrom&& q)
{
constexpr auto q_unit = From::unit;
if constexpr (q_unit == To::unit) {
if constexpr (equivalent(q_unit, To::unit)) {
// no scaling of the number needed
return {static_cast<To::rep>(std::forward<FwdFrom>(q).numerical_value_is_an_implementation_detail_),
To::reference}; // this is the only (and recommended) way to do a truncating conversion on a number, so we
@@ -149,8 +149,9 @@ template<Quantity To, typename FwdFrom, Quantity From = std::remove_cvref_t<FwdF
template<QuantityPoint ToQP, typename FwdFromQP, QuantityPoint FromQP = std::remove_cvref_t<FwdFromQP>>
requires(castable(FromQP::quantity_spec, ToQP::quantity_spec)) &&
(detail::same_absolute_point_origins(ToQP::point_origin, FromQP::point_origin)) &&
((FromQP::unit == ToQP::unit && std::constructible_from<typename ToQP::rep, typename FromQP::rep>) ||
(FromQP::unit != ToQP::unit))
(((equivalent(FromQP::unit, ToQP::unit)) &&
std::constructible_from<typename ToQP::rep, typename FromQP::rep>) ||
(!equivalent(FromQP::unit, ToQP::unit)))
[[nodiscard]] constexpr QuantityPoint auto sudo_cast(FwdFromQP&& qp)
{
if constexpr (is_same_v<std::remove_const_t<decltype(ToQP::point_origin)>,
@@ -105,7 +105,7 @@ concept CommonlyInvocableQuantities =
InvocableQuantities<Func, Q1, Q2, get_common_quantity_spec(Q1::quantity_spec, Q2::quantity_spec).character>;
template<auto R1, auto R2, typename Rep1, typename Rep2>
concept SameValueAs = SameReference<R1, R2> && std::same_as<Rep1, Rep2>;
concept SameValueAs = (equivalent(get_unit(R1), get_unit(R2))) && std::convertible_to<Rep1, Rep2>;
template<typename T>
using quantity_like_type = quantity<quantity_like_traits<T>::reference, typename quantity_like_traits<T>::rep>;
@@ -261,21 +261,21 @@ public:
// data access
template<Unit U>
requires(U{} == unit)
requires(equivalent(U{}, unit))
[[nodiscard]] constexpr rep& numerical_value_ref_in(U) & noexcept
{
return numerical_value_is_an_implementation_detail_;
}
template<Unit U>
requires(U{} == unit)
requires(equivalent(U{}, unit))
[[nodiscard]] constexpr const rep& numerical_value_ref_in(U) const& noexcept
{
return numerical_value_is_an_implementation_detail_;
}
template<Unit U>
requires(U{} == unit)
requires(equivalent(U{}, unit))
constexpr const rep&& numerical_value_ref_in(U) const&& noexcept
#if __cpp_deleted_function
= delete("Can't form a reference to a temporary");
+19 -7
View File
@@ -122,6 +122,12 @@ struct unit_less : std::bool_constant<type_name<Lhs>() < type_name<Rhs>()> {};
template<typename T1, typename T2>
using type_list_of_unit_less = expr_less<T1, T2, unit_less>;
template<typename From, typename To>
concept PotentiallyConvertibleTo = Unit<From> && Unit<To> &&
((AssociatedUnit<From> && AssociatedUnit<To> &&
implicitly_convertible(get_quantity_spec(From{}), get_quantity_spec(To{}))) ||
(!AssociatedUnit<From> && !AssociatedUnit<To>));
} // namespace detail
// TODO this should really be in the `details` namespace but is used in `chrono.h` (a part of mp_units.systems)
@@ -134,9 +140,11 @@ template<Unit From, Unit To>
{
if constexpr (is_same_v<From, To>)
return true;
else
else if constexpr (detail::PotentiallyConvertibleTo<From, To>)
return is_same_v<decltype(get_canonical_unit(from).reference_unit),
decltype(get_canonical_unit(to).reference_unit)>;
else
return false;
}
namespace detail {
@@ -192,12 +200,16 @@ struct unit_interface {
return expr_divide<derived_unit, struct one, type_list_of_unit_less>(lhs, rhs);
}
[[nodiscard]] friend consteval bool operator==(Unit auto lhs, Unit auto rhs)
template<Unit Lhs, Unit Rhs>
[[nodiscard]] friend consteval bool operator==(Lhs, Rhs)
{
auto canonical_lhs = get_canonical_unit(lhs);
auto canonical_rhs = get_canonical_unit(rhs);
return convertible(canonical_lhs.reference_unit, canonical_rhs.reference_unit) &&
canonical_lhs.mag == canonical_rhs.mag;
return is_same_v<Lhs, Rhs>;
}
[[nodiscard]] friend consteval bool equivalent(Unit auto lhs, Unit auto rhs)
requires(convertible(lhs, rhs))
{
return get_canonical_unit(lhs).mag == get_canonical_unit(rhs).mag;
}
};
@@ -662,7 +674,7 @@ template<Unit U1, Unit U2>
{
if constexpr (is_same_v<U1, U2>)
return u1;
else if constexpr (U1{} == U2{}) {
else if constexpr (equivalent(U1{}, U2{})) {
if constexpr (std::derived_from<U1, typename U2::_base_type_>)
return u1;
else if constexpr (std::derived_from<U2, typename U1::_base_type_>)