From e49e6c40ae7c9af17939b366171bb13aff592e89 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 23 Aug 2023 14:52:09 +0200 Subject: [PATCH] feat: `absolute()` member function removed from `quantity_point` --- example/currency.cpp | 7 +-- example/include/geographic.h | 4 +- example/unmanned_aerial_vehicle.cpp | 10 ++-- src/core/include/mp-units/quantity_point.h | 61 +++++++++------------- src/utility/include/mp-units/chrono.h | 2 +- 5 files changed, 38 insertions(+), 46 deletions(-) diff --git a/example/currency.cpp b/example/currency.cpp index c07d715d..c02b6490 100644 --- a/example/currency.cpp +++ b/example/currency.cpp @@ -81,7 +81,8 @@ quantity exchange_to(quantity q) template auto To, ReferenceOf auto From, auto PO, typename Rep> quantity_point exchange_to(quantity_point q) { - return quantity_point{zero + static_cast(exchange_rate() * q.absolute().number()) * To}; + return quantity_point{ + zero + static_cast(exchange_rate() * (q - q.absolute_point_origin).number()) * To}; } int main() @@ -89,6 +90,6 @@ int main() quantity_point price_usd = zero + 100 * us_dollar; quantity_point price_euro = exchange_to(price_usd); - std::cout << price_usd.absolute() << " -> " << price_euro.absolute() << "\n"; - // std::cout << price_usd.absolute() + price_euro.absolute() << "\n"; // does not compile + std::cout << price_usd.quantity_from_origin() << " -> " << price_euro.quantity_from_origin() << "\n"; + // std::cout << price_usd.quantity_from_origin() + price_euro.quantity_from_origin() << "\n"; // does not compile } diff --git a/example/include/geographic.h b/example/include/geographic.h index 036fda4b..082d25ba 100644 --- a/example/include/geographic.h +++ b/example/include/geographic.h @@ -46,7 +46,7 @@ using msl_altitude = mp_units::quantity_point std::basic_ostream& operator<<(std::basic_ostream& os, const msl_altitude& a) { - return os << a.absolute() << " AMSL"; + return os << a - mean_sea_level << " AMSL"; } } // namespace geographic @@ -56,7 +56,7 @@ struct MP_UNITS_STD_FMT::formatter : formatter auto format(const geographic::msl_altitude& a, FormatContext& ctx) { - formatter::format(a.absolute(), ctx); + formatter::format(a - geographic::mean_sea_level, ctx); return MP_UNITS_STD_FMT::format_to(ctx.out(), " AMSL"); } }; diff --git a/example/unmanned_aerial_vehicle.cpp b/example/unmanned_aerial_vehicle.cpp index 036abdbe..3996b7fd 100644 --- a/example/unmanned_aerial_vehicle.cpp +++ b/example/unmanned_aerial_vehicle.cpp @@ -71,7 +71,7 @@ template requires(is_hae(QP::absolute_point_origin)) std::basic_ostream& operator<<(std::basic_ostream& os, const QP& a) { - return os << a.absolute() << " HAE(" << to_text(a.absolute_point_origin.egm) << ")"; + return os << a - a.absolute_point_origin << " HAE(" << to_text(a.absolute_point_origin.egm) << ")"; } template @@ -80,7 +80,7 @@ struct MP_UNITS_STD_FMT::formatter : formatter { template auto format(const QP& a, FormatContext& ctx) { - formatter::format(a.absolute(), ctx); + formatter::format(a - a.absolute_point_origin, ctx); return MP_UNITS_STD_FMT::format_to(ctx.out(), " HAE({})", to_text(QP::absolute_point_origin.egm)); } }; @@ -98,7 +98,7 @@ hae_altitude to_hae(msl_altitude msl, position pos) { const auto geoid_undulation = isq::height(GeographicLibWhatsMyOffset(pos.lat.number_in(si::degree), pos.lon.number_in(si::degree)) * si::metre); - return height_above_ellipsoid + (msl.absolute() - geoid_undulation); + return height_above_ellipsoid + (msl - mean_sea_level - geoid_undulation); } @@ -113,7 +113,7 @@ using hal_altitude = quantity_point std::basic_ostream& operator<<(std::basic_ostream& os, const hal_altitude& a) { - return os << a.absolute() << " HAL"; + return os << a.quantity_from_origin() << " HAL"; } template<> @@ -121,7 +121,7 @@ struct MP_UNITS_STD_FMT::formatter : formatter auto format(const hal_altitude& a, FormatContext& ctx) { - formatter::format(a.absolute(), ctx); + formatter::format(a.quantity_from_origin(), ctx); return MP_UNITS_STD_FMT::format_to(ctx.out(), " HAL"); } }; diff --git a/src/core/include/mp-units/quantity_point.h b/src/core/include/mp-units/quantity_point.h index d0fc63ff..81645ce9 100644 --- a/src/core/include/mp-units/quantity_point.h +++ b/src/core/include/mp-units/quantity_point.h @@ -118,14 +118,10 @@ public: constexpr explicit(!std::convertible_to) quantity_point(const QP& qp) : q_([&] { if constexpr (is_same_v, - std::remove_const_t>) { + std::remove_const_t>) return qp.quantity_from_origin(); - } else if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin< - std::remove_const_t>) { - return qp.absolute(); - } else { - return qp.absolute() - zero().absolute(); - } + else + return qp - point_origin; }()) { } @@ -144,14 +140,12 @@ public: quantity_point& operator=(quantity_point&&) = default; template NewPO> - [[nodiscard]] constexpr QuantityPointOf auto point_from(NewPO origin) const + [[nodiscard]] constexpr QuantityPointOf auto point_from(NewPO new_origin) const { if constexpr (is_same_v>) return *this; - else if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin) - return make_quantity_point(absolute()); else - return make_quantity_point(absolute() - origin.quantity_point.absolute()); + return make_quantity_point(*this - new_origin); } // data access @@ -168,15 +162,6 @@ public: [[nodiscard]] constexpr const quantity_type&& quantity_from_origin() const&& noexcept { return std::move(q_); } #endif - [[nodiscard]] constexpr Quantity auto absolute() const noexcept - { - if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin< - std::remove_const_t>) - return quantity_from_origin(); - else - return point_origin.quantity_point.absolute() + quantity_from_origin(); - } - template requires detail::QuantityConvertibleTo{}, Rep>> [[nodiscard]] constexpr quantity_point<::mp_units::reference{}, PO, Rep> operator[](U) const @@ -298,29 +283,35 @@ template template QP2> [[nodiscard]] constexpr Quantity auto operator-(const QP1& lhs, const QP2& rhs) - requires requires { lhs.absolute() - rhs.absolute(); } + // TODO consider constraining it for both branches + requires requires { lhs.quantity_from_origin() - rhs.quantity_from_origin(); } { if constexpr (is_same_v, - std::remove_const_t>) { + std::remove_const_t>) return lhs.quantity_from_origin() - rhs.quantity_from_origin(); - } else - return lhs.absolute() - rhs.absolute(); + else + return lhs.quantity_from_origin() - rhs.quantity_from_origin() + (lhs.point_origin - rhs.point_origin); } template QP> requires ReferenceOf, PO::quantity_spec> -[[nodiscard]] constexpr Quantity auto operator-(const QP& qp, PO) +[[nodiscard]] constexpr Quantity auto operator-(const QP& qp, PO po) { - if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin) { - if constexpr (is_same_v, std::remove_const_t>) + if constexpr (is_same_v, std::remove_const_t>) + return qp.quantity_from_origin(); + else if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin) { + if constexpr (is_same_v, + std::remove_const_t>) return qp.quantity_from_origin(); else - return qp.absolute(); + return qp.quantity_from_origin() + (qp.point_origin - qp.absolute_point_origin); } else { - if constexpr (is_same_v, std::remove_const_t>) - return qp.quantity_from_origin(); + if constexpr (is_same_v, + std::remove_const_t>) + return qp.quantity_from_origin() - po.quantity_point.quantity_from_origin(); else - return qp.absolute() - PO::quantity_point.absolute(); + return qp.quantity_from_origin() - po.quantity_point.quantity_from_origin() + + (qp.point_origin - po.quantity_point.point_origin); } } @@ -338,9 +329,9 @@ template PO2> [[nodiscard]] constexpr Quantity auto operator-(PO1 po1, PO2 po2) { if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin) { - return -po2.quantity_point.absolute(); + return -(po2.quantity_point - po2.quantity_point.absolute_point_origin); } else if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin) { - return po1.quantity_point.absolute(); + return po1.quantity_point - po1.quantity_point.absolute_point_origin; } else { return po1.quantity_point - po2.quantity_point; } @@ -354,7 +345,7 @@ template QP2> std::remove_const_t>) return lhs.quantity_from_origin() <=> rhs.quantity_from_origin(); else - return lhs.absolute() <=> rhs.absolute(); + return lhs - lhs.absolute_point_origin <=> rhs - rhs.absolute_point_origin; } template QP2> @@ -365,7 +356,7 @@ template QP2> std::remove_const_t>) return lhs.quantity_from_origin() == rhs.quantity_from_origin(); else - return lhs.absolute() == rhs.absolute(); + return lhs - lhs.absolute_point_origin == rhs - rhs.absolute_point_origin; } // make_quantity_point diff --git a/src/utility/include/mp-units/chrono.h b/src/utility/include/mp-units/chrono.h index 512b5df3..3bfb479d 100644 --- a/src/utility/include/mp-units/chrono.h +++ b/src/utility/include/mp-units/chrono.h @@ -104,7 +104,7 @@ template QP> constexpr auto canonical = detail::get_canonical_unit(QP::unit); constexpr ratio r = as_ratio(canonical.mag); using ret_type = std::chrono::time_point>>; - return ret_type(to_chrono_duration(qp.absolute())); + return ret_type(to_chrono_duration(qp - qp.absolute_point_origin)); } } // namespace mp_units