|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef MOZILLA_GFX_BASESIZE_H_ |
|
7 #define MOZILLA_GFX_BASESIZE_H_ |
|
8 |
|
9 #include "mozilla/Attributes.h" |
|
10 |
|
11 namespace mozilla { |
|
12 namespace gfx { |
|
13 |
|
14 /** |
|
15 * Do not use this class directly. Subclass it, pass that subclass as the |
|
16 * Sub parameter, and only use that subclass. This allows methods to safely |
|
17 * cast 'this' to 'Sub*'. |
|
18 */ |
|
19 template <class T, class Sub> |
|
20 struct BaseSize { |
|
21 T width, height; |
|
22 |
|
23 // Constructors |
|
24 MOZ_CONSTEXPR BaseSize() : width(0), height(0) {} |
|
25 MOZ_CONSTEXPR BaseSize(T aWidth, T aHeight) : width(aWidth), height(aHeight) {} |
|
26 |
|
27 void SizeTo(T aWidth, T aHeight) { width = aWidth; height = aHeight; } |
|
28 |
|
29 bool IsEmpty() const { |
|
30 return width == 0 || height == 0; |
|
31 } |
|
32 |
|
33 // Note that '=' isn't defined so we'll get the |
|
34 // compiler generated default assignment operator |
|
35 |
|
36 bool operator==(const Sub& aSize) const { |
|
37 return width == aSize.width && height == aSize.height; |
|
38 } |
|
39 bool operator!=(const Sub& aSize) const { |
|
40 return width != aSize.width || height != aSize.height; |
|
41 } |
|
42 bool operator<=(const Sub& aSize) const { |
|
43 return width <= aSize.width && height <= aSize.height; |
|
44 } |
|
45 bool operator<(const Sub& aSize) const { |
|
46 return *this <= aSize && *this != aSize; |
|
47 } |
|
48 |
|
49 Sub operator+(const Sub& aSize) const { |
|
50 return Sub(width + aSize.width, height + aSize.height); |
|
51 } |
|
52 Sub operator-(const Sub& aSize) const { |
|
53 return Sub(width - aSize.width, height - aSize.height); |
|
54 } |
|
55 Sub& operator+=(const Sub& aSize) { |
|
56 width += aSize.width; |
|
57 height += aSize.height; |
|
58 return *static_cast<Sub*>(this); |
|
59 } |
|
60 Sub& operator-=(const Sub& aSize) { |
|
61 width -= aSize.width; |
|
62 height -= aSize.height; |
|
63 return *static_cast<Sub*>(this); |
|
64 } |
|
65 |
|
66 Sub operator*(T aScale) const { |
|
67 return Sub(width * aScale, height * aScale); |
|
68 } |
|
69 Sub operator/(T aScale) const { |
|
70 return Sub(width / aScale, height / aScale); |
|
71 } |
|
72 void Scale(T aXScale, T aYScale) { |
|
73 width *= aXScale; |
|
74 height *= aYScale; |
|
75 } |
|
76 |
|
77 Sub operator*(const Sub& aSize) const { |
|
78 return Sub(width * aSize.width, height * aSize.height); |
|
79 } |
|
80 Sub operator/(const Sub& aSize) const { |
|
81 return Sub(width / aSize.width, height / aSize.height); |
|
82 } |
|
83 }; |
|
84 |
|
85 } |
|
86 } |
|
87 |
|
88 #endif /* MOZILLA_GFX_BASESIZE_H_ */ |