QNativeIpcKey: use new comparison helper macros

Replace public friend operators operator==(), operator!=() of
QNativeIpcKey to friend methods comparesEqual() and
Q_DECLARE_EQUALITY_COMPARABLE macro.

Task-number: QTBUG-120304
Change-Id: If18d86fb18e44f8d2210cba7ca93e4ac478a2a48
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Tatiana Borisova 2024-03-27 18:48:53 +01:00
parent 7f3e43b32c
commit 3059f14524
4 changed files with 31 additions and 15 deletions

View File

@ -244,6 +244,8 @@ QNativeIpcKey QtIpcCommon::platformSafeKey(const QString &key, QtIpcCommon::IpcT
\since 6.6 \since 6.6
\brief The QNativeIpcKey class holds a native key used by QSystemSemaphore and QSharedMemory. \brief The QNativeIpcKey class holds a native key used by QSystemSemaphore and QSharedMemory.
\compares equality
The \l QSharedMemory and \l QSystemSemaphore classes identify their The \l QSharedMemory and \l QSystemSemaphore classes identify their
resource using a system-wide identifier known as a "key". The low-level key resource using a system-wide identifier known as a "key". The low-level key
value as well as the key type are encapsulated in Qt using the \l value as well as the key type are encapsulated in Qt using the \l

View File

@ -157,21 +157,6 @@ private:
friend size_t qHash(const QNativeIpcKey &ipcKey) noexcept friend size_t qHash(const QNativeIpcKey &ipcKey) noexcept
{ return qHash(ipcKey, 0); } { return qHash(ipcKey, 0); }
friend bool operator==(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept
{
if (!(lhs.typeAndFlags == rhs.typeAndFlags))
return false;
if (lhs.key != rhs.key)
return false;
if (lhs.d == rhs.d)
return true;
return compare_internal(lhs, rhs) == 0;
}
friend bool operator!=(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept
{
return !(lhs == rhs);
}
Q_CORE_EXPORT void copy_internal(const QNativeIpcKey &other); Q_CORE_EXPORT void copy_internal(const QNativeIpcKey &other);
Q_CORE_EXPORT void move_internal(QNativeIpcKey &&other) noexcept; Q_CORE_EXPORT void move_internal(QNativeIpcKey &&other) noexcept;
Q_CORE_EXPORT QNativeIpcKey &assign_internal(const QNativeIpcKey &other); Q_CORE_EXPORT QNativeIpcKey &assign_internal(const QNativeIpcKey &other);
@ -184,6 +169,17 @@ private:
#ifdef Q_OS_DARWIN #ifdef Q_OS_DARWIN
Q_DECL_CONST_FUNCTION Q_CORE_EXPORT static Type defaultTypeForOs_internal() noexcept; Q_DECL_CONST_FUNCTION Q_CORE_EXPORT static Type defaultTypeForOs_internal() noexcept;
#endif #endif
friend bool comparesEqual(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept
{
if (!(lhs.typeAndFlags == rhs.typeAndFlags))
return false;
if (lhs.key != rhs.key)
return false;
if (lhs.d == rhs.d)
return true;
return compare_internal(lhs, rhs) == 0;
}
Q_DECLARE_EQUALITY_COMPARABLE(QNativeIpcKey)
}; };
// not a shared type, exactly, but this works too // not a shared type, exactly, but this works too

View File

@ -10,4 +10,6 @@ endif()
qt_internal_add_test(tst_qnativeipckey qt_internal_add_test(tst_qnativeipckey
SOURCES SOURCES
tst_qnativeipckey.cpp tst_qnativeipckey.cpp
LIBRARIES
Qt::TestPrivate
) )

View File

@ -3,6 +3,7 @@
#include <QtCore/QNativeIpcKey> #include <QtCore/QNativeIpcKey>
#include <QtTest/QTest> #include <QtTest/QTest>
#include <QtTest/private/qcomparisontesthelper_p.h>
#include "../ipctestcommon.h" #include "../ipctestcommon.h"
@ -25,6 +26,7 @@ class tst_QNativeIpcKey : public QObject
{ {
Q_OBJECT Q_OBJECT
private slots: private slots:
void compareCompiles();
void defaultTypes(); void defaultTypes();
void construct(); void construct();
void getSetCheck(); void getSetCheck();
@ -39,6 +41,11 @@ private slots:
void legacyKeys(); void legacyKeys();
}; };
void tst_QNativeIpcKey::compareCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QNativeIpcKey>();
}
void tst_QNativeIpcKey::defaultTypes() void tst_QNativeIpcKey::defaultTypes()
{ {
auto isKnown = [](QNativeIpcKey::Type t) { auto isKnown = [](QNativeIpcKey::Type t) {
@ -180,35 +187,43 @@ void tst_QNativeIpcKey::equality()
QNativeIpcKey key1, key2; QNativeIpcKey key1, key2;
QCOMPARE(key1, key2); QCOMPARE(key1, key2);
QVERIFY(!(key1 != key2)); QVERIFY(!(key1 != key2));
QT_TEST_EQUALITY_OPS(key1, key2, true);
key1.setNativeKey("key1"); key1.setNativeKey("key1");
QCOMPARE_NE(key1, key2); QCOMPARE_NE(key1, key2);
QVERIFY(!(key1 == key2)); QVERIFY(!(key1 == key2));
QT_TEST_EQUALITY_OPS(key1, key2, false);
key2.setType({}); key2.setType({});
QCOMPARE_NE(key1, key2); QCOMPARE_NE(key1, key2);
QVERIFY(!(key1 == key2)); QVERIFY(!(key1 == key2));
QT_TEST_EQUALITY_OPS(key1, key2, false);
key2.setNativeKey(key1.nativeKey()); key2.setNativeKey(key1.nativeKey());
QCOMPARE_NE(key1, key2); QCOMPARE_NE(key1, key2);
QVERIFY(!(key1 == key2)); QVERIFY(!(key1 == key2));
QT_TEST_EQUALITY_OPS(key1, key2, false);
key2.setType(QNativeIpcKey::DefaultTypeForOs); key2.setType(QNativeIpcKey::DefaultTypeForOs);
QCOMPARE(key1, key2); QCOMPARE(key1, key2);
QVERIFY(!(key1 != key2)); QVERIFY(!(key1 != key2));
QT_TEST_EQUALITY_OPS(key1, key2, true);
key1 = makeLegacyKey("key1", QNativeIpcKey::DefaultTypeForOs); key1 = makeLegacyKey("key1", QNativeIpcKey::DefaultTypeForOs);
QCOMPARE_NE(key1, key2); QCOMPARE_NE(key1, key2);
QVERIFY(!(key1 == key2)); QVERIFY(!(key1 == key2));
QT_TEST_EQUALITY_OPS(key1, key2, false);
key2 = key1; key2 = key1;
QCOMPARE(key1, key2); QCOMPARE(key1, key2);
QVERIFY(!(key1 != key2)); QVERIFY(!(key1 != key2));
QT_TEST_EQUALITY_OPS(key1, key2, true);
// just setting the native key won't make them equal again! // just setting the native key won't make them equal again!
key2.setNativeKey(key1.nativeKey()); key2.setNativeKey(key1.nativeKey());
QCOMPARE_NE(key1, key2); QCOMPARE_NE(key1, key2);
QVERIFY(!(key1 == key2)); QVERIFY(!(key1 == key2));
QT_TEST_EQUALITY_OPS(key1, key2, false);
} }
void tst_QNativeIpcKey::hash() void tst_QNativeIpcKey::hash()
@ -410,6 +425,7 @@ void tst_QNativeIpcKey::legacyKeys()
QString string = key.toString(); QString string = key.toString();
QNativeIpcKey key2 = QNativeIpcKey::fromString(string); QNativeIpcKey key2 = QNativeIpcKey::fromString(string);
QCOMPARE(key2, key); QCOMPARE(key2, key);
QT_TEST_EQUALITY_OPS(key, key2, true);
if (!legacyKey.isEmpty()) { if (!legacyKey.isEmpty()) {
// confirm it shows up in the encoded form // confirm it shows up in the encoded form