You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2018 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. #ifndef API_UNITS_TIME_DELTA_H_
  11. #define API_UNITS_TIME_DELTA_H_
  12. #ifdef UNIT_TEST
  13. #include <ostream> // no-presubmit-check TODO(webrtc:8982)
  14. #endif // UNIT_TEST
  15. #include <cstdlib>
  16. #include <string>
  17. #include <type_traits>
  18. #include "rtc_base/units/unit_base.h"
  19. namespace webrtc {
  20. // TimeDelta represents the difference between two timestamps. Commonly this can
  21. // be a duration. However since two Timestamps are not guaranteed to have the
  22. // same epoch (they might come from different computers, making exact
  23. // synchronisation infeasible), the duration covered by a TimeDelta can be
  24. // undefined. To simplify usage, it can be constructed and converted to
  25. // different units, specifically seconds (s), milliseconds (ms) and
  26. // microseconds (us).
  27. class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> {
  28. public:
  29. TimeDelta() = delete;
  30. template <int64_t seconds>
  31. static constexpr TimeDelta Seconds() {
  32. return FromStaticFraction<seconds, 1000000>();
  33. }
  34. template <int64_t ms>
  35. static constexpr TimeDelta Millis() {
  36. return FromStaticFraction<ms, 1000>();
  37. }
  38. template <int64_t us>
  39. static constexpr TimeDelta Micros() {
  40. return FromStaticValue<us>();
  41. }
  42. template <typename T>
  43. static TimeDelta seconds(T seconds) {
  44. static_assert(std::is_arithmetic<T>::value, "");
  45. return FromFraction<1000000>(seconds);
  46. }
  47. template <typename T>
  48. static TimeDelta ms(T milliseconds) {
  49. static_assert(std::is_arithmetic<T>::value, "");
  50. return FromFraction<1000>(milliseconds);
  51. }
  52. template <typename T>
  53. static TimeDelta us(T microseconds) {
  54. static_assert(std::is_arithmetic<T>::value, "");
  55. return FromValue(microseconds);
  56. }
  57. template <typename T = int64_t>
  58. T seconds() const {
  59. return ToFraction<1000000, T>();
  60. }
  61. template <typename T = int64_t>
  62. T ms() const {
  63. return ToFraction<1000, T>();
  64. }
  65. template <typename T = int64_t>
  66. T us() const {
  67. return ToValue<T>();
  68. }
  69. template <typename T = int64_t>
  70. T ns() const {
  71. return ToMultiple<1000, T>();
  72. }
  73. constexpr int64_t seconds_or(int64_t fallback_value) const {
  74. return ToFractionOr<1000000>(fallback_value);
  75. }
  76. constexpr int64_t ms_or(int64_t fallback_value) const {
  77. return ToFractionOr<1000>(fallback_value);
  78. }
  79. constexpr int64_t us_or(int64_t fallback_value) const {
  80. return ToValueOr(fallback_value);
  81. }
  82. TimeDelta Abs() const { return TimeDelta::us(std::abs(us())); }
  83. private:
  84. friend class rtc_units_impl::UnitBase<TimeDelta>;
  85. using RelativeUnit::RelativeUnit;
  86. static constexpr bool one_sided = false;
  87. };
  88. std::string ToString(TimeDelta value);
  89. inline std::string ToLogString(TimeDelta value) {
  90. return ToString(value);
  91. }
  92. #ifdef UNIT_TEST
  93. inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
  94. std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
  95. TimeDelta value) {
  96. return stream << ToString(value);
  97. }
  98. #endif // UNIT_TEST
  99. } // namespace webrtc
  100. #endif // API_UNITS_TIME_DELTA_H_