Skip to content

Commit

Permalink
Support scaling a unit symbol by a magnitude (#341)
Browse files Browse the repository at this point in the history
I found this useful in handling some "liters per 100 km" test cases.
  • Loading branch information
chiphogg authored Dec 2, 2024
1 parent bb7911f commit 72c2d06
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion au/code/au/unit_symbol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace au {
template <typename Unit>
struct SymbolFor : detail::MakesQuantityFromNumber<SymbolFor, Unit>,
detail::ScalesQuantity<SymbolFor, Unit>,
detail::ComposesWith<SymbolFor, Unit, SymbolFor, SymbolFor> {};
detail::ComposesWith<SymbolFor, Unit, SymbolFor, SymbolFor>,
detail::CanScaleByMagnitude<SymbolFor, Unit> {};

//
// Create a unit symbol using the more fluent APIs that unit slots make possible. For example:
Expand Down
8 changes: 8 additions & 0 deletions au/code/au/unit_symbol_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ TEST(SymbolFor, ScalesUnitsOfExistingQuantity) {

TEST(SymbolFor, CompatibleWithUnitSlot) { EXPECT_THAT(meters(35u).in(m), SameTypeAndValue(35u)); }

TEST(SymbolFor, CanScaleByMagnitude) {
// Yes, the identifier name is a little awkward for these symbols for anonymous scaled units.
// But it's still important to have this functionality.
constexpr auto u100_m = mag<100>() * m;

EXPECT_THAT(3.5f / u100_m, SameTypeAndValue(inverse(meters * mag<100>())(3.5f)));
}

} // namespace au

0 comments on commit 72c2d06

Please sign in to comment.