Bug 1276066 - Add Union with "components" member to math classes to enable array access to members. r=bas
authorKearwood (Kip) Gilbert <kgilbert@mozilla.com>
Mon, 18 Apr 2016 12:12:36 -0700
changeset 340490 8b31e8fca3492dad2436558ab439a678d7c7de4c
parent 340489 bf99ab13bbbf08391810989d4dbb0ff408c426e4
child 340491 57253d7ecce8215f302330af0a5d2504268e1641
push id1183
push userraliiev@mozilla.com
push dateMon, 05 Sep 2016 20:01:49 +0000
treeherdermozilla-release@3148731bed45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbas
bugs1276066
milestone49.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 1276066 - Add Union with "components" member to math classes to enable array access to members. r=bas - Adding an array member to access the components of vector, size, and matrix classes reduces the code needed when passing all of the members to functions. MozReview-Commit-ID: A6XL7y3zwsV
gfx/2d/BasePoint.h
gfx/2d/BasePoint3D.h
gfx/2d/BasePoint4D.h
gfx/2d/BaseSize.h
gfx/2d/Matrix.h
--- a/gfx/2d/BasePoint.h
+++ b/gfx/2d/BasePoint.h
@@ -17,17 +17,22 @@ namespace gfx {
 
 /**
  * Do not use this class directly. Subclass it, pass that subclass as the
  * Sub parameter, and only use that subclass. This allows methods to safely
  * cast 'this' to 'Sub*'.
  */
 template <class T, class Sub, class Coord = T>
 struct BasePoint {
-  T x, y;
+  union {
+    struct {
+      T x, y;
+    };
+    T components[2];
+  };
 
   // Constructors
   MOZ_CONSTEXPR BasePoint() : x(0), y(0) {}
   MOZ_CONSTEXPR BasePoint(Coord aX, Coord aY) : x(aX), y(aY) {}
 
   void MoveTo(T aX, T aY) { x = aX; y = aY; }
   void MoveBy(T aDx, T aDy) { x += aDx; y += aDy; }
 
--- a/gfx/2d/BasePoint3D.h
+++ b/gfx/2d/BasePoint3D.h
@@ -13,17 +13,22 @@ namespace gfx {
 
 /**
  * Do not use this class directly. Subclass it, pass that subclass as the
  * Sub parameter, and only use that subclass. This allows methods to safely
  * cast 'this' to 'Sub*'.
  */
 template <class T, class Sub>
 struct BasePoint3D {
-  T x, y, z;
+  union {
+    struct {
+      T x, y, z;
+    };
+    T components[3];
+  };
 
   // Constructors
   BasePoint3D() : x(0), y(0), z(0) {}
   BasePoint3D(T aX, T aY, T aZ) : x(aX), y(aY), z(aZ) {}
 
   void MoveTo(T aX, T aY, T aZ) { x = aX; y = aY; z = aZ; }
   void MoveBy(T aDx, T aDy, T aDz) { x += aDx; y += aDy; z += aDz; }
 
--- a/gfx/2d/BasePoint4D.h
+++ b/gfx/2d/BasePoint4D.h
@@ -13,17 +13,22 @@ namespace gfx {
 
 /**
  * Do not use this class directly. Subclass it, pass that subclass as the
  * Sub parameter, and only use that subclass. This allows methods to safely
  * cast 'this' to 'Sub*'.
  */
 template <class T, class Sub>
 struct BasePoint4D {
-  T x, y, z, w;
+  union {
+    struct {
+      T x, y, z, w;
+    };
+    T components[4];
+  };
 
   // Constructors
   BasePoint4D() : x(0), y(0), z(0), w(0) {}
   BasePoint4D(T aX, T aY, T aZ, T aW) : x(aX), y(aY), z(aZ), w(aW) {}
 
   void MoveTo(T aX, T aY, T aZ, T aW) { x = aX; y = aY; z = aZ; w = aW; }
   void MoveBy(T aDx, T aDy, T aDz, T aDw) { x += aDx; y += aDy; z += aDz; w += aDw; }
 
--- a/gfx/2d/BaseSize.h
+++ b/gfx/2d/BaseSize.h
@@ -13,17 +13,22 @@ namespace gfx {
 
 /**
  * Do not use this class directly. Subclass it, pass that subclass as the
  * Sub parameter, and only use that subclass. This allows methods to safely
  * cast 'this' to 'Sub*'.
  */
 template <class T, class Sub>
 struct BaseSize {
-  T width, height;
+  union {
+    struct {
+      T width, height;
+    };
+    T components[2];
+  };
 
   // Constructors
   MOZ_CONSTEXPR BaseSize() : width(0), height(0) {}
   MOZ_CONSTEXPR BaseSize(T aWidth, T aHeight) : width(aWidth), height(aHeight) {}
 
   void SizeTo(T aWidth, T aHeight) { width = aWidth; height = aHeight; }
 
   bool IsEmpty() const {
--- a/gfx/2d/Matrix.h
+++ b/gfx/2d/Matrix.h
@@ -32,19 +32,24 @@ public:
     , _21(0), _22(1.0f)
     , _31(0), _32(0)
   {}
   Matrix(Float a11, Float a12, Float a21, Float a22, Float a31, Float a32)
     : _11(a11), _12(a12)
     , _21(a21), _22(a22)
     , _31(a31), _32(a32)
   {}
-  Float _11, _12;
-  Float _21, _22;
-  Float _31, _32;
+  union {
+    struct {
+      Float _11, _12;
+      Float _21, _22;
+      Float _31, _32;
+    };
+    Float components[6];
+  };
 
   MOZ_ALWAYS_INLINE Matrix Copy() const
   {
     return Matrix(*this);
   }
 
   friend std::ostream& operator<<(std::ostream& aStream, const Matrix& aMatrix);
 
@@ -456,20 +461,25 @@ public:
     , _41(a41), _42(a42), _43(a43), _44(a44)
   {}
 
   Matrix4x4Typed(const Matrix4x4Typed& aOther)
   {
     memcpy(this, &aOther, sizeof(*this));
   }
 
-  Float _11, _12, _13, _14;
-  Float _21, _22, _23, _24;
-  Float _31, _32, _33, _34;
-  Float _41, _42, _43, _44;
+  union {
+    struct {
+      Float _11, _12, _13, _14;
+      Float _21, _22, _23, _24;
+      Float _31, _32, _33, _34;
+      Float _41, _42, _43, _44;
+    };
+    Float components[16];
+  };
 
   friend std::ostream& operator<<(std::ostream& aStream, const Matrix4x4Typed& aMatrix)
   {
     const Float *f = &aMatrix._11;
     aStream << "[ " << f[0] << " "  << f[1] << " " << f[2] << " " << f[3] << " ;" << std::endl; f += 4;
     aStream << "  " << f[0] << " "  << f[1] << " " << f[2] << " " << f[3] << " ;" << std::endl; f += 4;
     aStream << "  " << f[0] << " "  << f[1] << " " << f[2] << " " << f[3] << " ;" << std::endl; f += 4;
     aStream << "  " << f[0] << " "  << f[1] << " " << f[2] << " " << f[3] << " ]" << std::endl;
@@ -1613,19 +1623,24 @@ public:
   }
 
   Matrix5x4& operator*=(const Matrix5x4 &aMatrix)
   {
     *this = *this * aMatrix;
     return *this;
   }
 
-  Float _11, _12, _13, _14;
-  Float _21, _22, _23, _24;
-  Float _31, _32, _33, _34;
-  Float _41, _42, _43, _44;
-  Float _51, _52, _53, _54;
+  union {
+    struct {
+      Float _11, _12, _13, _14;
+      Float _21, _22, _23, _24;
+      Float _31, _32, _33, _34;
+      Float _41, _42, _43, _44;
+      Float _51, _52, _53, _54;
+    };
+    Float components[20];
+  };
 };
 
 } // namespace gfx
 } // namespace mozilla
 
 #endif /* MOZILLA_GFX_MATRIX_H_ */