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