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