Bug 1259248. Add an ArrayView class. r=botond
authorJeff Muizelaar <jmuizelaar@mozilla.com>
Wed, 23 Mar 2016 21:20:41 -0400
changeset 290232 d95bbecda3af65a2fdb4e95a876fd508a8727df9
parent 290231 bc443e4eaecaf1007c242cbcd954aaf1fe9ad493
child 290233 5ed70dba80c697db0c1ef4d2a05e48c7f518624e
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbotond
bugs1259248
milestone48.0a1
Bug 1259248. Add an ArrayView class. r=botond
gfx/src/ArrayView.h
gfx/src/moz.build
gfx/src/nsRegion.h
gfx/tests/gtest/TestArrayView.cpp
gfx/tests/gtest/moz.build
new file mode 100644
--- /dev/null
+++ b/gfx/src/ArrayView.h
@@ -0,0 +1,46 @@
+/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ * 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_GFX_ARRAY_VIEW_H_
+#define MOZILLA_GFX_ARRAY_VIEW_H_
+
+#include "nsTArray.h"
+
+namespace mozilla {
+namespace gfx {
+
+template<typename T>
+class ArrayView
+{
+    public:
+        ArrayView(const nsTArray<T>& aData) :
+            mData(aData.Elements()), mLength(aData.Length())
+        {
+        }
+        ArrayView(const T* aData, const size_t aLength) :
+            mData(aData), mLength(aLength)
+        {
+        }
+        const T& operator[](const size_t aIdx) const
+        {
+            return mData[aIdx];
+        }
+        size_t Length() const
+        {
+            return mLength;
+        }
+        const T* Data() const
+        {
+            return mData;
+        }
+    private:
+        const T* mData;
+        const size_t mLength;
+};
+
+} // namespace gfx
+} // namespace mozilla
+
+#endif /* MOZILLA_GFX_ARRAY_VIEW_H_ */
--- a/gfx/src/moz.build
+++ b/gfx/src/moz.build
@@ -38,16 +38,17 @@ EXPORTS += [
     'nsThemeConstants.h',
     'nsTransform2D.h',
     'PingPongRegion.h',
     'RegionBuilder.h',
 ]
 
 EXPORTS.mozilla += [
     'AppUnits.h',
+    'ArrayView.h',
 ]
 
 if CONFIG['MOZ_X11']:
     EXPORTS.mozilla += ['X11Util.h']
     SOURCES += [
         'X11Util.cpp',
     ]
 
--- a/gfx/src/nsRegion.h
+++ b/gfx/src/nsRegion.h
@@ -14,18 +14,18 @@
 #include <ostream>                      // for std::ostream
 #include "nsCoord.h"                    // for nscoord
 #include "nsError.h"                    // for nsresult
 #include "nsPoint.h"                    // for nsIntPoint, nsPoint
 #include "nsRect.h"                     // for mozilla::gfx::IntRect, nsRect
 #include "nsMargin.h"                   // for nsIntMargin
 #include "nsRegionFwd.h"                // for nsIntRegion
 #include "nsStringGlue.h"               // for nsCString
-#include "nsTArray.h"                   // for nsTArray, nsTArray_Impl
 #include "xpcom-config.h"               // for CPP_THROW_NEW
