Bug 1350791 - Add constexpr and some handy functions to TimeUnit. r?gerald
MozReview-Commit-ID: 2UfVSKWSa42
--- a/dom/media/TimeUnits.h
+++ b/dom/media/TimeUnits.h
@@ -7,16 +7,17 @@
#ifndef TIME_UNITS_H
#define TIME_UNITS_H
#include "Intervals.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/Maybe.h"
#include "mozilla/dom/TimeRanges.h"
+#include "mozilla/TimeStamp.h"
namespace mozilla {
namespace media {
class TimeIntervals;
} // namespace media
} // namespace mozilla
// CopyChooser specalization for nsTArray
template<>
@@ -100,32 +101,40 @@ public:
return FromMicroseconds(INT64_MAX);
} else if (val <= double(INT64_MIN)) {
return FromMicroseconds(INT64_MIN);
} else {
return FromMicroseconds(int64_t(val));
}
}
- static TimeUnit FromMicroseconds(int64_t aValue) {
+ static constexpr TimeUnit FromMicroseconds(int64_t aValue) {
return TimeUnit(aValue);
}
static TimeUnit FromMicroseconds(Microseconds aValue) {
return TimeUnit(aValue.mValue);
}
- static TimeUnit FromNanoseconds(int64_t aValue) {
+ static constexpr TimeUnit FromNanoseconds(int64_t aValue) {
return TimeUnit(aValue / 1000);
}
- static TimeUnit FromInfinity() {
+ static constexpr TimeUnit FromInfinity() {
return TimeUnit(INT64_MAX);
}
+ static TimeUnit FromTimeDuration(const TimeDuration& aDuration) {
+ return FromSeconds(aDuration.ToSeconds());
+ }
+
+ static constexpr TimeUnit Zero() {
+ return TimeUnit(0);
+ }
+
static TimeUnit Invalid() {
TimeUnit ret;
ret.mValue = CheckedInt64(INT64_MAX);
// Force an overflow to render the CheckedInt invalid.
ret.mValue += 1;
return ret;
}
@@ -139,16 +148,20 @@ public:
double ToSeconds() const {
if (IsInfinite()) {
return PositiveInfinity<double>();
}
return double(mValue.value()) / USECS_PER_S;
}
+ TimeDuration ToTimeDuration() const {
+ return TimeDuration::FromMicroseconds(mValue.value());
+ }
+
bool IsInfinite() const {
return mValue.value() == INT64_MAX;
}
bool operator == (const TimeUnit& aOther) const {
MOZ_ASSERT(IsValid() && aOther.IsValid());
return mValue.value() == aOther.mValue.value();
}
@@ -202,17 +215,17 @@ public:
return TimeUnit(aUnit.mValue / aVal);
}
bool IsValid() const
{
return mValue.isValid();
}
- TimeUnit()
+ constexpr TimeUnit()
: mValue(CheckedInt64(0))
{}
explicit TimeUnit(const Microseconds& aMicroseconds)
: mValue(aMicroseconds.mValue)
{}
TimeUnit& operator = (const Microseconds& aMicroseconds)
{
@@ -220,17 +233,17 @@ public:
return *this;
}
TimeUnit(const TimeUnit&) = default;
TimeUnit& operator = (const TimeUnit&) = default;
private:
- explicit TimeUnit(CheckedInt64 aMicroseconds)
+ explicit constexpr TimeUnit(CheckedInt64 aMicroseconds)
: mValue(aMicroseconds)
{}
// Our internal representation is in microseconds.
CheckedInt64 mValue;
};
typedef Maybe<TimeUnit> NullableTimeUnit;