123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #ifndef API_UNITS_TIME_DELTA_H_
- #define API_UNITS_TIME_DELTA_H_
- #ifdef UNIT_TEST
- #include <ostream> // no-presubmit-check TODO(webrtc:8982)
- #endif
- #include <cstdlib>
- #include <string>
- #include <type_traits>
- #include "rtc_base/units/unit_base.h"
- namespace webrtc {
- class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> {
- public:
- template <typename T>
- static constexpr TimeDelta Seconds(T value) {
- static_assert(std::is_arithmetic<T>::value, "");
- return FromFraction(1'000'000, value);
- }
- template <typename T>
- static constexpr TimeDelta Millis(T value) {
- static_assert(std::is_arithmetic<T>::value, "");
- return FromFraction(1'000, value);
- }
- template <typename T>
- static constexpr TimeDelta Micros(T value) {
- static_assert(std::is_arithmetic<T>::value, "");
- return FromValue(value);
- }
- TimeDelta() = delete;
- template <typename T = int64_t>
- constexpr T seconds() const {
- return ToFraction<1000000, T>();
- }
- template <typename T = int64_t>
- constexpr T ms() const {
- return ToFraction<1000, T>();
- }
- template <typename T = int64_t>
- constexpr T us() const {
- return ToValue<T>();
- }
- template <typename T = int64_t>
- constexpr T ns() const {
- return ToMultiple<1000, T>();
- }
- constexpr int64_t seconds_or(int64_t fallback_value) const {
- return ToFractionOr<1000000>(fallback_value);
- }
- constexpr int64_t ms_or(int64_t fallback_value) const {
- return ToFractionOr<1000>(fallback_value);
- }
- constexpr int64_t us_or(int64_t fallback_value) const {
- return ToValueOr(fallback_value);
- }
- constexpr TimeDelta Abs() const {
- return us() < 0 ? TimeDelta::Micros(-us()) : *this;
- }
- private:
- friend class rtc_units_impl::UnitBase<TimeDelta>;
- using RelativeUnit::RelativeUnit;
- static constexpr bool one_sided = false;
- };
- std::string ToString(TimeDelta value);
- inline std::string ToLogString(TimeDelta value) {
- return ToString(value);
- }
- #ifdef UNIT_TEST
- inline std::ostream& operator<<(
- std::ostream& stream,
- TimeDelta value) {
- return stream << ToString(value);
- }
- #endif
- }
- #endif
|