mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-16 15:20:42 +01:00

These factories suppose to replace set of old constexpr factories that takes parameter as template rather than function parameter, as well as fix function naming to follow style guide of the second set of factory functions. Bug: None Change-Id: Icd76302b821b2a4027f9d6765cf91bc9190f551c Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/167521 Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/master@{#30482}
168 lines
5.2 KiB
C++
168 lines
5.2 KiB
C++
/*
|
|
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#ifndef API_UNITS_TIMESTAMP_H_
|
|
#define API_UNITS_TIMESTAMP_H_
|
|
|
|
#ifdef UNIT_TEST
|
|
#include <ostream> // no-presubmit-check TODO(webrtc:8982)
|
|
#endif // UNIT_TEST
|
|
|
|
#include <string>
|
|
#include <type_traits>
|
|
|
|
#include "api/units/time_delta.h"
|
|
#include "rtc_base/checks.h"
|
|
|
|
namespace webrtc {
|
|
// Timestamp represents the time that has passed since some unspecified epoch.
|
|
// The epoch is assumed to be before any represented timestamps, this means that
|
|
// negative values are not valid. The most notable feature is that the
|
|
// difference of two Timestamps results in a TimeDelta.
|
|
class Timestamp final : public rtc_units_impl::UnitBase<Timestamp> {
|
|
public:
|
|
template <typename T>
|
|
static constexpr Timestamp Seconds(T value) {
|
|
static_assert(std::is_arithmetic<T>::value, "");
|
|
return FromFraction(1'000'000, value);
|
|
}
|
|
template <typename T>
|
|
static constexpr Timestamp Millis(T value) {
|
|
static_assert(std::is_arithmetic<T>::value, "");
|
|
return FromFraction(1'000, value);
|
|
}
|
|
template <typename T>
|
|
static constexpr Timestamp Micros(T value) {
|
|
static_assert(std::is_arithmetic<T>::value, "");
|
|
return FromValue(value);
|
|
}
|
|
|
|
Timestamp() = delete;
|
|
|
|
// TODO(danilchap): Migrate all code to the 3 factories above and delete the
|
|
// 6 factories below.
|
|
template <int64_t seconds>
|
|
static constexpr Timestamp Seconds() {
|
|
return FromFraction(1'000'000, seconds);
|
|
}
|
|
template <int64_t ms>
|
|
static constexpr Timestamp Millis() {
|
|
return FromFraction(1000, ms);
|
|
}
|
|
template <int64_t us>
|
|
static constexpr Timestamp Micros() {
|
|
return FromValue(us);
|
|
}
|
|
|
|
template <typename T>
|
|
static constexpr Timestamp seconds(T seconds) {
|
|
static_assert(std::is_arithmetic<T>::value, "");
|
|
return FromFraction(1'000'000, seconds);
|
|
}
|
|
template <typename T>
|
|
static constexpr Timestamp ms(T milliseconds) {
|
|
static_assert(std::is_arithmetic<T>::value, "");
|
|
return FromFraction(1000, milliseconds);
|
|
}
|
|
template <typename T>
|
|
static constexpr Timestamp us(T microseconds) {
|
|
static_assert(std::is_arithmetic<T>::value, "");
|
|
return FromValue(microseconds);
|
|
}
|
|
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>();
|
|
}
|
|
|
|
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 Timestamp operator+(const TimeDelta delta) const {
|
|
if (IsPlusInfinity() || delta.IsPlusInfinity()) {
|
|
RTC_DCHECK(!IsMinusInfinity());
|
|
RTC_DCHECK(!delta.IsMinusInfinity());
|
|
return PlusInfinity();
|
|
} else if (IsMinusInfinity() || delta.IsMinusInfinity()) {
|
|
RTC_DCHECK(!IsPlusInfinity());
|
|
RTC_DCHECK(!delta.IsPlusInfinity());
|
|
return MinusInfinity();
|
|
}
|
|
return Timestamp::us(us() + delta.us());
|
|
}
|
|
constexpr Timestamp operator-(const TimeDelta delta) const {
|
|
if (IsPlusInfinity() || delta.IsMinusInfinity()) {
|
|
RTC_DCHECK(!IsMinusInfinity());
|
|
RTC_DCHECK(!delta.IsPlusInfinity());
|
|
return PlusInfinity();
|
|
} else if (IsMinusInfinity() || delta.IsPlusInfinity()) {
|
|
RTC_DCHECK(!IsPlusInfinity());
|
|
RTC_DCHECK(!delta.IsMinusInfinity());
|
|
return MinusInfinity();
|
|
}
|
|
return Timestamp::us(us() - delta.us());
|
|
}
|
|
constexpr TimeDelta operator-(const Timestamp other) const {
|
|
if (IsPlusInfinity() || other.IsMinusInfinity()) {
|
|
RTC_DCHECK(!IsMinusInfinity());
|
|
RTC_DCHECK(!other.IsPlusInfinity());
|
|
return TimeDelta::PlusInfinity();
|
|
} else if (IsMinusInfinity() || other.IsPlusInfinity()) {
|
|
RTC_DCHECK(!IsPlusInfinity());
|
|
RTC_DCHECK(!other.IsMinusInfinity());
|
|
return TimeDelta::MinusInfinity();
|
|
}
|
|
return TimeDelta::us(us() - other.us());
|
|
}
|
|
constexpr Timestamp& operator-=(const TimeDelta delta) {
|
|
*this = *this - delta;
|
|
return *this;
|
|
}
|
|
constexpr Timestamp& operator+=(const TimeDelta delta) {
|
|
*this = *this + delta;
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
friend class rtc_units_impl::UnitBase<Timestamp>;
|
|
using UnitBase::UnitBase;
|
|
static constexpr bool one_sided = true;
|
|
};
|
|
|
|
std::string ToString(Timestamp value);
|
|
inline std::string ToLogString(Timestamp value) {
|
|
return ToString(value);
|
|
}
|
|
|
|
#ifdef UNIT_TEST
|
|
inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
|
|
std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
|
|
Timestamp value) {
|
|
return stream << ToString(value);
|
|
}
|
|
#endif // UNIT_TEST
|
|
|
|
} // namespace webrtc
|
|
|
|
#endif // API_UNITS_TIMESTAMP_H_
|