mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-04 12:54:25 +02:00
feat: absolute()
member function removed from quantity_point
This commit is contained in:
@@ -81,7 +81,8 @@ quantity<To, Rep> exchange_to(quantity<From, Rep> q)
|
||||
template<ReferenceOf<currency> auto To, ReferenceOf<currency> auto From, auto PO, typename Rep>
|
||||
quantity_point<To, PO, Rep> exchange_to(quantity_point<From, PO, Rep> q)
|
||||
{
|
||||
return quantity_point{zero + static_cast<Rep>(exchange_rate<q.unit, get_unit(To)>() * q.absolute().number()) * To};
|
||||
return quantity_point{
|
||||
zero + static_cast<Rep>(exchange_rate<q.unit, get_unit(To)>() * (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<euro>(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
|
||||
}
|
||||
|
@@ -46,7 +46,7 @@ using msl_altitude = mp_units::quantity_point<mp_units::isq::altitude[mp_units::
|
||||
template<class CharT, class Traits>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& 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<geographic::msl_altitude> : formatter<geograp
|
||||
template<typename FormatContext>
|
||||
auto format(const geographic::msl_altitude& a, FormatContext& ctx)
|
||||
{
|
||||
formatter<geographic::msl_altitude::quantity_type>::format(a.absolute(), ctx);
|
||||
formatter<geographic::msl_altitude::quantity_type>::format(a - geographic::mean_sea_level, ctx);
|
||||
return MP_UNITS_STD_FMT::format_to(ctx.out(), " AMSL");
|
||||
}
|
||||
};
|
||||
|
@@ -71,7 +71,7 @@ template<class CharT, class Traits, QuantityPoint QP>
|
||||
requires(is_hae(QP::absolute_point_origin))
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& 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<QuantityPoint QP>
|
||||
@@ -80,7 +80,7 @@ struct MP_UNITS_STD_FMT::formatter<QP> : formatter<typename QP::quantity_type> {
|
||||
template<typename FormatContext>
|
||||
auto format(const QP& a, FormatContext& ctx)
|
||||
{
|
||||
formatter<typename QP::quantity_type>::format(a.absolute(), ctx);
|
||||
formatter<typename QP::quantity_type>::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<M> to_hae(msl_altitude msl, position<long double> 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<M> + (msl.absolute() - geoid_undulation);
|
||||
return height_above_ellipsoid<M> + (msl - mean_sea_level - geoid_undulation);
|
||||
}
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ using hal_altitude = quantity_point<isq::altitude[si::metre], height_above_launc
|
||||
template<class CharT, class Traits>
|
||||
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& 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<hal_altitude> : formatter<hal_altitude::quant
|
||||
template<typename FormatContext>
|
||||
auto format(const hal_altitude& a, FormatContext& ctx)
|
||||
{
|
||||
formatter<hal_altitude::quantity_type>::format(a.absolute(), ctx);
|
||||
formatter<hal_altitude::quantity_type>::format(a.quantity_from_origin(), ctx);
|
||||
return MP_UNITS_STD_FMT::format_to(ctx.out(), " HAL");
|
||||
}
|
||||
};
|
||||
|
@@ -118,14 +118,10 @@ public:
|
||||
constexpr explicit(!std::convertible_to<typename QP::quantity_type, quantity_type>) quantity_point(const QP& qp) :
|
||||
q_([&] {
|
||||
if constexpr (is_same_v<std::remove_const_t<decltype(point_origin)>,
|
||||
std::remove_const_t<decltype(QP::point_origin)>>) {
|
||||
std::remove_const_t<decltype(QP::point_origin)>>)
|
||||
return qp.quantity_from_origin();
|
||||
} else if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin<
|
||||
std::remove_const_t<decltype(point_origin)>>) {
|
||||
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<PointOriginFor<quantity_spec> NewPO>
|
||||
[[nodiscard]] constexpr QuantityPointOf<NewPO{}> auto point_from(NewPO origin) const
|
||||
[[nodiscard]] constexpr QuantityPointOf<NewPO{}> auto point_from(NewPO new_origin) const
|
||||
{
|
||||
if constexpr (is_same_v<NewPO, std::remove_const_t<decltype(point_origin)>>)
|
||||
return *this;
|
||||
else if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin<NewPO>)
|
||||
return make_quantity_point<NewPO{}>(absolute());
|
||||
else
|
||||
return make_quantity_point<NewPO{}>(absolute() - origin.quantity_point.absolute());
|
||||
return make_quantity_point<new_origin>(*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<decltype(point_origin)>>)
|
||||
return quantity_from_origin();
|
||||
else
|
||||
return point_origin.quantity_point.absolute() + quantity_from_origin();
|
||||
}
|
||||
|
||||
template<Unit U>
|
||||
requires detail::QuantityConvertibleTo<quantity_type, quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep>>
|
||||
[[nodiscard]] constexpr quantity_point<::mp_units::reference<quantity_spec, U{}>{}, PO, Rep> operator[](U) const
|
||||
@@ -298,29 +283,35 @@ template<PointOrigin PO, Quantity Q>
|
||||
|
||||
template<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> 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<decltype(QP1::point_origin)>,
|
||||
std::remove_const_t<decltype(QP2::point_origin)>>) {
|
||||
std::remove_const_t<decltype(QP2::point_origin)>>)
|
||||
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<PointOrigin PO, QuantityPointOf<PO{}> QP>
|
||||
requires ReferenceOf<std::remove_const_t<decltype(QP::reference)>, 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<PO>) {
|
||||
if constexpr (is_same_v<std::remove_const_t<PO>, std::remove_const_t<decltype(QP::point_origin)>>)
|
||||
if constexpr (is_same_v<std::remove_const_t<decltype(QP::point_origin)>, std::remove_const_t<PO>>)
|
||||
return qp.quantity_from_origin();
|
||||
else if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin<PO>) {
|
||||
if constexpr (is_same_v<std::remove_const_t<decltype(QP::point_origin)>,
|
||||
std::remove_const_t<decltype(QP::absolute_point_origin)>>)
|
||||
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<PO>, std::remove_const_t<decltype(QP::point_origin)>>)
|
||||
return qp.quantity_from_origin();
|
||||
if constexpr (is_same_v<std::remove_const_t<decltype(QP::point_origin)>,
|
||||
std::remove_const_t<decltype(po.quantity_point.point_origin)>>)
|
||||
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<PointOrigin PO1, PointOriginOf<PO1{}> PO2>
|
||||
[[nodiscard]] constexpr Quantity auto operator-(PO1 po1, PO2 po2)
|
||||
{
|
||||
if constexpr (detail::is_derived_from_specialization_of_absolute_point_origin<PO1>) {
|
||||
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<PO2>) {
|
||||
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<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
|
||||
std::remove_const_t<decltype(QP2::point_origin)>>)
|
||||
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<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
|
||||
@@ -365,7 +356,7 @@ template<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
|
||||
std::remove_const_t<decltype(QP2::point_origin)>>)
|
||||
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
|
||||
|
@@ -104,7 +104,7 @@ template<QuantityPointOf<isq::time> QP>
|
||||
constexpr auto canonical = detail::get_canonical_unit(QP::unit);
|
||||
constexpr ratio r = as_ratio(canonical.mag);
|
||||
using ret_type = std::chrono::time_point<clock, std::chrono::duration<rep, std::ratio<r.num, r.den>>>;
|
||||
return ret_type(to_chrono_duration(qp.absolute()));
|
||||
return ret_type(to_chrono_duration(qp - qp.absolute_point_origin));
|
||||
}
|
||||
|
||||
} // namespace mp_units
|
||||
|
Reference in New Issue
Block a user