gfx/2d/BasePoint.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/BasePoint.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,83 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_GFX_BASEPOINT_H_
    1.10 +#define MOZILLA_GFX_BASEPOINT_H_
    1.11 +
    1.12 +#include <cmath>
    1.13 +#include "mozilla/Attributes.h"
    1.14 +
    1.15 +namespace mozilla {
    1.16 +namespace gfx {
    1.17 +
    1.18 +/**
    1.19 + * Do not use this class directly. Subclass it, pass that subclass as the
    1.20 + * Sub parameter, and only use that subclass. This allows methods to safely
    1.21 + * cast 'this' to 'Sub*'.
    1.22 + */
    1.23 +template <class T, class Sub>
    1.24 +struct BasePoint {
    1.25 +  T x, y;
    1.26 +
    1.27 +  // Constructors
    1.28 +  MOZ_CONSTEXPR BasePoint() : x(0), y(0) {}
    1.29 +  MOZ_CONSTEXPR BasePoint(T aX, T aY) : x(aX), y(aY) {}
    1.30 +
    1.31 +  void MoveTo(T aX, T aY) { x = aX; y = aY; }
    1.32 +  void MoveBy(T aDx, T aDy) { x += aDx; y += aDy; }
    1.33 +
    1.34 +  // Note that '=' isn't defined so we'll get the
    1.35 +  // compiler generated default assignment operator
    1.36 +
    1.37 +  bool operator==(const Sub& aPoint) const {
    1.38 +    return x == aPoint.x && y == aPoint.y;
    1.39 +  }
    1.40 +  bool operator!=(const Sub& aPoint) const {
    1.41 +    return x != aPoint.x || y != aPoint.y;
    1.42 +  }
    1.43 +
    1.44 +  Sub operator+(const Sub& aPoint) const {
    1.45 +    return Sub(x + aPoint.x, y + aPoint.y);
    1.46 +  }
    1.47 +  Sub operator-(const Sub& aPoint) const {
    1.48 +    return Sub(x - aPoint.x, y - aPoint.y);
    1.49 +  }
    1.50 +  Sub& operator+=(const Sub& aPoint) {
    1.51 +    x += aPoint.x;
    1.52 +    y += aPoint.y;
    1.53 +    return *static_cast<Sub*>(this);
    1.54 +  }
    1.55 +  Sub& operator-=(const Sub& aPoint) {
    1.56 +    x -= aPoint.x;
    1.57 +    y -= aPoint.y;
    1.58 +    return *static_cast<Sub*>(this);
    1.59 +  }
    1.60 +
    1.61 +  Sub operator*(T aScale) const {
    1.62 +    return Sub(x * aScale, y * aScale);
    1.63 +  }
    1.64 +  Sub operator/(T aScale) const {
    1.65 +    return Sub(x / aScale, y / aScale);
    1.66 +  }
    1.67 +
    1.68 +  Sub operator-() const {
    1.69 +    return Sub(-x, -y);
    1.70 +  }
    1.71 +
    1.72 +  // Round() is *not* rounding to nearest integer if the values are negative.
    1.73 +  // They are always rounding as floor(n + 0.5).
    1.74 +  // See https://bugzilla.mozilla.org/show_bug.cgi?id=410748#c14
    1.75 +  Sub& Round() {
    1.76 +    x = static_cast<T>(floor(x + 0.5));
    1.77 +    y = static_cast<T>(floor(y + 0.5));
    1.78 +    return *static_cast<Sub*>(this);
    1.79 +  }
    1.80 +
    1.81 +};
    1.82 +
    1.83 +}
    1.84 +}
    1.85 +
    1.86 +#endif /* MOZILLA_GFX_BASEPOINT_H_ */

mercurial