time_delta.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. template <typename T>
  30. static constexpr TimeDelta Seconds(T value) {
  31. static_assert(std::is_arithmetic<T>::value, "");
  32. return FromFraction(1'000'000, value);
  33. }
  34. template <typename T>
  35. static constexpr TimeDelta Millis(T value) {
  36. static_assert(std::is_arithmetic<T>::value, "");
  37. return FromFraction(1'000, value);
  38. }
  39. template <typename T>
  40. static constexpr TimeDelta Micros(T value) {
  41. static_assert(std::is_arithmetic<T>::value, "");
  42. return FromValue(value);
  43. }
  44. TimeDelta() = delete;
  45. template <typename T = int64_t>
  46. constexpr T seconds() const {
  47. return ToFraction<1000000, T>();
  48. }
  49. template <typename T = int64_t>
  50. constexpr T ms() const {
  51. return ToFraction<1000, T>();
  52. }
  53. template <typename T = int64_t>
  54. constexpr T us() const {
  55. return ToValue<T>();
  56. }
  57. template <typename T = int64_t>
  58. constexpr T ns() const {
  59. return ToMultiple<1000, T>();
  60. }
  61. constexpr int64_t seconds_or(int64_t fallback_value) const {
  62. return ToFractionOr<1000000>(fallback_value);
  63. }
  64. constexpr int64_t ms_or(int64_t fallback_value) const {
  65. return ToFractionOr<1000>(fallback_value);
  66. }
  67. constexpr int64_t us_or(int64_t fallback_value) const {
  68. return ToValueOr(fallback_value);
  69. }
  70. constexpr TimeDelta Abs() const {
  71. return us() < 0 ? TimeDelta::Micros(-us()) : *this;
  72. }
  73. private:
  74. friend class rtc_units_impl::UnitBase<TimeDelta>;
  75. using RelativeUnit::RelativeUnit;
  76. static constexpr bool one_sided = false;
  77. };
  78. std::string ToString(TimeDelta value);
  79. inline std::string ToLogString(TimeDelta value) {
  80. return ToString(value);
  81. }
  82. #ifdef UNIT_TEST
  83. inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
  84. std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
  85. TimeDelta value) {
  86. return stream << ToString(value);
  87. }
  88. #endif // UNIT_TEST
  89. } // namespace webrtc
  90. #endif // API_UNITS_TIME_DELTA_H_