Skip to content

Commit

Permalink
refactor: quantity_point::quantity_from_origin() refactored to `qua…
Browse files Browse the repository at this point in the history
…ntity_point::quantity_ref_from(PO)`

Resolves #479 and relates to #477
  • Loading branch information
mpusz committed Aug 27, 2023
1 parent c5cb614 commit bbaf1b5
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 137 deletions.
4 changes: 2 additions & 2 deletions example/currency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ int main()
quantity_point price_usd = zero + 100 * us_dollar;
quantity_point price_euro = exchange_to<euro>(price_usd);

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
std::cout << price_usd.quantity_ref_from(zero) << " -> " << price_euro.quantity_ref_from(zero) << "\n";
// std::cout << price_usd.quantity_ref_from(zero) + price_euro.quantity_ref_from(zero) << "\n"; // does not compile
}
2 changes: 1 addition & 1 deletion example/kalman_filter/kalman.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ struct MP_UNITS_STD_FMT::formatter<kalman::estimation<Q>> {
if constexpr (mp_units::Quantity<Q>)
return t;
else
return t.quantity_from_origin();
return t.quantity_ref_from(t.point_origin);
}(kalman::get<0>(e.state));

std::string value_buffer;
Expand Down
2 changes: 1 addition & 1 deletion example/kalman_filter/kalman_filter-example_6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ template<QuantityPoint QP, QuantityOf<dimensionless> K>
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> next)
{
std::cout << MP_UNITS_STD_FMT::format("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain,
measured.quantity_from_origin(), current, next);
measured.quantity_ref_from(QP::point_origin), current, next);
}

int main()
Expand Down
2 changes: 1 addition & 1 deletion example/kalman_filter/kalman_filter-example_7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ template<QuantityPoint QP, QuantityOf<dimensionless> K>
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> next)
{
std::cout << MP_UNITS_STD_FMT::format("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain,
measured.quantity_from_origin(), current, next);
measured.quantity_ref_from(QP::point_origin), current, next);
}

int main()
Expand Down
2 changes: 1 addition & 1 deletion example/kalman_filter/kalman_filter-example_8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ template<QuantityPoint QP, QuantityOf<dimensionless> K>
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> next)
{
std::cout << MP_UNITS_STD_FMT::format("{:2} | {:7%.3Q} | {:10%.3Q %q} | {:>16.2} | {:>16.2}\n", iteration, gain,
measured.quantity_from_origin(), current, next);
measured.quantity_ref_from(QP::point_origin), current, next);
}

int main()
Expand Down
4 changes: 2 additions & 2 deletions example/unmanned_aerial_vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ 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.quantity_from_origin() << " HAL";
return os << a.quantity_ref_from(height_above_launch) << " HAL";
}

