From d11345b6df57d3d92dde42b2189d66bbcf1caef0 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Tue, 7 Jul 2015 05:17:09 +0300 Subject: [PATCH] Initial Point constexpr support: constructors and comparison operators --- SDL2pp/Point.cc | 23 ----------------------- SDL2pp/Point.hh | 21 ++++++++++++++------- tests/CMakeLists.txt | 1 + tests/test_pointrect_constexpr.cc | 24 ++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 tests/test_pointrect_constexpr.cc diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index d64136c..db131ed 100644 --- a/SDL2pp/Point.cc +++ b/SDL2pp/Point.cc @@ -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; } diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index 23b4165..f969d8a 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 103ec2e..1bf0232 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,7 @@ # simple command-line tests SET(CLI_TESTS test_pointrect + test_pointrect_constexpr test_rwops test_optional test_error diff --git a/tests/test_pointrect_constexpr.cc b/tests/test_pointrect_constexpr.cc new file mode 100644 index 0000000..8c71efc --- /dev/null +++ b/tests/test_pointrect_constexpr.cc @@ -0,0 +1,24 @@ +#include +#include + +#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()