From 3059f14524ce7d0abec562af5e96510f488ee525 Mon Sep 17 00:00:00 2001 From: Tatiana Borisova Date: Wed, 27 Mar 2024 18:48:53 +0100 Subject: [PATCH] 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 --- src/corelib/ipc/qtipccommon.cpp | 2 ++ src/corelib/ipc/qtipccommon.h | 26 ++++++++----------- .../corelib/ipc/qnativeipckey/CMakeLists.txt | 2 ++ .../ipc/qnativeipckey/tst_qnativeipckey.cpp | 16 ++++++++++++ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/corelib/ipc/qtipccommon.cpp b/src/corelib/ipc/qtipccommon.cpp index 1a92d173e60..b2ae9172fa5 100644 --- a/src/corelib/ipc/qtipccommon.cpp +++ b/src/corelib/ipc/qtipccommon.cpp @@ -244,6 +244,8 @@ QNativeIpcKey QtIpcCommon::platformSafeKey(const QString &key, QtIpcCommon::IpcT \since 6.6 \brief The QNativeIpcKey class holds a native key used by QSystemSemaphore and QSharedMemory. + \compares equality + The \l QSharedMemory and \l QSystemSemaphore classes identify their 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 diff --git a/src/corelib/ipc/qtipccommon.h b/src/corelib/ipc/qtipccommon.h index bf0936cb42a..74f30cb6a44 100644 --- a/src/corelib/ipc/qtipccommon.h +++ b/src/corelib/ipc/qtipccommon.h @@ -157,21 +157,6 @@ private: friend size_t qHash(const QNativeIpcKey &ipcKey) noexcept { 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 move_internal(QNativeIpcKey &&other) noexcept; Q_CORE_EXPORT QNativeIpcKey &assign_internal(const QNativeIpcKey &other); @@ -184,6 +169,17 @@ private: #ifdef Q_OS_DARWIN Q_DECL_CONST_FUNCTION Q_CORE_EXPORT static Type defaultTypeForOs_internal() noexcept; #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 diff --git a/tests/auto/corelib/ipc/qnativeipckey/CMakeLists.txt b/tests/auto/corelib/ipc/qnativeipckey/CMakeLists.txt index aba89460022..0cc6a7b18be 100644 --- a/tests/auto/corelib/ipc/qnativeipckey/CMakeLists.txt +++ b/tests/auto/corelib/ipc/qnativeipckey/CMakeLists.txt @@ -10,4 +10,6 @@ endif() qt_internal_add_test(tst_qnativeipckey SOURCES tst_qnativeipckey.cpp + LIBRARIES + Qt::TestPrivate ) diff --git a/tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp b/tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp index cd1a07a4d00..a01a108591e 100644 --- a/tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp +++ b/tests/auto/corelib/ipc/qnativeipckey/tst_qnativeipckey.cpp @@ -3,6 +3,7 @@ #include #include +#include #include "../ipctestcommon.h" @@ -25,6 +26,7 @@ class tst_QNativeIpcKey : public QObject { Q_OBJECT private slots: + void compareCompiles(); void defaultTypes(); void construct(); void getSetCheck(); @@ -39,6 +41,11 @@ private slots: void legacyKeys(); }; +void tst_QNativeIpcKey::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile(); +} + void tst_QNativeIpcKey::defaultTypes() { auto isKnown = [](QNativeIpcKey::Type t) { @@ -180,35 +187,43 @@ void tst_QNativeIpcKey::equality() QNativeIpcKey key1, key2; QCOMPARE(key1, key2); QVERIFY(!(key1 != key2)); + QT_TEST_EQUALITY_OPS(key1, key2, true); key1.setNativeKey("key1"); QCOMPARE_NE(key1, key2); QVERIFY(!(key1 == key2)); + QT_TEST_EQUALITY_OPS(key1, key2, false); key2.setType({}); QCOMPARE_NE(key1, key2); QVERIFY(!(key1 == key2)); + QT_TEST_EQUALITY_OPS(key1, key2, false); key2.setNativeKey(key1.nativeKey()); QCOMPARE_NE(key1, key2); QVERIFY(!(key1 == key2)); + QT_TEST_EQUALITY_OPS(key1, key2, false); key2.setType(QNativeIpcKey::DefaultTypeForOs); QCOMPARE(key1, key2); QVERIFY(!(key1 != key2)); + QT_TEST_EQUALITY_OPS(key1, key2, true); key1 = makeLegacyKey("key1", QNativeIpcKey::DefaultTypeForOs); QCOMPARE_NE(key1, key2); QVERIFY(!(key1 == key2)); + QT_TEST_EQUALITY_OPS(key1, key2, false); key2 = key1; QCOMPARE(key1, key2); QVERIFY(!(key1 != key2)); + QT_TEST_EQUALITY_OPS(key1, key2, true); // just setting the native key won't make them equal again! key2.setNativeKey(key1.nativeKey()); QCOMPARE_NE(key1, key2); QVERIFY(!(key1 == key2)); + QT_TEST_EQUALITY_OPS(key1, key2, false); } void tst_QNativeIpcKey::hash() @@ -410,6 +425,7 @@ void tst_QNativeIpcKey::legacyKeys() QString string = key.toString(); QNativeIpcKey key2 = QNativeIpcKey::fromString(string); QCOMPARE(key2, key); + QT_TEST_EQUALITY_OPS(key, key2, true); if (!legacyKey.isEmpty()) { // confirm it shows up in the encoded form