fix: gsl_ExpectsAudit() added for all relevant quantity and quantity_kind operations

Resolves @218
This commit is contained in:
Mateusz Pusz
2021-02-20 17:17:45 +01:00
parent 35e6253fb8
commit a41b3a42fe
2 changed files with 18 additions and 0 deletions

View File

@@ -223,6 +223,7 @@ public:
constexpr quantity& operator/=(const Rep2& rhs) constexpr quantity& operator/=(const Rep2& rhs)
requires requires(rep a, const Rep2 b) { { a /= b } -> std::same_as<rep&>; } requires requires(rep a, const Rep2 b) { { a /= b } -> std::same_as<rep&>; }
{ {
gsl_ExpectsAudit(rhs != quantity_values<Rep2>::zero());
value_ /= rhs; value_ /= rhs;
return *this; return *this;
} }
@@ -230,6 +231,7 @@ public:
constexpr quantity& operator/=(const dimensionless<units::one, Rep2>& rhs) constexpr quantity& operator/=(const dimensionless<units::one, Rep2>& rhs)
requires requires(rep a, const Rep2 b) { { a /= b } -> std::same_as<rep&>; } requires requires(rep a, const Rep2 b) { { a /= b } -> std::same_as<rep&>; }
{ {
gsl_ExpectsAudit(rhs.count() != quantity_values<Rep2>::zero());
value_ /= rhs.count(); value_ /= rhs.count();
return *this; return *this;
} }
@@ -239,6 +241,7 @@ public:
requires (!floating_point_<rep>) && (!floating_point_<Rep2>) && requires (!floating_point_<rep>) && (!floating_point_<Rep2>) &&
requires(rep a, const Rep2 b) { { a %= b } -> std::same_as<rep&>; } requires(rep a, const Rep2 b) { { a %= b } -> std::same_as<rep&>; }
{ {
gsl_ExpectsAudit(rhs != quantity_values<Rep2>::zero());
value_ %= rhs; value_ %= rhs;
return *this; return *this;
} }
@@ -247,6 +250,7 @@ public:
requires (!floating_point_<rep>) && requires (!floating_point_<rep>) &&
requires(rep a, rep b) { { a %= b } -> std::same_as<rep&>; } requires(rep a, rep b) { { a %= b } -> std::same_as<rep&>; }
{ {
gsl_ExpectsAudit(q.count() != quantity_values<rep>::zero());
value_ %= q.count(); value_ %= q.count();
return *this; return *this;
} }
@@ -328,6 +332,7 @@ public:
invoke_result_convertible_to_<rep, std::modulus<>, rep, const Value&> invoke_result_convertible_to_<rep, std::modulus<>, rep, const Value&>
[[nodiscard]] friend constexpr Quantity auto operator%(const quantity& q, const Value& v) [[nodiscard]] friend constexpr Quantity auto operator%(const quantity& q, const Value& v)
{ {
gsl_ExpectsAudit(v != quantity_values<Value>::zero());
using ret = quantity<D, U, std::invoke_result_t<std::modulus<>, rep, Value>>; using ret = quantity<D, U, std::invoke_result_t<std::modulus<>, rep, Value>>;
return ret(q.count() % v); return ret(q.count() % v);
} }
@@ -336,6 +341,7 @@ public:
requires (!floating_point_<rep>) && is_same_v<unit, units::one> && requires (!floating_point_<rep>) && is_same_v<unit, units::one> &&
invoke_result_convertible_to_<rep, std::modulus<>, rep, rep> invoke_result_convertible_to_<rep, std::modulus<>, rep, rep>
{ {
gsl_ExpectsAudit(rhs.count() != quantity_values<rep>::zero());
return units::quantity(lhs.count() % rhs.count()); return units::quantity(lhs.count() % rhs.count());
} }
@@ -402,6 +408,7 @@ template<typename D1, typename U1, typename Rep1, typename U2, typename Rep2>
quantity_value_for_<std::modulus<>, Rep1, Rep2> quantity_value_for_<std::modulus<>, Rep1, Rep2>
[[nodiscard]] constexpr Quantity auto operator%(const quantity<D1, U1, Rep1>& lhs, const quantity<dim_one, U2, Rep2>& rhs) [[nodiscard]] constexpr Quantity auto operator%(const quantity<D1, U1, Rep1>& lhs, const quantity<dim_one, U2, Rep2>& rhs)
{ {
gsl_ExpectsAudit(rhs.count() != quantity_values<Rep2>::zero());
using unit = downcast_unit<D1, U1::ratio * U2::ratio>; using unit = downcast_unit<D1, U1::ratio * U2::ratio>;
using ret = quantity<D1, unit, std::invoke_result_t<std::modulus<>, Rep1, Rep2>>; using ret = quantity<D1, unit, std::invoke_result_t<std::modulus<>, Rep1, Rep2>>;
return ret(lhs.count() % rhs.count()); return ret(lhs.count() % rhs.count());
@@ -412,6 +419,7 @@ template<Quantity Q1, QuantityEquivalentTo<Q1> Q2>
quantity_value_for_<std::modulus<>, typename Q1::rep, typename Q2::rep> quantity_value_for_<std::modulus<>, typename Q1::rep, typename Q2::rep>
[[nodiscard]] constexpr Quantity auto operator%(const Q1& lhs, const Q2& rhs) [[nodiscard]] constexpr Quantity auto operator%(const Q1& lhs, const Q2& rhs)
{ {
gsl_ExpectsAudit(rhs.count() != quantity_values<typename Q2::rep>::zero());
using ret = common_quantity_for<std::modulus<>, Q1, Q2>; using ret = common_quantity_for<std::modulus<>, Q1, Q2>;
return ret(ret(lhs).count() % ret(rhs).count()); return ret(ret(lhs).count() % ret(rhs).count());
} }

View File

@@ -185,6 +185,7 @@ public:
constexpr quantity_kind& operator/=(const Rep2& rhs) constexpr quantity_kind& operator/=(const Rep2& rhs)
requires requires(quantity_type q) { q /= rhs; } requires requires(quantity_type q) { q /= rhs; }
{ {
gsl_ExpectsAudit(rhs != quantity_values<Rep2>::zero());
q_ /= rhs; q_ /= rhs;
return *this; return *this;
} }
@@ -193,6 +194,7 @@ public:
constexpr quantity_kind& operator%=(const Rep2& rhs) constexpr quantity_kind& operator%=(const Rep2& rhs)
requires (!Quantity<Rep2>) && requires(quantity_type q, const Rep2 r) { q %= r; } requires (!Quantity<Rep2>) && requires(quantity_type q, const Rep2 r) { q %= r; }
{ {
gsl_ExpectsAudit(rhs != quantity_values<Rep2>::zero());
q_ %= rhs; q_ %= rhs;
return *this; return *this;
} }
@@ -200,6 +202,7 @@ public:
constexpr quantity_kind& operator%=(const quantity_kind& qk) constexpr quantity_kind& operator%=(const quantity_kind& qk)
requires requires(quantity_type q) { q %= qk.common(); } requires requires(quantity_type q) { q %= qk.common(); }
{ {
gsl_ExpectsAudit(qk.common().count() != quantity_values<rep>::zero());
q_ %= qk.common(); q_ %= qk.common();
return *this; return *this;
} }
@@ -224,6 +227,7 @@ public:
[[nodiscard]] friend constexpr QuantityKind auto operator/(const quantity_kind& qk, const Value& v) [[nodiscard]] friend constexpr QuantityKind auto operator/(const quantity_kind& qk, const Value& v)
requires requires { { qk.common() / v } -> Quantity; } requires requires { { qk.common() / v } -> Quantity; }
{ {
gsl_ExpectsAudit(v != quantity_values<Value>::zero());
return detail::make_quantity_kind<quantity_kind>(qk.common() / v); return detail::make_quantity_kind<quantity_kind>(qk.common() / v);
} }
@@ -231,6 +235,7 @@ public:
[[nodiscard]] friend constexpr QuantityKind auto operator/(const Value& v, const quantity_kind& qk) [[nodiscard]] friend constexpr QuantityKind auto operator/(const Value& v, const quantity_kind& qk)
requires requires { { v / qk.common() } -> Quantity; } requires requires { { v / qk.common() } -> Quantity; }
{ {
gsl_ExpectsAudit(qk.common().count() != quantity_values<rep>::zero());
return detail::downcasted_kind<quantity_kind>(v / qk.common()); return detail::downcasted_kind<quantity_kind>(v / qk.common());
} }
@@ -238,6 +243,7 @@ public:
[[nodiscard]] friend constexpr QuantityKind auto operator%(const quantity_kind& qk, const Value& v) [[nodiscard]] friend constexpr QuantityKind auto operator%(const quantity_kind& qk, const Value& v)
requires requires { qk.common() % v; } requires requires { qk.common() % v; }
{ {
gsl_ExpectsAudit(v != quantity_values<Value>::zero());
return detail::make_quantity_kind<quantity_kind>(qk.common() % v); return detail::make_quantity_kind<quantity_kind>(qk.common() % v);
} }
@@ -285,6 +291,7 @@ template<QuantityKind QK, Quantity Q>
[[nodiscard]] constexpr QuantityKind auto operator/(const QK& lhs, const Q& rhs) [[nodiscard]] constexpr QuantityKind auto operator/(const QK& lhs, const Q& rhs)
requires requires { lhs.common() / rhs; } requires requires { lhs.common() / rhs; }
{ {
gsl_ExpectsAudit(rhs.count() != quantity_values<typename Q::rep>::zero());
return detail::downcasted_kind<QK>(lhs.common() / rhs); return detail::downcasted_kind<QK>(lhs.common() / rhs);
} }
@@ -292,6 +299,7 @@ template<Quantity Q, QuantityKind QK>
[[nodiscard]] constexpr QuantityKind auto operator/(const Q& lhs, const QK& rhs) [[nodiscard]] constexpr QuantityKind auto operator/(const Q& lhs, const QK& rhs)
requires requires { lhs / rhs.common(); } requires requires { lhs / rhs.common(); }
{ {
gsl_ExpectsAudit(rhs.common().count() != quantity_values<typename QK::rep>::zero());
return detail::downcasted_kind<QK>(lhs / rhs.common()); return detail::downcasted_kind<QK>(lhs / rhs.common());
} }
@@ -299,6 +307,7 @@ template<QuantityKind QK, Dimensionless D>
[[nodiscard]] constexpr QuantityKind auto operator%(const QK& lhs, const D& rhs) [[nodiscard]] constexpr QuantityKind auto operator%(const QK& lhs, const D& rhs)
requires requires { lhs.common() % rhs; } requires requires { lhs.common() % rhs; }
{ {
gsl_ExpectsAudit(rhs.count() != quantity_values<typename D::rep>::zero());
return detail::make_quantity_kind<QK>(lhs.common() % rhs); return detail::make_quantity_kind<QK>(lhs.common() % rhs);
} }
@@ -306,6 +315,7 @@ template<QuantityKind QK1, QuantityKindEquivalentTo<QK1> QK2>
[[nodiscard]] constexpr QuantityKind auto operator%(const QK1& lhs, const QK2& rhs) [[nodiscard]] constexpr QuantityKind auto operator%(const QK1& lhs, const QK2& rhs)
requires requires { lhs.common() % rhs.common(); } requires requires { lhs.common() % rhs.common(); }
{ {
gsl_ExpectsAudit(rhs.common().count() != quantity_values<typename QK2::rep>::zero());
return detail::make_quantity_kind<QK1>(lhs.common() % rhs.common()); return detail::make_quantity_kind<QK1>(lhs.common() % rhs.common());
} }