-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move chrono interop utils to new library (#201)
This structure makes more sense and makes the library easier to understand.
- Loading branch information
Showing
5 changed files
with
118 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2023 Aurora Operations, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <chrono> | ||
#include <cstdint> | ||
|
||
#include "au/quantity.hh" | ||
#include "au/units/seconds.hh" | ||
|
||
namespace au { | ||
|
||
// Define 1:1 mapping between duration types of chrono library and our library. | ||
template <typename RepT, typename Period> | ||
struct CorrespondingQuantity<std::chrono::duration<RepT, Period>> { | ||
using Unit = decltype(Seconds{} * (mag<Period::num>() / mag<Period::den>())); | ||
using Rep = RepT; | ||
|
||
using ChronoDuration = std::chrono::duration<Rep, Period>; | ||
|
||
static constexpr Rep extract_value(ChronoDuration d) { return d.count(); } | ||
static constexpr ChronoDuration construct_from_value(Rep x) { return ChronoDuration{x}; } | ||
}; | ||
|
||
// 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) { | ||
constexpr auto ratio = unit_ratio(U{}, seconds); | ||
static_assert(is_rational(ratio), "Cannot convert to chrono::duration with non-rational ratio"); | ||
return std::chrono::duration<R, | ||
std::ratio<get_value<std::intmax_t>(numerator(ratio)), | ||
get_value<std::intmax_t>(denominator(ratio))>>{dt}; | ||
} | ||
|
||
} // namespace au |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2023 Aurora Operations, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "au/chrono_interop.hh" | ||
|
||
#include "au/prefix.hh" | ||
#include "au/testing.hh" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
namespace au { | ||
|
||
TEST(DurationQuantity, InterconvertsWithExactlyEquivalentChronoDuration) { | ||
constexpr QuantityD<Seconds> from_chrono = std::chrono::duration<double>{1.23}; | ||
EXPECT_THAT(from_chrono, SameTypeAndValue(seconds(1.23))); | ||
|
||
constexpr auto val = std::chrono::nanoseconds::rep{456}; | ||
constexpr std::chrono::nanoseconds from_au = nano(seconds)(val); | ||
EXPECT_THAT(from_au.count(), SameTypeAndValue(val)); | ||
} | ||
|
||
TEST(DurationQuantity, InterconvertsWithIndirectlyEquivalentChronoDuration) { | ||
constexpr QuantityD<Seconds> from_chrono = as_quantity(1234ms); | ||
EXPECT_THAT(from_chrono, SameTypeAndValue(seconds(1.234))); | ||
} | ||
|
||
TEST(AsChronoDuration, ProducesExpectedResults) { | ||
constexpr auto original = milli(seconds)(12.3f); | ||
constexpr auto result = as_chrono_duration(original); | ||
EXPECT_THAT(result.count(), SameTypeAndValue(12.3f)); | ||
EXPECT_THAT(as_quantity(result), QuantityEquivalent(original)); | ||
} | ||
|
||
} // namespace au |