gfx/2d/BaseMargin.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/BaseMargin.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_GFX_BASEMARGIN_H_
    1.10 +#define MOZILLA_GFX_BASEMARGIN_H_
    1.11 +
    1.12 +#include "Types.h"
    1.13 +
    1.14 +namespace mozilla {
    1.15 +namespace gfx {
    1.16 +
    1.17 +/**
    1.18 + * Do not use this class directly. Subclass it, pass that subclass as the
    1.19 + * Sub parameter, and only use that subclass.
    1.20 + */
    1.21 +template <class T, class Sub>
    1.22 +struct BaseMargin {
    1.23 +  typedef mozilla::css::Side SideT;
    1.24 +
    1.25 +  // Do not change the layout of these members; the Side() methods below
    1.26 +  // depend on this order.
    1.27 +  T top, right, bottom, left;
    1.28 +
    1.29 +  // Constructors
    1.30 +  BaseMargin() : top(0), right(0), bottom(0), left(0) {}
    1.31 +  BaseMargin(T aTop, T aRight, T aBottom, T aLeft) :
    1.32 +      top(aTop), right(aRight), bottom(aBottom), left(aLeft) {}
    1.33 +
    1.34 +  void SizeTo(T aTop, T aRight, T aBottom, T aLeft)
    1.35 +  {
    1.36 +    top = aTop; right = aRight; bottom = aBottom; left = aLeft;
    1.37 +  }
    1.38 +
    1.39 +  T LeftRight() const { return left + right; }
    1.40 +  T TopBottom() const { return top + bottom; }
    1.41 +
    1.42 +  T& Side(SideT aSide) {
    1.43 +    // This is ugly!
    1.44 +    return *(&top + T(aSide));
    1.45 +  }
    1.46 +  T Side(SideT aSide) const {
    1.47 +    // This is ugly!
    1.48 +    return *(&top + T(aSide));
    1.49 +  }
    1.50 +
    1.51 +  // Overloaded operators. Note that '=' isn't defined so we'll get the
    1.52 +  // compiler generated default assignment operator
    1.53 +  bool operator==(const Sub& aMargin) const {
    1.54 +    return top == aMargin.top && right == aMargin.right &&
    1.55 +           bottom == aMargin.bottom && left == aMargin.left;
    1.56 +  }
    1.57 +  bool operator!=(const Sub& aMargin) const {
    1.58 +    return !(*this == aMargin);
    1.59 +  }
    1.60 +  Sub operator+(const Sub& aMargin) const {
    1.61 +    return Sub(top + aMargin.top, right + aMargin.right,
    1.62 +               bottom + aMargin.bottom, left + aMargin.left);
    1.63 +  }
    1.64 +  Sub operator-(const Sub& aMargin) const {
    1.65 +    return Sub(top - aMargin.top, right - aMargin.right,
    1.66 +               bottom - aMargin.bottom, left - aMargin.left);
    1.67 +  }
    1.68 +  Sub& operator+=(const Sub& aMargin) {
    1.69 +    top += aMargin.top;
    1.70 +    right += aMargin.right;
    1.71 +    bottom += aMargin.bottom;
    1.72 +    left += aMargin.left;
    1.73 +    return *static_cast<Sub*>(this);
    1.74 +  }
    1.75 +};
    1.76 +
    1.77 +}
    1.78 +}
    1.79 +
    1.80 +#endif /* MOZILLA_GFX_BASEMARGIN_H_ */

mercurial