Skip to content

Commit

Permalink
refactor: Forwarding concept applied to quantity and `quantity_po…
Browse files Browse the repository at this point in the history
…int`
  • Loading branch information
mpusz committed Oct 29, 2024
1 parent 699b0e7 commit c51b6e1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
57 changes: 30 additions & 27 deletions src/core/include/mp-units/framework/quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ concept SameValueAs = (equivalent(get_unit(R1), get_unit(R2))) && std::convertib
template<typename T>
using quantity_like_type = quantity<quantity_like_traits<T>::reference, typename quantity_like_traits<T>::rep>;

template<typename T, typename U>
concept Forwarding = std::derived_from<std::remove_cvref_t<T>, U>;

} // namespace detail

MP_UNITS_EXPORT_BEGIN
Expand Down Expand Up @@ -335,14 +338,14 @@ class quantity {
return ::mp_units::quantity{-numerical_value_is_an_implementation_detail_, reference};
}

template<typename FwdQ, std::derived_from<quantity> Q = std::remove_cvref_t<FwdQ>>
friend constexpr decltype(auto) operator++(FwdQ&& q)
template<detail::Forwarding<quantity> Q>
friend constexpr decltype(auto) operator++(Q&& q)
requires requires(rep v) {
{ ++v } -> std::same_as<rep&>;
}
{
++q.numerical_value_is_an_implementation_detail_;
return std::forward<FwdQ>(q);
return std::forward<Q>(q);
}

[[nodiscard]] constexpr QuantityOf<quantity_spec> auto operator++(int)
Expand All @@ -353,14 +356,14 @@ class quantity {
return ::mp_units::quantity{numerical_value_is_an_implementation_detail_++, reference};
}

template<typename FwdQ, std::derived_from<quantity> Q = std::remove_cvref_t<FwdQ>>
friend constexpr decltype(auto) operator--(FwdQ&& q)
template<detail::Forwarding<quantity> Q>
friend constexpr decltype(auto) operator--(Q&& q)
requires requires(rep v) {
{ --v } -> std::same_as<rep&>;
}
{
--q.numerical_value_is_an_implementation_detail_;
return std::forward<FwdQ>(q);
return std::forward<Q>(q);
}

[[nodiscard]] constexpr QuantityOf<quantity_spec> auto operator--(int)
Expand All @@ -372,98 +375,98 @@ class quantity {
}

// compound assignment operators
template<typename FwdQ, auto R2, typename Rep2, std::derived_from<quantity> Q = std::remove_cvref_t<FwdQ>>
template<detail::Forwarding<quantity> Q, auto R2, typename Rep2>
requires detail::QuantityConvertibleTo<quantity<R2, rep>, quantity> && requires(rep a, Rep2 b) {
{ a += b } -> std::same_as<rep&>;
}
friend constexpr decltype(auto) operator+=(FwdQ&& lhs, const quantity<R2, Rep2>& rhs)
friend constexpr decltype(auto) operator+=(Q&& lhs, const quantity<R2, Rep2>& rhs)
{
if constexpr (equivalent(unit, get_unit(R2)))
lhs.numerical_value_is_an_implementation_detail_ += rhs.numerical_value_is_an_implementation_detail_;
else
lhs.numerical_value_is_an_implementation_detail_ += rhs.in(lhs.unit).numerical_value_is_an_implementation_detail_;
return std::forward<FwdQ>(lhs);
return std::forward<Q>(lhs);
}

template<typename FwdQ, auto R2, typename Rep2, std::derived_from<quantity> Q = std::remove_cvref_t<FwdQ>>
template<detail::Forwarding<quantity> Q, auto R2, typename Rep2>
requires detail::QuantityConvertibleTo<quantity<R2, rep>, quantity> && requires(rep a, Rep2 b) {
{ a -= b } -> std::same_as<rep&>;
}
friend constexpr decltype(auto) operator-=(FwdQ&& lhs, const quantity<R2, Rep2>& rhs)
friend constexpr decltype(auto) operator-=(Q&& lhs, const quantity<R2, Rep2>& rhs)
{
if constexpr (equivalent(unit, get_unit(R2)))
lhs.numerical_value_is_an_implementation_detail_ -= rhs.numerical_value_is_an_implementation_detail_;
else
lhs.numerical_value_is_an_implementation_detail_ -= rhs.in(lhs.unit).numerical_value_is_an_implementation_detail_;
return std::forward<FwdQ>(lhs);
return std::forward<Q>(lhs);
}

