Line data Source code
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 <algorithm>
10 : #include <ostream>
11 :
12 : #include "mozilla/Attributes.h"
13 :
14 : namespace mozilla {
15 : namespace gfx {
16 :
17 : /**
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*'.
21 : */
22 : template <class T, class Sub>
23 : struct BaseSize {
24 : union {
25 : struct {
26 : T width, height;
27 : };
28 : T components[2];
29 : };
30 :
31 : // Constructors
32 6132 : constexpr BaseSize() : width(0), height(0) {}
33 69936 : constexpr BaseSize(T aWidth, T aHeight) : width(aWidth), height(aHeight) {}
34 :
35 240 : void SizeTo(T aWidth, T aHeight) { width = aWidth; height = aHeight; }
36 :
37 2040 : bool IsEmpty() const {
38 2040 : return width <= 0 || height <= 0;
39 : }
40 :
41 1 : bool IsSquare() const {
42 1 : return width == height;
43 : }
44 :
45 : // Note that '=' isn't defined so we'll get the
46 : // compiler generated default assignment operator
47 :
48 2007 : bool operator==(const Sub& aSize) const {
49 2007 : return width == aSize.width && height == aSize.height;
50 : }
51 3114 : bool operator!=(const Sub& aSize) const {
52 3114 : return width != aSize.width || height != aSize.height;
53 : }
54 9 : bool operator<=(const Sub& aSize) const {
55 9 : return width <= aSize.width && height <= aSize.height;
56 : }
57 9 : bool operator<(const Sub& aSize) const {
58 9 : return *this <= aSize && *this != aSize;
59 : }
60 :
61 87 : Sub operator+(const Sub& aSize) const {
62 87 : return Sub(width + aSize.width, height + aSize.height);
63 : }
64 251 : Sub operator-(const Sub& aSize) const {
65 251 : return Sub(width - aSize.width, height - aSize.height);
66 : }
67 0 : Sub& operator+=(const Sub& aSize) {
68 0 : width += aSize.width;
69 0 : height += aSize.height;
70 0 : return *static_cast<Sub*>(this);
71 : }
72 0 : Sub& operator-=(const Sub& aSize) {
73 0 : width -= aSize.width;
74 0 : height -= aSize.height;
75 0 : return *static_cast<Sub*>(this);
76 : }
77 :
78 82 : Sub operator*(T aScale) const {
79 82 : return Sub(width * aScale, height * aScale);
80 : }
81 9 : Sub operator/(T aScale) const {
82 9 : return Sub(width / aScale, height / aScale);
83 : }
84 90 : friend Sub operator*(T aScale, const Sub& aSize) {
85 90 : return Sub(aScale * aSize.width, aScale * aSize.height);
86 : }
87 44 : void Scale(T aXScale, T aYScale) {
88 44 : width *= aXScale;
89 44 : height *= aYScale;
90 44 : }
91 :
92 22 : Sub operator*(const Sub& aSize) const {
93 22 : return Sub(width * aSize.width, height * aSize.height);
94 : }
95 : Sub operator/(const Sub& aSize) const {
96 : return Sub(width / aSize.width, height / aSize.height);
97 : }
98 :
99 0 : friend Sub Min(const Sub& aA, const Sub& aB) {
100 0 : return Sub(std::min(aA.width, aB.width),
101 0 : std::min(aA.height, aB.height));
102 : }
103 :
104 : friend Sub Max(const Sub& aA, const Sub& aB) {
105 : return Sub(std::max(aA.width, aB.width),
106 : std::max(aA.height, aB.height));
107 : }
108 :
109 : friend std::ostream& operator<<(std::ostream& aStream,
110 : const BaseSize<T, Sub>& aSize) {
111 : return aStream << aSize.width << " x " << aSize.height;
112 : }
113 : };
114 :
115 : } // namespace gfx
116 : } // namespace mozilla
117 :
118 : #endif /* MOZILLA_GFX_BASESIZE_H_ */
|