From d72f2ca9ea5e8bc7357f457370b2eb039bb48a2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Mon, 4 Apr 2022 19:40:52 +0200 Subject: [PATCH] QMetaType: Add qHash() overload This is needed for use in QHash and similar. It's essentially the same as using the integer id, but typesafe Change-Id: I9515b8e178c8f9828934cb0bb099cce5553ec393 Reviewed-by: Fabian Kosmale Reviewed-by: Marc Mutz --- src/corelib/kernel/qmetatype.cpp | 8 ++++++++ src/corelib/kernel/qmetatype.h | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index a73adaa99a8..f0f93e21a17 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -3002,6 +3002,14 @@ static const QtPrivate::QMetaTypeInterface *interfaceForType(int typeId) */ QMetaType::QMetaType(int typeId) : QMetaType(interfaceForType(typeId)) {} + +/*! \fn size_t qHash(QMetaType type, size_t seed = 0) + \relates QMetaType + \since 6.4 + + Returns the hash value for the \a type, using \a seed to seed the calculation. +*/ + namespace QtPrivate { #if !defined(QT_BOOTSTRAPPED) && !defined(Q_CC_MSVC) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index a8962f8b573..8b527524706 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -52,6 +52,7 @@ #ifndef QT_NO_QOBJECT #include #endif +#include #include #include @@ -2536,6 +2537,13 @@ constexpr const QtPrivate::QMetaTypeInterface *const qt_incomplete_metaTypeArray QtPrivate::qTryMetaTypeInterfaceForType()... }; +inline size_t qHash(QMetaType type, size_t seed = 0) +{ + // We cannot use d_ptr here since the same type in different DLLs + // might result in different pointers! + return qHash(type.id(), seed); +} + QT_END_NAMESPACE QT_DECL_METATYPE_EXTERN_TAGGED(QtMetaTypePrivate::QPairVariantInterfaceImpl,