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/. */
7 #ifndef MOZILLA_GFX_BASECOORD_H_
8 #define MOZILLA_GFX_BASECOORD_H_
12 #include "mozilla/Attributes.h"
13 #include "mozilla/MathAlgorithms.h"
15 namespace mozilla::gfx
{
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*'.
22 template <class T
, class Sub
>
27 constexpr BaseCoord() : value(0) {}
28 explicit constexpr BaseCoord(T aValue
) : value(aValue
) {}
30 // Note that '=' isn't defined so we'll get the
31 // compiler generated default assignment operator
33 friend constexpr Sub
Abs(BaseCoord aCoord
) { return Abs(aCoord
.value
); }
35 constexpr operator T() const { return value
; }
37 friend constexpr bool operator==(Sub aA
, Sub aB
) {
38 return aA
.value
== aB
.value
;
40 friend constexpr bool operator!=(Sub aA
, Sub aB
) {
41 return aA
.value
!= aB
.value
;
44 friend constexpr Sub
operator+(Sub aA
, Sub aB
) {
45 return Sub(aA
.value
+ aB
.value
);
47 friend constexpr Sub
operator-(Sub aA
, Sub aB
) {
48 return Sub(aA
.value
- aB
.value
);
50 friend constexpr Sub
operator*(Sub aCoord
, T aScale
) {
51 return Sub(aCoord
.value
* aScale
);
53 friend constexpr Sub
operator*(T aScale
, Sub aCoord
) {
54 return Sub(aScale
* aCoord
.value
);
56 friend constexpr Sub
operator/(Sub aCoord
, T aScale
) {
57 return Sub(aCoord
.value
/ aScale
);
59 // 'scale / coord' is intentionally omitted because it doesn't make sense.
61 constexpr Sub
& operator+=(Sub aCoord
) {
62 value
+= aCoord
.value
;
63 return *static_cast<Sub
*>(this);
65 constexpr Sub
& operator-=(Sub aCoord
) {
66 value
-= aCoord
.value
;
67 return *static_cast<Sub
*>(this);
69 constexpr Sub
& operator*=(T aScale
) {
71 return *static_cast<Sub
*>(this);
73 constexpr Sub
& operator/=(T aScale
) {
75 return *static_cast<Sub
*>(this);
78 // Since BaseCoord is implicitly convertible to its value type T, we need
79 // mixed-type operator overloads to avoid ambiguities at mixed-type call
80 // sites. As we transition more of our code to strongly-typed classes, we
81 // may be able to remove some or all of these overloads.
82 friend constexpr bool operator==(Sub aA
, T aB
) { return aA
.value
== aB
; }
83 friend constexpr bool operator==(T aA
, Sub aB
) { return aA
== aB
.value
; }
84 friend constexpr bool operator!=(Sub aA
, T aB
) { return aA
.value
!= aB
; }
85 friend constexpr bool operator!=(T aA
, Sub aB
) { return aA
!= aB
.value
; }
86 friend constexpr T
operator+(Sub aA
, T aB
) { return aA
.value
+ aB
; }
87 friend constexpr T
operator+(T aA
, Sub aB
) { return aA
+ aB
.value
; }
88 friend constexpr T
operator-(Sub aA
, T aB
) { return aA
.value
- aB
; }
89 friend constexpr T
operator-(T aA
, Sub aB
) { return aA
- aB
.value
; }
91 constexpr Sub
operator-() const { return Sub(-value
); }
93 friend std::ostream
& operator<<(std::ostream
& aStream
,
94 const BaseCoord
<T
, Sub
>& aCoord
) {
95 return aStream
<< aCoord
.value
;
99 } // namespace mozilla::gfx
101 #endif /* MOZILLA_GFX_BASECOORD_H_ */