1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef MOZILLA_GFX_BASECOORD_H_
8 #define MOZILLA_GFX_BASECOORD_H_
9 
10 #include <ostream>
11 
12 #include "mozilla/Attributes.h"
13 
14 namespace mozilla {
15 namespace gfx {
16 
17 /**
18  * Do not use this class directly. Subclass it, pass that subclass as the
19  * Sub parameter, and only use that subclass. This allows methods to safely
20  * cast 'this' to 'Sub*'.
21  */
22 template <class T, class Sub>
23 struct BaseCoord {
24   T value;
25 
26   // Constructors
BaseCoordBaseCoord27   constexpr BaseCoord() : value(0) {}
BaseCoordBaseCoord28   explicit constexpr BaseCoord(T aValue) : value(aValue) {}
29 
30   // Note that '=' isn't defined so we'll get the
31   // compiler generated default assignment operator
32 
TBaseCoord33   operator T() const { return value; }
34 
35   friend bool operator==(Sub aA, Sub aB) { return aA.value == aB.value; }
36   friend bool operator!=(Sub aA, Sub aB) { return aA.value != aB.value; }
37 
38   friend Sub operator+(Sub aA, Sub aB) { return Sub(aA.value + aB.value); }
39   friend Sub operator-(Sub aA, Sub aB) { return Sub(aA.value - aB.value); }
40   friend Sub operator*(Sub aCoord, T aScale) {
41     return Sub(aCoord.value * aScale);
42   }
43   friend Sub operator*(T aScale, Sub aCoord) {
44     return Sub(aScale * aCoord.value);
45   }
46   friend Sub operator/(Sub aCoord, T aScale) {
47     return Sub(aCoord.value / aScale);
48   }
49   // 'scale / coord' is intentionally omitted because it doesn't make sense.
50 
51   Sub& operator+=(Sub aCoord) {
52     value += aCoord.value;
53     return *static_cast<Sub*>(this);
54   }
55   Sub& operator-=(Sub aCoord) {
56     value -= aCoord.value;
57     return *static_cast<Sub*>(this);
58   }
59   Sub& operator*=(T aScale) {
60     value *= aScale;
61     return *static_cast<Sub*>(this);
62   }
63   Sub& operator/=(T aScale) {
64     value /= aScale;
65     return *static_cast<Sub*>(this);
66   }
67 
68   // Since BaseCoord is implicitly convertible to its value type T, we need
69   // mixed-type operator overloads to avoid ambiguities at mixed-type call
70   // sites. As we transition more of our code to strongly-typed classes, we
71   // may be able to remove some or all of these overloads.
72   friend bool operator==(Sub aA, T aB) { return aA.value == aB; }
73   friend bool operator==(T aA, Sub aB) { return aA == aB.value; }
74   friend bool operator!=(Sub aA, T aB) { return aA.value != aB; }
75   friend bool operator!=(T aA, Sub aB) { return aA != aB.value; }
76   friend T operator+(Sub aA, T aB) { return aA.value + aB; }
77   friend T operator+(T aA, Sub aB) { return aA + aB.value; }
78   friend T operator-(Sub aA, T aB) { return aA.value - aB; }
79   friend T operator-(T aA, Sub aB) { return aA - aB.value; }
80 
81   Sub operator-() const { return Sub(-value); }
82 
83   friend std::ostream& operator<<(std::ostream& aStream,
84                                   const BaseCoord<T, Sub>& aCoord) {
85     return aStream << aCoord.value;
86   }
87 };
88 
89 }  // namespace gfx
90 }  // namespace mozilla
91 
92 #endif /* MOZILLA_GFX_BASECOORD_H_ */
93