BaseCoord.h (3408B)
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/MathAlgorithms.h" 13 14 namespace mozilla::gfx { 15 16 /** 17 * Do not use this class directly. Subclass it, pass that subclass as the 18 * Sub parameter, and only use that subclass. This allows methods to safely 19 * cast 'this' to 'Sub*'. 20 */ 21 template <class T, class Sub> 22 struct BaseCoord { 23 T value; 24 25 // Constructors 26 constexpr BaseCoord() : value(0) {} 27 explicit constexpr BaseCoord(T aValue) : value(aValue) {} 28 29 // Note that '=' isn't defined so we'll get the 30 // compiler generated default assignment operator 31 32 friend constexpr Sub Abs(BaseCoord aCoord) { return Abs(aCoord.value); } 33 34 constexpr operator T() const { return value; } 35 36 friend constexpr bool operator==(Sub aA, Sub aB) { 37 return aA.value == aB.value; 38 } 39 friend constexpr bool operator!=(Sub aA, Sub aB) { 40 return aA.value != aB.value; 41 } 42 43 friend constexpr Sub operator+(Sub aA, Sub aB) { 44 return Sub(aA.value + aB.value); 45 } 46 friend constexpr Sub operator-(Sub aA, Sub aB) { 47 return Sub(aA.value - aB.value); 48 } 49 friend constexpr Sub operator*(Sub aCoord, T aScale) { 50 return Sub(aCoord.value * aScale); 51 } 52 friend constexpr Sub operator*(T aScale, Sub aCoord) { 53 return Sub(aScale * aCoord.value); 54 } 55 friend constexpr Sub operator/(Sub aCoord, T aScale) { 56 return Sub(aCoord.value / aScale); 57 } 58 // 'scale / coord' is intentionally omitted because it doesn't make sense. 59 60 constexpr Sub& operator+=(Sub aCoord) { 61 value += aCoord.value; 62 return *static_cast<Sub*>(this); 63 } 64 constexpr Sub& operator-=(Sub aCoord) { 65 value -= aCoord.value; 66 return *static_cast<Sub*>(this); 67 } 68 constexpr Sub& operator*=(T aScale) { 69 value *= aScale; 70 return *static_cast<Sub*>(this); 71 } 72 constexpr Sub& operator/=(T aScale) { 73 value /= aScale; 74 return *static_cast<Sub*>(this); 75 } 76 77 // Since BaseCoord is implicitly convertible to its value type T, we need 78 // mixed-type operator overloads to avoid ambiguities at mixed-type call 79 // sites. As we transition more of our code to strongly-typed classes, we 80 // may be able to remove some or all of these overloads. 81 friend constexpr bool operator==(Sub aA, T aB) { return aA.value == aB; } 82 friend constexpr bool operator==(T aA, Sub aB) { return aA == aB.value; } 83 friend constexpr bool operator!=(Sub aA, T aB) { return aA.value != aB; } 84 friend constexpr bool operator!=(T aA, Sub aB) { return aA != aB.value; } 85 friend constexpr T operator+(Sub aA, T aB) { return aA.value + aB; } 86 friend constexpr T operator+(T aA, Sub aB) { return aA + aB.value; } 87 friend constexpr T operator-(Sub aA, T aB) { return aA.value - aB; } 88 friend constexpr T operator-(T aA, Sub aB) { return aA - aB.value; } 89 90 constexpr Sub operator-() const { return Sub(-value); } 91 92 friend std::ostream& operator<<(std::ostream& aStream, 93 const BaseCoord<T, Sub>& aCoord) { 94 return aStream << aCoord.value; 95 } 96 }; 97 98 } // namespace mozilla::gfx 99 100 #endif /* MOZILLA_GFX_BASECOORD_H_ */