Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(430)

Side by Side Diff: base/time/time.h

Issue 1976703005: Allow constexpr variables of TimeDelta type. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: Generalize FromProduct a bit. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/time/time_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Time represents an absolute point in coordinated universal time (UTC), 5 // Time represents an absolute point in coordinated universal time (UTC),
6 // internally represented as microseconds (s/1,000,000) since the Windows epoch 6 // internally represented as microseconds (s/1,000,000) since the Windows epoch
7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are 7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are
8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump 8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump
9 // around as the operating system makes adjustments to synchronize (e.g., with 9 // around as the operating system makes adjustments to synchronize (e.g., with
10 // NTP servers). Thus, client code that uses the Time class must account for 10 // NTP servers). Thus, client code that uses the Time class must account for
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 } // namespace time_internal 99 } // namespace time_internal
100 100
101 // TimeDelta ------------------------------------------------------------------ 101 // TimeDelta ------------------------------------------------------------------
102 102
103 class BASE_EXPORT TimeDelta { 103 class BASE_EXPORT TimeDelta {
104 public: 104 public:
105 TimeDelta() : delta_(0) { 105 TimeDelta() : delta_(0) {
106 } 106 }
107 107
108 // Converts units of time to TimeDeltas. 108 // Converts units of time to TimeDeltas.
109 static TimeDelta FromDays(int days); 109 static constexpr TimeDelta FromDays(int days);
110 static TimeDelta FromHours(int hours); 110 static constexpr TimeDelta FromHours(int hours);
111 static TimeDelta FromMinutes(int minutes); 111 static constexpr TimeDelta FromMinutes(int minutes);
112 static TimeDelta FromSeconds(int64_t secs); 112 static constexpr TimeDelta FromSeconds(int64_t secs);
113 static TimeDelta FromMilliseconds(int64_t ms); 113 static constexpr TimeDelta FromMilliseconds(int64_t ms);
114 static TimeDelta FromSecondsD(double secs); 114 static constexpr TimeDelta FromSecondsD(double secs);
115 static TimeDelta FromMillisecondsD(double ms); 115 static constexpr TimeDelta FromMillisecondsD(double ms);
116 static TimeDelta FromMicroseconds(int64_t us); 116 static constexpr TimeDelta FromMicroseconds(int64_t us);
117 #if defined(OS_WIN) 117 #if defined(OS_WIN)
118 static TimeDelta FromQPCValue(LONGLONG qpc_value); 118 static TimeDelta FromQPCValue(LONGLONG qpc_value);
119 #endif 119 #endif
120 120
121 // Converts an integer value representing TimeDelta to a class. This is used 121 // Converts an integer value representing TimeDelta to a class. This is used
122 // when deserializing a |TimeDelta| structure, using a value known to be 122 // when deserializing a |TimeDelta| structure, using a value known to be
123 // compatible. It is not provided as a constructor because the integer type 123 // compatible. It is not provided as a constructor because the integer type
124 // may be unclear from the perspective of a caller. 124 // may be unclear from the perspective of a caller.
125 static TimeDelta FromInternalValue(int64_t delta) { return TimeDelta(delta); } 125 static TimeDelta FromInternalValue(int64_t delta) { return TimeDelta(delta); }
126 126
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 TimeDelta& operator/=(T a) { 215 TimeDelta& operator/=(T a) {
216 return *this = (*this / a); 216 return *this = (*this / a);
217 } 217 }
218 218
219 int64_t operator/(TimeDelta a) const { return delta_ / a.delta_; } 219 int64_t operator/(TimeDelta a) const { return delta_ / a.delta_; }
220 TimeDelta operator%(TimeDelta a) const { 220 TimeDelta operator%(TimeDelta a) const {
221 return TimeDelta(delta_ % a.delta_); 221 return TimeDelta(delta_ % a.delta_);
222 } 222 }
223 223
224 // Comparison operators. 224 // Comparison operators.
225 bool operator==(TimeDelta other) const { 225 constexpr bool operator==(TimeDelta other) const {
226 return delta_ == other.delta_; 226 return delta_ == other.delta_;
227 } 227 }
228 bool operator!=(TimeDelta other) const { 228 constexpr bool operator!=(TimeDelta other) const {
229 return delta_ != other.delta_; 229 return delta_ != other.delta_;
230 } 230 }
231 bool operator<(TimeDelta other) const { 231 constexpr bool operator<(TimeDelta other) const {
232 return delta_ < other.delta_; 232 return delta_ < other.delta_;
233 } 233 }
234 bool operator<=(TimeDelta other) const { 234 constexpr bool operator<=(TimeDelta other) const {
235 return delta_ <= other.delta_; 235 return delta_ <= other.delta_;
236 } 236 }
237 bool operator>(TimeDelta other) const { 237 constexpr bool operator>(TimeDelta other) const {
238 return delta_ > other.delta_; 238 return delta_ > other.delta_;
239 } 239 }
240 bool operator>=(TimeDelta other) const { 240 constexpr bool operator>=(TimeDelta other) const {
241 return delta_ >= other.delta_; 241 return delta_ >= other.delta_;
242 } 242 }
243 243
244 private: 244 private:
245 friend int64_t time_internal::SaturatedAdd(TimeDelta delta, int64_t value); 245 friend int64_t time_internal::SaturatedAdd(TimeDelta delta, int64_t value);
246 friend int64_t time_internal::SaturatedSub(TimeDelta delta, int64_t value); 246 friend int64_t time_internal::SaturatedSub(TimeDelta delta, int64_t value);
247 247
248 // Constructs a delta given the duration in microseconds. This is private 248 // Constructs a delta given the duration in microseconds. This is private
249 // to avoid confusion by callers with an integer constructor. Use 249 // to avoid confusion by callers with an integer constructor. Use
250 // FromSeconds, FromMilliseconds, etc. instead. 250 // FromSeconds, FromMilliseconds, etc. instead.
251 explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {} 251 constexpr explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {}
252 252
253 // Private method to build a delta from a double. 253 // Private method to build a delta from a double.
254 static TimeDelta FromDouble(double value); 254 static constexpr TimeDelta FromDouble(double value);
255
256 // Private method to build a delta from the product of a user-provided value
257 // and a known-positive value.
258 static constexpr TimeDelta FromProduct(int64_t value, int64_t positive_value);
255 259
256 // Delta in microseconds. 260 // Delta in microseconds.
257 int64_t delta_; 261 int64_t delta_;
258 }; 262 };
259 263
260 template<typename T> 264 template<typename T>
261 inline TimeDelta operator*(T a, TimeDelta td) { 265 inline TimeDelta operator*(T a, TimeDelta td) {
262 return td * a; 266 return td * a;
263 } 267 }
264 268
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 // (e.g. "UTC" which is not specified in RFC822) is treated as if the 575 // (e.g. "UTC" which is not specified in RFC822) is treated as if the
572 // timezone is not specified. 576 // timezone is not specified.
573 static bool FromStringInternal(const char* time_string, 577 static bool FromStringInternal(const char* time_string,
574 bool is_local, 578 bool is_local,
575 Time* parsed_time); 579 Time* parsed_time);
576 }; 580 };
577 581
578 // Inline the TimeDelta factory methods, for fast TimeDelta construction. 582 // Inline the TimeDelta factory methods, for fast TimeDelta construction.
579 583
580 // static 584 // static
581 inline TimeDelta TimeDelta::FromDays(int days) { 585 constexpr inline TimeDelta TimeDelta::FromDays(int days) {
Peter Kasting 2016/05/18 20:53:28 Doesn't constexpr imply the same rules (e.g. for O
582 if (days == std::numeric_limits<int>::max()) 586 return days == std::numeric_limits<int>::max()
583 return Max(); 587 ? Max()
584 return TimeDelta(days * Time::kMicrosecondsPerDay); 588 : TimeDelta(days * Time::kMicrosecondsPerDay);
585 } 589 }
586 590
587 // static 591 // static
588 inline TimeDelta TimeDelta::FromHours(int hours) { 592 constexpr inline TimeDelta TimeDelta::FromHours(int hours) {
589 if (hours == std::numeric_limits<int>::max()) 593 return hours == std::numeric_limits<int>::max()
590 return Max(); 594 ? Max()
591 return TimeDelta(hours * Time::kMicrosecondsPerHour); 595 : TimeDelta(hours * Time::kMicrosecondsPerHour);
592 } 596 }
593 597
594 // static 598 // static
595 inline TimeDelta TimeDelta::FromMinutes(int minutes) { 599 constexpr inline TimeDelta TimeDelta::FromMinutes(int minutes) {
596 if (minutes == std::numeric_limits<int>::max()) 600 return minutes == std::numeric_limits<int>::max()
597 return Max(); 601 ? Max()
598 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); 602 : TimeDelta(minutes * Time::kMicrosecondsPerMinute);
599 } 603 }
600 604
601 // static 605 // static
602 inline TimeDelta TimeDelta::FromSeconds(int64_t secs) { 606 constexpr inline TimeDelta TimeDelta::FromSeconds(int64_t secs) {
603 return TimeDelta(secs) * Time::kMicrosecondsPerSecond; 607 return FromProduct(secs, Time::kMicrosecondsPerSecond);
604 } 608 }
605 609
606 // static 610 // static
607 inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) { 611 constexpr inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) {
608 return TimeDelta(ms) * Time::kMicrosecondsPerMillisecond; 612 return FromProduct(ms, Time::kMicrosecondsPerMillisecond);
609 } 613 }
610 614
611 // static 615 // static
612 inline TimeDelta TimeDelta::FromSecondsD(double secs) { 616 constexpr inline TimeDelta TimeDelta::FromSecondsD(double secs) {
613 return FromDouble(secs * Time::kMicrosecondsPerSecond); 617 return FromDouble(secs * Time::kMicrosecondsPerSecond);
614 } 618 }
615 619
616 // static 620 // static
617 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { 621 constexpr inline TimeDelta TimeDelta::FromMillisecondsD(double ms) {
618 return FromDouble(ms * Time::kMicrosecondsPerMillisecond); 622 return FromDouble(ms * Time::kMicrosecondsPerMillisecond);
619 } 623 }
620 624
621 // static 625 // static
622 inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) { 626 constexpr inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) {
623 return TimeDelta(us); 627 return TimeDelta(us);
624 } 628 }
625 629
626 // static 630 // static
627 inline TimeDelta TimeDelta::FromDouble(double value) { 631 constexpr inline TimeDelta TimeDelta::FromDouble(double value) {
628 double max_magnitude = std::numeric_limits<int64_t>::max(); 632 // TODO(crbug.com/612601): Use saturated_cast<int64_t>(value) once we sort out
629 TimeDelta delta = TimeDelta(static_cast<int64_t>(value)); 633 // the Min() behavior.
630 if (value > max_magnitude) 634 return value > std::numeric_limits<int64_t>::max()
631 delta = Max(); 635 ? Max()
632 else if (value < -max_magnitude) 636 : value < -std::numeric_limits<int64_t>::max()
633 delta = -Max(); 637 ? -Max()
634 return delta; 638 : TimeDelta(static_cast<int64_t>(value));
639 }
640
641 // static
642 constexpr inline TimeDelta TimeDelta::FromProduct(int64_t value,
643 int64_t positive_value) {
644 return (DCHECK(positive_value > 0),
645 value > std::numeric_limits<int64_t>::max() / positive_value
646 ? Max()
647 : value < -std::numeric_limits<int64_t>::max() / positive_value
648 ? -Max()
649 : TimeDelta(value * positive_value));
635 } 650 }
636 651
637 // For logging use only. 652 // For logging use only.
638 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); 653 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time);
639 654
640 // TimeTicks ------------------------------------------------------------------ 655 // TimeTicks ------------------------------------------------------------------
641 656
642 // Represents monotonically non-decreasing clock time. 657 // Represents monotonically non-decreasing clock time.
643 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { 658 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> {
644 public: 659 public:
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 static void WaitUntilInitializedWin(); 791 static void WaitUntilInitializedWin();
777 #endif 792 #endif
778 }; 793 };
779 794
780 // For logging use only. 795 // For logging use only.
781 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); 796 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks);
782 797
783 } // namespace base 798 } // namespace base
784 799
785 #endif // BASE_TIME_TIME_H_ 800 #endif // BASE_TIME_TIME_H_
OLDNEW
« no previous file with comments | « no previous file | base/time/time_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698