diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2021-08-14 03:20:01 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2021-08-15 10:18:49 +0200 |
commit | 6c12c659fb22aeab1d1d5d0e8298662e2a602499 (patch) | |
tree | cb2ed47b989a24ec4d7a8944d87520533ae7b186 /include | |
parent | ffaaec23ad94f9bec5fd0fcb02603270eb660b81 (diff) |
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@collabora.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/tools/gen.hxx | 86 |
1 files changed, 29 insertions, 57 deletions
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); } } |