summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-08-02 15:51:52 +0200
committerQt by Nokia <qt-info@nokia.com>2012-08-20 21:59:21 +0200
commit373845dfd8acae0c941432c8cd615b77bcae0a2a (patch)
treed8d88bdd6657a18f55456bfe2ac5dfff682e76aa
parent291938aea6771a0ce4c11a164583a73c6eef026e (diff)
Add Q_DECL_NOTHROW to some qHash functions
The hashing functions for QDateTime and QHostAddress did not get the noexcept keyword because they might allocate memory. QDateTime doesn't do it now, but it could in the future. QHostAddress does allocate memory today. Change-Id: Ia5f80942944bfc2b8c405306c467bfd88ef0e48c Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> Reviewed-by: Marc Mutz <marc.mutz@kdab.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/io/qurl.cpp2
-rw-r--r--src/corelib/io/qurl.h2
-rw-r--r--src/corelib/plugin/quuid.cpp2
-rw-r--r--src/corelib/plugin/quuid.h2
-rw-r--r--src/corelib/tools/qbitarray.h2
-rw-r--r--src/corelib/tools/qdatetime.cpp6
-rw-r--r--src/corelib/tools/qdatetime.h6
-rw-r--r--src/corelib/tools/qhash.cpp16
-rw-r--r--src/corelib/tools/qhash.h43
-rw-r--r--src/network/kernel/qhostaddress.cpp1
10 files changed, 44 insertions, 38 deletions
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp
index 25006b14f7..3b49b82637 100644
--- a/src/corelib/io/qurl.cpp
+++ b/src/corelib/io/qurl.cpp
@@ -3445,7 +3445,7 @@ QString QUrl::errorString() const
\relates QHash
\since 5.0
*/
-uint qHash(const QUrl &url, uint seed)
+uint qHash(const QUrl &url, uint seed) Q_DECL_NOTHROW
{
if (!url.d)
return qHash(-1, seed); // the hash of an unset port (-1)
diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h
index 475806e7e4..8055129e4b 100644
--- a/src/corelib/io/qurl.h
+++ b/src/corelib/io/qurl.h
@@ -321,7 +321,7 @@ public:
static QByteArray toAce(const QString &);
static QStringList idnWhitelist();
static void setIdnWhitelist(const QStringList &);
- friend Q_CORE_EXPORT uint qHash(const QUrl &url, uint seed = 0);
+ friend Q_CORE_EXPORT uint qHash(const QUrl &url, uint seed = 0) Q_DECL_NOTHROW;
private:
QUrlPrivate *d;
diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp
index dc8958de4f..095fcda1f7 100644
--- a/src/corelib/plugin/quuid.cpp
+++ b/src/corelib/plugin/quuid.cpp
@@ -1006,7 +1006,7 @@ QDebug operator<<(QDebug dbg, const QUuid &id)
\relates QUuid
Returns a hash of the UUID \a uuid, using \a seed to seed the calculation.
*/
-uint qHash(const QUuid &uuid, uint seed)
+uint qHash(const QUuid &uuid, uint seed) Q_DECL_NOTHROW
{
return uuid.data1 ^ uuid.data2 ^ (uuid.data3 << 16)
^ ((uuid.data4[0] << 24) | (uuid.data4[1] << 16) | (uuid.data4[2] << 8) | uuid.data4[3])
diff --git a/src/corelib/plugin/quuid.h b/src/corelib/plugin/quuid.h
index 7fd32a307b..acc073c1a9 100644
--- a/src/corelib/plugin/quuid.h
+++ b/src/corelib/plugin/quuid.h
@@ -227,7 +227,7 @@ Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUuid &);
Q_CORE_EXPORT QDebug operator<<(QDebug, const QUuid &);
#endif
-Q_CORE_EXPORT uint qHash(const QUuid &uuid, uint seed = 0);
+Q_CORE_EXPORT uint qHash(const QUuid &uuid, uint seed = 0) Q_DECL_NOTHROW;
QT_END_NAMESPACE
diff --git a/src/corelib/tools/qbitarray.h b/src/corelib/tools/qbitarray.h
index 403c266941..6718fbea0f 100644
--- a/src/corelib/tools/qbitarray.h
+++ b/src/corelib/tools/qbitarray.h
@@ -54,7 +54,7 @@ class Q_CORE_EXPORT QBitArray
{
friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
- friend Q_CORE_EXPORT uint qHash(const QBitArray &key, uint seed);
+ friend Q_CORE_EXPORT uint qHash(const QBitArray &key, uint seed) Q_DECL_NOTHROW;
QByteArray d;
public:
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index 8c9944b001..162117602b 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -4097,7 +4097,7 @@ uint qHash(const QDateTime &key, uint seed)
Returns the hash value for the \a key, using \a seed to seed the calculation.
*/
-uint qHash(const QDate &key, uint seed)
+uint qHash(const QDate &key, uint seed) Q_DECL_NOTHROW
{
return qHash(key.toJulianDay(), seed);
}
@@ -4108,9 +4108,9 @@ uint qHash(const QDate &key, uint seed)
Returns the hash value for the \a key, using \a seed to seed the calculation.
*/
-uint qHash(const QTime &key, uint seed)
+uint qHash(const QTime &key, uint seed) Q_DECL_NOTHROW
{
- return QTime(0, 0, 0, 0).msecsTo(key) ^ seed;
+ return qHash(QTime(0, 0, 0, 0).msecsTo(key), seed);
}
#ifndef QT_BOOTSTRAPPED
diff --git a/src/corelib/tools/qdatetime.h b/src/corelib/tools/qdatetime.h
index aa7082b4e1..13c7ed0979 100644
--- a/src/corelib/tools/qdatetime.h
+++ b/src/corelib/tools/qdatetime.h
@@ -288,9 +288,11 @@ Q_CORE_EXPORT QDebug operator<<(QDebug, const QTime &);
Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
#endif
+// QDateTime is not noexcept for now -- to be revised once
+// timezone and calendaring support is added
Q_CORE_EXPORT uint qHash(const QDateTime &key, uint seed = 0);
-Q_CORE_EXPORT uint qHash(const QDate &key, uint seed = 0);
-Q_CORE_EXPORT uint qHash(const QTime &key, uint seed = 0);
+Q_CORE_EXPORT uint qHash(const QDate &key, uint seed = 0) Q_DECL_NOTHROW;
+Q_CORE_EXPORT uint qHash(const QTime &key, uint seed = 0) Q_DECL_NOTHROW;
QT_END_NAMESPACE
diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp
index 97f846c6c8..d8169a647b 100644
--- a/src/corelib/tools/qhash.cpp
+++ b/src/corelib/tools/qhash.cpp
@@ -93,7 +93,7 @@ QT_BEGIN_NAMESPACE
(for instance, gcc 4.4 does that even at -O0).
*/
-static inline uint hash(const uchar *p, int len, uint seed)
+static inline uint hash(const uchar *p, int len, uint seed) Q_DECL_NOTHROW
{
uint h = seed;
@@ -103,7 +103,7 @@ static inline uint hash(const uchar *p, int len, uint seed)
return h;
}
-static inline uint hash(const QChar *p, int len, uint seed)
+static inline uint hash(const QChar *p, int len, uint seed) Q_DECL_NOTHROW
{
uint h = seed;
@@ -113,22 +113,22 @@ static inline uint hash(const QChar *p, int len, uint seed)
return h;
}
-uint qHash(const QByteArray &key, uint seed)
+uint qHash(const QByteArray &key, uint seed) Q_DECL_NOTHROW
{
return hash(reinterpret_cast<const uchar *>(key.constData()), key.size(), seed);
}
-uint qHash(const QString &key, uint seed)
+uint qHash(const QString &key, uint seed) Q_DECL_NOTHROW
{
return hash(key.unicode(), key.size(), seed);
}
-uint qHash(const QStringRef &key, uint seed)
+uint qHash(const QStringRef &key, uint seed) Q_DECL_NOTHROW
{
return hash(key.unicode(), key.size(), seed);
}
-uint qHash(const QBitArray &bitArray, uint seed)
+uint qHash(const QBitArray &bitArray, uint seed) Q_DECL_NOTHROW
{
int m = bitArray.d.size() - 1;
uint result = hash(reinterpret_cast<const uchar *>(bitArray.d.constData()), qMax(0, m), seed);
@@ -141,7 +141,7 @@ uint qHash(const QBitArray &bitArray, uint seed)
return result;
}
-uint qHash(QLatin1String key, uint seed)
+uint qHash(QLatin1String key, uint seed) Q_DECL_NOTHROW
{
return hash(reinterpret_cast<const uchar *>(key.data()), key.size(), seed);
}
@@ -242,7 +242,7 @@ static void qt_initialize_qhash_seed()
This function must *never* change its results.
*/
-uint qt_hash(const QString &key)
+uint qt_hash(const QString &key) Q_DECL_NOTHROW
{
const QChar *p = key.unicode();
int n = key.size();
diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h
index 80ed456ace..8e86fd26c1 100644
--- a/src/corelib/tools/qhash.h
+++ b/src/corelib/tools/qhash.h
@@ -58,14 +58,14 @@ class QString;
class QStringRef;
class QLatin1String;
-inline uint qHash(char key, uint seed = 0) { return uint(key) ^ seed; }
-inline uint qHash(uchar key, uint seed = 0) { return uint(key) ^ seed; }
-inline uint qHash(signed char key, uint seed = 0) { return uint(key) ^ seed; }
-inline uint qHash(ushort key, uint seed = 0) { return uint(key) ^ seed; }
-inline uint qHash(short key, uint seed = 0) { return uint(key) ^ seed; }
-inline uint qHash(uint key, uint seed = 0) { return key ^ seed; }
-inline uint qHash(int key, uint seed = 0) { return uint(key) ^ seed; }
-inline uint qHash(ulong key, uint seed = 0)
+inline uint qHash(char key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
+inline uint qHash(uchar key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
+inline uint qHash(signed char key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
+inline uint qHash(ushort key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
+inline uint qHash(short key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
+inline uint qHash(uint key, uint seed = 0) Q_DECL_NOTHROW { return key ^ seed; }
+inline uint qHash(int key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
+inline uint qHash(ulong key, uint seed = 0) Q_DECL_NOTHROW
{
if (sizeof(ulong) > sizeof(uint)) {
return uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)) ^ seed;
@@ -73,8 +73,8 @@ inline uint qHash(ulong key, uint seed = 0)
return uint(key & (~0U)) ^ seed;
}
}
-inline uint qHash(long key, uint seed = 0) { return qHash(ulong(key), seed); }
-inline uint qHash(quint64 key, uint seed = 0)
+inline uint qHash(long key, uint seed = 0) Q_DECL_NOTHROW { return qHash(ulong(key), seed); }
+inline uint qHash(quint64 key, uint seed = 0) Q_DECL_NOTHROW
{
if (sizeof(quint64) > sizeof(uint)) {
return uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)) ^ seed;
@@ -82,20 +82,20 @@ inline uint qHash(quint64 key, uint seed = 0)
return uint(key & (~0U)) ^ seed;
}
}
-inline uint qHash(qint64 key, uint seed = 0) { return qHash(quint64(key), seed); }
-inline uint qHash(QChar key, uint seed = 0) { return qHash(key.unicode(), seed); }
-Q_CORE_EXPORT uint qHash(const QByteArray &key, uint seed = 0);
-Q_CORE_EXPORT uint qHash(const QString &key, uint seed = 0);
-Q_CORE_EXPORT uint qHash(const QStringRef &key, uint seed = 0);
-Q_CORE_EXPORT uint qHash(const QBitArray &key, uint seed = 0);
-Q_CORE_EXPORT uint qHash(QLatin1String key, uint seed = 0);
-Q_CORE_EXPORT uint qt_hash(const QString &key);
+inline uint qHash(qint64 key, uint seed = 0) Q_DECL_NOTHROW { return qHash(quint64(key), seed); }
+inline uint qHash(QChar key, uint seed = 0) Q_DECL_NOTHROW { return qHash(key.unicode(), seed); }
+Q_CORE_EXPORT uint qHash(const QByteArray &key, uint seed = 0) Q_DECL_NOTHROW;
+Q_CORE_EXPORT uint qHash(const QString &key, uint seed = 0) Q_DECL_NOTHROW;
+Q_CORE_EXPORT uint qHash(const QStringRef &key, uint seed = 0) Q_DECL_NOTHROW;
+Q_CORE_EXPORT uint qHash(const QBitArray &key, uint seed = 0) Q_DECL_NOTHROW;
+Q_CORE_EXPORT uint qHash(QLatin1String key, uint seed = 0) Q_DECL_NOTHROW;
+Q_CORE_EXPORT uint qt_hash(const QString &key) Q_DECL_NOTHROW;
#if defined(Q_CC_MSVC)
#pragma warning( push )
#pragma warning( disable : 4311 ) // disable pointer truncation warning
#endif
-template <class T> inline uint qHash(const T *key, uint seed = 0)
+template <class T> inline uint qHash(const T *key, uint seed = 0) Q_DECL_NOTHROW
{
return qHash(reinterpret_cast<quintptr>(key), seed);
}
@@ -103,9 +103,12 @@ template <class T> inline uint qHash(const T *key, uint seed = 0)
#pragma warning( pop )
#endif
-template<typename T> inline uint qHash(const T &t, uint seed) { return (qHash(t) ^ seed); }
+template<typename T> inline uint qHash(const T &t, uint seed)
+ Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(t)))
+{ return (qHash(t) ^ seed); }
template <typename T1, typename T2> inline uint qHash(const QPair<T1, T2> &key, uint seed = 0)
+ Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(key.first, seed)) && noexcept(qHash(key.second, seed)))
{
uint h1 = qHash(key.first, seed);
uint h2 = qHash(key.second, seed);
diff --git a/src/network/kernel/qhostaddress.cpp b/src/network/kernel/qhostaddress.cpp
index 3ca0f84c39..310315e636 100644
--- a/src/network/kernel/qhostaddress.cpp
+++ b/src/network/kernel/qhostaddress.cpp
@@ -1031,6 +1031,7 @@ QDebug operator<<(QDebug d, const QHostAddress &address)
uint qHash(const QHostAddress &key, uint seed)
{
+ // both lines might throw
QT_ENSURE_PARSED(&key);
return qHash(QByteArray::fromRawData(reinterpret_cast<const char *>(key.d->a6.c), 16), seed);
}