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"
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 operator T() const { return value
; }
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
; }
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
);
43 friend Sub
operator*(T aScale
, Sub aCoord
) {
44 return Sub(aScale
* aCoord
.value
);
46 friend Sub
operator/(Sub aCoord
, T aScale
) {
47 return Sub(aCoord
.value
/ aScale
);
49 // 'scale / coord' is intentionally omitted because it doesn't make sense.
51 Sub
& operator+=(Sub aCoord
) {
52 value
+= aCoord
.value
;
53 return *static_cast<Sub
*>(this);
55 Sub
& operator-=(Sub aCoord
) {
56 value
-= aCoord
.value
;
57 return *static_cast<Sub
*>(this);
59 Sub
& operator*=(T aScale
) {
61 return *static_cast<Sub
*>(this);
63 Sub
& operator/=(T aScale
) {
65 return *static_cast<Sub
*>(this);
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
; }
81 Sub
operator-() const { return Sub(-value
); }
83 friend std::ostream
& operator<<(std::ostream
& aStream
,
84 const BaseCoord
<T
, Sub
>& aCoord
) {
85 return aStream
<< aCoord
.value
;
90 } // namespace mozilla
92 #endif /* MOZILLA_GFX_BASECOORD_H_ */