+#include "mozilla/ArrayView.h"          // for ArrayView
 #include "mozilla/Move.h"               // for mozilla::Move
 #include "mozilla/gfx/MatrixFwd.h"      // for mozilla::gfx::Matrix4x4
 
 #include "pixman.h"
 
 /* For information on the internal representation look at pixman-region.c
  *
  * This replaces an older homebrew implementation of nsRegion. The
@@ -55,19 +55,19 @@ public:
   typedef nsMargin MarginType;
 
   nsRegion () { pixman_region32_init(&mImpl); }
   MOZ_IMPLICIT nsRegion (const nsRect& aRect) { pixman_region32_init_rect(&mImpl,
                                                                           aRect.x,
                                                                           aRect.y,
                                                                           aRect.width,
                                                                           aRect.height); }
-  explicit nsRegion (const nsTArray<pixman_box32_t>& aRects)
+  explicit nsRegion (mozilla::gfx::ArrayView<pixman_box32_t> aRects)
   {
-    pixman_region32_init_rects(&mImpl, aRects.Elements(), aRects.Length());
+    pixman_region32_init_rects(&mImpl, aRects.Data(), aRects.Length());
   }
   nsRegion (const nsRegion& aRegion) { pixman_region32_init(&mImpl); pixman_region32_copy(&mImpl,aRegion.Impl()); }
   nsRegion (nsRegion&& aRegion) { mImpl = aRegion.mImpl; pixman_region32_init(&aRegion.mImpl); }
   nsRegion& operator = (nsRegion&& aRegion) {
       pixman_region32_fini(&mImpl);
       mImpl = aRegion.mImpl;
       pixman_region32_init(&aRegion.mImpl);
       return *this;
@@ -475,17 +475,17 @@ class BaseIntRegion
 
 public:
   typedef Rect RectType;
   typedef Point PointType;
   typedef Margin MarginType;
 
   BaseIntRegion () {}
   MOZ_IMPLICIT BaseIntRegion (const Rect& aRect) : mImpl (ToRect(aRect)) {}
-  explicit BaseIntRegion (const nsTArray<pixman_box32_t>& aRects) : mImpl (aRects) {}
+  explicit BaseIntRegion (mozilla::gfx::ArrayView<pixman_box32_t> aRects) : mImpl (aRects) {}
   BaseIntRegion (const BaseIntRegion& aRegion) : mImpl (aRegion.mImpl) {}
   BaseIntRegion (BaseIntRegion&& aRegion) : mImpl (mozilla::Move(aRegion.mImpl)) {}
   Derived& operator = (const Rect& aRect) { mImpl = ToRect (aRect); return This(); }
   Derived& operator = (const Derived& aRegion) { mImpl = aRegion.mImpl; return This(); }
   Derived& operator = (Derived&& aRegion) { mImpl = mozilla::Move(aRegion.mImpl); return This(); }
 
   bool operator==(const Derived& aRgn) const
   {
@@ -827,17 +827,17 @@ public:
   typedef IntRectTyped<units> RectType;
   typedef IntPointTyped<units> PointType;
   typedef IntMarginTyped<units> MarginType;
 
   // Forward constructors.
   IntRegionTyped() {}
   MOZ_IMPLICIT IntRegionTyped(const IntRectTyped<units>& aRect) : Super(aRect) {}
   IntRegionTyped(const IntRegionTyped& aRegion) : Super(aRegion) {}
-  explicit IntRegionTyped(const nsTArray<pixman_box32_t>& aRects) : Super(aRects) {}
+  explicit IntRegionTyped(mozilla::gfx::ArrayView<pixman_box32_t> aRects) : Super(aRects) {}
   IntRegionTyped(IntRegionTyped&& aRegion) : Super(mozilla::Move(aRegion)) {}
 
   // Assignment operators need to be forwarded as well, otherwise the compiler
   // will declare deleted ones.
   IntRegionTyped& operator=(const IntRegionTyped& aRegion)
   {
     return Super::operator=(aRegion);
   }
new file mode 100644
--- /dev/null
+++ b/gfx/tests/gtest/TestArrayView.cpp
@@ -0,0 +1,18 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+#include <limits>
+
+#include "gtest/gtest.h"
+
+#include "mozilla/ArrayView.h"
+
+using namespace mozilla::gfx;
+
+TEST(Gfx, ArrayView) {
+    nsTArray<int> p = {5, 6};
+    ArrayView<int> pv(p);
+    ASSERT_EQ(pv[1], 6);
+    ASSERT_EQ(*pv.Data(), 5);
+}
--- a/gfx/tests/gtest/moz.build
+++ b/gfx/tests/gtest/moz.build
@@ -4,16 +4,17 @@
 # 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/.
 
 UNIFIED_SOURCES += [
     'gfxSurfaceRefCountTest.cpp',
     # Disabled on suspicion of causing bug 904227
     #'gfxWordCacheTest.cpp',
     'TestArena.cpp',
+    'TestArrayView.cpp',
     'TestBufferRotation.cpp',
     'TestColorNames.cpp',
     'TestCompositor.cpp',
     'TestGfxPrefs.cpp',
     'TestGfxWidgets.cpp',
     'TestJobScheduler.cpp',
     'TestLayers.cpp',
     'TestMoz2D.cpp',