gfx/2d/BasePoint3D.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef MOZILLA_BASEPOINT3D_H_
     7 #define MOZILLA_BASEPOINT3D_H_
     9 #include "mozilla/Assertions.h"
    11 namespace mozilla {
    12 namespace gfx {
    14 /**
    15  * Do not use this class directly. Subclass it, pass that subclass as the
    16  * Sub parameter, and only use that subclass. This allows methods to safely
    17  * cast 'this' to 'Sub*'.
    18  */
    19 template <class T, class Sub>
    20 struct BasePoint3D {
    21   T x, y, z;
    23   // Constructors
    24   BasePoint3D() : x(0), y(0), z(0) {}
    25   BasePoint3D(T aX, T aY, T aZ) : x(aX), y(aY), z(aZ) {}
    27   void MoveTo(T aX, T aY, T aZ) { x = aX; y = aY; z = aZ; }
    28   void MoveBy(T aDx, T aDy, T aDz) { x += aDx; y += aDy; z += aDz; }
    30   // Note that '=' isn't defined so we'll get the
    31   // compiler generated default assignment operator
    33   T& operator[](int aIndex) {
    34     MOZ_ASSERT(aIndex >= 0 && aIndex <= 2);
    35     return *((&x)+aIndex);
    36   }
    38   const T& operator[](int aIndex) const {
    39     MOZ_ASSERT(aIndex >= 0 && aIndex <= 2);
    40     return *((&x)+aIndex);
    41   }
    43   bool operator==(const Sub& aPoint) const {
    44     return x == aPoint.x && y == aPoint.y && z == aPoint.z;
    45   }
    46   bool operator!=(const Sub& aPoint) const {
    47     return x != aPoint.x || y != aPoint.y || z != aPoint.z;
    48   }
    50   Sub operator+(const Sub& aPoint) const {
    51     return Sub(x + aPoint.x, y + aPoint.y, z + aPoint.z);
    52   }
    53   Sub operator-(const Sub& aPoint) const {
    54     return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z);
    55   }
    56   Sub& operator+=(const Sub& aPoint) {
    57     x += aPoint.x;
    58     y += aPoint.y;
    59     z += aPoint.z;
    60     return *static_cast<Sub*>(this);
    61   }
    62   Sub& operator-=(const Sub& aPoint) {
    63     x -= aPoint.x;
    64     y -= aPoint.y;
    65     z -= aPoint.z;
    66     return *static_cast<Sub*>(this);
    67   }
    69   Sub operator*(T aScale) const {
    70     return Sub(x * aScale, y * aScale, z * aScale);
    71   }
    72   Sub operator/(T aScale) const {
    73     return Sub(x / aScale, y / aScale, z / aScale);
    74   }
    76   Sub& operator*=(T aScale) {
    77     x *= aScale;
    78     y *= aScale;
    79     z *= aScale;
    80     return *static_cast<Sub*>(this);
    81   }
    83   Sub& operator/=(T aScale) {
    84       x /= aScale;
    85       y /= aScale;
    86       z /= aScale;
    87       return *static_cast<Sub*>(this);
    88   }
    90   Sub operator-() const {
    91     return Sub(-x, -y, -z);
    92   }
    94   Sub CrossProduct(const Sub& aPoint) const {
    95       return Sub(y * aPoint.z - aPoint.y * z,
    96                  z * aPoint.x - aPoint.z * x,
    97                  x * aPoint.y - aPoint.x * y);
    98   }
   100   T DotProduct(const Sub& aPoint) const {
   101       return x * aPoint.x + y * aPoint.y + z * aPoint.z;
   102   }
   104   T Length() const {
   105       return sqrt(x*x + y*y + z*z);
   106   }
   108   // Invalid for points with distance from origin of 0.
   109   void Normalize() {
   110       *this /= Length();
   111   }
   112 };
   114 }
   115 }
   117 #endif /* MOZILLA_BASEPOINT3D_H_ */

mercurial