gfx/2d/ScaleFactor.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: 20; 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_GFX_SCALEFACTOR_H_
     7 #define MOZILLA_GFX_SCALEFACTOR_H_
     9 #include "mozilla/Attributes.h"
    11 #include "gfxPoint.h"
    13 namespace mozilla {
    14 namespace gfx {
    16 /*
    17  * This class represents a scaling factor between two different pixel unit
    18  * systems. This is effectively a type-safe float, intended to be used in
    19  * combination with the known-type instances of gfx::Point, gfx::Rect, etc.
    20  *
    21  * Note that some parts of the code that pre-date this class used separate
    22  * scaling factors for the x and y axes. However, at runtime these values
    23  * were always expected to be the same, so this class uses only one scale
    24  * factor for both axes. The two constructors that take two-axis scaling
    25  * factors check to ensure that this assertion holds.
    26  */
    27 template<class src, class dst>
    28 struct ScaleFactor {
    29   float scale;
    31   MOZ_CONSTEXPR ScaleFactor() : scale(1.0) {}
    32   MOZ_CONSTEXPR ScaleFactor(const ScaleFactor<src, dst>& aCopy) : scale(aCopy.scale) {}
    33   explicit MOZ_CONSTEXPR ScaleFactor(float aScale) : scale(aScale) {}
    35   explicit ScaleFactor(float aX, float aY) : scale(aX) {
    36     MOZ_ASSERT(fabs(aX - aY) < 1e-6);
    37   }
    39   ScaleFactor<dst, src> Inverse() {
    40     return ScaleFactor<dst, src>(1 / scale);
    41   }
    43   bool operator==(const ScaleFactor<src, dst>& aOther) const {
    44     return scale == aOther.scale;
    45   }
    47   bool operator!=(const ScaleFactor<src, dst>& aOther) const {
    48     return !(*this == aOther);
    49   }
    51   bool operator<(const ScaleFactor<src, dst>& aOther) const {
    52     return scale < aOther.scale;
    53   }
    55   bool operator<=(const ScaleFactor<src, dst>& aOther) const {
    56     return scale <= aOther.scale;
    57   }
    59   bool operator>(const ScaleFactor<src, dst>& aOther) const {
    60     return scale > aOther.scale;
    61   }
    63   bool operator>=(const ScaleFactor<src, dst>& aOther) const {
    64     return scale >= aOther.scale;
    65   }
    67   template<class other>
    68   ScaleFactor<other, dst> operator/(const ScaleFactor<src, other>& aOther) const {
    69     return ScaleFactor<other, dst>(scale / aOther.scale);
    70   }
    72   template<class other>
    73   ScaleFactor<src, other> operator/(const ScaleFactor<other, dst>& aOther) const {
    74     return ScaleFactor<src, other>(scale / aOther.scale);
    75   }
    77   template<class other>
    78   ScaleFactor<src, other> operator*(const ScaleFactor<dst, other>& aOther) const {
    79     return ScaleFactor<src, other>(scale * aOther.scale);
    80   }
    82   template<class other>
    83   ScaleFactor<other, dst> operator*(const ScaleFactor<other, src>& aOther) const {
    84     return ScaleFactor<other, dst>(scale * aOther.scale);
    85   }
    86 };
    88 }
    89 }
    91 #endif /* MOZILLA_GFX_SCALEFACTOR_H_ */

mercurial