Make QPoint*, QSize*, and QRect* binary operators hidden friends

Moves them to class scope, which will avoid them showing up as
possibilities in error messages for missing operators.

Also consolidates how they are compared, so QRectF and QSizeF act
similar to QPointF.

Change-Id: I1e12cb7e5a5c65e85c32281878da03c6136c17de
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Allan Sandfeld Jensen 2020-10-20 16:56:34 +02:00
parent b39b018f4a
commit f7f1a71ea4
4 changed files with 102 additions and 243 deletions

View File

@ -80,19 +80,32 @@ public:
constexpr static inline int dotProduct(const QPoint &p1, const QPoint &p2)
{ return p1.xp * p2.xp + p1.yp * p2.yp; }
friend constexpr inline bool operator==(const QPoint &, const QPoint &) noexcept;
friend constexpr inline bool operator!=(const QPoint &, const QPoint &) noexcept;
friend constexpr inline const QPoint operator+(const QPoint &, const QPoint &);
friend constexpr inline const QPoint operator-(const QPoint &, const QPoint &);
friend constexpr inline const QPoint operator*(const QPoint &, float);
friend constexpr inline const QPoint operator*(float, const QPoint &);
friend constexpr inline const QPoint operator*(const QPoint &, double);
friend constexpr inline const QPoint operator*(double, const QPoint &);
friend constexpr inline const QPoint operator*(const QPoint &, int);
friend constexpr inline const QPoint operator*(int, const QPoint &);
friend constexpr inline const QPoint operator+(const QPoint &);
friend constexpr inline const QPoint operator-(const QPoint &);
friend constexpr inline const QPoint operator/(const QPoint &, qreal);
friend constexpr inline bool operator==(const QPoint &p1, const QPoint &p2) noexcept
{ return p1.xp == p2.xp && p1.yp == p2.yp; }
friend constexpr inline bool operator!=(const QPoint &p1, const QPoint &p2) noexcept
{ return p1.xp != p2.xp || p1.yp != p2.yp; }
friend constexpr inline QPoint operator+(const QPoint &p1, const QPoint &p2) noexcept
{ return QPoint(p1.xp + p2.xp, p1.yp + p2.yp); }
friend constexpr inline QPoint operator-(const QPoint &p1, const QPoint &p2) noexcept
{ return QPoint(p1.xp - p2.xp, p1.yp - p2.yp); }
friend constexpr inline QPoint operator*(const QPoint &p, float factor)
{ return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
friend constexpr inline QPoint operator*(const QPoint &p, double factor)
{ return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
friend constexpr inline QPoint operator*(const QPoint &p, int factor) noexcept
{ return QPoint(p.xp * factor, p.yp * factor); }
friend constexpr inline QPoint operator*(float factor, const QPoint &p)
{ return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
friend constexpr inline QPoint operator*(double factor, const QPoint &p)
{ return QPoint(qRound(p.xp * factor), qRound(p.yp * factor)); }
friend constexpr inline QPoint operator*(int factor, const QPoint &p) noexcept
{ return QPoint(p.xp * factor, p.yp * factor); }
friend constexpr inline QPoint operator+(const QPoint &p) noexcept
{ return p; }
friend constexpr inline QPoint operator-(const QPoint &p) noexcept
{ return QPoint(-p.xp, -p.yp); }
friend constexpr inline QPoint operator/(const QPoint &p, qreal c)
{ return QPoint(qRound(p.xp / c), qRound(p.yp / c)); }
#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
[[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept;
@ -197,66 +210,6 @@ constexpr inline QPoint &QPoint::operator*=(int factor)
return *this;
}
constexpr inline bool operator==(const QPoint &p1, const QPoint &p2) noexcept
{
return p1.xp == p2.xp && p1.yp == p2.yp;
}
constexpr inline bool operator!=(const QPoint &p1, const QPoint &p2) noexcept
{
return p1.xp != p2.xp || p1.yp != p2.yp;
}
constexpr inline const QPoint operator+(const QPoint &p1, const QPoint &p2)
{
return QPoint(p1.xp + p2.xp, p1.yp + p2.yp);
}
constexpr inline const QPoint operator-(const QPoint &p1, const QPoint &p2)
{
return QPoint(p1.xp - p2.xp, p1.yp - p2.yp);
}
constexpr inline const QPoint operator*(const QPoint &p, float factor)
{
return QPoint(qRound(p.xp * factor), qRound(p.yp * factor));
}
constexpr inline const QPoint operator*(const QPoint &p, double factor)
{
return QPoint(qRound(p.xp * factor), qRound(p.yp * factor));
}
constexpr inline const QPoint operator*(const QPoint &p, int factor)
{
return QPoint(p.xp * factor, p.yp * factor);
}
constexpr inline const QPoint operator*(float factor, const QPoint &p)
{
return QPoint(qRound(p.xp * factor), qRound(p.yp * factor));
}
constexpr inline const QPoint operator*(double factor, const QPoint &p)
{
return QPoint(qRound(p.xp * factor), qRound(p.yp * factor));
}
constexpr inline const QPoint operator*(int factor, const QPoint &p)
{
return QPoint(p.xp * factor, p.yp * factor);
}
constexpr inline const QPoint operator+(const QPoint &p)
{
return p;
}
constexpr inline const QPoint operator-(const QPoint &p)
{
return QPoint(-p.xp, -p.yp);
}
constexpr inline QPoint &QPoint::operator/=(qreal c)
{
xp = qRound(xp / c);
@ -264,11 +217,6 @@ constexpr inline QPoint &QPoint::operator/=(qreal c)
return *this;
}
constexpr inline const QPoint operator/(const QPoint &p, qreal c)
{
return QPoint(qRound(p.xp / c), qRound(p.yp / c));
}
#ifndef QT_NO_DEBUG_STREAM
Q_CORE_EXPORT QDebug operator<<(QDebug, const QPoint &);
#endif
@ -309,15 +257,33 @@ public:
return p1.xp * p2.xp + p1.yp * p2.yp;
}
friend constexpr inline bool operator==(const QPointF &, const QPointF &);
friend constexpr inline bool operator!=(const QPointF &, const QPointF &);
friend constexpr inline const QPointF operator+(const QPointF &, const QPointF &);
friend constexpr inline const QPointF operator-(const QPointF &, const QPointF &);
friend constexpr inline const QPointF operator*(qreal, const QPointF &);
friend constexpr inline const QPointF operator*(const QPointF &, qreal);
friend constexpr inline const QPointF operator+(const QPointF &);
friend constexpr inline const QPointF operator-(const QPointF &);
friend constexpr inline const QPointF operator/(const QPointF &, qreal);
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
friend constexpr inline bool operator==(const QPointF &p1, const QPointF &p2)
{
return ((!p1.xp || !p2.xp) ? qFuzzyIsNull(p1.xp - p2.xp) : qFuzzyCompare(p1.xp, p2.xp))
&& ((!p1.yp || !p2.yp) ? qFuzzyIsNull(p1.yp - p2.yp) : qFuzzyCompare(p1.yp, p2.yp));
}
friend constexpr inline bool operator!=(const QPointF &p1, const QPointF &p2)
{
return !(p1 == p2);
}
QT_WARNING_POP
friend constexpr inline QPointF operator+(const QPointF &p1, const QPointF &p2)
{ return QPointF(p1.xp + p2.xp, p1.yp + p2.yp); }
friend constexpr inline QPointF operator-(const QPointF &p1, const QPointF &p2)
{ return QPointF(p1.xp - p2.xp, p1.yp - p2.yp); }
friend constexpr inline QPointF operator*(const QPointF &p, qreal c)
{ return QPointF(p.xp * c, p.yp * c); }
friend constexpr inline QPointF operator*(qreal c, const QPointF &p)
{ return QPointF(p.xp * c, p.yp * c); }
friend constexpr inline QPointF operator+(const QPointF &p)
{ return p; }
friend constexpr inline QPointF operator-(const QPointF &p)
{ return QPointF(-p.xp, -p.yp); }
friend constexpr inline QPointF operator/(const QPointF &p, qreal divisor)
{ return QPointF(p.xp / divisor, p.yp / divisor); }
constexpr QPoint toPoint() const;
@ -414,52 +380,6 @@ constexpr inline QPointF &QPointF::operator*=(qreal c)
return *this;
}
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
constexpr inline bool operator==(const QPointF &p1, const QPointF &p2)
{
return ((!p1.xp || !p2.xp) ? qFuzzyIsNull(p1.xp - p2.xp) : qFuzzyCompare(p1.xp, p2.xp))
&& ((!p1.yp || !p2.yp) ? qFuzzyIsNull(p1.yp - p2.yp) : qFuzzyCompare(p1.yp, p2.yp));
}
constexpr inline bool operator!=(const QPointF &p1, const QPointF &p2)
{
return !(p1 == p2);
}
QT_WARNING_POP
constexpr inline const QPointF operator+(const QPointF &p1, const QPointF &p2)
{
return QPointF(p1.xp + p2.xp, p1.yp + p2.yp);
}
constexpr inline const QPointF operator-(const QPointF &p1, const QPointF &p2)
{
return QPointF(p1.xp - p2.xp, p1.yp - p2.yp);
}
constexpr inline const QPointF operator*(const QPointF &p, qreal c)
{
return QPointF(p.xp * c, p.yp * c);
}
constexpr inline const QPointF operator*(qreal c, const QPointF &p)
{
return QPointF(p.xp * c, p.yp * c);
}
constexpr inline const QPointF operator+(const QPointF &p)
{
return p;
}
constexpr inline const QPointF operator-(const QPointF &p)
{
return QPointF(-p.xp, -p.yp);
}
constexpr inline QPointF &QPointF::operator/=(qreal divisor)
{
xp /= divisor;
@ -467,11 +387,6 @@ constexpr inline QPointF &QPointF::operator/=(qreal divisor)
return *this;
}
constexpr inline const QPointF operator/(const QPointF &p, qreal divisor)
{
return QPointF(p.xp / divisor, p.yp / divisor);
}
constexpr inline QPoint QPointF::toPoint() const
{
return QPoint(qRound(xp), qRound(yp));

View File

@ -2130,7 +2130,7 @@ bool QRectF::contains(const QRectF &r) const noexcept
Intersects this rectangle with the given \a rectangle.
\sa intersected(), operator|=()
\sa intersected(), operator&()
*/

View File

@ -148,8 +148,10 @@ public:
[[nodiscard]] static constexpr inline QRect span(const QPoint &p1, const QPoint &p2) noexcept;
friend constexpr inline bool operator==(const QRect &, const QRect &) noexcept;
friend constexpr inline bool operator!=(const QRect &, const QRect &) noexcept;
friend constexpr inline bool operator==(const QRect &r1, const QRect &r2) noexcept
{ return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2; }
friend constexpr inline bool operator!=(const QRect &r1, const QRect &r2) noexcept
{ return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2; }
friend constexpr inline size_t qHash(const QRect &, size_t) noexcept;
#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
@ -450,16 +452,6 @@ inline QRect QRect::united(const QRect &r) const noexcept
return *this | r;
}
constexpr inline bool operator==(const QRect &r1, const QRect &r2) noexcept
{
return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2;
}
constexpr inline bool operator!=(const QRect &r1, const QRect &r2) noexcept
{
return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2;
}
constexpr inline size_t qHash(const QRect &r, size_t seed = 0) noexcept
{
return qHashMulti(seed, r.x1, r.x2, r.y1, r.y2);
@ -611,8 +603,16 @@ public:
constexpr inline QRectF &operator+=(const QMarginsF &margins) noexcept;
constexpr inline QRectF &operator-=(const QMarginsF &margins) noexcept;
friend constexpr inline bool operator==(const QRectF &, const QRectF &) noexcept;
friend constexpr inline bool operator!=(const QRectF &, const QRectF &) noexcept;
friend constexpr inline bool operator==(const QRectF &r1, const QRectF &r2) noexcept
{
return r1.topLeft() == r2.topLeft()
&& r1.size() == r2.size();
}
friend constexpr inline bool operator!=(const QRectF &r1, const QRectF &r2) noexcept
{
return r1.topLeft() != r2.topLeft()
|| r1.size() != r2.size();
}
[[nodiscard]] constexpr inline QRect toRect() const noexcept;
[[nodiscard]] QRect toAlignedRect() const noexcept;
@ -860,18 +860,6 @@ inline QRectF QRectF::united(const QRectF &r) const noexcept
return *this | r;
}
constexpr inline bool operator==(const QRectF &r1, const QRectF &r2) noexcept
{
return qFuzzyCompare(r1.xp, r2.xp) && qFuzzyCompare(r1.yp, r2.yp)
&& qFuzzyCompare(r1.w, r2.w) && qFuzzyCompare(r1.h, r2.h);
}
constexpr inline bool operator!=(const QRectF &r1, const QRectF &r2) noexcept
{
return !qFuzzyCompare(r1.xp, r2.xp) || !qFuzzyCompare(r1.yp, r2.yp)
|| !qFuzzyCompare(r1.w, r2.w) || !qFuzzyCompare(r1.h, r2.h);
}
constexpr inline QRect QRectF::toRect() const noexcept
{
// This rounding is designed to minimize the maximum possible difference

View File

@ -89,14 +89,21 @@ public:
constexpr inline QSize &operator*=(qreal c) noexcept;
inline QSize &operator/=(qreal c);
friend inline constexpr bool operator==(const QSize &, const QSize &) noexcept;
friend inline constexpr bool operator!=(const QSize &, const QSize &) noexcept;
friend inline constexpr bool operator==(const QSize &s1, const QSize &s2) noexcept
{ return s1.wd == s2.wd && s1.ht == s2.ht; }
friend inline constexpr bool operator!=(const QSize &s1, const QSize &s2) noexcept
{ return s1.wd != s2.wd || s1.ht != s2.ht; }
friend inline constexpr QSize operator+(const QSize &s1, const QSize &s2) noexcept
{ return QSize(s1.wd + s2.wd, s1.ht + s2.ht); }
friend inline constexpr QSize operator-(const QSize &s1, const QSize &s2) noexcept
{ return QSize(s1.wd - s2.wd, s1.ht - s2.ht); }
friend inline constexpr QSize operator*(const QSize &s, qreal c) noexcept
{ return QSize(qRound(s.wd * c), qRound(s.ht * c)); }
friend inline constexpr QSize operator*(qreal c, const QSize &s) noexcept
{ return s * c; }
friend inline QSize operator/(const QSize &s, qreal c)
{ Q_ASSERT(!qFuzzyIsNull(c)); return QSize(qRound(s.wd / c), qRound(s.ht / c)); }
friend inline constexpr size_t qHash(const QSize &, size_t) noexcept;
friend inline constexpr const QSize operator+(const QSize &, const QSize &) noexcept;
friend inline constexpr const QSize operator-(const QSize &, const QSize &) noexcept;
friend inline constexpr const QSize operator*(const QSize &, qreal) noexcept;
friend inline constexpr const QSize operator*(qreal, const QSize &) noexcept;
friend inline const QSize operator/(const QSize &, qreal);
#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
[[nodiscard]] CGSize toCGSize() const noexcept;
@ -186,35 +193,9 @@ constexpr inline QSize &QSize::operator*=(qreal c) noexcept
return *this;
}
constexpr inline bool operator==(const QSize &s1, const QSize &s2) noexcept
{ return s1.wd == s2.wd && s1.ht == s2.ht; }
constexpr inline bool operator!=(const QSize &s1, const QSize &s2) noexcept
{ return s1.wd != s2.wd || s1.ht != s2.ht; }
constexpr inline size_t qHash(const QSize &s, size_t seed = 0) noexcept
{ return qHashMulti(seed, s.wd, s.ht); }
constexpr inline const QSize operator+(const QSize &s1, const QSize &s2) noexcept
{
return QSize(s1.wd + s2.wd, s1.ht + s2.ht);
}
constexpr inline const QSize operator-(const QSize &s1, const QSize &s2) noexcept
{
return QSize(s1.wd - s2.wd, s1.ht - s2.ht);
}
constexpr inline const QSize operator*(const QSize &s, qreal c) noexcept
{
return QSize(qRound(s.wd * c), qRound(s.ht * c));
}
constexpr inline const QSize operator*(qreal c, const QSize &s) noexcept
{
return QSize(qRound(s.wd * c), qRound(s.ht * c));
}
inline QSize &QSize::operator/=(qreal c)
{
Q_ASSERT(!qFuzzyIsNull(c));
@ -223,12 +204,6 @@ inline QSize &QSize::operator/=(qreal c)
return *this;
}
inline const QSize operator/(const QSize &s, qreal c)
{
Q_ASSERT(!qFuzzyIsNull(c));
return QSize(qRound(s.wd / c), qRound(s.ht / c));
}
constexpr inline QSize QSize::expandedTo(const QSize & otherSize) const noexcept
{
return QSize(qMax(wd,otherSize.wd), qMax(ht,otherSize.ht));
@ -283,13 +258,26 @@ public:
constexpr inline QSizeF &operator*=(qreal c) noexcept;
inline QSizeF &operator/=(qreal c);
friend constexpr inline bool operator==(const QSizeF &, const QSizeF &) noexcept;
friend constexpr inline bool operator!=(const QSizeF &, const QSizeF &) noexcept;
friend constexpr inline const QSizeF operator+(const QSizeF &, const QSizeF &) noexcept;
friend constexpr inline const QSizeF operator-(const QSizeF &, const QSizeF &) noexcept;
friend constexpr inline const QSizeF operator*(const QSizeF &, qreal) noexcept;
friend constexpr inline const QSizeF operator*(qreal, const QSizeF &) noexcept;
friend inline const QSizeF operator/(const QSizeF &, qreal);
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
friend constexpr inline bool operator==(const QSizeF &s1, const QSizeF &s2)
{
return ((!s1.wd || !s2.wd) ? qFuzzyIsNull(s1.wd - s2.wd) : qFuzzyCompare(s1.wd, s2.wd))
&& ((!s1.ht || !s2.ht) ? qFuzzyIsNull(s1.ht - s2.ht) : qFuzzyCompare(s1.ht, s2.ht));
}
QT_WARNING_POP
friend constexpr inline bool operator!=(const QSizeF &s1, const QSizeF &s2)
{ return !(s1 == s2); }
friend constexpr inline QSizeF operator+(const QSizeF &s1, const QSizeF &s2) noexcept
{ return QSizeF(s1.wd + s2.wd, s1.ht + s2.ht); }
friend constexpr inline QSizeF operator-(const QSizeF &s1, const QSizeF &s2) noexcept
{ return QSizeF(s1.wd - s2.wd, s1.ht - s2.ht); }
friend constexpr inline QSizeF operator*(const QSizeF &s, qreal c) noexcept
{ return QSizeF(s.wd * c, s.ht * c); }
friend constexpr inline QSizeF operator*(qreal c, const QSizeF &s) noexcept
{ return s * c; }
friend inline QSizeF operator/(const QSizeF &s, qreal c)
{ Q_ASSERT(!qFuzzyIsNull(c)); return QSizeF(s.wd / c, s.ht / c); }
constexpr inline QSize toSize() const noexcept;
@ -385,32 +373,6 @@ constexpr inline QSizeF &QSizeF::operator*=(qreal c) noexcept
return *this;
}
constexpr inline bool operator==(const QSizeF &s1, const QSizeF &s2) noexcept
{ return qFuzzyCompare(s1.wd, s2.wd) && qFuzzyCompare(s1.ht, s2.ht); }
constexpr inline bool operator!=(const QSizeF &s1, const QSizeF &s2) noexcept
{ return !qFuzzyCompare(s1.wd, s2.wd) || !qFuzzyCompare(s1.ht, s2.ht); }
constexpr inline const QSizeF operator+(const QSizeF &s1, const QSizeF &s2) noexcept
{
return QSizeF(s1.wd + s2.wd, s1.ht + s2.ht);
}
constexpr inline const QSizeF operator-(const QSizeF &s1, const QSizeF &s2) noexcept
{
return QSizeF(s1.wd - s2.wd, s1.ht - s2.ht);
}
constexpr inline const QSizeF operator*(const QSizeF &s, qreal c) noexcept
{
return QSizeF(s.wd * c, s.ht * c);
}
constexpr inline const QSizeF operator*(qreal c, const QSizeF &s) noexcept
{
return QSizeF(s.wd * c, s.ht * c);
}
inline QSizeF &QSizeF::operator/=(qreal c)
{
Q_ASSERT(!qFuzzyIsNull(c));
@ -419,12 +381,6 @@ inline QSizeF &QSizeF::operator/=(qreal c)
return *this;
}
inline const QSizeF operator/(const QSizeF &s, qreal c)
{
Q_ASSERT(!qFuzzyIsNull(c));
return QSizeF(s.wd / c, s.ht / c);
}
constexpr inline QSizeF QSizeF::expandedTo(const QSizeF &otherSize) const noexcept
{
return QSizeF(qMax(wd, otherSize.wd), qMax(ht, otherSize.ht));