Bug 1276573 - Add a new constructor for Range<T>. draft
authorBoris Chiou <boris.chiou@gmail.com>
Mon, 30 May 2016 13:58:13 +0800
changeset 382159 8f8e9cd682234b44ba5fa8925ec4004bab2c27dc
parent 381966 e45890951ce77c3df05575bd54072b9f300d77b0
child 524118 b9bdba212a26613d50fa00f28ed0af0e32efd331
push id21642
push userbmo:boris.chiou@gmail.com
push dateWed, 29 Jun 2016 03:27:19 +0000
bugs1276573
milestone50.0a1
Bug 1276573 - Add a new constructor for Range<T>. These new constructor accepts two RangedPtr<T> arguments. MozReview-Commit-ID: 8a3bYserLMr
mfbt/Range.h
mfbt/RangedPtr.h
--- a/mfbt/Range.h
+++ b/mfbt/Range.h
@@ -21,16 +21,23 @@ class Range
   const RangedPtr<T> mEnd;
 
 public:
   Range() : mStart(nullptr, 0), mEnd(nullptr, 0) {}
   Range(T* aPtr, size_t aLength)
     : mStart(aPtr, aPtr, aPtr + aLength),
       mEnd(aPtr + aLength, aPtr, aPtr + aLength)
   {}
+  Range(const RangedPtr<T>& aStart, const RangedPtr<T>& aEnd)
+    : mStart(aStart.get(), aStart.get(), aEnd.get()),
+      mEnd(aEnd.get(), aStart.get(), aEnd.get())
+  {
+    // Only accept two RangedPtrs with the same range.
+    aStart.checkIdenticalRange(aEnd);
+  }
 
   RangedPtr<T> start() const { return mStart; }
   RangedPtr<T> end() const { return mEnd; }
   size_t length() const { return mEnd - mStart; }
 
   T& operator[](size_t aOffset) const { return mStart[aOffset]; }
 
   explicit operator bool() const { return mStart != nullptr; }
--- a/mfbt/RangedPtr.h
+++ b/mfbt/RangedPtr.h
@@ -112,30 +112,35 @@ public:
   {
     checkSanity();
   }
 
   T* get() const { return mPtr; }
 
   explicit operator bool() const { return mPtr != nullptr; }
 
+  void checkIdenticalRange(const RangedPtr<T>& aOther) const
+  {
+    MOZ_ASSERT(mRangeStart == aOther.mRangeStart);
+    MOZ_ASSERT(mRangeEnd == aOther.mRangeEnd);
+  }
+
   /*
    * You can only assign one RangedPtr into another if the two pointers have
    * the same valid range:
    *
    *   char arr1[] = "hi";
    *   char arr2[] = "bye";
    *   RangedPtr<char> p1(arr1, 2);
    *   p1 = RangedPtr<char>(arr1 + 1, arr1, arr1 + 2); // works
    *   p1 = RangedPtr<char>(arr2, 3);                  // asserts
    */
   RangedPtr<T>& operator=(const RangedPtr<T>& aOther)
   {
-    MOZ_ASSERT(mRangeStart == aOther.mRangeStart);
-    MOZ_ASSERT(mRangeEnd == aOther.mRangeEnd);
+    checkIdenticalRange(aOther);
     mPtr = aOther.mPtr;
     checkSanity();
     return *this;
   }
 
   RangedPtr<T> operator+(size_t aInc) const
   {
     MOZ_ASSERT(aInc <= size_t(-1) / sizeof(T));