OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #ifndef WEBRTC_BASE_TIMEDELTA_H_ |
| 12 #define WEBRTC_BASE_TIMEDELTA_H_ |
| 13 |
| 14 #include "webrtc/base/basictypes.h" |
| 15 #include "webrtc/base/timeutils.h" |
| 16 |
| 17 // Convenience class to convert between different units of relative time. |
| 18 // Stores time to precision of nanoseconds, as int64_t internally. |
| 19 // Doesn't check for overflow/underflow. |
| 20 // |
| 21 // Based on TimeDelta in: |
| 22 // https://code.google.com/p/chromium/codesearch#chromium/src/base/time/time.h |
| 23 namespace rtc { |
| 24 |
| 25 class TimeDelta { |
| 26 public: |
| 27 TimeDelta() : delta_(0) {} |
| 28 |
| 29 // Converts units of time to TimeDeltas. |
| 30 static constexpr TimeDelta FromSeconds(int64_t secs) { |
| 31 return TimeDelta(secs * kNumNanosecsPerSec); |
| 32 } |
| 33 static constexpr TimeDelta FromMilliseconds(int64_t ms) { |
| 34 return TimeDelta(ms * kNumNanosecsPerMillisec); |
| 35 } |
| 36 static constexpr TimeDelta FromMicroseconds(int64_t us) { |
| 37 return TimeDelta(us * kNumNanosecsPerMicrosec); |
| 38 } |
| 39 static constexpr TimeDelta FromNanoseconds(int64_t ns) { |
| 40 return TimeDelta(ns); |
| 41 } |
| 42 |
| 43 // Returns true if the time delta is zero. |
| 44 bool is_zero() const { return delta_ == 0; } |
| 45 |
| 46 // Converts TimeDelta to units of time. |
| 47 int64_t ToSeconds() const { return delta_ / kNumNanosecsPerSec; } |
| 48 int64_t ToMilliseconds() const { return delta_ / kNumNanosecsPerMillisec; } |
| 49 int64_t ToMicroseconds() const { return delta_ / kNumNanosecsPerMicrosec; } |
| 50 int64_t ToNanoseconds() const { return delta_; } |
| 51 |
| 52 TimeDelta& operator=(TimeDelta other) { |
| 53 delta_ = other.delta_; |
| 54 return *this; |
| 55 } |
| 56 |
| 57 // Computations with other deltas. |
| 58 TimeDelta operator+(TimeDelta other) const { |
| 59 return TimeDelta(delta_ + other.delta_); |
| 60 } |
| 61 TimeDelta operator-(TimeDelta other) const { |
| 62 return TimeDelta(delta_ + other.delta_); |
| 63 } |
| 64 |
| 65 TimeDelta& operator+=(TimeDelta other) { return *this = (*this + other); } |
| 66 TimeDelta& operator-=(TimeDelta other) { return *this = (*this - other); } |
| 67 TimeDelta operator-() const { return TimeDelta(-delta_); } |
| 68 |
| 69 // Computations with numeric types. |
| 70 template <typename T> |
| 71 TimeDelta operator*(T a) const { |
| 72 return TimeDelta(delta_ * a); |
| 73 } |
| 74 template <typename T> |
| 75 TimeDelta operator/(T a) const { |
| 76 return TimeDelta(delta_ / a); |
| 77 } |
| 78 template <typename T> |
| 79 TimeDelta& operator*=(T a) { |
| 80 return *this = (*this * a); |
| 81 } |
| 82 template <typename T> |
| 83 TimeDelta& operator/=(T a) { |
| 84 return *this = (*this / a); |
| 85 } |
| 86 |
| 87 TimeDelta operator%(TimeDelta a) const { |
| 88 return TimeDelta(delta_ % a.delta_); |
| 89 } |
| 90 |
| 91 // Comparison operators. |
| 92 constexpr bool operator==(TimeDelta other) const { |
| 93 return delta_ == other.delta_; |
| 94 } |
| 95 constexpr bool operator!=(TimeDelta other) const { |
| 96 return delta_ != other.delta_; |
| 97 } |
| 98 constexpr bool operator<(TimeDelta other) const { |
| 99 return delta_ < other.delta_; |
| 100 } |
| 101 constexpr bool operator<=(TimeDelta other) const { |
| 102 return delta_ <= other.delta_; |
| 103 } |
| 104 constexpr bool operator>(TimeDelta other) const { |
| 105 return delta_ > other.delta_; |
| 106 } |
| 107 constexpr bool operator>=(TimeDelta other) const { |
| 108 return delta_ >= other.delta_; |
| 109 } |
| 110 |
| 111 private: |
| 112 // Constructs a delta given the duration in nanoseconds. This is private |
| 113 // to avoid confusion by callers with an integer constructor. Use |
| 114 // FromSeconds, FromMilliseconds, etc. instead. |
| 115 constexpr explicit TimeDelta(int64_t delta_ns) : delta_(delta_ns) {} |
| 116 |
| 117 // Delta in nanoseconds. |
| 118 int64_t delta_; |
| 119 }; |
| 120 |
| 121 template <typename T> |
| 122 inline TimeDelta operator*(T a, TimeDelta td) { |
| 123 return td * a; |
| 124 } |
| 125 |
| 126 } // namespace rtc |
| 127 |
| 128 #endif // WEBRTC_BASE_TIMEDELTA_H_ |
OLD | NEW |