Bug 1159578: Add a TimeUnit object. r=mattwoodrow
authorJean-Yves Avenard <jyavenard@mozilla.com>
Mon, 04 May 2015 21:16:34 +1000
changeset 273611 6f56bbf8cb5c66418a362c1961ed0948c507db4b
parent 273610 096da3d7f850fd5a1e6b07d92c05dbe5d3d2ace6
child 273612 4b23f2a48e8406a519f8f3f08f31a7bc1b005112
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmattwoodrow
bugs1159578
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1159578: Add a TimeUnit object. r=mattwoodrow A TimeUnit describes a point in time, without explicit units (it is not a microseconds nor a second) though can be converted in one or the other.
dom/media/TimeUnits.h
--- a/dom/media/TimeUnits.h
+++ b/dom/media/TimeUnits.h
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef TIME_UNITS_H
 #define TIME_UNITS_H
 
 #include "VideoUtils.h"
+#include "mozilla/CheckedInt.h"
 #include "mozilla/FloatingPoint.h"
 
 namespace mozilla {
 namespace media {
 
 struct Microseconds {
   Microseconds()
     : mValue(0)
@@ -50,13 +51,86 @@ struct Microseconds {
   }
   bool operator <= (const Microseconds& aOther) const {
     return mValue <= aOther.mValue;
   }
 
   int64_t mValue;
 };
 
+class TimeUnit final {
+public:
+  static TimeUnit FromSeconds(double aValue) {
+    MOZ_ASSERT(!IsNaN(aValue));
 
-}
-}
+    double val = aValue * USECS_PER_S;
+    if (val >= double(INT64_MAX)) {
+      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) {
+    return TimeUnit(aValue);
+  }
+
+  static TimeUnit FromMicroseconds(Microseconds aValue) {
+    return TimeUnit(aValue.mValue);
+  }
+
+  int64_t ToMicroseconds() const {
+    return mValue.value();
+  }
+
+  double ToSeconds() const {
+    return double(mValue.value()) / USECS_PER_S;
+  }
+
+  bool operator >= (const TimeUnit& aOther) const {
+    MOZ_ASSERT(IsValid() && aOther.IsValid());
+    return mValue.value() >= aOther.mValue.value();
+  }
+  bool operator > (const TimeUnit& aOther) const {
+    return !(*this <= aOther);
+  }
+  bool operator <= (const TimeUnit& aOther) const {
+    MOZ_ASSERT(IsValid() && aOther.IsValid());
+    return mValue.value() <= aOther.mValue.value();
+  }
+  bool operator < (const TimeUnit& aOther) const {
+    return !(*this >= aOther);
+  }
+  TimeUnit operator + (const TimeUnit& aOther) const {
+    return TimeUnit(mValue + aOther.mValue);
+  }
+  TimeUnit operator - (const TimeUnit& aOther) const {
+    return TimeUnit(mValue - aOther.mValue);
+  }
+
+  bool IsValid() const
+  {
+    return mValue.isValid();
+  }
+
+  explicit TimeUnit(const Microseconds& aMicroseconds)
+    : mValue(aMicroseconds.mValue)
+  {}
+
+  TimeUnit(const TimeUnit&) = default;
+
+  TimeUnit& operator = (const TimeUnit&) = default;
+
+private:
+  explicit TimeUnit(CheckedInt64 aMicroseconds)
+    : mValue(aMicroseconds)
+  {}
+
+  // Our internal representation is in microseconds.
+  CheckedInt64 mValue;
+};
+
+} // namespace media
+} // namespace mozilla
 
 #endif // TIME_UNITS_H