template<>
struct MP_UNITS_STD_FMT::formatter<hal_altitude> : formatter<hal_altitude::quantity_type> {
template<typename FormatContext>
auto format(const hal_altitude& a, FormatContext& ctx)
{
formatter<hal_altitude::quantity_type>::format(a.quantity_from_origin(), ctx);
formatter<hal_altitude::quantity_type>::format(a.quantity_ref_from(height_above_launch), ctx);
return MP_UNITS_STD_FMT::format_to(ctx.out(), " HAL");
}
};
Expand Down
65 changes: 42 additions & 23 deletions src/core/include/mp-units/quantity_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class quantity_point {
q_([&] {
if constexpr (is_same_v<std::remove_const_t<decltype(point_origin)>,
std::remove_const_t<decltype(QP::point_origin)>>)
return qp.quantity_from_origin();
return qp.quantity_ref_from(point_origin);
else
return qp - point_origin;
}())
Expand Down Expand Up @@ -146,23 +146,39 @@ class quantity_point {

// data access
#ifdef __cpp_explicit_this_parameter
template<typename Self>
[[nodiscard]] constexpr auto&& quantity_from_origin(this Self&& self) noexcept
template<typename Self, std::same_as<std::remove_const_t<decltype(PO)>> PO2>
[[nodiscard]] constexpr auto&& quantity_ref_from(this Self&& self, PO2) noexcept
{
return std::forward<Self>(self).q_;
}
#else
[[nodiscard]] constexpr quantity_type& quantity_from_origin() & noexcept { return q_; }
[[nodiscard]] constexpr const quantity_type& quantity_from_origin() const& noexcept { return q_; }
[[nodiscard]] constexpr quantity_type&& quantity_from_origin() && noexcept { return std::move(q_); }
[[nodiscard]] constexpr const quantity_type&& quantity_from_origin() const&& noexcept { return std::move(q_); }
template<std::same_as<std::remove_const_t<decltype(PO)>> PO2>
[[nodiscard]] constexpr quantity_type& quantity_ref_from(PO2) & noexcept
{
return q_;
}
template<std::same_as<std::remove_const_t<decltype(PO)>> PO2>
[[nodiscard]] constexpr const quantity_type& quantity_ref_from(PO2) const& noexcept
{
return q_;
}
template<std::same_as<std::remove_const_t<decltype(PO)>> PO2>
[[nodiscard]] constexpr quantity_type&& quantity_ref_from(PO2) && noexcept
{
return std::move(q_);
}
template<std::same_as<std::remove_const_t<decltype(PO)>> PO2>
[[nodiscard]] constexpr const quantity_type&& quantity_ref_from(PO2) const&& noexcept
{
return std::move(q_);
}
#endif

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> in(U) const
{
return make_quantity_point<PO>(quantity_from_origin().in(U{}));
return make_quantity_point<PO>(quantity_ref_from(PO).in(U{}));
}

// member unary operators
Expand Down Expand Up @@ -234,17 +250,17 @@ template<auto R1, auto PO1, typename Rep1, auto R2, typename Rep2>
requires ReferenceOf<std::remove_const_t<decltype(R2)>, PO1.quantity_spec>
[[nodiscard]] constexpr QuantityPoint auto operator+(const quantity_point<R1, PO1, Rep1>& qp,
const quantity<R2, Rep2>& q)
requires requires { qp.quantity_from_origin() + q; }
requires requires { qp.quantity_ref_from(PO1) + q; }
{
return make_quantity_point<PO1>(qp.quantity_from_origin() + q);
return make_quantity_point<PO1>(qp.quantity_ref_from(PO1) + q);
}

template<auto R1, typename Rep1, auto R2, auto PO2, typename Rep2>
// TODO simplify when gcc catches up
requires ReferenceOf<std::remove_const_t<decltype(R1)>, PO2.quantity_spec>
[[nodiscard]] constexpr QuantityPoint auto operator+(const quantity<R1, Rep1>& q,
const quantity_point<R2, PO2, Rep2>& qp)
requires requires { q + qp.quantity_from_origin(); }
requires requires { q + qp.quantity_ref_from(PO2); }
{
return qp + q;
}
Expand All @@ -268,9 +284,9 @@ template<auto R1, auto PO1, typename Rep1, auto R2, typename Rep2>
requires ReferenceOf<std::remove_const_t<decltype(R2)>, PO1.quantity_spec>
[[nodiscard]] constexpr QuantityPoint auto operator-(const quantity_point<R1, PO1, Rep1>& qp,
const quantity<R2, Rep2>& q)
requires requires { qp.quantity_from_origin() - q; }
requires requires { qp.quantity_ref_from(PO1) - q; }
{
return make_quantity_point<PO1>(qp.quantity_from_origin() - q);
return make_quantity_point<PO1>(qp.quantity_ref_from(PO1) - q);
}

template<PointOrigin PO, Quantity Q>
Expand All @@ -284,33 +300,36 @@ 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)
// TODO consider constraining it for both branches
requires requires { lhs.quantity_from_origin() - rhs.quantity_from_origin(); }
requires requires { lhs.quantity_ref_from(QP1::point_origin) - rhs.quantity_ref_from(QP2::point_origin); }
{
if constexpr (is_same_v<std::remove_const_t<decltype(QP1::point_origin)>,
std::remove_const_t<decltype(QP2::point_origin)>>)
return lhs.quantity_from_origin() - rhs.quantity_from_origin();
return lhs.quantity_ref_from(QP1::point_origin) - rhs.quantity_ref_from(QP2::point_origin);
else
return lhs.quantity_from_origin() - rhs.quantity_from_origin() + (lhs.point_origin - rhs.point_origin);
return lhs.quantity_ref_from(QP1::point_origin) - rhs.quantity_ref_from(QP2::point_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 po)
{
if constexpr (is_same_v<std::remove_const_t<decltype(QP::point_origin)>, std::remove_const_t<PO>>)
return qp.quantity_from_origin();
return qp.quantity_ref_from(QP::point_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();
return qp.quantity_ref_from(QP::point_origin);
else
return qp.quantity_from_origin() + (qp.point_origin - qp.absolute_point_origin);
return qp.quantity_ref_from(QP::point_origin) + (qp.point_origin - qp.absolute_point_origin);
} else {
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();
return qp.quantity_ref_from(QP::point_origin) -
po.quantity_point.quantity_ref_from(po.quantity_point.point_origin);
else
return qp.quantity_from_origin() - po.quantity_point.quantity_from_origin() +
return qp.quantity_ref_from(QP::point_origin) -
po.quantity_point.quantity_ref_from(po.quantity_point.point_origin) +
(qp.point_origin - po.quantity_point.point_origin);
}
}
Expand Down Expand Up @@ -343,7 +362,7 @@ template<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
{
if constexpr (is_same_v<std::remove_const_t<decltype(QP1::point_origin)>,
std::remove_const_t<decltype(QP2::point_origin)>>)
return lhs.quantity_from_origin() <=> rhs.quantity_from_origin();
return lhs.quantity_ref_from(QP1::point_origin) <=> rhs.quantity_ref_from(QP2::point_origin);
else
return lhs - lhs.absolute_point_origin <=> rhs - rhs.absolute_point_origin;
}
Expand All @@ -354,7 +373,7 @@ template<QuantityPoint QP1, QuantityPointOf<QP1::absolute_point_origin> QP2>
{
if constexpr (is_same_v<std::remove_const_t<decltype(QP1::point_origin)>,
std::remove_const_t<decltype(QP2::point_origin)>>)
return lhs.quantity_from_origin() == rhs.quantity_from_origin();
return lhs.quantity_ref_from(QP1::point_origin) == rhs.quantity_ref_from(QP2::point_origin);
else
return lhs - lhs.absolute_point_origin == rhs - rhs.absolute_point_origin;
}
Expand Down
Loading

0 comments on commit bbaf1b5

Please sign in to comment.