|
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_BASEMARGIN_H_ |
|
7 #define MOZILLA_GFX_BASEMARGIN_H_ |
|
8 |
|
9 #include "Types.h" |
|
10 |
|
11 namespace mozilla { |
|
12 namespace gfx { |
|
13 |
|
14 /** |
|
15 * Do not use this class directly. Subclass it, pass that subclass as the |
|
16 * Sub parameter, and only use that subclass. |
|
17 */ |
|
18 template <class T, class Sub> |
|
19 struct BaseMargin { |
|
20 typedef mozilla::css::Side SideT; |
|
21 |
|
22 // Do not change the layout of these members; the Side() methods below |
|
23 // depend on this order. |
|
24 T top, right, bottom, left; |
|
25 |
|
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) {} |
|
30 |
|
31 void SizeTo(T aTop, T aRight, T aBottom, T aLeft) |
|
32 { |
|
33 top = aTop; right = aRight; bottom = aBottom; left = aLeft; |
|
34 } |
|
35 |
|
36 T LeftRight() const { return left + right; } |
|
37 T TopBottom() const { return top + bottom; } |
|
38 |
|
39 T& Side(SideT aSide) { |
|
40 // This is ugly! |
|
41 return *(&top + T(aSide)); |
|
42 } |
|
43 T Side(SideT aSide) const { |
|
44 // This is ugly! |
|
45 return *(&top + T(aSide)); |
|
46 } |
|
47 |
|
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; |
|
53 } |
|
54 bool operator!=(const Sub& aMargin) const { |
|
55 return !(*this == aMargin); |
|
56 } |
|
57 Sub operator+(const Sub& aMargin) const { |
|
58 return Sub(top + aMargin.top, right + aMargin.right, |
|
59 bottom + aMargin.bottom, left + aMargin.left); |
|
60 } |
|
61 Sub operator-(const Sub& aMargin) const { |
|
62 return Sub(top - aMargin.top, right - aMargin.right, |
|
63 bottom - aMargin.bottom, left - aMargin.left); |
|
64 } |
|
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); |
|
71 } |
|
72 }; |
|
73 |
|
74 } |
|
75 } |
|
76 |
|
77 #endif /* MOZILLA_GFX_BASEMARGIN_H_ */ |