Constexprify Point arith ops

This commit is contained in:
Dmitry Marakasov 2015-07-07 05:36:05 +03:00
parent d11345b6df
commit 5a55e91987
3 changed files with 50 additions and 55 deletions

View File

@ -25,60 +25,16 @@
namespace SDL2pp {
int Point::GetX() const {
return x;
}
Point& Point::SetX(int nx) {
x = nx;
return *this;
}
int Point::GetY() const {
return y;
}
Point& Point::SetY(int ny) {
y = ny;
return *this;
}
Point Point::operator-() const {
return Point(-x, -y);
}
Point Point::operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
Point Point::operator-(const Point& other) const {
return Point(x - other.x, y - other.y);
}
Point Point::operator/(int value) const {
return Point(x / value, y / value);
}
Point Point::operator/(const Point& other) const {
return Point(x / other.x, y / other.y);
}
Point Point::operator%(int value) const {
return Point(x % value, y % value);
}
Point Point::operator%(const Point& other) const {
return Point(x % other.x, y % other.y);
}
Point Point::operator*(int value) const {
return Point(x * value, y * value);
}
Point Point::operator*(const Point& other) const {
return Point(x * other.x, y * other.y);
}
Point& Point::operator+=(const Point& other) {
x += other.x;
y += other.y;

View File

@ -133,7 +133,9 @@ public:
/// \returns X coordinate of the point
///
////////////////////////////////////////////////////////////
int GetX() const;
constexpr int GetX() const {
return x;
}
////////////////////////////////////////////////////////////
/// \brief Set X coordinate of the point
@ -151,7 +153,9 @@ public:
/// \returns Y coordinate of the point
///
////////////////////////////////////////////////////////////
int GetY() const;
constexpr int GetY() const {
return y;
}
////////////////////////////////////////////////////////////
/// \brief Set Y coordinate of the point
@ -169,7 +173,9 @@ public:
/// \returns New Point representing memberwise negation
///
////////////////////////////////////////////////////////////
Point operator-() const;
constexpr Point operator-() const {
return Point(-x, -y);
}
////////////////////////////////////////////////////////////
/// \brief Get point's memberwise addition with another point
@ -179,7 +185,9 @@ public:
/// \returns New Point representing memberwise addition with another point
///
////////////////////////////////////////////////////////////
Point operator+(const Point& other) const;
constexpr Point operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
////////////////////////////////////////////////////////////
/// \brief Get point's memberwise subtraction with another point
@ -189,7 +197,9 @@ public:
/// \returns New Point representing memberwise subtraction of another point
///
////////////////////////////////////////////////////////////
Point operator-(const Point& other) const;
constexpr Point operator-(const Point& other) const {
return Point(x - other.x, y - other.y);
}
////////////////////////////////////////////////////////////
/// \brief Get point's memberwise division by an integer
@ -200,7 +210,9 @@ public:
/// point by an integer
///
////////////////////////////////////////////////////////////
Point operator/(int value) const;
constexpr Point operator/(int value) const {
return Point(x / value, y / value);
}
////////////////////////////////////////////////////////////
/// \brief Get point's memberwise division by another point
@ -211,7 +223,9 @@ public:
/// point by another point
///
////////////////////////////////////////////////////////////
Point operator/(const Point& other) const;
constexpr Point operator/(const Point& other) const {
return Point(x / other.x, y / other.y);
}
////////////////////////////////////////////////////////////
/// \brief Get point's memberwise remainder from division
@ -223,7 +237,9 @@ public:
/// from division by an integer
///
////////////////////////////////////////////////////////////
Point operator%(int value) const;
constexpr Point operator%(int value) const {
return Point(x % value, y % value);
}
////////////////////////////////////////////////////////////
/// \brief Get point's memberwise remainder from division
@ -235,7 +251,9 @@ public:
/// from division by another point
///
////////////////////////////////////////////////////////////
Point operator%(const Point& other) const;
constexpr Point operator%(const Point& other) const {
return Point(x % other.x, y % other.y);
}
////////////////////////////////////////////////////////////
/// \brief Get point's memberwise multiplication by an
@ -247,7 +265,9 @@ public:
/// of point by an integer
///
////////////////////////////////////////////////////////////
Point operator*(int value) const;
constexpr Point operator*(int value) const {
return Point(x * value, y * value);
}
////////////////////////////////////////////////////////////
/// \brief Get point's memberwise multiplication by anoter
@ -259,7 +279,9 @@ public:
/// of point by another point
///
////////////////////////////////////////////////////////////
Point operator*(const Point& other) const;
constexpr Point operator*(const Point& other) const {
return Point(x * other.x, y * other.y);
}
////////////////////////////////////////////////////////////
/// \brief Memberwise add another point

View File

@ -20,5 +20,22 @@ BEGIN_TEST()
EXPECT_TRUE(b1);
EXPECT_TRUE(b2);
constexpr int x = p1.GetX();
constexpr int y = p1.GetY();
EXPECT_TRUE(x == 0 && y == 0);
constexpr Point neg = -p1;
constexpr Point sum = p1 + p2;
constexpr Point diff = p1 - p2;
constexpr Point mul1 = p1 * p2;
constexpr Point mul2 = p1 * 2;
constexpr Point div1 = p1 / p2;
constexpr Point div2 = p1 * 2;
constexpr Point rem1 = p1 % p2;
constexpr Point rem2 = p1 % 2;
EXPECT_EQUAL(neg + sum + diff + mul1 + mul2 + div1 + div2 + rem1 + rem2, Point(0, 0));
}
END_TEST()