Initial Point constexpr support: constructors and comparison operators

This commit is contained in:
Dmitry Marakasov 2015-07-07 05:17:09 +03:00
parent 6b80bc2c3c
commit d11345b6df
4 changed files with 39 additions and 30 deletions

View File

@ -25,29 +25,6 @@
namespace SDL2pp {
Point::Point() {
x = 0;
y = 0;
}
Point::Point(const SDL_Point& point) {
x = point.x;
y = point.y;
}
Point::Point(int nx, int ny) {
x = nx;
y = ny;
}
bool Point::operator==(const Point& other) const {
return x == other.x && y == other.y;
}
bool Point::operator!=(const Point& other) const {
return !(*this == other);
}
int Point::GetX() const {
return x;
}

View File

@ -53,7 +53,8 @@ public:
/// Creates a Point(0, 0)
///
////////////////////////////////////////////////////////////
Point();
constexpr Point() : SDL_Point{0, 0} {
}
////////////////////////////////////////////////////////////
/// \brief Construct a point from existing SDL_Point
@ -61,16 +62,18 @@ public:
/// \param[in] point Existing SDL_Point
///
////////////////////////////////////////////////////////////
Point(const SDL_Point& point);
constexpr Point(const SDL_Point& point) : SDL_Point{point.x, point.y} {
}
////////////////////////////////////////////////////////////
/// \brief Construct the point from given coordinates
///
/// \param[in] nx X coordinate
/// \param[in] ny Y coordinate
/// \param[in] x X coordinate
/// \param[in] y Y coordinate
///
////////////////////////////////////////////////////////////
Point(int nx, int ny);
constexpr Point(int x, int y) : SDL_Point{x, y} {
}
////////////////////////////////////////////////////////////
/// \brief Copy constructor
@ -108,7 +111,9 @@ public:
/// \returns True if two points are identical
///
////////////////////////////////////////////////////////////
bool operator==(const Point& other) const;
constexpr bool operator==(const Point& other) const {
return x == other.x && y == other.y;
}
////////////////////////////////////////////////////////////
/// \brief Inequality operator
@ -118,7 +123,9 @@ public:
/// \returns True if two points are not identical
///
////////////////////////////////////////////////////////////
bool operator!=(const Point& other) const;
constexpr bool operator!=(const Point& other) const {
return x != other.x || y != other.y;
}
////////////////////////////////////////////////////////////
/// \brief Get X coordinate of the point

View File

@ -1,6 +1,7 @@
# simple command-line tests
SET(CLI_TESTS
test_pointrect
test_pointrect_constexpr
test_rwops
test_optional
test_error

View File

@ -0,0 +1,24 @@
#include <SDL2pp/Point.hh>
#include <SDL2pp/Rect.hh>
#include "testing.h"
using namespace SDL2pp;
BEGIN_TEST()
// note that this is merely a compilation test; EXPECT_s are mainly
// used to silence `unused variable' warnings
{
constexpr SDL_Point sp{1, 2};
constexpr Point p1;
constexpr Point p2(sp);
constexpr Point p3(1, 2);
constexpr Point p4(p2);
constexpr bool b1 = p2 == p4;
constexpr bool b2 = p1 != p2;
EXPECT_TRUE(b1);
EXPECT_TRUE(b2);
}
END_TEST()