template<typename FwdQ, auto R2, typename Rep2, std::derived_from<quantity> Q = std::remove_cvref_t<FwdQ>>
template<detail::Forwarding<quantity> Q, auto R2, typename Rep2>
requires detail::QuantityConvertibleTo<quantity<R2, rep>, quantity> && (!treat_as_floating_point<rep>) &&
requires(rep a, Rep2 b) {
{ a %= b } -> std::same_as<rep&>;
}
friend constexpr decltype(auto) operator%=(FwdQ&& lhs, const quantity<R2, Rep2>& rhs)
friend constexpr decltype(auto) operator%=(Q&& lhs, const quantity<R2, Rep2>& rhs)

{
MP_UNITS_EXPECTS_DEBUG(rhs != zero());
if constexpr (equivalent(unit, get_unit(R2)))
lhs.numerical_value_is_an_implementation_detail_ %= rhs.numerical_value_is_an_implementation_detail_;
else
lhs.numerical_value_is_an_implementation_detail_ %= rhs.in(lhs.unit).numerical_value_is_an_implementation_detail_;
return std::forward<FwdQ>(lhs);
return std::forward<Q>(lhs);
}

template<typename FwdQ, typename Value, std::derived_from<quantity> Q = std::remove_cvref_t<FwdQ>>
template<detail::Forwarding<quantity> Q, typename Value>
requires(!Quantity<Value>) && requires(rep a, Value b) {
{ a *= b } -> std::same_as<rep&>;
}
friend constexpr decltype(auto) operator*=(FwdQ&& lhs, const Value& v)
friend constexpr decltype(auto) operator*=(Q&& lhs, const Value& v)
{
// TODO use *= when compiler bug is resolved:
// https://developercommunity.visualstudio.com/t/Discrepancy-in-Behavior-of-operator-an/10732445
lhs.numerical_value_is_an_implementation_detail_ = lhs.numerical_value_is_an_implementation_detail_ * v;
return std::forward<FwdQ>(lhs);
return std::forward<Q>(lhs);
}

template<typename FwdQ1, QuantityOf<dimensionless> Q2, std::derived_from<quantity> Q1 = std::remove_cvref_t<FwdQ1>>
template<detail::Forwarding<quantity> Q1, QuantityOf<dimensionless> Q2>
requires(Q2::unit == ::mp_units::one) && requires(rep a, Q2::rep b) {
{ a *= b } -> std::same_as<rep&>;
}
friend constexpr decltype(auto) operator*=(FwdQ1&& lhs, const Q2& rhs)
friend constexpr decltype(auto) operator*=(Q1&& lhs, const Q2& rhs)
{
// TODO use *= when compiler bug is resolved:
// https://developercommunity.visualstudio.com/t/Discrepancy-in-Behavior-of-operator-an/10732445
lhs.numerical_value_is_an_implementation_detail_ =
lhs.numerical_value_is_an_implementation_detail_ * rhs.numerical_value_is_an_implementation_detail_;
return std::forward<FwdQ1>(lhs);
return std::forward<Q1>(lhs);
}

template<typename FwdQ, typename Value, std::derived_from<quantity> Q = std::remove_cvref_t<FwdQ>>
template<detail::Forwarding<quantity> Q, typename Value>
requires(!Quantity<Value>) && requires(rep a, Value b) {
{ a /= b } -> std::same_as<rep&>;
}
friend constexpr decltype(auto) operator/=(FwdQ&& lhs, const Value& v)
friend constexpr decltype(auto) operator/=(Q&& lhs, const Value& v)
{
MP_UNITS_EXPECTS_DEBUG(v != quantity_values<Value>::zero());
// TODO use /= when compiler bug is resolved:
// https://developercommunity.visualstudio.com/t/Discrepancy-in-Behavior-of-operator-an/10732445
lhs.numerical_value_is_an_implementation_detail_ = lhs.numerical_value_is_an_implementation_detail_ / v;
return std::forward<FwdQ>(lhs);
return std::forward<Q>(lhs);
}

