QFont::Tag: use new comparison helper macros

Task-number: QTBUG-104111
Change-Id: Id57b075d00e657c2606b6c1a8f1215ed0d067cbd
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Volker Hilsheimer 2023-10-13 12:10:00 +02:00
parent cd39a469a9
commit c0b1eaaaaa
3 changed files with 37 additions and 9 deletions

View File

@ -2311,9 +2311,8 @@ void QFont::cacheStatistics()
*/
/*!
\fn bool QFont::Tag::operator==(QFont::Tag::Tag lhs, QFont::Tag::Tag rhs) noexcept
\fn bool QFont::Tag::operator!=(QFont::Tag::Tag lhs, QFont::Tag::Tag rhs) noexcept
\fn bool QFont::Tag::operator<(QFont::Tag::Tag lhs, QFont::Tag::Tag rhs) noexcept
\fn bool QFont::Tag::comparesEqual(const QFont::Tag &lhs, const QFont::Tag &rhs) noexcept
\fn Qt::strong_ordering QFont::Tag::compareThreeWay(const QFont::Tag &lhs, const QFont::Tag &rhs) noexcept
Compare \a lhs with \a rhs for equality and ordering.
*/

View File

@ -4,6 +4,7 @@
#ifndef QFONT_H
#define QFONT_H
#include <QtCore/qcompare.h>
#include <QtCore/qendian.h>
#include <QtCore/qshareddata.h>
#include <QtGui/qtguiglobal.h>
@ -221,12 +222,6 @@ public:
static_assert(N == 5, "The tag name must be exactly 4 characters long!");
}
friend inline bool operator==(Tag lhs, Tag rhs) noexcept
{ return lhs.m_value == rhs.m_value; }
friend inline bool operator!=(Tag lhs, Tag rhs) noexcept
{ return lhs.m_value != rhs.m_value; }
friend inline bool operator<(Tag lhs, Tag rhs) noexcept
{ return lhs.m_value < rhs.m_value; }
constexpr bool isValid() const noexcept { return m_value != 0; }
constexpr quint32 value() const noexcept { return m_value; }
@ -262,6 +257,12 @@ public:
{ return qHash(key.value(), seed); }
private:
friend constexpr bool comparesEqual(const Tag &lhs, const Tag &rhs) noexcept
{ return lhs.m_value == rhs.m_value; }
friend constexpr Qt::strong_ordering compareThreeWay(const Tag &lhs, const Tag &rhs) noexcept
{ return Qt::compareThreeWay(lhs.m_value, rhs.m_value); }
Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QFont::Tag)
quint32 m_value = 0;
};

View File

@ -21,6 +21,7 @@
#endif
#include <qlist.h>
#include <QtTest/private/qemulationdetector_p.h>
#include <private/qcomparisontesthelper_p.h>
using namespace Qt::StringLiterals;
@ -59,6 +60,8 @@ private slots:
void setFamiliesAndFamily_data();
void setFamiliesAndFamily();
void featureAccessors();
void tagCompares_data();
void tagCompares();
};
// Testing get/set functions
@ -899,5 +902,30 @@ void tst_QFont::featureAccessors()
};
}
void tst_QFont::tagCompares_data()
{
QTestPrivate::testAllComparisonOperatorsCompile<QFont::Tag>();
QTest::addColumn<QFont::Tag>("lhs");
QTest::addColumn<QFont::Tag>("rhs");
QTest::addColumn<Qt::strong_ordering>("expectedOrder");
auto row = [](QFont::Tag left, QFont::Tag right) {
QTest::addRow("%s<=>%s", left.toString().constData(), right.toString().constData())
<< left << right << Qt::compareThreeWay(left.value(), right.value());
};
row("frac", "wght");
}
void tst_QFont::tagCompares()
{
QFETCH(QFont::Tag, lhs);
QFETCH(QFont::Tag, rhs);
QFETCH(Qt::strong_ordering, expectedOrder);
QVERIFY(comparesEqual(lhs, lhs));
QCOMPARE(compareThreeWay(lhs, rhs), expectedOrder);
}
QTEST_MAIN(tst_QFont)
#include "tst_qfont.moc"