diff options
Diffstat (limited to 'src/corelib/ipc/qtipccommon.h')
-rw-r--r-- | src/corelib/ipc/qtipccommon.h | 109 |
1 files changed, 54 insertions, 55 deletions
diff --git a/src/corelib/ipc/qtipccommon.h b/src/corelib/ipc/qtipccommon.h index a922c0f54f..74f30cb6a4 100644 --- a/src/corelib/ipc/qtipccommon.h +++ b/src/corelib/ipc/qtipccommon.h @@ -18,7 +18,7 @@ class QNativeIpcKey { Q_GADGET_EXPORT(Q_CORE_EXPORT) public: - enum class Type : quintptr { + enum class Type : quint16 { // 0 is reserved for the invalid type // keep 1 through 0xff free, except for SystemV SystemV = 0x51, // 'Q' @@ -37,27 +37,28 @@ public: ; static Type legacyDefaultTypeForOs() noexcept; - explicit constexpr QNativeIpcKey(Type type = DefaultTypeForOs) noexcept - : d() + constexpr QNativeIpcKey() noexcept = default; + + explicit constexpr QNativeIpcKey(Type type) noexcept + : typeAndFlags{type} { - typeAndFlags.type = type; } Q_IMPLICIT QNativeIpcKey(const QString &k, Type type = DefaultTypeForOs) - : d(), key(k) + : key(k), typeAndFlags{type} { - typeAndFlags.type = type; } QNativeIpcKey(const QNativeIpcKey &other) - : d(other.d), key(other.key) + : d(other.d), key(other.key), typeAndFlags(other.typeAndFlags) { if (isSlowPath()) copy_internal(other); } QNativeIpcKey(QNativeIpcKey &&other) noexcept - : d(std::exchange(other.d, 0)), key(std::move(other.key)) + : d(std::exchange(other.d, nullptr)), key(std::move(other.key)), + typeAndFlags(std::move(other.typeAndFlags)) { if (isSlowPath()) move_internal(std::move(other)); @@ -71,34 +72,34 @@ public: QNativeIpcKey &operator=(const QNativeIpcKey &other) { + typeAndFlags = other.typeAndFlags; key = other.key; if (isSlowPath() || other.isSlowPath()) return assign_internal(other); - d = other.d; + Q_ASSERT(!d); return *this; } QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QNativeIpcKey) - void swap(QNativeIpcKey &other) + void swap(QNativeIpcKey &other) noexcept { std::swap(d, other.d); key.swap(other.key); + typeAndFlags.swap(other.typeAndFlags); } - bool isEmpty() const + bool isEmpty() const noexcept { return key.isEmpty(); } - bool isValid() const + bool isValid() const noexcept { return type() != Type{}; } constexpr Type type() const noexcept { - if (isSlowPath()) - return type_internal(); return typeAndFlags.type; } @@ -110,7 +111,9 @@ public: } QString nativeKey() const noexcept - { return key; } + { + return key; + } void setNativeKey(const QString &newKey) { key = newKey; @@ -123,57 +126,41 @@ public: private: struct TypeAndFlags { -#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN - // this is the LSB - quintptr isExtended : 1; - Type type : 15; -#endif - - quintptr reserved : sizeof(quintptr) * 8 - 16; - -#if Q_BYTE_ORDER == Q_BIG_ENDIAN - Type type : 15; - quint16 isExtended : 1; - // this was the LSB -#endif - }; - - // Bit 0: if set, holds a pointer (with the LSB set); if clear, holds the - // the TypeAndFlags structure. - union { - quintptr d = 0; - TypeAndFlags typeAndFlags; - static_assert(sizeof(typeAndFlags) == sizeof(d)); + Type type = DefaultTypeForOs; + quint16 reserved1 = {}; + quint32 reserved2 = {}; + + void swap(TypeAndFlags &other) noexcept + { + std::swap(type, other.type); + std::swap(reserved1, other.reserved1); + std::swap(reserved2, other.reserved2); + } + + friend constexpr bool operator==(const TypeAndFlags &lhs, const TypeAndFlags &rhs) noexcept + { + return lhs.type == rhs.type && + lhs.reserved1 == rhs.reserved1 && + lhs.reserved2 == rhs.reserved2; + } }; + QNativeIpcKeyPrivate *d = nullptr; QString key; + TypeAndFlags typeAndFlags; friend class QNativeIpcKeyPrivate; - QNativeIpcKeyPrivate *d_func(); - const QNativeIpcKeyPrivate *d_func() const; constexpr bool isSlowPath() const noexcept - { return Q_UNLIKELY(typeAndFlags.isExtended); } + { return Q_UNLIKELY(d); } - friend bool operator==(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept - { - if (lhs.key != rhs.key) - return false; - if (lhs.d == rhs.d) - return true; - if (lhs.isSlowPath() && rhs.isSlowPath()) - return compare_internal(lhs, rhs) == 0; - return lhs.d == rhs.d; - } - friend bool operator!=(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept - { - return !(lhs == rhs); - } + friend Q_CORE_EXPORT size_t qHash(const QNativeIpcKey &ipcKey, size_t seed) noexcept; + friend size_t qHash(const QNativeIpcKey &ipcKey) noexcept + { return qHash(ipcKey, 0); } 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); Q_CORE_EXPORT void destroy_internal() noexcept; - Q_DECL_PURE_FUNCTION Q_CORE_EXPORT Type type_internal() const noexcept; Q_CORE_EXPORT void setType_internal(Type); Q_CORE_EXPORT void setNativeKey_internal(const QString &); Q_DECL_PURE_FUNCTION Q_CORE_EXPORT static int @@ -182,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 @@ -200,8 +198,9 @@ inline auto QNativeIpcKey::legacyDefaultTypeForOs() noexcept -> Type #endif } +QT_END_NAMESPACE + #endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore) -QT_END_NAMESPACE #endif // QNATIVEIPCKEY_H |