diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-02-16 13:42:19 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-05-08 01:36:32 +0200 |
commit | b4a05b123343f0412973f0848bf8443d1fbc046b (patch) | |
tree | bd5e52995d07b7255f3c76b531f60b092ac618a5 | |
parent | 657fa0462d552110e2ba14bcac46275e6066993f (diff) |
Provide qHash for all C++ fundamental types
Except for (void and) bool, which we may not want to have
to avoid accidental implicit conversions.
Drive-by, rearrange qHash overloads to C++ types first,
and then Qt ones.
Change-Id: I9c4ecef5f28568d35ca52e339583851ce53b3bae
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r-- | src/corelib/tools/qhash.cpp | 35 | ||||
-rw-r--r-- | src/corelib/tools/qhashfunctions.h | 30 |
2 files changed, 56 insertions, 9 deletions
diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index 97dda58748..bf5031523d 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -769,6 +769,34 @@ uint qt_hash(QStringView key, uint chained) noexcept Returns the hash value for the \a key, using \a seed to seed the calculation. */ +/*! \fn size_t qHash(char8_t key, size_t seed = 0) + \relates QHash + \since 6.0 + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + +/*! \fn size_t qHash(char16_t key, size_t seed = 0) + \relates QHash + \since 6.0 + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + +/*! \fn size_t qHash(char32_t key, size_t seed = 0) + \relates QHash + \since 6.0 + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + +/*! \fn size_t qHash(wchar_t key, size_t seed = 0) + \relates QHash + \since 6.0 + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + /*! \fn size_t qHash(float key, size_t seed) noexcept \relates QHash \since 5.3 @@ -870,6 +898,13 @@ size_t qHash(long double key, size_t seed) noexcept Returns the hash value for the \a key, using \a seed to seed the calculation. */ +/*! \fn template <class T> size_t qHash(std::nullptr_t key, size_t seed = 0) + \relates QHash + \since 6.0 + + Returns the hash value for the \a key, using \a seed to seed the calculation. +*/ + /*! \class QHash \inmodule QtCore diff --git a/src/corelib/tools/qhashfunctions.h b/src/corelib/tools/qhashfunctions.h index e52688b88d..aaa7758758 100644 --- a/src/corelib/tools/qhashfunctions.h +++ b/src/corelib/tools/qhashfunctions.h @@ -95,6 +95,7 @@ Q_DECL_CONST_FUNCTION constexpr size_t hash(size_t key, size_t seed) noexcept Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHashBits(const void *p, size_t size, size_t seed = 0) noexcept; +// C++ builtin types Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(char key, size_t seed = 0) noexcept { return QHashPrivate::hash(size_t(key), seed); } Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(uchar key, size_t seed = 0) noexcept @@ -132,6 +133,26 @@ Q_CORE_EXPORT Q_DECL_CONST_FUNCTION size_t qHash(double key, size_t seed = 0) no #if !defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC) Q_CORE_EXPORT Q_DECL_CONST_FUNCTION size_t qHash(long double key, size_t seed = 0) noexcept; #endif +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(wchar_t key, size_t seed = 0) noexcept +{ return QHashPrivate::hash(size_t(key), seed); } +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(char16_t key, size_t seed = 0) noexcept +{ return QHashPrivate::hash(size_t(key), seed); } +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(char32_t key, size_t seed = 0) noexcept +{ return QHashPrivate::hash(size_t(key), seed); } +#ifdef __cpp_char8_t +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(char8_t key, size_t seed = 0) noexcept +{ return QHashPrivate::hash(size_t(key), seed); } +#endif +template <class T> inline size_t qHash(const T *key, size_t seed = 0) noexcept +{ + return qHash(reinterpret_cast<quintptr>(key), seed); +} +Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(std::nullptr_t, size_t seed = 0) noexcept +{ + return seed; +} + +// (some) Qt types Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(const QChar key, size_t seed = 0) noexcept { return qHash(key.unicode(), seed); } Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QByteArray &key, size_t seed = 0) noexcept; #if QT_STRINGVIEW_LEVEL < 2 @@ -143,15 +164,6 @@ Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QBitArray &key, size_t see Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(QLatin1String key, size_t seed = 0) noexcept; Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) noexcept; -Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(std::nullptr_t, size_t seed = 0) noexcept -{ - return seed; -} - -template <class T> inline size_t qHash(const T *key, size_t seed = 0) noexcept -{ - return qHash(reinterpret_cast<quintptr>(key), seed); -} template<typename T> inline size_t qHash(const T &t, size_t seed) noexcept(noexcept(qHash(t))) { return qHash(t) ^ seed; } |