michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_GFX_BASESIZE_H_ michael@0: #define MOZILLA_GFX_BASESIZE_H_ michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: /** michael@0: * Do not use this class directly. Subclass it, pass that subclass as the michael@0: * Sub parameter, and only use that subclass. This allows methods to safely michael@0: * cast 'this' to 'Sub*'. michael@0: */ michael@0: template michael@0: struct BaseSize { michael@0: T width, height; michael@0: michael@0: // Constructors michael@0: MOZ_CONSTEXPR BaseSize() : width(0), height(0) {} michael@0: MOZ_CONSTEXPR BaseSize(T aWidth, T aHeight) : width(aWidth), height(aHeight) {} michael@0: michael@0: void SizeTo(T aWidth, T aHeight) { width = aWidth; height = aHeight; } michael@0: michael@0: bool IsEmpty() const { michael@0: return width == 0 || height == 0; michael@0: } michael@0: michael@0: // Note that '=' isn't defined so we'll get the michael@0: // compiler generated default assignment operator michael@0: michael@0: bool operator==(const Sub& aSize) const { michael@0: return width == aSize.width && height == aSize.height; michael@0: } michael@0: bool operator!=(const Sub& aSize) const { michael@0: return width != aSize.width || height != aSize.height; michael@0: } michael@0: bool operator<=(const Sub& aSize) const { michael@0: return width <= aSize.width && height <= aSize.height; michael@0: } michael@0: bool operator<(const Sub& aSize) const { michael@0: return *this <= aSize && *this != aSize; michael@0: } michael@0: michael@0: Sub operator+(const Sub& aSize) const { michael@0: return Sub(width + aSize.width, height + aSize.height); michael@0: } michael@0: Sub operator-(const Sub& aSize) const { michael@0: return Sub(width - aSize.width, height - aSize.height); michael@0: } michael@0: Sub& operator+=(const Sub& aSize) { michael@0: width += aSize.width; michael@0: height += aSize.height; michael@0: return *static_cast(this); michael@0: } michael@0: Sub& operator-=(const Sub& aSize) { michael@0: width -= aSize.width; michael@0: height -= aSize.height; michael@0: return *static_cast(this); michael@0: } michael@0: michael@0: Sub operator*(T aScale) const { michael@0: return Sub(width * aScale, height * aScale); michael@0: } michael@0: Sub operator/(T aScale) const { michael@0: return Sub(width / aScale, height / aScale); michael@0: } michael@0: void Scale(T aXScale, T aYScale) { michael@0: width *= aXScale; michael@0: height *= aYScale; michael@0: } michael@0: michael@0: Sub operator*(const Sub& aSize) const { michael@0: return Sub(width * aSize.width, height * aSize.height); michael@0: } michael@0: Sub operator/(const Sub& aSize) const { michael@0: return Sub(width / aSize.width, height / aSize.height); michael@0: } michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif /* MOZILLA_GFX_BASESIZE_H_ */