QMargins(F): use comparison helper macros

Also explicitly add QMarginsF vs QMargins comparison. Previously such
comparison was implicitly converting QMargins to QMarginsF, and doing the
fuzzy comparison. We have to keep the old behavior to avoid
breaking user code, so use fuzzy comparison in the new operators
as well.

Task-number: QTBUG-120308
Change-Id: Ic82f64078cd991859b851f24aa7017ef0b91a4e1
Reviewed-by: Tatiana Borisova <tatiana.borisova@qt.io>
This commit is contained in:
Ivan Solovev 2024-04-19 18:06:27 +02:00
parent 8a54d25a46
commit eb178d3e51
3 changed files with 39 additions and 27 deletions

View File

@ -14,6 +14,10 @@ QT_BEGIN_NAMESPACE
\ingroup painting
\since 4.6
\compares equality
\compareswith equality QMarginsF
\endcompareswith
\brief The QMargins class defines the four margins of a rectangle.
QMargin defines a set of four margins; left, top, right, and bottom,
@ -107,15 +111,15 @@ QT_BEGIN_NAMESPACE
*/
/*!
\fn bool QMargins::operator==(const QMargins &m1, const QMargins &m2)
\fn bool QMargins::operator==(const QMargins &lhs, const QMargins &rhs)
Returns \c true if \a m1 and \a m2 are equal; otherwise returns \c false.
Returns \c true if \a lhs and \a rhs are equal; otherwise returns \c false.
*/
/*!
\fn bool QMargins::operator!=(const QMargins &m1, const QMargins &m2)
\fn bool QMargins::operator!=(const QMargins &lhs, const QMargins &rhs)
Returns \c true if \a m1 and \a m2 are different; otherwise returns \c false.
Returns \c true if \a lhs and \a rhs are different; otherwise returns \c false.
*/
/*!
@ -438,6 +442,10 @@ QDebug operator<<(QDebug dbg, const QMargins &m)
\ingroup painting
\since 5.3
\compares equality
\compareswith equality QMargins
\endcompareswith
\brief The QMarginsF class defines the four margins of a rectangle.
QMarginsF defines a set of four margins; left, top, right, and bottom,

View File

@ -4,6 +4,7 @@
#ifndef QMARGINS_H
#define QMARGINS_H
#include <QtCore/qcompare.h>
#include <QtCore/qnamespace.h>
#include <QtCore/q20type_traits.h>
@ -54,19 +55,14 @@ private:
int m_right;
int m_bottom;
friend constexpr inline bool operator==(const QMargins &m1, const QMargins &m2) noexcept
friend constexpr bool comparesEqual(const QMargins &lhs, const QMargins &rhs) noexcept
{
return
m1.m_left == m2.m_left &&
m1.m_top == m2.m_top &&
m1.m_right == m2.m_right &&
m1.m_bottom == m2.m_bottom;
}
friend constexpr inline bool operator!=(const QMargins &m1, const QMargins &m2) noexcept
{
return !(m1 == m2);
return lhs.m_left == rhs.m_left
&& lhs.m_top == rhs.m_top
&& lhs.m_right == rhs.m_right
&& lhs.m_bottom == rhs.m_bottom;
}
Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QMargins)
template <std::size_t I,
typename M,
@ -306,23 +302,23 @@ private:
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
friend constexpr inline bool operator==(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
friend constexpr bool comparesEqual(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
{
return ((!lhs.m_left || !rhs.m_left) ? qFuzzyIsNull(lhs.m_left - rhs.m_left)
: qFuzzyCompare(lhs.m_left, rhs.m_left))
&& ((!lhs.m_top || !rhs.m_top) ? qFuzzyIsNull(lhs.m_top - rhs.m_top)
: qFuzzyCompare(lhs.m_top, rhs.m_top))
&& ((!lhs.m_right || !rhs.m_right) ? qFuzzyIsNull(lhs.m_right - rhs.m_right)
: qFuzzyCompare(lhs.m_right, rhs.m_right))
&& ((!lhs.m_bottom || !rhs.m_bottom) ? qFuzzyIsNull(lhs.m_bottom - rhs.m_bottom)
: qFuzzyCompare(lhs.m_bottom, rhs.m_bottom));
}
friend constexpr inline bool operator!=(const QMarginsF &lhs, const QMarginsF &rhs) noexcept
{
return !(lhs == rhs);
&& ((!lhs.m_top || !rhs.m_top) ? qFuzzyIsNull(lhs.m_top - rhs.m_top)
: qFuzzyCompare(lhs.m_top, rhs.m_top))
&& ((!lhs.m_right || !rhs.m_right) ? qFuzzyIsNull(lhs.m_right - rhs.m_right)
: qFuzzyCompare(lhs.m_right, rhs.m_right))
&& ((!lhs.m_bottom || !rhs.m_bottom) ? qFuzzyIsNull(lhs.m_bottom - rhs.m_bottom)
: qFuzzyCompare(lhs.m_bottom, rhs.m_bottom));
}
QT_WARNING_POP
Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QMarginsF)
friend constexpr bool comparesEqual(const QMarginsF &lhs, const QMargins &rhs) noexcept
{ return comparesEqual(lhs, rhs.toMarginsF()); }
Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QMarginsF, QMargins)
template <std::size_t I,
typename M,

View File

@ -44,6 +44,7 @@ class tst_QMargins : public QObject
{
Q_OBJECT
private slots:
void comparisonCompiles();
void comparison_data();
void comparison();
@ -71,6 +72,13 @@ private slots:
void toMarginsF();
};
void tst_QMargins::comparisonCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QMargins>();
QTestPrivate::testEqualityOperatorsCompile<QMarginsF>();
QTestPrivate::testEqualityOperatorsCompile<QMarginsF, QMargins>();
}
void tst_QMargins::comparison_data()
{
QTest::addColumn<QMarginsF>("lhs");