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/. */
6 #ifndef MOZILLA_GFX_BASESIZE_H_
7 #define MOZILLA_GFX_BASESIZE_H_
9 #include "mozilla/Attributes.h"
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*'.
19 template <class T
, class Sub
>
24 MOZ_CONSTEXPR
BaseSize() : width(0), height(0) {}
25 MOZ_CONSTEXPR
BaseSize(T aWidth
, T aHeight
) : width(aWidth
), height(aHeight
) {}
27 void SizeTo(T aWidth
, T aHeight
) { width
= aWidth
; height
= aHeight
; }
29 bool IsEmpty() const {
30 return width
== 0 || height
== 0;
33 // Note that '=' isn't defined so we'll get the
34 // compiler generated default assignment operator
36 bool operator==(const Sub
& aSize
) const {
37 return width
== aSize
.width
&& height
== aSize
.height
;
39 bool operator!=(const Sub
& aSize
) const {
40 return width
!= aSize
.width
|| height
!= aSize
.height
;
42 bool operator<=(const Sub
& aSize
) const {
43 return width
<= aSize
.width
&& height
<= aSize
.height
;
45 bool operator<(const Sub
& aSize
) const {
46 return *this <= aSize
&& *this != aSize
;
49 Sub
operator+(const Sub
& aSize
) const {
50 return Sub(width
+ aSize
.width
, height
+ aSize
.height
);
52 Sub
operator-(const Sub
& aSize
) const {
53 return Sub(width
- aSize
.width
, height
- aSize
.height
);
55 Sub
& operator+=(const Sub
& aSize
) {
57 height
+= aSize
.height
;
58 return *static_cast<Sub
*>(this);
60 Sub
& operator-=(const Sub
& aSize
) {
62 height
-= aSize
.height
;
63 return *static_cast<Sub
*>(this);
66 Sub
operator*(T aScale
) const {
67 return Sub(width
* aScale
, height
* aScale
);
69 Sub
operator/(T aScale
) const {
70 return Sub(width
/ aScale
, height
/ aScale
);
72 void Scale(T aXScale
, T aYScale
) {
77 Sub
operator*(const Sub
& aSize
) const {
78 return Sub(width
* aSize
.width
, height
* aSize
.height
);
80 Sub
operator/(const Sub
& aSize
) const {
81 return Sub(width
/ aSize
.width
, height
/ aSize
.height
);
88 #endif /* MOZILLA_GFX_BASESIZE_H_ */