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_
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
>
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
;
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
) {
41 return *(&top
+ aSide
);
43 T
Side(SideT aSide
) const {
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
) {
67 right
+= aMargin
.right
;
68 bottom
+= aMargin
.bottom
;
70 return *static_cast<Sub
*>(this);
77 #endif /* MOZILLA_GFX_BASEMARGIN_H_ */