Math 3D classes: make comparison operators hidden friends

Reduce ADL noise from QVector2/3/4D and from QQuaternion. Also mark as
noexcept.

Task-number: QTBUG-87973
Change-Id: Id14fd17c0da43517766b218ed85c3b41f91b43c8
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
This commit is contained in:
Volker Hilsheimer 2020-10-27 23:59:16 +01:00
parent 2222e695fc
commit 7372dde4ec
8 changed files with 59 additions and 76 deletions

View File

@ -785,16 +785,14 @@ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)
#endif // QT_NO_VECTOR3D
/*!
\fn bool operator==(const QQuaternion &q1, const QQuaternion &q2)
\relates QQuaternion
\fn bool QQuaternion::operator==(const QQuaternion &q1, const QQuaternion &q2)
Returns \c true if \a q1 is equal to \a q2; otherwise returns \c false.
This operator uses an exact floating-point comparison.
*/
/*!
\fn bool operator!=(const QQuaternion &q1, const QQuaternion &q2)
\relates QQuaternion
\fn bool QQuaternion::operator!=(const QQuaternion &q1, const QQuaternion &q2)
Returns \c true if \a q1 is not equal to \a q2; otherwise returns \c false.
This operator uses an exact floating-point comparison.

View File

@ -105,8 +105,18 @@ public:
QQuaternion &operator*=(const QQuaternion &quaternion);
QQuaternion &operator/=(float divisor);
friend inline bool operator==(const QQuaternion &q1, const QQuaternion &q2);
friend inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2);
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
friend inline bool operator==(const QQuaternion &q1, const QQuaternion &q2) noexcept
{
return q1.wp == q2.wp && q1.xp == q2.xp && q1.yp == q2.yp && q1.zp == q2.zp;
}
friend inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2) noexcept
{
return !(q1 == q2);
}
QT_WARNING_POP
friend inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2);
friend inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2);
friend inline const QQuaternion operator*(float factor, const QQuaternion &quaternion);
@ -177,11 +187,6 @@ inline bool QQuaternion::isIdentity() const
{
return wp == 1.0f && xp == 0.0f && yp == 0.0f && zp == 0.0f;
}
inline bool operator==(const QQuaternion &q1, const QQuaternion &q2)
{
return q1.wp == q2.wp && q1.xp == q2.xp && q1.yp == q2.yp && q1.zp == q2.zp;
}
QT_WARNING_POP
inline float QQuaternion::x() const { return xp; }
@ -275,11 +280,6 @@ inline QQuaternion &QQuaternion::operator/=(float divisor)
return *this;
}
inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2)
{
return !operator==(q1, q2);
}
inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2)
{
return QQuaternion(q1.wp + q2.wp, q1.xp + q2.xp, q1.yp + q2.yp, q1.zp + q2.zp);

View File

@ -351,16 +351,14 @@ float QVector2D::dotProduct(const QVector2D& v1, const QVector2D& v2)
}
/*!
\fn bool operator==(const QVector2D &v1, const QVector2D &v2)
\relates QVector2D
\fn bool QVector2D::operator==(const QVector2D &v1, const QVector2D &v2)
Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false.
This operator uses an exact floating-point comparison.
*/
/*!
\fn bool operator!=(const QVector2D &v1, const QVector2D &v2)
\relates QVector2D
\fn bool QVector2D::operator!=(const QVector2D &v1, const QVector2D &v2)
Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false.
This operator uses an exact floating-point comparison.

View File

@ -97,8 +97,19 @@ public:
static float dotProduct(const QVector2D& v1, const QVector2D& v2); //In Qt 6 convert to inline and constexpr
constexpr friend inline bool operator==(const QVector2D &v1, const QVector2D &v2);
constexpr friend inline bool operator!=(const QVector2D &v1, const QVector2D &v2);
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
constexpr friend inline bool operator==(const QVector2D &v1, const QVector2D &v2) noexcept
{
return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1];
}
constexpr friend inline bool operator!=(const QVector2D &v1, const QVector2D &v2) noexcept
{
return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1];
}
QT_WARNING_POP
constexpr friend inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2);
constexpr friend inline const QVector2D operator-(const QVector2D &v1, const QVector2D &v2);
constexpr friend inline const QVector2D operator*(float factor, const QVector2D &vector);
@ -204,20 +215,6 @@ inline QVector2D &QVector2D::operator/=(const QVector2D &vector)
return *this;
}
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
constexpr inline bool operator==(const QVector2D &v1, const QVector2D &v2)
{
return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1];
}
constexpr inline bool operator!=(const QVector2D &v1, const QVector2D &v2)
{
return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1];
}
QT_WARNING_POP
constexpr inline const QVector2D operator+(const QVector2D &v1, const QVector2D &v2)
{
return QVector2D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1]);

View File

@ -519,16 +519,14 @@ float QVector3D::distanceToLine
}
/*!
\fn bool operator==(const QVector3D &v1, const QVector3D &v2)
\relates QVector3D
\fn bool QVector3D::operator==(const QVector3D &v1, const QVector3D &v2)
Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false.
This operator uses an exact floating-point comparison.
*/
/*!
\fn bool operator!=(const QVector3D &v1, const QVector3D &v2)
\relates QVector3D
\fn bool QVector3D::operator!=(const QVector3D &v1, const QVector3D &v2)
Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false.
This operator uses an exact floating-point comparison.

View File

@ -112,8 +112,19 @@ public:
float distanceToPlane(const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const;
float distanceToLine(const QVector3D& point, const QVector3D& direction) const;
constexpr friend inline bool operator==(const QVector3D &v1, const QVector3D &v2);
constexpr friend inline bool operator!=(const QVector3D &v1, const QVector3D &v2);
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
constexpr friend inline bool operator==(const QVector3D &v1, const QVector3D &v2) noexcept
{
return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1] && v1.v[2] == v2.v[2];
}
constexpr friend inline bool operator!=(const QVector3D &v1, const QVector3D &v2) noexcept
{
return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1] || v1.v[2] != v2.v[2];
}
QT_WARNING_POP
constexpr friend inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2);
constexpr friend inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2);
constexpr friend inline const QVector3D operator*(float factor, const QVector3D &vector);
@ -229,20 +240,6 @@ inline QVector3D &QVector3D::operator/=(const QVector3D &vector)
return *this;
}
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
constexpr inline bool operator==(const QVector3D &v1, const QVector3D &v2)
{
return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1] && v1.v[2] == v2.v[2];
}
constexpr inline bool operator!=(const QVector3D &v1, const QVector3D &v2)
{
return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1] || v1.v[2] != v2.v[2];
}
QT_WARNING_POP
constexpr inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2)
{
return QVector3D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1], v1.v[2] + v2.v[2]);

View File

@ -394,16 +394,14 @@ float QVector4D::dotProduct(const QVector4D& v1, const QVector4D& v2)
}
/*!
\fn bool operator==(const QVector4D &v1, const QVector4D &v2)
\relates QVector4D
\fn bool QVector4D::operator==(const QVector4D &v1, const QVector4D &v2)
Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false.
This operator uses an exact floating-point comparison.
*/
/*!
\fn bool operator!=(const QVector4D &v1, const QVector4D &v2)
\relates QVector4D
\fn bool QVector4D::operator!=(const QVector4D &v1, const QVector4D &v2)
Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false.
This operator uses an exact floating-point comparison.

View File

@ -100,8 +100,19 @@ public:
static float dotProduct(const QVector4D& v1, const QVector4D& v2); //In Qt 6 convert to inline and constexpr
constexpr friend inline bool operator==(const QVector4D &v1, const QVector4D &v2);
constexpr friend inline bool operator!=(const QVector4D &v1, const QVector4D &v2);
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
constexpr friend inline bool operator==(const QVector4D &v1, const QVector4D &v2) noexcept
{
return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1] && v1.v[2] == v2.v[2] && v1.v[3] == v2.v[3];
}
constexpr friend inline bool operator!=(const QVector4D &v1, const QVector4D &v2) noexcept
{
return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1] || v1.v[2] != v2.v[2] || v1.v[3] != v2.v[3];
}
QT_WARNING_POP
constexpr friend inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2);
constexpr friend inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2);
constexpr friend inline const QVector4D operator*(float factor, const QVector4D &vector);
@ -229,20 +240,6 @@ inline QVector4D &QVector4D::operator/=(const QVector4D &vector)
return *this;
}
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
constexpr inline bool operator==(const QVector4D &v1, const QVector4D &v2)
{
return v1.v[0] == v2.v[0] && v1.v[1] == v2.v[1] && v1.v[2] == v2.v[2] && v1.v[3] == v2.v[3];
}
constexpr inline bool operator!=(const QVector4D &v1, const QVector4D &v2)
{
return v1.v[0] != v2.v[0] || v1.v[1] != v2.v[1] || v1.v[2] != v2.v[2] || v1.v[3] != v2.v[3];
}
QT_WARNING_POP
constexpr inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
{
return QVector4D(v1.v[0] + v2.v[0], v1.v[1] + v2.v[1], v1.v[2] + v2.v[2], v1.v[3] + v2.v[3]);