aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorJordan Bayles <jophba@chromium.org>2020-06-02 15:51:31 -0700
committerCommit Bot <commit-bot@chromium.org>2020-06-03 14:49:40 +0000
commitc9201dd611b0aa8d66fe7ef2749c7ce0c157f0a1 (patch)
tree35865dd50de7765c9c82abb42c74a7d70a4b8178 /util
parent724a60fb10c7042a3e00eb4f1cf25b41b88b0c8b (diff)
downloadopenscreen-c9201dd611b0aa8d66fe7ef2749c7ce0c157f0a1.tar.gz
Move std::chrono using statements to util/
This patch moves openscreen::<time type> using statements to be part of a new util/ header, chrono_helpers.h. Some helpful convenience functions are provided for doing conversions, and a new conversion method, to_duration, is added to the Clock implementation. Change-Id: I5a153c4da5266bceea97de0cad00a71a739f71ca Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/2222684 Reviewed-by: mark a. foltz <mfoltz@chromium.org> Reviewed-by: Ryan Keane <rwkeane@google.com> Commit-Queue: Jordan Bayles <jophba@chromium.org>
Diffstat (limited to 'util')
-rw-r--r--util/BUILD.gn1
-rw-r--r--util/alarm_unittest.cc14
-rw-r--r--util/chrono_helpers.h50
3 files changed, 59 insertions, 6 deletions
diff --git a/util/BUILD.gn b/util/BUILD.gn
index b2100346..52f18bd8 100644
--- a/util/BUILD.gn
+++ b/util/BUILD.gn
@@ -23,6 +23,7 @@ source_set("util") {
"alarm.h",
"big_endian.cc",
"big_endian.h",
+ "chrono_helpers.h",
"crypto/certificate_utils.cc",
"crypto/certificate_utils.h",
"crypto/digest_sign.cc",
diff --git a/util/alarm_unittest.cc b/util/alarm_unittest.cc
index 5fad74bf..094afc9c 100644
--- a/util/alarm_unittest.cc
+++ b/util/alarm_unittest.cc
@@ -5,10 +5,12 @@
#include "util/alarm.h"
#include <algorithm>
+#include <chrono>
#include "gtest/gtest.h"
#include "platform/test/fake_clock.h"
#include "platform/test/fake_task_runner.h"
+#include "util/chrono_helpers.h"
namespace openscreen {
namespace {
@@ -26,7 +28,7 @@ class AlarmTest : public testing::Test {
};
TEST_F(AlarmTest, RunsTaskAsClockAdvances) {
- constexpr Clock::duration kDelay = std::chrono::milliseconds(20);
+ constexpr Clock::duration kDelay = milliseconds(20);
const Clock::time_point alarm_time = FakeClock::now() + kDelay;
Clock::time_point actual_run_time{};
@@ -61,12 +63,12 @@ TEST_F(AlarmTest, RunsTaskImmediately) {
ASSERT_EQ(expected_run_time, actual_run_time);
// Confirm the lambda is only run once.
- clock()->Advance(std::chrono::seconds(2));
+ clock()->Advance(seconds(2));
ASSERT_EQ(expected_run_time, actual_run_time);
}
TEST_F(AlarmTest, CancelsTaskWhenGoingOutOfScope) {
- constexpr Clock::duration kDelay = std::chrono::milliseconds(20);
+ constexpr Clock::duration kDelay = milliseconds(20);
constexpr Clock::time_point kNever{};
Clock::time_point actual_run_time{};
@@ -85,7 +87,7 @@ TEST_F(AlarmTest, CancelsTaskWhenGoingOutOfScope) {
}
TEST_F(AlarmTest, Cancels) {
- constexpr Clock::duration kDelay = std::chrono::milliseconds(20);
+ constexpr Clock::duration kDelay = milliseconds(20);
const Clock::time_point alarm_time = FakeClock::now() + kDelay;
Clock::time_point actual_run_time{};
@@ -104,8 +106,8 @@ TEST_F(AlarmTest, Cancels) {
}
TEST_F(AlarmTest, CancelsAndRearms) {
- constexpr Clock::duration kShorterDelay = std::chrono::milliseconds(10);
- constexpr Clock::duration kLongerDelay = std::chrono::milliseconds(100);
+ constexpr Clock::duration kShorterDelay = milliseconds(10);
+ constexpr Clock::duration kLongerDelay = milliseconds(100);
// Run the test twice: Once when scheduling first with a long delay, then a
// shorter delay; and once when scheduling first with a short delay, then a
diff --git a/util/chrono_helpers.h b/util/chrono_helpers.h
new file mode 100644
index 00000000..a3711bbe
--- /dev/null
+++ b/util/chrono_helpers.h
@@ -0,0 +1,50 @@
+// Copyright 2020 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef UTIL_CHRONO_HELPERS_H_
+#define UTIL_CHRONO_HELPERS_H_
+
+#include <chrono>
+
+// This file is a collection of helpful utilities and using statement for
+// working with std::chrono. In practice we previously defined these frequently,
+// this header allows for a single set of convenience statements.
+namespace openscreen {
+
+using hours = std::chrono::hours;
+using microseconds = std::chrono::microseconds;
+using milliseconds = std::chrono::milliseconds;
+using nanoseconds = std::chrono::nanoseconds;
+using seconds = std::chrono::seconds;
+
+// Casting statements. Note that duration_cast is not a type, it's a function,
+// so its behavior is different than the using statements above.
+template <typename D>
+static constexpr hours to_hours(D d) {
+ return std::chrono::duration_cast<hours>(d);
+}
+
+template <typename D>
+static constexpr microseconds to_microseconds(D d) {
+ return std::chrono::duration_cast<microseconds>(d);
+}
+
+template <typename D>
+static constexpr milliseconds to_milliseconds(D d) {
+ return std::chrono::duration_cast<milliseconds>(d);
+}
+
+template <typename D>
+static constexpr nanoseconds to_nanoseconds(D d) {
+ return std::chrono::duration_cast<nanoseconds>(d);
+}
+
+template <typename D>
+static constexpr seconds to_seconds(D d) {
+ return std::chrono::duration_cast<seconds>(d);
+}
+
+} // namespace openscreen
+
+#endif // UTIL_CHRONO_HELPERS_H_