base: Make constructors for TimeDelta constexpr

Change-Id: I87d17d087f47eb60efbd2ddaf04d5d574c73164c
Reviewed-on: https://chromium-review.googlesource.com/c/1299245
Reviewed-by: Hannes Payer <hpayer@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57008}
This commit is contained in:
Michael Lippautz 2018-10-25 21:26:22 +02:00 committed by Commit Bot
parent ef503f0757
commit 5d41105bf3
2 changed files with 40 additions and 55 deletions

View File

@ -132,36 +132,6 @@ V8_INLINE uint64_t QPCNowRaw() {
namespace v8 {
namespace base {
TimeDelta TimeDelta::FromDays(int days) {
return TimeDelta(days * Time::kMicrosecondsPerDay);
}
TimeDelta TimeDelta::FromHours(int hours) {
return TimeDelta(hours * Time::kMicrosecondsPerHour);
}
TimeDelta TimeDelta::FromMinutes(int minutes) {
return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
}
TimeDelta TimeDelta::FromSeconds(int64_t seconds) {
return TimeDelta(seconds * Time::kMicrosecondsPerSecond);
}
TimeDelta TimeDelta::FromMilliseconds(int64_t milliseconds) {
return TimeDelta(milliseconds * Time::kMicrosecondsPerMillisecond);
}
TimeDelta TimeDelta::FromNanoseconds(int64_t nanoseconds) {
return TimeDelta(nanoseconds / Time::kNanosecondsPerMicrosecond);
}
int TimeDelta::InDays() const {
if (IsMax()) {
// Preserve max to prevent overflow.

View File

@ -39,6 +39,25 @@ template<class TimeClass>
class TimeBase;
}
class TimeConstants {
public:
static constexpr int64_t kHoursPerDay = 24;
static constexpr int64_t kMillisecondsPerSecond = 1000;
static constexpr int64_t kMillisecondsPerDay =
kMillisecondsPerSecond * 60 * 60 * kHoursPerDay;
static constexpr int64_t kMicrosecondsPerMillisecond = 1000;
static constexpr int64_t kMicrosecondsPerSecond =
kMicrosecondsPerMillisecond * kMillisecondsPerSecond;
static constexpr int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
static constexpr int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
static constexpr int64_t kMicrosecondsPerDay =
kMicrosecondsPerHour * kHoursPerDay;
static constexpr int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
static constexpr int64_t kNanosecondsPerMicrosecond = 1000;
static constexpr int64_t kNanosecondsPerSecond =
kNanosecondsPerMicrosecond * kMicrosecondsPerSecond;
};
// -----------------------------------------------------------------------------
// TimeDelta
//
@ -50,15 +69,27 @@ class V8_BASE_EXPORT TimeDelta final {
constexpr TimeDelta() : delta_(0) {}
// Converts units of time to TimeDeltas.
static TimeDelta FromDays(int days);
static TimeDelta FromHours(int hours);
static TimeDelta FromMinutes(int minutes);
static TimeDelta FromSeconds(int64_t seconds);
static TimeDelta FromMilliseconds(int64_t milliseconds);
static TimeDelta FromMicroseconds(int64_t microseconds) {
static constexpr TimeDelta FromDays(int days) {
return TimeDelta(days * TimeConstants::kMicrosecondsPerDay);
}
static constexpr TimeDelta FromHours(int hours) {
return TimeDelta(hours * TimeConstants::kMicrosecondsPerHour);
}
static constexpr TimeDelta FromMinutes(int minutes) {
return TimeDelta(minutes * TimeConstants::kMicrosecondsPerMinute);
}
static constexpr TimeDelta FromSeconds(int64_t seconds) {
return TimeDelta(seconds * TimeConstants::kMicrosecondsPerSecond);
}
static constexpr TimeDelta FromMilliseconds(int64_t milliseconds) {
return TimeDelta(milliseconds * TimeConstants::kMicrosecondsPerMillisecond);
}
static constexpr TimeDelta FromMicroseconds(int64_t microseconds) {
return TimeDelta(microseconds);
}
static TimeDelta FromNanoseconds(int64_t nanoseconds);
static constexpr TimeDelta FromNanoseconds(int64_t nanoseconds) {
return TimeDelta(nanoseconds / TimeConstants::kNanosecondsPerMicrosecond);
}
// Returns the maximum time delta, which should be greater than any reasonable
// time delta we might compare it to. Adding or subtracting the maximum time
@ -201,25 +232,9 @@ namespace time_internal {
// classes. Each subclass provides for strong type-checking to ensure
// semantically meaningful comparison/math of time values from the same clock
// source or timeline.
template<class TimeClass>
class TimeBase {
template <class TimeClass>
class TimeBase : public TimeConstants {
public:
static constexpr int64_t kHoursPerDay = 24;
static constexpr int64_t kMillisecondsPerSecond = 1000;
static constexpr int64_t kMillisecondsPerDay =
kMillisecondsPerSecond * 60 * 60 * kHoursPerDay;
static constexpr int64_t kMicrosecondsPerMillisecond = 1000;
static constexpr int64_t kMicrosecondsPerSecond =
kMicrosecondsPerMillisecond * kMillisecondsPerSecond;
static constexpr int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
static constexpr int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
static constexpr int64_t kMicrosecondsPerDay =
kMicrosecondsPerHour * kHoursPerDay;
static constexpr int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
static constexpr int64_t kNanosecondsPerMicrosecond = 1000;
static constexpr int64_t kNanosecondsPerSecond =
kNanosecondsPerMicrosecond * kMicrosecondsPerSecond;
#if V8_OS_WIN
// To avoid overflow in QPC to Microseconds calculations, since we multiply
// by kMicrosecondsPerSecond, then the QPC value should not exceed