Bug 811060 - Implement mozilla::Range; r=Waldo
authorTerrence Cole <terrence@mozilla.com>
Mon, 12 Nov 2012 16:37:33 -0800
changeset 129481 f8c06ade84d6e513c38f2f13122fc6f235c0f6dc
parent 129480 7c102b657a17761295a0eedbd18f5f8cee5f8d5b
child 129482 6acc72608961cc79aa9fbd28627f3c37c3a03346
push id317
push userbbajaj@mozilla.com
push dateTue, 07 May 2013 01:20:33 +0000
treeherdermozilla-release@159a10910249 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs811060
milestone21.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 811060 - Implement mozilla::Range; r=Waldo
mfbt/Range.h
mfbt/RangedPtr.h
mfbt/exported_headers.mk
new file mode 100644
--- /dev/null
+++ b/mfbt/Range.h
@@ -0,0 +1,49 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: set ts=8 sw=4 et tw=78:
+ *
+ * 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 mozilla_Range_h_
+#define mozilla_Range_h_
+
+#include "mozilla/NullPtr.h"
+#include "mozilla/RangedPtr.h"
+
+#include <stddef.h>
+
+namespace mozilla {
+
+// Range<T> is a tuple containing a pointer and a length.
+template <typename T>
+class Range
+{
+    RangedPtr<T> mStart;
+    RangedPtr<T> mEnd;
+
+    typedef void (Range::* ConvertibleToBool)();
+    void nonNull() {}
+
+  public:
+    Range() : mStart(nullptr, 0), mEnd(nullptr, 0) {}
+    Range(T* p, size_t len)
+      : mStart(p, p, p + len),
+        mEnd(p + len, p, p + len)
+    {}
+
+    RangedPtr<T> start() const { return mStart; }
+    RangedPtr<T> end() const { return mEnd; }
+    size_t length() const { return mEnd - mStart; }
+
+    T& operator[](size_t offset) {
+      return mStart[offset];
+    }
+
+    operator ConvertibleToBool() const { return mStart ? &Range::nonNull : 0; }
+};
+
+} // namespace mozilla
+
+#endif // mozilla_Range_h_
+
--- a/mfbt/RangedPtr.h
+++ b/mfbt/RangedPtr.h
@@ -41,16 +41,19 @@ class RangedPtr
 {
     T* ptr;
 
 #ifdef DEBUG
     T* const rangeStart;
     T* const rangeEnd;
 #endif
 
+    typedef void (RangedPtr::* ConvertibleToBool)();
+    void nonNull() {}
+
     void checkSanity() {
       MOZ_ASSERT(rangeStart <= ptr);
       MOZ_ASSERT(ptr <= rangeEnd);
     }
 
     /* Creates a new pointer for |ptr|, restricted to this pointer's range. */
     RangedPtr<T> create(T *ptr) const {
 #ifdef DEBUG
@@ -105,16 +108,18 @@ class RangedPtr
     {
       checkSanity();
     }
 
     T* get() const {
       return ptr;
     }
 
+    operator ConvertibleToBool() const { return ptr ? &RangedPtr::nonNull : 0; }
+
     /*
      * 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
@@ -237,14 +242,13 @@ class RangedPtr
     size_t operator-(const RangedPtr<T>& other) const {
       MOZ_ASSERT(ptr >= other.ptr);
       return PointerRangeSize(other.ptr, ptr);
     }
 
   private:
     RangedPtr() MOZ_DELETE;
     T* operator&() MOZ_DELETE;
-    operator T*() const MOZ_DELETE;
 };
 
 } /* namespace mozilla */
 
 #endif  /* mozilla_RangedPtr_h_ */
--- a/mfbt/exported_headers.mk
+++ b/mfbt/exported_headers.mk
@@ -20,16 +20,17 @@ EXPORTS_mozilla += \
   FloatingPoint.h \
   GuardObjects.h \
   HashFunctions.h \
   Likely.h \
   LinkedList.h \
   MathAlgorithms.h \
   MSStdInt.h \
   NullPtr.h \
+  Range.h \
   RangedPtr.h \
   RefPtr.h \
   Scoped.h \
   StandardInteger.h \
   SHA1.h \
   ThreadLocal.h \
   TypeTraits.h \
   Types.h \