Bug 1028460 - part 1, Implement a mozilla::Sides type that represents a set of sides that can be used for the GetSkipSides() result among other things. r=roc

This commit is contained in:
Mats Palmgren 2014-06-28 10:13:13 +00:00
parent 16fc4405c3
commit 6eab5af9a1
2 changed files with 75 additions and 16 deletions

View File

@ -9,6 +9,57 @@
#include "Types.h"
namespace mozilla {
/**
* Sides represents a set of physical sides.
*/
struct Sides MOZ_FINAL {
Sides() : mBits(0) {}
explicit Sides(SideBits aSideBits)
{
MOZ_ASSERT((aSideBits & ~eSideBitsAll) == 0, "illegal side bits");
mBits = aSideBits;
}
bool IsEmpty() const { return mBits == 0; }
bool Top() const { return mBits & eSideBitsTop; }
bool Right() const { return mBits & eSideBitsRight; }
bool Bottom() const { return mBits & eSideBitsBottom; }
bool Left() const { return mBits & eSideBitsLeft; }
bool Contains(SideBits aSideBits) const
{
MOZ_ASSERT((aSideBits & ~eSideBitsAll) == 0, "illegal side bits");
return (mBits & aSideBits) == aSideBits;
}
Sides operator|(Sides aOther) const
{
return Sides(SideBits(mBits | aOther.mBits));
}
Sides operator|(SideBits aSideBits) const
{
return *this | Sides(aSideBits);
}
Sides& operator|=(Sides aOther)
{
mBits |= aOther.mBits;
return *this;
}
Sides& operator|=(SideBits aSideBits)
{
return *this |= Sides(aSideBits);
}
bool operator==(Sides aOther) const
{
return mBits == aOther.mBits;
}
bool operator!=(Sides aOther) const
{
return !(*this == aOther);
}
private:
uint8_t mBits;
};
namespace gfx {
/**
@ -17,7 +68,7 @@ namespace gfx {
*/
template <class T, class Sub>
struct BaseMargin {
typedef mozilla::css::Side SideT;
typedef mozilla::Side SideT; // because we have a method named Side
// Do not change the layout of these members; the Side() methods below
// depend on this order.
@ -45,18 +96,18 @@ struct BaseMargin {
return *(&top + T(aSide));
}
void ApplySkipSides(int aSkipSides)
void ApplySkipSides(Sides aSkipSides)
{
if (aSkipSides & (1 << mozilla::css::eSideTop)) {
if (aSkipSides.Top()) {
top = 0;
}
if (aSkipSides & (1 << mozilla::css::eSideRight)) {
if (aSkipSides.Right()) {
right = 0;
}
if (aSkipSides & (1 << mozilla::css::eSideBottom)) {
if (aSkipSides.Bottom()) {
bottom = 0;
}
if (aSkipSides & (1 << mozilla::css::eSideLeft)) {
if (aSkipSides.Left()) {
left = 0;
}
}

View File

@ -264,17 +264,25 @@ struct GradientStop
#define GFX2D_API
#endif
// Side constants for use in various places
namespace mozilla {
namespace css {
enum Side {eSideTop, eSideRight, eSideBottom, eSideLeft};
}
}
// Side constants for use in various places.
enum Side { eSideTop, eSideRight, eSideBottom, eSideLeft };
// XXX - These don't really belong here. But for now this is where they go.
#define NS_SIDE_TOP mozilla::css::eSideTop
#define NS_SIDE_RIGHT mozilla::css::eSideRight
#define NS_SIDE_BOTTOM mozilla::css::eSideBottom
#define NS_SIDE_LEFT mozilla::css::eSideLeft
enum SideBits {
eSideBitsNone = 0,
eSideBitsTop = 1 << eSideTop,
eSideBitsRight = 1 << eSideRight,
eSideBitsBottom = 1 << eSideBottom,
eSideBitsLeft = 1 << eSideLeft,
eSideBitsTopBottom = eSideBitsTop | eSideBitsBottom,
eSideBitsLeftRight = eSideBitsLeft | eSideBitsRight,
eSideBitsAll = eSideBitsTopBottom | eSideBitsLeftRight
};
} // namespace mozilla
#define NS_SIDE_TOP mozilla::eSideTop
#define NS_SIDE_RIGHT mozilla::eSideRight
#define NS_SIDE_BOTTOM mozilla::eSideBottom
#define NS_SIDE_LEFT mozilla::eSideLeft
#endif /* MOZILLA_GFX_TYPES_H_ */