1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Robert O'Callahan <robert@ocallahan.org>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef MOZILLA_GFX_BASEMARGIN_H_
39 : #define MOZILLA_GFX_BASEMARGIN_H_
40 :
41 : #include "Types.h"
42 :
43 : namespace mozilla {
44 : namespace gfx {
45 :
46 : /**
47 : * Do not use this class directly. Subclass it, pass that subclass as the
48 : * Sub parameter, and only use that subclass.
49 : */
50 : template <class T, class Sub>
51 : struct BaseMargin {
52 : typedef mozilla::css::Side SideT;
53 :
54 : // Do not change the layout of these members; the Side() methods below
55 : // depend on this order.
56 : T top, right, bottom, left;
57 :
58 : // Constructors
59 0 : BaseMargin() : top(0), right(0), bottom(0), left(0) {}
60 0 : BaseMargin(T aLeft, T aTop, T aRight, T aBottom) :
61 0 : top(aTop), right(aRight), bottom(aBottom), left(aLeft) {}
62 :
63 0 : void SizeTo(T aLeft, T aTop, T aRight, T aBottom)
64 : {
65 0 : left = aLeft; top = aTop; right = aRight; bottom = aBottom;
66 0 : }
67 :
68 0 : T LeftRight() const { return left + right; }
69 0 : T TopBottom() const { return top + bottom; }
70 :
71 0 : T& Side(SideT aSide) {
72 : // This is ugly!
73 0 : return *(&top + aSide);
74 : }
75 0 : T Side(SideT aSide) const {
76 : // This is ugly!
77 0 : return *(&top + aSide);
78 : }
79 :
80 : // Overloaded operators. Note that '=' isn't defined so we'll get the
81 : // compiler generated default assignment operator
82 0 : bool operator==(const Sub& aMargin) const {
83 : return left == aMargin.left && top == aMargin.top &&
84 0 : right == aMargin.right && bottom == aMargin.bottom;
85 : }
86 0 : bool operator!=(const Sub& aMargin) const {
87 0 : return !(*this == aMargin);
88 : }
89 0 : Sub operator+(const Sub& aMargin) const {
90 : return Sub(left + aMargin.left, top + aMargin.top,
91 0 : right + aMargin.right, bottom + aMargin.bottom);
92 : }
93 0 : Sub operator-(const Sub& aMargin) const {
94 : return Sub(left - aMargin.left, top - aMargin.top,
95 0 : right - aMargin.right, bottom - aMargin.bottom);
96 : }
97 0 : Sub& operator+=(const Sub& aMargin) {
98 0 : left += aMargin.left;
99 0 : top += aMargin.top;
100 0 : right += aMargin.right;
101 0 : bottom += aMargin.bottom;
102 0 : return *static_cast<Sub*>(this);
103 : }
104 : };
105 :
106 : }
107 : }
108 :
109 : #endif /* MOZILLA_GFX_BASEMARGIN_H_ */
|