Commit bad4e03c authored by jyasskin's avatar jyasskin Committed by Commit bot

Allow constexpr variables of TimeDelta type.

This allows TimeDelta variables to be global per https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables since they don't have to run a dynamic initializer anymore.

Review-Url: https://codereview.chromium.org/1976703005
Cr-Commit-Position: refs/heads/master@{#394527}
parent d6dce260
......@@ -106,14 +106,14 @@ class BASE_EXPORT TimeDelta {
}
// 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 secs);
static TimeDelta FromMilliseconds(int64_t ms);
static TimeDelta FromSecondsD(double secs);
static TimeDelta FromMillisecondsD(double ms);
static TimeDelta FromMicroseconds(int64_t us);
static constexpr TimeDelta FromDays(int days);
static constexpr TimeDelta FromHours(int hours);
static constexpr TimeDelta FromMinutes(int minutes);
static constexpr TimeDelta FromSeconds(int64_t secs);
static constexpr TimeDelta FromMilliseconds(int64_t ms);
static constexpr TimeDelta FromSecondsD(double secs);
static constexpr TimeDelta FromMillisecondsD(double ms);
static constexpr TimeDelta FromMicroseconds(int64_t us);
#if defined(OS_WIN)
static TimeDelta FromQPCValue(LONGLONG qpc_value);
#endif
......@@ -222,22 +222,22 @@ class BASE_EXPORT TimeDelta {
}
// Comparison operators.
bool operator==(TimeDelta other) const {
constexpr bool operator==(TimeDelta other) const {
return delta_ == other.delta_;
}
bool operator!=(TimeDelta other) const {
constexpr bool operator!=(TimeDelta other) const {
return delta_ != other.delta_;
}
bool operator<(TimeDelta other) const {
constexpr bool operator<(TimeDelta other) const {
return delta_ < other.delta_;
}
bool operator<=(TimeDelta other) const {
constexpr bool operator<=(TimeDelta other) const {
return delta_ <= other.delta_;
}
bool operator>(TimeDelta other) const {
constexpr bool operator>(TimeDelta other) const {
return delta_ > other.delta_;
}
bool operator>=(TimeDelta other) const {
constexpr bool operator>=(TimeDelta other) const {
return delta_ >= other.delta_;
}
......@@ -248,10 +248,14 @@ class BASE_EXPORT TimeDelta {
// Constructs a delta given the duration in microseconds. This is private
// to avoid confusion by callers with an integer constructor. Use
// FromSeconds, FromMilliseconds, etc. instead.
explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {}
constexpr explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {}
// Private method to build a delta from a double.
static TimeDelta FromDouble(double value);
static constexpr TimeDelta FromDouble(double value);
// Private method to build a delta from the product of a user-provided value
// and a known-positive value.
static constexpr TimeDelta FromProduct(int64_t value, int64_t positive_value);
// Delta in microseconds.
int64_t delta_;
......@@ -578,60 +582,71 @@ class BASE_EXPORT Time : public time_internal::TimeBase<Time> {
// Inline the TimeDelta factory methods, for fast TimeDelta construction.
// static
inline TimeDelta TimeDelta::FromDays(int days) {
if (days == std::numeric_limits<int>::max())
return Max();
return TimeDelta(days * Time::kMicrosecondsPerDay);
constexpr inline TimeDelta TimeDelta::FromDays(int days) {
return days == std::numeric_limits<int>::max()
? Max()
: TimeDelta(days * Time::kMicrosecondsPerDay);
}
// static
inline TimeDelta TimeDelta::FromHours(int hours) {
if (hours == std::numeric_limits<int>::max())
return Max();
return TimeDelta(hours * Time::kMicrosecondsPerHour);
constexpr inline TimeDelta TimeDelta::FromHours(int hours) {
return hours == std::numeric_limits<int>::max()
? Max()
: TimeDelta(hours * Time::kMicrosecondsPerHour);
}
// static
inline TimeDelta TimeDelta::FromMinutes(int minutes) {
if (minutes == std::numeric_limits<int>::max())
return Max();
return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
constexpr inline TimeDelta TimeDelta::FromMinutes(int minutes) {
return minutes == std::numeric_limits<int>::max()
? Max()
: TimeDelta(minutes * Time::kMicrosecondsPerMinute);
}
// static
inline TimeDelta TimeDelta::FromSeconds(int64_t secs) {
return TimeDelta(secs) * Time::kMicrosecondsPerSecond;
constexpr inline TimeDelta TimeDelta::FromSeconds(int64_t secs) {
return FromProduct(secs, Time::kMicrosecondsPerSecond);
}
// static
inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) {
return TimeDelta(ms) * Time::kMicrosecondsPerMillisecond;
constexpr inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) {
return FromProduct(ms, Time::kMicrosecondsPerMillisecond);
}
// static
inline TimeDelta TimeDelta::FromSecondsD(double secs) {
constexpr inline TimeDelta TimeDelta::FromSecondsD(double secs) {
return FromDouble(secs * Time::kMicrosecondsPerSecond);
}
// static
inline TimeDelta TimeDelta::FromMillisecondsD(double ms) {
constexpr inline TimeDelta TimeDelta::FromMillisecondsD(double ms) {
return FromDouble(ms * Time::kMicrosecondsPerMillisecond);
}
// static
inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) {
constexpr inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) {
return TimeDelta(us);
}
// static
inline TimeDelta TimeDelta::FromDouble(double value) {
double max_magnitude = std::numeric_limits<int64_t>::max();
TimeDelta delta = TimeDelta(static_cast<int64_t>(value));
if (value > max_magnitude)
delta = Max();
else if (value < -max_magnitude)
delta = -Max();
return delta;
constexpr inline TimeDelta TimeDelta::FromDouble(double value) {
// TODO(crbug.com/612601): Use saturated_cast<int64_t>(value) once we sort out
// the Min() behavior.
return value > std::numeric_limits<int64_t>::max()
? Max()
: value < -std::numeric_limits<int64_t>::max()
? -Max()
: TimeDelta(static_cast<int64_t>(value));
}
// static
constexpr inline TimeDelta TimeDelta::FromProduct(int64_t value,
int64_t positive_value) {
return (DCHECK(positive_value > 0),
value > std::numeric_limits<int64_t>::max() / positive_value
? Max()
: value < -std::numeric_limits<int64_t>::max() / positive_value
? -Max()
: TimeDelta(value * positive_value));
}
// For logging use only.
......
......@@ -723,16 +723,21 @@ TEST(TimeTicks, SnappedToNextTickOverflow) {
}
TEST(TimeDelta, FromAndIn) {
EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
EXPECT_TRUE(TimeDelta::FromMilliseconds(2) ==
TimeDelta::FromMicroseconds(2000));
EXPECT_TRUE(TimeDelta::FromSecondsD(2.3) ==
TimeDelta::FromMilliseconds(2300));
EXPECT_TRUE(TimeDelta::FromMillisecondsD(2.5) ==
TimeDelta::FromMicroseconds(2500));
// static_assert also checks that the contained expression is a constant
// expression, meaning all its components are suitable for initializing global
// variables.
static_assert(TimeDelta::FromDays(2) == TimeDelta::FromHours(48), "");
static_assert(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180), "");
static_assert(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120), "");
static_assert(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000),
"");
static_assert(
TimeDelta::FromMilliseconds(2) == TimeDelta::FromMicroseconds(2000), "");
static_assert(
TimeDelta::FromSecondsD(2.3) == TimeDelta::FromMilliseconds(2300), "");
static_assert(
TimeDelta::FromMillisecondsD(2.5) == TimeDelta::FromMicroseconds(2500),
"");
EXPECT_EQ(13, TimeDelta::FromDays(13).InDays());
EXPECT_EQ(13, TimeDelta::FromHours(13).InHours());
EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes());
......
......@@ -27,9 +27,6 @@ const char kHistograms[] = "";
const char kImageData[] = "";
const char kFileData[] = "";
const base::TimeDelta kRetryDelayForTest =
base::TimeDelta::FromMilliseconds(100);
using content::BrowserThread;
class MockUploader : public feedback::FeedbackUploader, public KeyedService {
......
......@@ -42,10 +42,6 @@ enum ClearAllStatus {
CLEAR_ALL_STATUS_COUNT
};
// Threshold for how old offline copy of a page should be before we offer to
// delete it to free up space.
const base::TimeDelta kPageCleanUpThreshold = base::TimeDelta::FromDays(30);
// The maximum histogram size for the metrics that measure time between views of
// a given page.
const base::TimeDelta kMaxOpenedPageHistogramBucket =
......
......@@ -33,9 +33,6 @@ const char kTestData4[] = "supercalifragilisticexpialidocious";
const int kTestDataLength1 = arraysize(kTestData1) - 1;
const int kTestDataLength2 = arraysize(kTestData2) - 1;
const int kTestDataLength4 = arraysize(kTestData4) - 1;
const int kElapsedTimeSeconds = 5;
const base::TimeDelta kElapsedTimeDelta = base::TimeDelta::FromSeconds(
kElapsedTimeSeconds);
// SHA-256 hash of kTestData1 (excluding terminating NUL).
const uint8_t kHashOfTestData1[] = {
......
......@@ -30,7 +30,6 @@ namespace {
const char kGenericName[] = "name";
const Config kFrozenConfig = {0, TimeDelta::FromDays(0)};
const Config k2PerMinute = {2, TimeDelta::FromMinutes(1)};
const Config k20PerHour = {20, TimeDelta::FromHours(1)};
const TimeTicks kStartTime = TimeTicks();
const TimeTicks k1MinuteAfterStart = kStartTime + TimeDelta::FromMinutes(1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment