Skip to content

Commit

Permalink
Tweak corresponding quantity for main chrono types (#276)
Browse files Browse the repository at this point in the history
Currently, we have a single generic correspondence for all chrono
durations.  This is perfectly unit safe, but it means we have no labels
for any duration type, including common types like `std::chrono::hours`.
This means that `as_quantity(std::chrono::hours{3})` would print as
`"3 [UNLABELED UNIT]"` instead of `"3 h"`.

This PR defines a special correspondence for each commonly used chrono
duration type, which fixes the above behaviour.

As part of doing this, we found a missing dependency on `:quantity`, so
we added it in the BUILD file.

Fixes #216.

---------

Co-authored-by: Michael Hordijk <[email protected]>
  • Loading branch information
chiphogg and hoffbrinkle authored Aug 5, 2024
1 parent 40c8e0c commit f77cc5e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions au/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ cc_library(
includes = ["code"],
deps = [
":units",
":prefix",
":quantity",
],
)

Expand Down
1 change: 1 addition & 0 deletions au/code/au/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ header_only_library(
HEADERS
chrono_interop.hh
DEPS
prefix
quantity
units
GTEST_SRCS
Expand Down
37 changes: 37 additions & 0 deletions au/code/au/chrono_interop.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
#include <chrono>
#include <cstdint>

#include "au/prefix.hh"
#include "au/quantity.hh"
#include "au/units/hours.hh"
#include "au/units/minutes.hh"
#include "au/units/seconds.hh"

namespace au {
Expand All @@ -34,6 +37,40 @@ struct CorrespondingQuantity<std::chrono::duration<RepT, Period>> {
static constexpr ChronoDuration construct_from_value(Rep x) { return ChronoDuration{x}; }
};

// Define special mappings for widely used chrono types.
template <typename ChronoType, typename AuUnit>
struct SpecialCorrespondingQuantity {
using Unit = AuUnit;
using Rep = decltype(ChronoType{}.count());

static constexpr Rep extract_value(ChronoType d) { return d.count(); }
static constexpr ChronoType construct_from_value(Rep x) { return ChronoType{x}; }
};

template <>
struct CorrespondingQuantity<std::chrono::nanoseconds>
: SpecialCorrespondingQuantity<std::chrono::nanoseconds, Nano<Seconds>> {};

template <>
struct CorrespondingQuantity<std::chrono::microseconds>
: SpecialCorrespondingQuantity<std::chrono::microseconds, Micro<Seconds>> {};

template <>
struct CorrespondingQuantity<std::chrono::milliseconds>
: SpecialCorrespondingQuantity<std::chrono::milliseconds, Milli<Seconds>> {};

template <>
struct CorrespondingQuantity<std::chrono::seconds>
: SpecialCorrespondingQuantity<std::chrono::seconds, Seconds> {};

template <>
struct CorrespondingQuantity<std::chrono::minutes>
: SpecialCorrespondingQuantity<std::chrono::minutes, Minutes> {};

template <>
struct CorrespondingQuantity<std::chrono::hours>
: SpecialCorrespondingQuantity<std::chrono::hours, Hours> {};

// Convert any Au duration quantity to an equivalent `std::chrono::duration`.
template <typename U, typename R>
constexpr auto as_chrono_duration(Quantity<U, R> dt) {
Expand Down
43 changes: 43 additions & 0 deletions au/code/au/chrono_interop_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@

#include "au/chrono_interop.hh"

#include <sstream>

#include "au/prefix.hh"
#include "au/testing.hh"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using ::testing::StrEq;

using namespace std::chrono_literals;

namespace au {
namespace {
template <typename T>
std::string stream_to_string(const T &t) {
std::ostringstream oss;
oss << t;
return oss.str();
}
} // namespace

TEST(DurationQuantity, InterconvertsWithExactlyEquivalentChronoDuration) {
constexpr QuantityD<Seconds> from_chrono = std::chrono::duration<double>{1.23};
Expand All @@ -36,6 +49,36 @@ TEST(DurationQuantity, InterconvertsWithIndirectlyEquivalentChronoDuration) {
EXPECT_THAT(from_chrono, SameTypeAndValue(seconds(1.234)));
}

TEST(DurationQuantity, EquivalentOfChronoNanosecondsHasNsLabel) {
constexpr auto from_chrono_ns = as_quantity(std::chrono::nanoseconds{123});
EXPECT_THAT(stream_to_string(from_chrono_ns), StrEq("123 ns"));
}

TEST(DurationQuantity, EquivalentOfChronoMicrosecondsHasUsLabel) {
constexpr auto from_chrono_us = as_quantity(std::chrono::microseconds{123});
EXPECT_THAT(stream_to_string(from_chrono_us), StrEq("123 us"));
}

TEST(DurationQuantity, EquivalentOfChronoMillisecondsHasMsLabel) {
constexpr auto from_chrono_ms = as_quantity(std::chrono::milliseconds{123});
EXPECT_THAT(stream_to_string(from_chrono_ms), StrEq("123 ms"));
}

TEST(DurationQuantity, EquivalentOfChronoSecondsHasSLabel) {
constexpr auto from_chrono_s = as_quantity(std::chrono::seconds{123});
EXPECT_THAT(stream_to_string(from_chrono_s), StrEq("123 s"));
}

TEST(DurationQuantity, EquivalentOfChronoMinutesHasMinLabel) {
constexpr auto from_chrono_min = as_quantity(std::chrono::minutes{123});
EXPECT_THAT(stream_to_string(from_chrono_min), StrEq("123 min"));
}

TEST(DurationQuantity, EquivalentOfChronoHoursHasHLabel) {
constexpr auto from_chrono_h = as_quantity(std::chrono::hours{123});
EXPECT_THAT(stream_to_string(from_chrono_h), StrEq("123 h"));
}

TEST(AsChronoDuration, ProducesExpectedResults) {
constexpr auto original = milli(seconds)(12.3f);
constexpr auto result = as_chrono_duration(original);
Expand Down

0 comments on commit f77cc5e

Please sign in to comment.