Implement stream output operators for Point and Rect

This commit is contained in:
Dmitry Marakasov 2015-01-20 02:56:34 +03:00
parent fa7ba94957
commit fdcbcddc1d
5 changed files with 40 additions and 0 deletions

View File

@ -149,3 +149,8 @@ Point& Point::Wrap(const Rect& rect) {
}
}
std::ostream& operator<<(std::ostream& stream, const SDL2pp::Point& point) {
stream << "[x:" << point.x << ",y:" << point.y << "]";
return stream;
}

View File

@ -22,6 +22,8 @@
#ifndef SDL2PP_POINT_HH
#define SDL2PP_POINT_HH
#include <iostream>
#include <SDL2/SDL_rect.h>
namespace SDL2pp {
@ -277,4 +279,15 @@ public:
}
////////////////////////////////////////////////////////////
/// \brief Stream output operator overload for SDL2pp::Point
///
/// \param[in] stream Stream to output to
/// \param[in] point Point to output
///
/// \returns stream
///
////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream& stream, const SDL2pp::Point& point);
#endif

View File

@ -228,3 +228,8 @@ Rect& Rect::operator-=(const Point& offset) {
}
}
std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect) {
stream << "[x:" << rect.x << ",y:" << rect.y << ",w:" << rect.w << ",h:" << rect.h << "]";
return stream;
}

View File

@ -458,4 +458,15 @@ public:
}
////////////////////////////////////////////////////////////
/// \brief Stream output operator overload for SDL2pp::Rect
///
/// \param[in] stream Stream to output to
/// \param[in] rect Rect to output
///
/// \returns stream
///
////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect);
#endif

View File

@ -333,4 +333,10 @@ BEGIN_TEST()
EXPECT_TRUE(Point(-19, -19).GetWrapped(rect) == Point(11, 21));
EXPECT_TRUE(Point(-21, -21).GetWrapped(rect) == Point(39, 59));
}
{
// streams
EXPECT_EQUAL((std::stringstream() << Point(1, 2)).str(), "[x:1,y:2]");
EXPECT_EQUAL((std::stringstream() << Rect(1, 2, 3, 4)).str(), "[x:1,y:2,w:3,h:4]");
}
END_TEST()