Bug 1350791 - Add constexpr and some handy functions to TimeUnit. r?gerald draft
authorJW Wang <jwwang@mozilla.com>
Mon, 27 Mar 2017 10:59:56 +0800
changeset 551639 885461e5ee3368280221031848667a3596a094df
parent 551573 613ff1c945a877573784bd6a85fad8557c8160e7
child 551640 5389c0f48de6fb2723b3661320f111a3e00621e3
push id51103
push userjwwang@mozilla.com
push dateMon, 27 Mar 2017 05:58:00 +0000
reviewersgerald
bugs1350791
milestone55.0a1
Bug 1350791 - Add constexpr and some handy functions to TimeUnit. r?gerald MozReview-Commit-ID: 2UfVSKWSa42
dom/media/TimeUnits.h
--- 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;