Make comparison operators in gui/painting classes hidden friends

Reduce ADL noise from QColorSpace, QPageSize, and QPageLayout with the
help of a private equals method.

Change-Id: I0082597dd216b982e8d8eb5a4bd7dd29a5d3263b
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
bb10
Volker Hilsheimer 2020-10-28 00:02:59 +01:00
parent 979c8cf1b7
commit a45a3b1ece
6 changed files with 92 additions and 76 deletions

View File

@ -692,55 +692,10 @@ bool QColorSpace::isValid() const noexcept
/*!
\fn bool QColorSpace::operator==(const QColorSpace &colorSpace1, const QColorSpace &colorSpace2)
\relates QColorSpace
Returns \c true if colorspace \a colorSpace1 is equal to colorspace \a colorSpace2;
otherwise returns \c false
*/
bool operator==(const QColorSpace &colorSpace1, const QColorSpace &colorSpace2)
{
if (colorSpace1.d_ptr == colorSpace2.d_ptr)
return true;
if (!colorSpace1.d_ptr || !colorSpace2.d_ptr)
return false;
if (colorSpace1.d_ptr->namedColorSpace && colorSpace2.d_ptr->namedColorSpace)
return colorSpace1.d_ptr->namedColorSpace == colorSpace2.d_ptr->namedColorSpace;
const bool valid1 = colorSpace1.isValid();
const bool valid2 = colorSpace2.isValid();
if (valid1 != valid2)
return false;
if (!valid1 && !valid2) {
if (!colorSpace1.d_ptr->iccProfile.isEmpty() || !colorSpace2.d_ptr->iccProfile.isEmpty())
return colorSpace1.d_ptr->iccProfile == colorSpace2.d_ptr->iccProfile;
}
// At this point one or both color spaces are unknown, and must be compared in detail instead
if (colorSpace1.primaries() != QColorSpace::Primaries::Custom && colorSpace2.primaries() != QColorSpace::Primaries::Custom) {
if (colorSpace1.primaries() != colorSpace2.primaries())
return false;
} else {
if (colorSpace1.d_ptr->toXyz != colorSpace2.d_ptr->toXyz)
return false;
}
if (colorSpace1.transferFunction() != QColorSpace::TransferFunction::Custom &&
colorSpace2.transferFunction() != QColorSpace::TransferFunction::Custom) {
if (colorSpace1.transferFunction() != colorSpace2.transferFunction())
return false;
if (colorSpace1.transferFunction() == QColorSpace::TransferFunction::Gamma)
return (qAbs(colorSpace1.gamma() - colorSpace2.gamma()) <= (1.0f / 512.0f));
return true;
}
if (colorSpace1.d_ptr->trc[0] != colorSpace2.d_ptr->trc[0] ||
colorSpace1.d_ptr->trc[1] != colorSpace2.d_ptr->trc[1] ||
colorSpace1.d_ptr->trc[2] != colorSpace2.d_ptr->trc[2])
return false;
return true;
}
/*!
\fn bool QColorSpace::operator!=(const QColorSpace &colorSpace1, const QColorSpace &colorSpace2)
@ -749,6 +704,55 @@ bool operator==(const QColorSpace &colorSpace1, const QColorSpace &colorSpace2)
otherwise returns \c false
*/
/*!
\internal
*/
bool QColorSpace::equals(const QColorSpace &other) const
{
if (d_ptr == other.d_ptr)
return true;
if (!d_ptr || !other.d_ptr)
return false;
if (d_ptr->namedColorSpace && other.d_ptr->namedColorSpace)
return d_ptr->namedColorSpace == other.d_ptr->namedColorSpace;
const bool valid1 = isValid();
const bool valid2 = other.isValid();
if (valid1 != valid2)
return false;
if (!valid1 && !valid2) {
if (!d_ptr->iccProfile.isEmpty() || !other.d_ptr->iccProfile.isEmpty())
return d_ptr->iccProfile == other.d_ptr->iccProfile;
}
// At this point one or both color spaces are unknown, and must be compared in detail instead
if (primaries() != QColorSpace::Primaries::Custom && other.primaries() != QColorSpace::Primaries::Custom) {
if (primaries() != other.primaries())
return false;
} else {
if (d_ptr->toXyz != other.d_ptr->toXyz)
return false;
}
if (transferFunction() != QColorSpace::TransferFunction::Custom &&
other.transferFunction() != QColorSpace::TransferFunction::Custom) {
if (transferFunction() != other.transferFunction())
return false;
if (transferFunction() == QColorSpace::TransferFunction::Gamma)
return (qAbs(gamma() - other.gamma()) <= (1.0f / 512.0f));
return true;
}
if (d_ptr->trc[0] != other.d_ptr->trc[0] ||
d_ptr->trc[1] != other.d_ptr->trc[1] ||
d_ptr->trc[2] != other.d_ptr->trc[2])
return false;
return true;
}
/*!
Generates and returns a color space transformation from this color space to
\a colorspace.

View File

@ -119,8 +119,10 @@ public:
void detach();
bool isValid() const noexcept;
friend Q_GUI_EXPORT bool operator==(const QColorSpace &colorSpace1, const QColorSpace &colorSpace2);
friend inline bool operator!=(const QColorSpace &colorSpace1, const QColorSpace &colorSpace2);
friend inline bool operator==(const QColorSpace &colorSpace1, const QColorSpace &colorSpace2)
{ return colorSpace1.equals(colorSpace2); }
friend inline bool operator!=(const QColorSpace &colorSpace1, const QColorSpace &colorSpace2)
{ return !(colorSpace1 == colorSpace2); }
static QColorSpace fromIccProfile(const QByteArray &iccProfile);
QByteArray iccProfile() const;
@ -131,6 +133,8 @@ public:
private:
friend class QColorSpacePrivate;
bool equals(const QColorSpace &other) const;
QExplicitlySharedDataPointer<QColorSpacePrivate> d_ptr;
#ifndef QT_NO_DEBUG_STREAM
@ -138,11 +142,6 @@ private:
#endif
};
inline bool operator!=(const QColorSpace &colorSpace1, const QColorSpace &colorSpace2)
{
return !(colorSpace1 == colorSpace2);
}
Q_DECLARE_SHARED(QColorSpace)
// QColorSpace stream functions

View File

@ -415,7 +415,7 @@ QPageLayout &QPageLayout::operator=(const QPageLayout &other)
*/
/*!
\relates QPageLayout
\fn bool QPageLayout::operator==(const QPageLayout &lhs, const QPageLayout &rhs)
Returns \c true if page layout \a lhs is equal to page layout \a rhs,
i.e. if all the attributes are exactly equal.
@ -427,14 +427,8 @@ QPageLayout &QPageLayout::operator=(const QPageLayout &other)
\sa QPageLayout::isEquivalentTo()
*/
bool operator==(const QPageLayout &lhs, const QPageLayout &rhs)
{
return lhs.d == rhs.d || *lhs.d == *rhs.d;
}
/*!
\fn bool operator!=(const QPageLayout &lhs, const QPageLayout &rhs)
\relates QPageLayout
\fn bool QPageLayout::operator!=(const QPageLayout &lhs, const QPageLayout &rhs)
Returns \c true if page layout \a lhs is not equal to page layout \a rhs,
i.e. if any of the attributes differ.
@ -446,6 +440,15 @@ bool operator==(const QPageLayout &lhs, const QPageLayout &rhs)
\sa QPageLayout::isEquivalentTo()
*/
/*!
\internal
*/
bool QPageLayout::equals(const QPageLayout &other) const
{
return d == other.d || *d == *other.d;
}
/*!
Returns \c true if this page layout is equivalent to the \a other page layout,
i.e. if the page has the same size, margins and orientation.

View File

@ -132,15 +132,18 @@ public:
private:
friend class QPageLayoutPrivate;
bool equals(const QPageLayout &other) const;
friend inline bool operator==(const QPageLayout &lhs, const QPageLayout &rhs)
{ return lhs.equals(rhs); }
friend inline bool operator!=(const QPageLayout &lhs, const QPageLayout &rhs)
{ return !lhs.equals(rhs); }
QExplicitlySharedDataPointer<QPageLayoutPrivate> d;
};
Q_DECLARE_SHARED(QPageLayout)
Q_GUI_EXPORT bool operator==(const QPageLayout &lhs, const QPageLayout &rhs);
inline bool operator!=(const QPageLayout &lhs, const QPageLayout &rhs)
{ return !operator==(lhs, rhs); }
#ifndef QT_NO_DEBUG_STREAM
Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QPageLayout &pageLayout);
#endif

View File

@ -1249,26 +1249,30 @@ QPageSize &QPageSize::operator=(const QPageSize &other)
*/
/*!
\relates QPageSize
\fn bool QPageSize::operator==(const QPageSize &lhs, const QPageSize &rhs)
Returns \c true if page size \a lhs is equal to page size \a rhs,
i.e. if the page sizes have the same attributes. Current
attributes are size and name.
*/
bool operator==(const QPageSize &lhs, const QPageSize &rhs)
{
return lhs.d == rhs.d || *lhs.d == *rhs.d;
}
/*!
\fn bool operator!=(const QPageSize &lhs, const QPageSize &rhs)
\relates QPageSize
\fn bool QPageSize::operator!=(const QPageSize &lhs, const QPageSize &rhs)
Returns \c true if page size \a lhs is unequal to page size \a
rhs, i.e. if the page size has different attributes. Current
attributes are size and name.
*/
/*!
\internal
*/
bool QPageSize::equals(const QPageSize &other) const
{
return d == other.d || *d == *other.d;
}
/*!
Returns \c true if this page is equivalent to the \a other page, i.e. if the
page has the same size regardless of other attributes like name.

View File

@ -283,6 +283,13 @@ public:
private:
friend class QPageSizePrivate;
friend class QPlatformPrintDevice;
bool equals(const QPageSize &other) const;
friend inline bool operator==(const QPageSize &lhs, const QPageSize &rhs)
{ return lhs.equals(rhs); }
friend inline bool operator!=(const QPageSize &lhs, const QPageSize &rhs)
{ return !(lhs == rhs); }
QPageSize(const QString &key, const QSize &pointSize, const QString &name);
QPageSize(int windowsId, const QSize &pointSize, const QString &name);
QPageSize(QPageSizePrivate &dd);
@ -291,10 +298,6 @@ private:
Q_DECLARE_SHARED(QPageSize)
Q_GUI_EXPORT bool operator==(const QPageSize &lhs, const QPageSize &rhs);
inline bool operator!=(const QPageSize &lhs, const QPageSize &rhs)
{ return !operator==(lhs, rhs); }
#ifndef QT_NO_DEBUG_STREAM
Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QPageSize &pageSize);
#endif