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