/src/mozilla-central/gfx/2d/BaseMargin.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef MOZILLA_GFX_BASEMARGIN_H_ |
8 | | #define MOZILLA_GFX_BASEMARGIN_H_ |
9 | | |
10 | | #include <ostream> |
11 | | |
12 | | #include "Types.h" |
13 | | |
14 | | namespace mozilla { |
15 | | |
16 | | /** |
17 | | * Sides represents a set of physical sides. |
18 | | */ |
19 | | struct Sides final { |
20 | | Sides() : mBits(0) {} |
21 | | explicit Sides(SideBits aSideBits) |
22 | | { |
23 | | MOZ_ASSERT((aSideBits & ~eSideBitsAll) == 0, "illegal side bits"); |
24 | | mBits = aSideBits; |
25 | | } |
26 | | bool IsEmpty() const { return mBits == 0; } |
27 | | bool Top() const { return (mBits & eSideBitsTop) != 0; } |
28 | | bool Right() const { return (mBits & eSideBitsRight) != 0; } |
29 | | bool Bottom() const { return (mBits & eSideBitsBottom) != 0; } |
30 | | bool Left() const { return (mBits & eSideBitsLeft) != 0; } |
31 | | bool Contains(SideBits aSideBits) const |
32 | | { |
33 | | MOZ_ASSERT((aSideBits & ~eSideBitsAll) == 0, "illegal side bits"); |
34 | | return (mBits & aSideBits) == aSideBits; |
35 | | } |
36 | | Sides operator|(Sides aOther) const |
37 | | { |
38 | | return Sides(SideBits(mBits | aOther.mBits)); |
39 | | } |
40 | | Sides operator|(SideBits aSideBits) const |
41 | | { |
42 | | return *this | Sides(aSideBits); |
43 | | } |
44 | | Sides& operator|=(Sides aOther) |
45 | | { |
46 | | mBits |= aOther.mBits; |
47 | | return *this; |
48 | | } |
49 | | Sides& operator|=(SideBits aSideBits) |
50 | | { |
51 | | return *this |= Sides(aSideBits); |
52 | | } |
53 | | bool operator==(Sides aOther) const |
54 | | { |
55 | | return mBits == aOther.mBits; |
56 | | } |
57 | | bool operator!=(Sides aOther) const |
58 | | { |
59 | | return !(*this == aOther); |
60 | | } |
61 | | |
62 | | private: |
63 | | uint8_t mBits; |
64 | | }; |
65 | | |
66 | | namespace gfx { |
67 | | |
68 | | /** |
69 | | * Do not use this class directly. Subclass it, pass that subclass as the |
70 | | * Sub parameter, and only use that subclass. |
71 | | */ |
72 | | template <class T, class Sub> |
73 | | struct BaseMargin { |
74 | | typedef mozilla::Side SideT; // because we have a method named Side |
75 | | |
76 | | // Do not change the layout of these members; the Side() methods below |
77 | | // depend on this order. |
78 | | T top, right, bottom, left; |
79 | | |
80 | | // Constructors |
81 | | BaseMargin() : top(0), right(0), bottom(0), left(0) {} |
82 | | BaseMargin(T aTop, T aRight, T aBottom, T aLeft) : |
83 | 0 | top(aTop), right(aRight), bottom(aBottom), left(aLeft) {} |
84 | | |
85 | | void SizeTo(T aTop, T aRight, T aBottom, T aLeft) |
86 | | { |
87 | | top = aTop; right = aRight; bottom = aBottom; left = aLeft; |
88 | | } |
89 | | |
90 | 0 | T LeftRight() const { return left + right; } Unexecuted instantiation: mozilla::gfx::BaseMargin<float, mozilla::gfx::MarginTyped<mozilla::gfx::UnknownUnits, float> >::LeftRight() const Unexecuted instantiation: mozilla::gfx::BaseMargin<double, mozilla::gfx::MarginTyped<mozilla::gfx::UnknownUnits, double> >::LeftRight() const |
91 | 0 | T TopBottom() const { return top + bottom; } Unexecuted instantiation: mozilla::gfx::BaseMargin<float, mozilla::gfx::MarginTyped<mozilla::gfx::UnknownUnits, float> >::TopBottom() const Unexecuted instantiation: mozilla::gfx::BaseMargin<double, mozilla::gfx::MarginTyped<mozilla::gfx::UnknownUnits, double> >::TopBottom() const |
92 | | |
93 | | T& Side(SideT aSide) { |
94 | | // This is ugly! |
95 | | return *(&top + int(aSide)); |
96 | | } |
97 | | T Side(SideT aSide) const { |
98 | | // This is ugly! |
99 | | return *(&top + int(aSide)); |
100 | | } |
101 | | |
102 | | void ApplySkipSides(Sides aSkipSides) |
103 | | { |
104 | | if (aSkipSides.Top()) { |
105 | | top = 0; |
106 | | } |
107 | | if (aSkipSides.Right()) { |
108 | | right = 0; |
109 | | } |
110 | | if (aSkipSides.Bottom()) { |
111 | | bottom = 0; |
112 | | } |
113 | | if (aSkipSides.Left()) { |
114 | | left = 0; |
115 | | } |
116 | | } |
117 | | |
118 | | // Overloaded operators. Note that '=' isn't defined so we'll get the |
119 | | // compiler generated default assignment operator |
120 | | bool operator==(const Sub& aMargin) const { |
121 | | return top == aMargin.top && right == aMargin.right && |
122 | | bottom == aMargin.bottom && left == aMargin.left; |
123 | | } |
124 | | bool operator!=(const Sub& aMargin) const { |
125 | | return !(*this == aMargin); |
126 | | } |
127 | | Sub operator+(const Sub& aMargin) const { |
128 | | return Sub(top + aMargin.top, right + aMargin.right, |
129 | | bottom + aMargin.bottom, left + aMargin.left); |
130 | | } |
131 | | Sub operator-(const Sub& aMargin) const { |
132 | | return Sub(top - aMargin.top, right - aMargin.right, |
133 | | bottom - aMargin.bottom, left - aMargin.left); |
134 | | } |
135 | | Sub& operator+=(const Sub& aMargin) { |
136 | | top += aMargin.top; |
137 | | right += aMargin.right; |
138 | | bottom += aMargin.bottom; |
139 | | left += aMargin.left; |
140 | | return *static_cast<Sub*>(this); |
141 | | } |
142 | | |
143 | | friend std::ostream& operator<<(std::ostream& aStream, |
144 | | const BaseMargin& aMargin) { |
145 | | return aStream << '(' << aMargin.top << ',' << aMargin.right << ',' |
146 | | << aMargin.bottom << ',' << aMargin.left << ')'; |
147 | | } |
148 | | }; |
149 | | |
150 | | } // namespace gfx |
151 | | } // namespace mozilla |
152 | | |
153 | | #endif /* MOZILLA_GFX_BASEMARGIN_H_ */ |