[Libreoffice-commits] core.git: include/tools tools/source
Mike Kaganski (via logerrit)
logerrit at kemper.freedesktop.org
Sun Aug 15 08:19:23 UTC 2021
include/tools/gen.hxx | 86 ++++++++++++++-----------------------------
tools/source/generic/gen.cxx | 54 +++++++++------------------
2 files changed, 47 insertions(+), 93 deletions(-)
New commits:
commit 6c12c659fb22aeab1d1d5d0e8298662e2a602499
Author: Mike Kaganski <mike.kaganski at collabora.com>
AuthorDate: Sat Aug 14 03:20:01 2021 +0300
Commit: Mike Kaganski <mike.kaganski at collabora.com>
CommitDate: Sun Aug 15 10:18:49 2021 +0200
Simplify tools::Rectangle a bit
1. Simplify/delegate ctors
2. Simplify getWidth/getHeight
3. Simplify expand
4. Simplify operators += / -= / + / -
Change-Id: I023aa1bb2905394fbbd29adc7c544d629f9ae2d6
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/120476
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski at collabora.com>
diff --git a/include/tools/gen.hxx b/include/tools/gen.hxx
index b8bd6eeb35d4..3c97728c7402 100644
--- a/include/tools/gen.hxx
+++ b/include/tools/gen.hxx
@@ -464,7 +464,7 @@ class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Rectangle final
{
static constexpr short RECT_EMPTY = -32767;
public:
- constexpr Rectangle();
+ constexpr Rectangle() = default;
constexpr Rectangle( const Point& rLT, const Point& rRB );
constexpr Rectangle( tools::Long nLeft, tools::Long nTop,
tools::Long nRight, tools::Long nBottom );
@@ -475,14 +475,14 @@ public:
static Rectangle Justify( const Point& rLT, const Point& rRB );
constexpr tools::Long Left() const { return nLeft; }
- constexpr tools::Long Right() const { return nRight == RECT_EMPTY ? nLeft : nRight; }
+ constexpr tools::Long Right() const { return IsWidthEmpty() ? nLeft : nRight; }
constexpr tools::Long Top() const { return nTop; }
- constexpr tools::Long Bottom() const { return nBottom == RECT_EMPTY ? nTop : nBottom; }
+ constexpr tools::Long Bottom() const { return IsHeightEmpty() ? nTop : nBottom; }
- void SetLeft(tools::Long v) { nLeft = v; }
- void SetRight(tools::Long v) { nRight = v; }
- void SetTop(tools::Long v) { nTop = v; }
- void SetBottom(tools::Long v) { nBottom = v; }
+ constexpr void SetLeft(tools::Long v) { nLeft = v; }
+ constexpr void SetRight(tools::Long v) { nRight = v; }
+ constexpr void SetTop(tools::Long v) { nTop = v; }
+ constexpr void SetBottom(tools::Long v) { nBottom = v; }
constexpr Point TopLeft() const { return { Left(), Top() }; }
constexpr Point TopRight() const { return { Right(), Top() }; }
@@ -511,7 +511,7 @@ public:
{
tools::Long n = 0;
- if (nRight != RECT_EMPTY)
+ if (!IsWidthEmpty())
{
n = nRight - nLeft;
if (n < 0)
@@ -528,7 +528,7 @@ public:
{
tools::Long n = 0;
- if (nBottom != RECT_EMPTY)
+ if (!IsHeightEmpty())
{
n = nBottom - nTop;
if (n < 0)
@@ -554,9 +554,9 @@ public:
void SetEmpty() { nRight = nBottom = RECT_EMPTY; }
void SetWidthEmpty() { nRight = RECT_EMPTY; }
void SetHeightEmpty() { nBottom = RECT_EMPTY; }
- constexpr bool IsEmpty() const { return (nRight == RECT_EMPTY) || (nBottom == RECT_EMPTY); }
- bool IsWidthEmpty() const { return nRight == RECT_EMPTY; }
- bool IsHeightEmpty() const { return nBottom == RECT_EMPTY; }
+ constexpr bool IsEmpty() const { return IsWidthEmpty() || IsHeightEmpty(); }
+ constexpr bool IsWidthEmpty() const { return nRight == RECT_EMPTY; }
+ constexpr bool IsHeightEmpty() const { return nBottom == RECT_EMPTY; }
inline bool operator == ( const tools::Rectangle& rRect ) const;
inline bool operator != ( const tools::Rectangle& rRect ) const;
@@ -571,9 +571,9 @@ public:
tools::Long getX() const { return nLeft; }
tools::Long getY() const { return nTop; }
/// Returns the difference between right and left, assuming the range includes one end, but not the other.
- tools::Long getWidth() const;
+ tools::Long getWidth() const { return Right() - Left(); }
/// Returns the difference between bottom and top, assuming the range includes one end, but not the other.
- tools::Long getHeight() const;
+ tools::Long getHeight() const { return Bottom() - Top(); }
/// Set the left edge of the rectangle to x, preserving the width
void setX( tools::Long x );
/// Set the top edge of the rectangle to y, preserving the height
@@ -597,25 +597,15 @@ public:
void SaturatingSetY(tools::Long y);
private:
- tools::Long nLeft;
- tools::Long nTop;
- tools::Long nRight;
- tools::Long nBottom;
+ tools::Long nLeft = 0;
+ tools::Long nTop = 0;
+ tools::Long nRight = RECT_EMPTY;
+ tools::Long nBottom = RECT_EMPTY;
};
}
-constexpr inline tools::Rectangle::Rectangle()
- : nLeft( 0 )
- , nTop( 0 )
- , nRight( RECT_EMPTY )
- , nBottom( RECT_EMPTY )
-{}
-
constexpr inline tools::Rectangle::Rectangle( const Point& rLT, const Point& rRB )
- : nLeft( rLT.X())
- , nTop( rLT.Y())
- , nRight( rRB.X())
- , nBottom( rRB.Y())
+ : Rectangle(rLT.X(), rLT.Y(), rRB.X(), rRB.Y())
{}
constexpr inline tools::Rectangle::Rectangle( tools::Long _nLeft, tools::Long _nTop,
@@ -627,10 +617,8 @@ constexpr inline tools::Rectangle::Rectangle( tools::Long _nLeft, tools::Long _
{}
constexpr inline tools::Rectangle::Rectangle( tools::Long _nLeft, tools::Long _nTop )
- : nLeft( _nLeft )
- , nTop( _nTop )
- , nRight( RECT_EMPTY )
- , nBottom( RECT_EMPTY )
+ : nLeft(_nLeft)
+ , nTop(_nTop)
{}
constexpr inline tools::Rectangle::Rectangle( const Point& rLT, const Size& rSize )
@@ -644,17 +632,17 @@ inline void tools::Rectangle::Move( tools::Long nHorzMove, tools::Long nVertMove
{
nLeft += nHorzMove;
nTop += nVertMove;
- if ( nRight != RECT_EMPTY )
+ if (!IsWidthEmpty())
nRight += nHorzMove;
- if ( nBottom != RECT_EMPTY )
+ if (!IsHeightEmpty())
nBottom += nVertMove;
}
inline void tools::Rectangle::SetPos( const Point& rPoint )
{
- if ( nRight != RECT_EMPTY )
+ if (!IsWidthEmpty())
nRight += rPoint.X() - nLeft;
- if ( nBottom != RECT_EMPTY )
+ if (!IsHeightEmpty())
nBottom += rPoint.Y() - nTop;
nLeft = rPoint.X();
nTop = rPoint.Y();
@@ -690,23 +678,13 @@ inline bool tools::Rectangle::operator != ( const tools::Rectangle& rRect ) cons
inline tools::Rectangle& tools::Rectangle::operator +=( const Point& rPt )
{
- nLeft += rPt.X();
- nTop += rPt.Y();
- if ( nRight != RECT_EMPTY )
- nRight += rPt.X();
- if ( nBottom != RECT_EMPTY )
- nBottom += rPt.Y();
+ Move(rPt.X(), rPt.Y());
return *this;
}
inline tools::Rectangle& tools::Rectangle::operator -= ( const Point& rPt )
{
- nLeft -= rPt.X();
- nTop -= rPt.Y();
- if ( nRight != RECT_EMPTY )
- nRight -= rPt.X();
- if ( nBottom != RECT_EMPTY )
- nBottom -= rPt.Y();
+ Move(-rPt.X(), -rPt.Y());
return *this;
}
@@ -714,18 +692,12 @@ namespace tools
{
inline Rectangle operator + ( const Rectangle& rRect, const Point& rPt )
{
- return rRect.IsEmpty()
- ? Rectangle( rRect.nLeft + rPt.X(), rRect.nTop + rPt.Y() )
- : Rectangle( rRect.nLeft + rPt.X(), rRect.nTop + rPt.Y(),
- rRect.nRight + rPt.X(), rRect.nBottom + rPt.Y() );
+ return Rectangle{ rRect }.operator+=(rPt);
}
inline Rectangle operator - ( const Rectangle& rRect, const Point& rPt )
{
- return rRect.IsEmpty()
- ? Rectangle( rRect.nLeft - rPt.X(), rRect.nTop - rPt.Y() )
- : Rectangle( rRect.nLeft - rPt.X(), rRect.nTop - rPt.Y(),
- rRect.nRight - rPt.X(), rRect.nBottom - rPt.Y() );
+ return Rectangle{ rRect }.operator-=(rPt);
}
}
diff --git a/tools/source/generic/gen.cxx b/tools/source/generic/gen.cxx
index 2edb63c465d9..5196ef0bd96e 100644
--- a/tools/source/generic/gen.cxx
+++ b/tools/source/generic/gen.cxx
@@ -51,14 +51,14 @@ void tools::Rectangle::SetSize( const Size& rSize )
else if ( rSize.Width() > 0 )
nRight = nLeft + rSize.Width() -1;
else
- nRight = RECT_EMPTY;
+ SetWidthEmpty();
if ( rSize.Height() < 0 )
nBottom = nTop + rSize.Height() +1;
else if ( rSize.Height() > 0 )
nBottom = nTop + rSize.Height() -1;
else
- nBottom = RECT_EMPTY;
+ SetHeightEmpty();
}
void tools::Rectangle::SaturatingSetSize(const Size& rSize)
@@ -68,26 +68,26 @@ void tools::Rectangle::SaturatingSetSize(const Size& rSize)
else if ( rSize.Width() > 0 )
nRight = o3tl::saturating_add(nLeft, (rSize.Width() - 1));
else
- nRight = RECT_EMPTY;
+ SetWidthEmpty();
if ( rSize.Height() < 0 )
nBottom = o3tl::saturating_add(nTop, (rSize.Height() + 1));
else if ( rSize.Height() > 0 )
nBottom = o3tl::saturating_add(nTop, (rSize.Height() - 1));
else
- nBottom = RECT_EMPTY;
+ SetHeightEmpty();
}
void tools::Rectangle::SaturatingSetX(tools::Long x)
{
- if (nRight != RECT_EMPTY)
+ if (!IsWidthEmpty())
nRight = o3tl::saturating_add(nRight, x - nLeft);
nLeft = x;
}
void tools::Rectangle::SaturatingSetY(tools::Long y)
{
- if (nBottom != RECT_EMPTY)
+ if (!IsHeightEmpty())
nBottom = o3tl::saturating_add(nBottom, y - nTop);
nTop = y;
}
@@ -138,12 +138,12 @@ tools::Rectangle& tools::Rectangle::Intersection( const tools::Rectangle& rRect
void tools::Rectangle::Justify()
{
- if ( (nRight < nLeft) && (nRight != RECT_EMPTY) )
+ if ((nRight < nLeft) && (!IsWidthEmpty()))
{
std::swap(nLeft, nRight);
}
- if ( (nBottom < nTop) && (nBottom != RECT_EMPTY) )
+ if ((nBottom < nTop) && (!IsHeightEmpty()))
{
std::swap(nBottom, nTop);
}
@@ -198,31 +198,25 @@ OString tools::Rectangle::toString() const
void tools::Rectangle::expand(tools::Long nExpandBy)
{
- nLeft -= nExpandBy;
- nTop -= nExpandBy;
- if (nRight == RECT_EMPTY)
- nRight = nLeft + nExpandBy - 1;
- else
- nRight += nExpandBy;
- if (nBottom == RECT_EMPTY)
- nBottom = nTop + nExpandBy - 1;
- else
- nBottom += nExpandBy;
+ AdjustLeft(-nExpandBy);
+ AdjustTop(-nExpandBy);
+ AdjustRight(nExpandBy);
+ AdjustBottom(nExpandBy);
}
void tools::Rectangle::shrink(tools::Long nShrinkBy)
{
nLeft += nShrinkBy;
nTop += nShrinkBy;
- if (nRight != RECT_EMPTY)
+ if (!IsWidthEmpty())
nRight -= nShrinkBy;
- if (nBottom != RECT_EMPTY)
+ if (!IsHeightEmpty())
nBottom -= nShrinkBy;
}
tools::Long tools::Rectangle::AdjustRight(tools::Long nHorzMoveDelta)
{
- if (nRight == RECT_EMPTY)
+ if (IsWidthEmpty())
nRight = nLeft + nHorzMoveDelta - 1;
else
nRight += nHorzMoveDelta;
@@ -231,7 +225,7 @@ tools::Long tools::Rectangle::AdjustRight(tools::Long nHorzMoveDelta)
tools::Long tools::Rectangle::AdjustBottom( tools::Long nVertMoveDelta )
{
- if (nBottom == RECT_EMPTY)
+ if (IsHeightEmpty())
nBottom = nTop + nVertMoveDelta - 1;
else
nBottom += nVertMoveDelta;
@@ -240,28 +234,16 @@ tools::Long tools::Rectangle::AdjustBottom( tools::Long nVertMoveDelta )
void tools::Rectangle::setX( tools::Long x )
{
- if (nRight != RECT_EMPTY)
+ if (!IsWidthEmpty())
nRight += x - nLeft;
nLeft = x;
}
void tools::Rectangle::setY( tools::Long y )
{
- if (nBottom != RECT_EMPTY)
+ if (!IsHeightEmpty())
nBottom += y - nTop;
nTop = y;
}
-/// Returns the difference between right and left, assuming the range includes one end, but not the other.
-tools::Long tools::Rectangle::getWidth() const
-{
- return nRight == RECT_EMPTY ? 0 : nRight - nLeft;
-}
-
-/// Returns the difference between bottom and top, assuming the range includes one end, but not the other.
-tools::Long tools::Rectangle::getHeight() const
-{
- return nBottom == RECT_EMPTY ? 0 : nBottom - nTop;
-}
-
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
More information about the Libreoffice-commits
mailing list