Merge m-c to inbound.
[gecko.git] / gfx / 2d / BaseMargin.h
blobe7203b0afb7b312ec7418f611429d48352e5e659
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_BASEMARGIN_H_
7 #define MOZILLA_GFX_BASEMARGIN_H_
9 #include "Types.h"
11 namespace mozilla {
12 namespace gfx {
14 /**
15 * Do not use this class directly. Subclass it, pass that subclass as the
16 * Sub parameter, and only use that subclass.
18 template <class T, class Sub>
19 struct BaseMargin {
20 typedef mozilla::css::Side SideT;
22 // Do not change the layout of these members; the Side() methods below
23 // depend on this order.
24 T top, right, bottom, left;
26 // Constructors
27 BaseMargin() : top(0), right(0), bottom(0), left(0) {}
28 BaseMargin(T aTop, T aRight, T aBottom, T aLeft) :
29 top(aTop), right(aRight), bottom(aBottom), left(aLeft) {}
31 void SizeTo(T aTop, T aRight, T aBottom, T aLeft)
33 top = aTop; right = aRight; bottom = aBottom; left = aLeft;
36 T LeftRight() const { return left + right; }
37 T TopBottom() const { return top + bottom; }
39 T& Side(SideT aSide) {
40 // This is ugly!
41 return *(&top + aSide);
43 T Side(SideT aSide) const {
44 // This is ugly!
45 return *(&top + aSide);
48 // Overloaded operators. Note that '=' isn't defined so we'll get the
49 // compiler generated default assignment operator
50 bool operator==(const Sub& aMargin) const {
51 return top == aMargin.top && right == aMargin.right &&
52 bottom == aMargin.bottom && left == aMargin.left;
54 bool operator!=(const Sub& aMargin) const {
55 return !(*this == aMargin);
57 Sub operator+(const Sub& aMargin) const {
58 return Sub(top + aMargin.top, right + aMargin.right,
59 bottom + aMargin.bottom, left + aMargin.left);
61 Sub operator-(const Sub& aMargin) const {
62 return Sub(top - aMargin.top, right - aMargin.right,
63 bottom - aMargin.bottom, left - aMargin.left);
65 Sub& operator+=(const Sub& aMargin) {
66 top += aMargin.top;
67 right += aMargin.right;
68 bottom += aMargin.bottom;
69 left += aMargin.left;
70 return *static_cast<Sub*>(this);
77 #endif /* MOZILLA_GFX_BASEMARGIN_H_ */