From d45096073caa549dab3f0739a628515511e98397 Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Mon, 2 Dec 2024 10:54:09 -0500 Subject: [PATCH] Add `as_raw_number(...)` utility (#333) This lets us pave the way to change the behavior of quantity arithmetic when all units cancel out (#185). If we make this change all at once, it'll generally be too big and hard to handle. But if we provide this utility now, then people can migrate individual callsites gradually over time. Then, at the end (probably 0.5.0?), making the switch to the new behavior won't be such a big change. Helps #185. --- au/code/au/quantity.hh | 14 ++++++++++++++ au/code/au/quantity_test.cc | 29 +++++++++++++++++++++++++++++ docs/reference/quantity.md | 28 ++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/au/code/au/quantity.hh b/au/code/au/quantity.hh index 3cdf2b93..cb9c7866 100644 --- a/au/code/au/quantity.hh +++ b/au/code/au/quantity.hh @@ -534,6 +534,20 @@ constexpr auto operator%(Quantity q1, Quantity q2) { return make_quantity(q1.in(U{}) % q2.in(U{})); } +// Callsite-readable way to convert a `Quantity` to a raw number. +// +// Only works for dimensionless `Quantities`; will return a compile-time error otherwise. +// +// Identity for non-`Quantity` types. +template +constexpr R as_raw_number(Quantity q) { + return q.as(UnitProductT<>{}); +} +template +constexpr T as_raw_number(T x) { + return x; +} + // Type trait to detect whether two Quantity types are equivalent. // // In this library, Quantity types are "equivalent" exactly when they use the same Rep, and are diff --git a/au/code/au/quantity_test.cc b/au/code/au/quantity_test.cc index b9dce430..986c33c7 100644 --- a/au/code/au/quantity_test.cc +++ b/au/code/au/quantity_test.cc @@ -55,6 +55,12 @@ static constexpr QuantityMaker meters{}; static_assert(are_units_quantity_equivalent(Centi{} * mag<254>(), Inches{} * mag<100>()), "Double-check this ad hoc definition of meters"); +struct Unos : decltype(UnitProductT<>{}) {}; +constexpr auto unos = QuantityMaker{}; + +struct Percent : decltype(Unos{} / mag<100>()) {}; +constexpr auto percent = QuantityMaker{}; + struct Hours : UnitImpl