From beaa9ed3b82b3c30d58eb3bfdebff3c10d5e9ee5 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Tue, 7 Jul 2015 16:27:03 +0300 Subject: [PATCH] Constexprify more Rect methods --- SDL2pp/Rect.cc | 71 ------------------------------ SDL2pp/Rect.hh | 72 ++++++++++++++++++++++++------- tests/test_pointrect_constexpr.cc | 22 ++++++++++ 3 files changed, 78 insertions(+), 87 deletions(-) diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index f628247..2b65068 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -27,15 +27,6 @@ namespace SDL2pp { -bool Rect::operator==(const Rect& other) const { - return x == other.x && y == other.y && - w == other.w && h == other.h; -} - -bool Rect::operator!=(const Rect& other) const { - return !(*this == other); -} - Rect Rect::FromCenter(int cx, int cy, int w, int h) { return Rect(cx - w/2, cy - h/2, w, h); } @@ -52,60 +43,6 @@ Rect Rect::FromCorners(const Point& p1, const Point& p2) { return Rect(p1, p2 - p1 + Point(1, 1)); } -int Rect::GetX() const { - return x; -} - -Rect& Rect::SetX(int nx) { - x = nx; - return *this; -} - -int Rect::GetY() const { - return y; -} - -Rect& Rect::SetY(int ny) { - y = ny; - return *this; -} - -int Rect::GetW() const { - return w; -} - -Rect& Rect::SetW(int nw) { - w = nw; - return *this; -} - -int Rect::GetH() const { - return h; -} - -Rect& Rect::SetH(int nh) { - h = nh; - return *this; -} - -int Rect::GetX2() const { - return x + w - 1; -} - -Rect& Rect::SetX2(int x2) { - w = x2 - x + 1; - return *this; -} - -int Rect::GetY2() const { - return y + h - 1; -} - -Rect& Rect::SetY2(int y2) { - h = y2 - y + 1; - return *this; -} - bool Rect::Contains(int px, int py) const { return px >= x && py >= y && px <= GetX2() && py <= GetY2(); } @@ -177,10 +114,6 @@ bool Rect::IntersectLine(Point& p1, Point& p2) const { return SDL_IntersectRectAndLine(this, &p1.x, &p1.y, &p2.x, &p2.y) == SDL_TRUE; } -Rect Rect::operator+(const Point& offset) const { - return Rect(x + offset.x, y + offset.y, w, h); -} - Rect& Rect::operator+=(const Point& offset) { x += offset.x; y += offset.y; @@ -188,10 +121,6 @@ Rect& Rect::operator+=(const Point& offset) { return *this; } -Rect Rect::operator-(const Point& offset) const { - return Rect(x - offset.x, y - offset.y, w, h); -} - Rect& Rect::operator-=(const Point& offset) { x -= offset.x; y -= offset.y; diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index 2f66395..dbb676d 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -162,7 +162,10 @@ public: /// \returns True if two rectangles are identical /// //////////////////////////////////////////////////////////// - bool operator==(const Rect& other) const; + constexpr bool operator==(const Rect& other) const { + return x == other.x && y == other.y && + w == other.w && h == other.h; + } //////////////////////////////////////////////////////////// /// \brief Inequality operator @@ -172,7 +175,10 @@ public: /// \returns True if two rectangles are not identical /// //////////////////////////////////////////////////////////// - bool operator!=(const Rect& other) const; + constexpr bool operator!=(const Rect& other) const { + return x != other.x || y != other.y || + w != other.w || h != other.h; + } //////////////////////////////////////////////////////////// /// \brief Get X coordinate of the rect corner @@ -180,7 +186,9 @@ public: /// \returns X coordinate of the rect corner /// //////////////////////////////////////////////////////////// - int GetX() const; + constexpr int GetX() const { + return x; + } //////////////////////////////////////////////////////////// /// \brief Set X coordinate of the rect corner @@ -190,7 +198,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetX(int nx); + Rect& SetX(int nx) { + x = nx; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get Y coordinate of the rect corner @@ -198,7 +209,9 @@ public: /// \returns Y coordinate of the rect corner /// //////////////////////////////////////////////////////////// - int GetY() const; + constexpr int GetY() const { + return y; + } //////////////////////////////////////////////////////////// /// \brief Set Y coordinate of the rect corner @@ -208,7 +221,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetY(int ny); + Rect& SetY(int ny) { + y = ny; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get width of the rect @@ -216,7 +232,9 @@ public: /// \returns Width of the rect /// //////////////////////////////////////////////////////////// - int GetW() const; + constexpr int GetW() const { + return w; + } //////////////////////////////////////////////////////////// /// \brief Set width of the rect @@ -226,7 +244,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetW(int nw); + Rect& SetW(int nw) { + w = nw; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get height of the rect @@ -234,7 +255,9 @@ public: /// \returns Height of the rect /// //////////////////////////////////////////////////////////// - int GetH() const; + constexpr int GetH() const { + return h; + } //////////////////////////////////////////////////////////// /// \brief Set height of the rect @@ -244,7 +267,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetH(int nh); + Rect& SetH(int nh) { + h = nh; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get X coordinate of the rect second corner @@ -252,7 +278,9 @@ public: /// \returns X coordinate of the rect second corner /// //////////////////////////////////////////////////////////// - int GetX2() const; + constexpr int GetX2() const { + return x + w - 1; + } //////////////////////////////////////////////////////////// /// \brief Set X coordinate of the rect second corner @@ -262,7 +290,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetX2(int x2); + Rect& SetX2(int x2) { + w = x2 - x + 1; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Get Y coordinate of the rect second corner @@ -272,7 +303,9 @@ public: /// This modifies rectangle width internally /// //////////////////////////////////////////////////////////// - int GetY2() const; + constexpr int GetY2() const { + return y + h - 1; + } //////////////////////////////////////////////////////////// /// \brief Set Y coordinate of the rect second corner @@ -284,7 +317,10 @@ public: /// \returns Reference to self /// //////////////////////////////////////////////////////////// - Rect& SetY2(int y2); + Rect& SetY2(int y2) { + h = y2 - y + 1; + return *this; + } //////////////////////////////////////////////////////////// /// \brief Check whether the rect contains given point @@ -424,7 +460,9 @@ public: /// \returns Moved rectangle /// //////////////////////////////////////////////////////////// - Rect operator+(const Point& offset) const; + constexpr Rect operator+(const Point& offset) const { + return Rect(x + offset.x, y + offset.y, w, h); + } //////////////////////////////////////////////////////////// /// \brief Get rectangle moved by an opposite of given offset @@ -434,7 +472,9 @@ public: /// \returns Moved rectangle /// //////////////////////////////////////////////////////////// - Rect operator-(const Point& offset) const; + constexpr Rect operator-(const Point& offset) const { + return Rect(x - offset.x, y - offset.y, w, h); + } //////////////////////////////////////////////////////////// /// \brief Move by then given offset diff --git a/tests/test_pointrect_constexpr.cc b/tests/test_pointrect_constexpr.cc index af08362..0b6d02d 100644 --- a/tests/test_pointrect_constexpr.cc +++ b/tests/test_pointrect_constexpr.cc @@ -49,5 +49,27 @@ BEGIN_TEST() EXPECT_EQUAL(r4, r2); EXPECT_EQUAL(r5, r3); + + constexpr bool b1 = r2 == r3; + constexpr bool b2 = r1 != r3; + + EXPECT_TRUE(b1); + EXPECT_TRUE(b2); + + constexpr int x = r5.GetX(); + constexpr int y = r5.GetY(); + constexpr int w = r5.GetW(); + constexpr int h = r5.GetH(); + constexpr int x2 = r5.GetX2(); + constexpr int y2 = r5.GetY2(); + + EXPECT_TRUE(x == y); + EXPECT_TRUE(w == h); + EXPECT_TRUE(x2 == y2); + + constexpr Rect add = r1 + Point(1, 1); + constexpr Rect sub = r1 - Point(1, 1); + + EXPECT_EQUAL(add - Point(2, 2), sub); } END_TEST()