Add unary operator+ to QPoint and QPointF.

As requested by Winfried Schenke:

"QPoint should have an unary operator+ (the unary operator- exists).
Classes with arithmetic operators should provide a complete set of
operators, because some template code relies on it."

Task-number: QTBUG-22913
Change-Id: Ib0c5105975f56c15f00bb48d83c8d911f5a204ac
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Mitch Curtis 2012-07-06 15:12:41 +02:00 committed by Qt by Nokia
parent 74b0206a71
commit 623bfe2093
4 changed files with 56 additions and 0 deletions

View File

@ -325,6 +325,14 @@ QT_BEGIN_NAMESPACE
\sa QPoint::operator*=()
*/
/*!
\fn const QPoint operator+(const QPoint &point)
\relates QPoint
\since 5.0
Returns \a point unmodified.
*/
/*!
\fn const QPoint operator-(const QPoint &point)
\overload
@ -663,6 +671,14 @@ QDebug operator<<(QDebug d, const QPointF &p)
Returns a copy of the given \a point, multiplied by the given \a factor.
*/
/*!
\fn const QPointF operator+(const QPointF &point)
\relates QPointF
\since 5.0
Returns \a point unmodified.
*/
/*!
\fn const QPointF operator-(const QPointF &point)
\relates QPointF

View File

@ -86,6 +86,7 @@ public:
friend Q_DECL_CONSTEXPR inline const QPoint operator*(double, const QPoint &);
friend Q_DECL_CONSTEXPR inline const QPoint operator*(const QPoint &, int);
friend Q_DECL_CONSTEXPR inline const QPoint operator*(int, const QPoint &);
friend Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &);
friend Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &);
friend Q_DECL_CONSTEXPR inline const QPoint operator/(const QPoint &, qreal);
@ -182,6 +183,9 @@ Q_DECL_CONSTEXPR inline const QPoint operator*(double factor, const QPoint &p)
Q_DECL_CONSTEXPR inline const QPoint operator*(int factor, const QPoint &p)
{ return QPoint(p.xp*factor, p.yp*factor); }
Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &p)
{ return p; }
Q_DECL_CONSTEXPR inline const QPoint operator-(const QPoint &p)
{ return QPoint(-p.xp, -p.yp); }
@ -235,6 +239,7 @@ public:
friend Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &, const QPointF &);
friend Q_DECL_CONSTEXPR inline const QPointF operator*(qreal, const QPointF &);
friend Q_DECL_CONSTEXPR inline const QPointF operator*(const QPointF &, qreal);
friend Q_DECL_CONSTEXPR inline const QPointF operator+(const QPointF &);
friend Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &);
friend Q_DECL_CONSTEXPR inline const QPointF operator/(const QPointF &, qreal);
@ -355,6 +360,11 @@ Q_DECL_CONSTEXPR inline const QPointF operator*(qreal c, const QPointF &p)
return QPointF(p.xp*c, p.yp*c);
}
Q_DECL_CONSTEXPR inline const QPointF operator+(const QPointF &p)
{
return p;
}
Q_DECL_CONSTEXPR inline const QPointF operator-(const QPointF &p)
{
return QPointF(-p.xp, -p.yp);

View File

@ -71,6 +71,9 @@ private slots:
void operator_divide_data();
void operator_divide();
void operator_unary_plus_data();
void operator_unary_plus();
void operator_unary_minus_data();
void operator_unary_minus();
@ -269,6 +272,18 @@ void tst_QPoint::operator_divide()
QCOMPARE(point, expected);
}
void tst_QPoint::operator_unary_plus_data()
{
operator_unary_minus_data();
}
void tst_QPoint::operator_unary_plus()
{
QFETCH(QPoint, point);
// Should be a NOOP.
QCOMPARE(+point, point);
}
void tst_QPoint::operator_unary_minus_data()
{
QTest::addColumn<QPoint>("point");

View File

@ -76,6 +76,9 @@ private slots:
void operator_divide();
void division();
void operator_unary_plus_data();
void operator_unary_plus();
void operator_unary_minus_data();
void operator_unary_minus();
@ -283,6 +286,18 @@ void tst_QPointF::division()
}
}
void tst_QPointF::operator_unary_plus_data()
{
operator_unary_minus_data();
}
void tst_QPointF::operator_unary_plus()
{
QFETCH(QPointF, point);
// Should be a NOOP.
QCOMPARE(+point, point);
}
void tst_QPointF::operator_unary_minus_data()
{
QTest::addColumn<QPointF>("point");