template<typename FwdQ1, QuantityOf<dimensionless> Q2, std::derived_from<quantity> Q1 = std::remove_cvref_t<FwdQ1>>
template<detail::Forwarding<quantity> Q1, QuantityOf<dimensionless> Q2>
requires(Q2::unit == ::mp_units::one) && requires(rep a, Q2::rep b) {
{ a /= b } -> std::same_as<rep&>;
}
friend constexpr decltype(auto) operator/=(FwdQ1&& lhs, const Q2& rhs)
friend constexpr decltype(auto) operator/=(Q1&& lhs, const Q2& rhs)
{
MP_UNITS_EXPECTS_DEBUG(rhs != rhs.zero());
// TODO use /= when compiler bug is resolved:
// https://developercommunity.visualstudio.com/t/Discrepancy-in-Behavior-of-operator-an/10732445
lhs.numerical_value_is_an_implementation_detail_ =
lhs.numerical_value_is_an_implementation_detail_ / rhs.numerical_value_is_an_implementation_detail_;
return std::forward<FwdQ1>(lhs);
return std::forward<Q1>(lhs);
}

// binary operators on quantities
Expand Down
24 changes: 12 additions & 12 deletions src/core/include/mp-units/framework/quantity_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,12 @@ class quantity_point {
}

// member unary operators
template<typename FwdQP, std::derived_from<quantity_point> QP = std::remove_cvref_t<FwdQP>>
friend constexpr decltype(auto) operator++(FwdQP&& qp)
template<detail::Forwarding<quantity_point> QP>
friend constexpr decltype(auto) operator++(QP&& qp)
requires requires { ++qp.quantity_from_origin_is_an_implementation_detail_; }
{
++qp.quantity_from_origin_is_an_implementation_detail_;
return std::forward<FwdQP>(qp);
return std::forward<QP>(qp);
}

[[nodiscard]] constexpr quantity_point operator++(int)
Expand All @@ -416,12 +416,12 @@ class quantity_point {
return {quantity_from_origin_is_an_implementation_detail_++, PO};
}

template<typename FwdQP, std::derived_from<quantity_point> QP = std::remove_cvref_t<FwdQP>>
friend constexpr decltype(auto) operator--(FwdQP&& qp)
template<detail::Forwarding<quantity_point> QP>
friend constexpr decltype(auto) operator--(QP&& qp)
requires requires { --qp.quantity_from_origin_is_an_implementation_detail_; }
{
--qp.quantity_from_origin_is_an_implementation_detail_;
return std::forward<FwdQP>(qp);
return std::forward<QP>(qp);
}

[[nodiscard]] constexpr quantity_point operator--(int)
Expand All @@ -431,20 +431,20 @@ class quantity_point {
}

// compound assignment operators
template<typename FwdQP, std::derived_from<quantity_point> QP = std::remove_cvref_t<FwdQP>>
template<detail::Forwarding<quantity_point> QP>
requires requires(quantity_type q) { quantity_from_origin_is_an_implementation_detail_ += q; }
friend constexpr decltype(auto) operator+=(FwdQP&& qp, const quantity_type& q)
friend constexpr decltype(auto) operator+=(QP&& qp, const quantity_type& q)
{
qp.quantity_from_origin_is_an_implementation_detail_ += q;
return std::forward<FwdQP>(qp);
return std::forward<QP>(qp);
}

template<typename FwdQP, std::derived_from<quantity_point> QP = std::remove_cvref_t<FwdQP>>
template<detail::Forwarding<quantity_point> QP>
requires requires(quantity_type q) { quantity_from_origin_is_an_implementation_detail_ -= q; }
friend constexpr decltype(auto) operator-=(FwdQP&& qp, const quantity_type& q)
friend constexpr decltype(auto) operator-=(QP&& qp, const quantity_type& q)
{
qp.quantity_from_origin_is_an_implementation_detail_ -= q;
return std::forward<FwdQP>(qp);
return std::forward<QP>(qp);
}

// binary operators on quantity points
Expand Down

2 comments on commit c51b6e1

@JohelEGP
Copy link
Collaborator

@JohelEGP JohelEGP commented on c51b6e1 Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like you can also apply it to quantity_point::operator+ and related operations.
Ah, it's that those take a const& parameter.

@mpusz
Copy link
Owner Author

@mpusz mpusz commented on c51b6e1 Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what you mean here? Those operators do not take forwarding references.

Please sign in to comment.