From e608c7246e51c66b4767e84c925cab75e9123f50 Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Mon, 24 Jul 2023 13:16:06 -0400 Subject: [PATCH] Add Rep-named aliases for `int`, `unsigned int` (#150) This is all about the ability to express intent. Despite their reduced specificity compared to, e.g., `int32_t`, `unsigned int` and `int` are very common for end users to write in code. But right now it's disproportionately hard for them to spell the corresponding `Quantity` type concisely. I'm not sure if we'll add other aliases such as `long` or `long long`, but these two seem like a clear improvement. I also updated an obsolete comment which no longer referred to the correct Rep-named alias. --- au/conversion_policy.hh | 2 +- au/quantity.hh | 4 ++++ au/quantity_point.hh | 4 ++++ au/quantity_point_test.cc | 11 +++++++++++ au/quantity_test.cc | 11 +++++++++++ docs/reference/quantity.md | 2 ++ docs/tutorial/102-api-types.md | 2 ++ 7 files changed, 35 insertions(+), 1 deletion(-) diff --git a/au/conversion_policy.hh b/au/conversion_policy.hh index cbfb5e47..023e3c54 100644 --- a/au/conversion_policy.hh +++ b/au/conversion_policy.hh @@ -36,7 +36,7 @@ constexpr bool can_scale_without_overflow(Magnitude m, Rep value) { } namespace detail { -// Chosen so as to allow populating a `QuantityU32` with an input in MHz. +// Chosen so as to allow populating a `QuantityI32` with an input in MHz. constexpr auto OVERFLOW_THRESHOLD = 2'147; // This wrapper for `can_scale_without_overflow<...>(..., OVERFLOW_THRESHOLD)` can prevent an diff --git a/au/quantity.hh b/au/quantity.hh index 1f706a98..cfc8fd7d 100644 --- a/au/quantity.hh +++ b/au/quantity.hh @@ -456,6 +456,10 @@ using QuantityD = Quantity; template using QuantityF = Quantity; template +using QuantityI = Quantity; +template +using QuantityU = Quantity; +template using QuantityI32 = Quantity; template using QuantityU32 = Quantity; diff --git a/au/quantity_point.hh b/au/quantity_point.hh index f649271a..e9d39117 100644 --- a/au/quantity_point.hh +++ b/au/quantity_point.hh @@ -308,6 +308,10 @@ using QuantityPointD = QuantityPoint; template using QuantityPointF = QuantityPoint; template +using QuantityPointI = QuantityPoint; +template +using QuantityPointU = QuantityPoint; +template using QuantityPointI32 = QuantityPoint; template using QuantityPointU32 = QuantityPoint; diff --git a/au/quantity_point_test.cc b/au/quantity_point_test.cc index 6b3cb4a4..46f90eed 100644 --- a/au/quantity_point_test.cc +++ b/au/quantity_point_test.cc @@ -42,6 +42,17 @@ struct Celsius : Kelvins { constexpr QuantityMaker celsius_qty{}; constexpr QuantityPointMaker celsius_pt{}; +TEST(Quantity, HasCorrectRepNamedAliases) { + StaticAssertTypeEq, QuantityPoint>(); + StaticAssertTypeEq, QuantityPoint>(); + StaticAssertTypeEq, QuantityPoint>(); + StaticAssertTypeEq, QuantityPoint>(); + StaticAssertTypeEq, QuantityPoint>(); + StaticAssertTypeEq, QuantityPoint>(); + StaticAssertTypeEq, QuantityPoint>(); + StaticAssertTypeEq, QuantityPoint>(); +} + TEST(QuantityPoint, HasExpectedDiffType) { StaticAssertTypeEq::Diff, QuantityI32>(); StaticAssertTypeEq::Diff, QuantityF>(); diff --git a/au/quantity_test.cc b/au/quantity_test.cc index ffad2c2b..c4623769 100644 --- a/au/quantity_test.cc +++ b/au/quantity_test.cc @@ -70,6 +70,17 @@ TEST(Quantity, IsItaniumAbiRegisterCompatible) { EXPECT_TRUE(std::is_trivially_move_constructible>::value); } +TEST(Quantity, HasCorrectRepNamedAliases) { + StaticAssertTypeEq, Quantity>(); + StaticAssertTypeEq, Quantity>(); + StaticAssertTypeEq, Quantity>(); + StaticAssertTypeEq, Quantity>(); + StaticAssertTypeEq, Quantity>(); + StaticAssertTypeEq, Quantity>(); + StaticAssertTypeEq, Quantity>(); + StaticAssertTypeEq, Quantity>(); +} + TEST(Quantity, CanCreateAndReadValuesByNamingUnits) { constexpr auto x = feet(3.14); constexpr double output_value = x.in(feet); diff --git a/docs/reference/quantity.md b/docs/reference/quantity.md index 5d70050f..abf8caf3 100644 --- a/docs/reference/quantity.md +++ b/docs/reference/quantity.md @@ -30,6 +30,8 @@ aliases", so you can express the rep more precisely, and put the visual focus on |-----------------|----------------| | `QuantityD` | `Quantity` | | `QuantityF` | `Quantity` | +| `QuantityI` | `Quantity` | +| `QuantityU` | `Quantity` | | `QuantityI32` | `Quantity` | | `QuantityU32` | `Quantity` | | `QuantityI64` | `Quantity` | diff --git a/docs/tutorial/102-api-types.md b/docs/tutorial/102-api-types.md index 94ca8fe7..835c28e5 100644 --- a/docs/tutorial/102-api-types.md +++ b/docs/tutorial/102-api-types.md @@ -54,6 +54,8 @@ a table of the ones we support out of the box: |-----|-------| | `double` | `QuantityD` | | `float` | `QuantityF` | +| `int` | `QuantityI` | +| `unsigned int` | `QuantityU` | | `int32_t` | `QuantityI32` | | `uint32_t` | `QuantityU32` | | `int64_t` | `QuantityI64` |