From d40305001bfd61bef7e118c8580895f68d36b163 Mon Sep 17 00:00:00 2001 From: "danakj@chromium.org" Date: Tue, 23 Oct 2012 16:51:47 +0000 Subject: Make gfx::Rect class operations consistently mutate the class they are called on. Currently some methods mutate the class, and some return a new value, requiring API users to know what kind of method they are calling each time, and making inconsistent code. For example: gfx::Rect rect; rect.Inset(1, 1, 1, 1); rect = rect.Intersect(other_rect); rect.Offset(1, 1); Instead of: gfx::Rect rect; rect.Inset(1, 1, 1, 1); rect.Intersect(other_rect); rect.Offset(1, 1); We could go either way - making the class immutable or all methods return a new instance - but I believe it is better to instead make all methods mutate the class. This allows for shorter lines of code by avoiding having to repeat the object's name twice in order to change it. This patch changes gfx::Rect classes and all the callsites that uses these methods. It should make no change in behaviour, so no new tests added. R=sky BUG=147395 Review URL: https://codereview.chromium.org/11110004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@163579 0039d316-1c4b-4281-b951-d872f2087c98 --- ui/gfx/rect_base_impl.h | 53 ++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 23 deletions(-) (limited to 'ui/gfx/rect_base_impl.h') diff --git a/ui/gfx/rect_base_impl.h b/ui/gfx/rect_base_impl.h index ab71f42..291929d 100644 --- a/ui/gfx/rect_base_impl.h +++ b/ui/gfx/rect_base_impl.h @@ -165,8 +165,13 @@ template -Class RectBase::Intersect( - const Class& rect) const { +void RectBase::Intersect( + const Class& rect) { + if (IsEmpty() || rect.IsEmpty()) { + SetRect(0, 0, 0, 0); + return; + } + Type rx = std::max(x(), rect.x()); Type ry = std::max(y(), rect.y()); Type rr = std::min(right(), rect.right()); @@ -175,7 +180,7 @@ Class RectBase::Intersect( if (rx >= rr || ry >= rb) rx = ry = rr = rb = 0; // non-intersecting - return Class(rx, ry, rr - rx, rb - ry); + SetRect(rx, ry, rr - rx, rb - ry); } template -Class RectBase::Union( - const Class& rect) const { - // special case empty rects... - if (IsEmpty()) - return rect; +void RectBase::Union( + const Class& rect) { + if (IsEmpty()) { + *this = rect; + return; + } if (rect.IsEmpty()) - return *static_cast(this); + return; Type rx = std::min(x(), rect.x()); Type ry = std::min(y(), rect.y()); Type rr = std::max(right(), rect.right()); Type rb = std::max(bottom(), rect.bottom()); - return Class(rx, ry, rr - rx, rb - ry); + SetRect(rx, ry, rr - rx, rb - ry); } template -Class RectBase::Subtract( - const Class& rect) const { - // boundary cases: +void RectBase::Subtract( + const Class& rect) { if (!Intersects(rect)) - return *static_cast(this); - if (rect.Contains(*static_cast(this))) - return Class(); + return; + if (rect.Contains(*static_cast(this))) { + SetRect(0, 0, 0, 0); + return; + } Type rx = x(); Type ry = y(); @@ -232,7 +239,7 @@ Class RectBase::Subtract( rb = rect.y(); } } - return Class(rx, ry, rr - rx, rb - ry); + SetRect(rx, ry, rr - rx, rb - ry); } template -Class RectBase::AdjustToFit( - const Class& rect) const { +void RectBase::AdjustToFit( + const Class& rect) { Type new_x = x(); Type new_y = y(); Type new_width = width(); Type new_height = height(); AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); - return Class(new_x, new_y, new_width, new_height); + SetRect(new_x, new_y, new_width, new_height); } template -Class RectBase::Center( - const SizeClass& size) const { +void RectBase:: + ClampToCenteredSize(const SizeClass& size) { Type new_width = std::min(width(), size.width()); Type new_height = std::min(height(), size.height()); Type new_x = x() + (width() - new_width) / 2; Type new_y = y() + (height() - new_height) / 2; - return Class(new_x, new_y, new_width, new_height); + SetRect(new_x, new_y, new_width, new_height); } template