1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/2d/BaseSize.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef MOZILLA_GFX_BASESIZE_H_ 1.10 +#define MOZILLA_GFX_BASESIZE_H_ 1.11 + 1.12 +#include "mozilla/Attributes.h" 1.13 + 1.14 +namespace mozilla { 1.15 +namespace gfx { 1.16 + 1.17 +/** 1.18 + * Do not use this class directly. Subclass it, pass that subclass as the 1.19 + * Sub parameter, and only use that subclass. This allows methods to safely 1.20 + * cast 'this' to 'Sub*'. 1.21 + */ 1.22 +template <class T, class Sub> 1.23 +struct BaseSize { 1.24 + T width, height; 1.25 + 1.26 + // Constructors 1.27 + MOZ_CONSTEXPR BaseSize() : width(0), height(0) {} 1.28 + MOZ_CONSTEXPR BaseSize(T aWidth, T aHeight) : width(aWidth), height(aHeight) {} 1.29 + 1.30 + void SizeTo(T aWidth, T aHeight) { width = aWidth; height = aHeight; } 1.31 + 1.32 + bool IsEmpty() const { 1.33 + return width == 0 || height == 0; 1.34 + } 1.35 + 1.36 + // Note that '=' isn't defined so we'll get the 1.37 + // compiler generated default assignment operator 1.38 + 1.39 + bool operator==(const Sub& aSize) const { 1.40 + return width == aSize.width && height == aSize.height; 1.41 + } 1.42 + bool operator!=(const Sub& aSize) const { 1.43 + return width != aSize.width || height != aSize.height; 1.44 + } 1.45 + bool operator<=(const Sub& aSize) const { 1.46 + return width <= aSize.width && height <= aSize.height; 1.47 + } 1.48 + bool operator<(const Sub& aSize) const { 1.49 + return *this <= aSize && *this != aSize; 1.50 + } 1.51 + 1.52 + Sub operator+(const Sub& aSize) const { 1.53 + return Sub(width + aSize.width, height + aSize.height); 1.54 + } 1.55 + Sub operator-(const Sub& aSize) const { 1.56 + return Sub(width - aSize.width, height - aSize.height); 1.57 + } 1.58 + Sub& operator+=(const Sub& aSize) { 1.59 + width += aSize.width; 1.60 + height += aSize.height; 1.61 + return *static_cast<Sub*>(this); 1.62 + } 1.63 + Sub& operator-=(const Sub& aSize) { 1.64 + width -= aSize.width; 1.65 + height -= aSize.height; 1.66 + return *static_cast<Sub*>(this); 1.67 + } 1.68 + 1.69 + Sub operator*(T aScale) const { 1.70 + return Sub(width * aScale, height * aScale); 1.71 + } 1.72 + Sub operator/(T aScale) const { 1.73 + return Sub(width / aScale, height / aScale); 1.74 + } 1.75 + void Scale(T aXScale, T aYScale) { 1.76 + width *= aXScale; 1.77 + height *= aYScale; 1.78 + } 1.79 + 1.80 + Sub operator*(const Sub& aSize) const { 1.81 + return Sub(width * aSize.width, height * aSize.height); 1.82 + } 1.83 + Sub operator/(const Sub& aSize) const { 1.84 + return Sub(width / aSize.width, height / aSize.height); 1.85 + } 1.86 +}; 1.87 + 1.88 +} 1.89 +} 1.90 + 1.91 +#endif /* MOZILLA_GFX_BASESIZE_H_ */