From eb178d3e51019747bcf9446eef62358e9b9072d2 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Fri, 19 Apr 2024 18:06:27 +0200 Subject: [PATCH] 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 --- src/corelib/tools/qmargins.cpp | 16 +++++-- src/corelib/tools/qmargins.h | 42 +++++++++---------- .../corelib/tools/qmargins/tst_qmargins.cpp | 8 ++++ 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/corelib/tools/qmargins.cpp b/src/corelib/tools/qmargins.cpp index 1d2cb7d6e5d..fbb431e7a43 100644 --- a/src/corelib/tools/qmargins.cpp +++ b/src/corelib/tools/qmargins.cpp @@ -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, diff --git a/src/corelib/tools/qmargins.h b/src/corelib/tools/qmargins.h index 63c0b2a8e23..feaa91824a1 100644 --- a/src/corelib/tools/qmargins.h +++ b/src/corelib/tools/qmargins.h @@ -4,6 +4,7 @@ #ifndef QMARGINS_H #define QMARGINS_H +#include #include #include @@ -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 (); + QTestPrivate::testEqualityOperatorsCompile(); + QTestPrivate::testEqualityOperatorsCompile(); +} + void tst_QMargins::comparison_data() { QTest::addColumn("lhs");