feat: quantity compound assignment now preserves the value category

This commit is contained in:
Mateusz Pusz
2023-09-13 19:57:05 +02:00
parent 6d619d67ef
commit 6a1e600f65

View File

@@ -269,88 +269,97 @@ public:
} }
// compound assignment operators // compound assignment operators
constexpr quantity& operator+=(const quantity& q) template<typename Q>
requires requires(rep a, rep b) { requires std::derived_from<std::remove_cvref_t<Q>, quantity> && requires(rep a, rep b) {
{ {
a += b a += b
} -> std::same_as<rep&>; } -> std::same_as<rep&>;
} }
friend constexpr decltype(auto) operator+=(Q&& lhs, const quantity& rhs)
{ {
numerical_value_ += q.numerical_value_ref_in(unit); lhs.numerical_value_ += rhs.numerical_value_;
return *this; return std::forward<Q>(lhs);
} }
constexpr quantity& operator-=(const quantity& q) template<typename Q>
requires requires(rep a, rep b) { requires std::derived_from<std::remove_cvref_t<Q>, quantity> && requires(rep a, rep b) {
{ {
a -= b a -= b
} -> std::same_as<rep&>; } -> std::same_as<rep&>;
} }
friend constexpr decltype(auto) operator-=(Q&& lhs, const quantity& rhs)
{ {
numerical_value_ -= q.numerical_value_ref_in(unit); lhs.numerical_value_ -= rhs.numerical_value_;
return *this; return std::forward<Q>(lhs);
} }
constexpr quantity& operator%=(const quantity& q) template<typename Q>
requires(!treat_as_floating_point<rep>) && requires(rep a, rep b) { requires std::derived_from<std::remove_cvref_t<Q>, quantity> && (!treat_as_floating_point<rep>) &&
{ requires(rep a, rep b) {
a %= b {
} -> std::same_as<rep&>; a %= b
} } -> std::same_as<rep&>;
}
friend constexpr decltype(auto) operator%=(Q&& lhs, const quantity& rhs)
{ {
gsl_ExpectsAudit(q != zero()); gsl_ExpectsAudit(rhs != zero());
numerical_value_ %= q.numerical_value_ref_in(unit); lhs.numerical_value_ %= rhs.numerical_value_;
return *this; return std::forward<Q>(lhs);
} }
template<typename Value> template<typename Q, typename Value>
requires(!Quantity<Value>) && requires(rep a, const Value b) { requires std::derived_from<std::remove_cvref_t<Q>, quantity> && (!Quantity<Value>) &&
{ requires(rep a, const Value b) {
a *= b {
} -> std::same_as<rep&>; a *= b
} } -> std::same_as<rep&>;
constexpr quantity& operator*=(const Value& v) }
friend constexpr decltype(auto) operator*=(Q&& lhs, const Value& v)
{ {
numerical_value_ *= v; lhs.numerical_value_ *= v;
return *this; return std::forward<Q>(lhs);
} }
template<QuantityOf<dimension_one> Q> template<typename Q1, QuantityOf<dimension_one> Q2>
requires(Q::unit == ::mp_units::one) && requires(rep a, const typename Q::rep b) { requires std::derived_from<std::remove_cvref_t<Q1>, quantity> && (Q2::unit == ::mp_units::one) &&
{ requires(rep a, const typename Q2::rep b) {
a *= b {
} -> std::same_as<rep&>; a *= b
} } -> std::same_as<rep&>;
constexpr quantity& operator*=(const Q& rhs) }
friend constexpr decltype(auto) operator*=(Q1&& lhs, const Q2& rhs)
{ {
numerical_value_ *= rhs.numerical_value_ref_in(::mp_units::one); lhs.numerical_value_ *= rhs.numerical_value_;
return *this; return std::forward<Q1>(lhs);
} }
template<typename Value> template<typename Q, typename Value>
requires(!Quantity<Value>) && requires(rep a, const Value b) { requires std::derived_from<std::remove_cvref_t<Q>, quantity> && (!Quantity<Value>) &&
{ requires(rep a, const Value b) {
a /= b {
} -> std::same_as<rep&>; a /= b
} } -> std::same_as<rep&>;
constexpr quantity& operator/=(const Value& v) }
friend constexpr decltype(auto) operator/=(Q&& lhs, const Value& v)
{ {
gsl_ExpectsAudit(v != quantity_values<Value>::zero()); gsl_ExpectsAudit(v != quantity_values<Value>::zero());
numerical_value_ /= v; lhs.numerical_value_ /= v;
return *this; return std::forward<Q>(lhs);
} }
template<QuantityOf<dimension_one> Q> template<typename Q1, QuantityOf<dimension_one> Q2>
requires(Q::unit == ::mp_units::one) && requires(rep a, const typename Q::rep b) { requires std::derived_from<std::remove_cvref_t<Q1>, quantity> && (Q2::unit == ::mp_units::one) &&
{ requires(rep a, const typename Q2::rep b) {
a /= b {
} -> std::same_as<rep&>; a /= b
} } -> std::same_as<rep&>;
constexpr quantity& operator/=(const Q& rhs) }
friend constexpr decltype(auto) operator/=(Q1&& lhs, const Q2& rhs)
{ {
gsl_ExpectsAudit(rhs != rhs.zero()); gsl_ExpectsAudit(rhs != rhs.zero());
numerical_value_ /= rhs.numerical_value_ref_in(::mp_units::one); lhs.numerical_value_ /= rhs.numerical_value_;
return *this; return std::forward<Q1>(lhs);
} }
private: private: