JSON: Add qHash functions for JSON and CBOR types

This way we can easily use them as keys in QHash and QSet.

Change-Id: Ie744c3b5ad1176ba2ab035c7e650af483757a0c9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ulf Hermann 2018-06-28 15:09:23 +02:00
parent 112c0632d9
commit 231273b130
13 changed files with 111 additions and 0 deletions

View File

@ -1181,6 +1181,11 @@ void QCborArray::detach(qsizetype reserved)
Returns the offset of this iterator relative to \a other.
*/
uint qHash(const QCborArray &array, uint seed)
{
return qHashRange(array.begin(), array.end(), seed);
}
#if !defined(QT_NO_DEBUG_STREAM)
QDebug operator<<(QDebug dbg, const QCborArray &a)
{

View File

@ -286,6 +286,8 @@ inline QCborArray QCborValueRef::toArray(const QCborArray &a) const
return concrete().toArray(a);
}
Q_CORE_EXPORT uint qHash(const QCborArray &array, uint seed = 0);
#if !defined(QT_NO_DEBUG_STREAM)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborArray &a);
#endif

View File

@ -1734,6 +1734,11 @@ void QCborMap::detach(qsizetype reserved)
\sa operator+=(), operator-()
*/
uint qHash(const QCborMap &map, uint seed)
{
return qHashRange(map.begin(), map.end(), seed);
}
#if !defined(QT_NO_DEBUG_STREAM)
QDebug operator<<(QDebug dbg, const QCborMap &m)
{

View File

@ -337,6 +337,8 @@ inline QCborMap QCborValueRef::toMap(const QCborMap &m) const
return concrete().toMap(m);
}
Q_CORE_EXPORT uint qHash(const QCborMap &map, uint seed = 0);
#if !defined(QT_NO_DEBUG_STREAM)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborMap &m);
#endif

View File

@ -2365,6 +2365,53 @@ inline QCborMap::QCborMap(QCborContainerPrivate &dd) noexcept
{
}
uint qHash(const QCborValue &value, uint seed)
{
switch (value.type()) {
case QCborValue::Integer:
return qHash(value.toInteger(), seed);
case QCborValue::ByteArray:
return qHash(value.toByteArray(), seed);
case QCborValue::String:
return qHash(value.toString(), seed);
case QCborValue::Array:
return qHash(value.toArray(), seed);
case QCborValue::Map:
return qHash(value.toMap(), seed);
case QCborValue::Tag: {
QtPrivate::QHashCombine hash;
seed = hash(seed, value.tag());
seed = hash(seed, value.taggedValue());
return seed;
}
case QCborValue::SimpleType:
break;
case QCborValue::False:
return qHash(false, seed);
case QCborValue::True:
return qHash(true, seed);
case QCborValue::Null:
return qHash(nullptr, seed);
case QCborValue::Undefined:
return seed;
case QCborValue::Double:
return qHash(value.toDouble(), seed);
case QCborValue::DateTime:
return qHash(value.toDateTime(), seed);
case QCborValue::Url:
return qHash(value.toUrl(), seed);
case QCborValue::RegularExpression:
return qHash(value.toRegularExpression(), seed);
case QCborValue::Uuid:
return qHash(value.toUuid(), seed);
case QCborValue::Invalid:
return seed;
}
Q_ASSERT(value.isSimpleType());
return qHash(value.toSimpleType(), seed);
}
#if !defined(QT_NO_DEBUG_STREAM)
static QDebug debugContents(QDebug &dbg, const QCborValue &v)
{

View File

@ -451,6 +451,8 @@ private:
qsizetype i;
};
Q_CORE_EXPORT uint qHash(const QCborValue &value, uint seed = 0);
#if !defined(QT_NO_DEBUG_STREAM)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborValue &v);
#endif

View File

@ -1237,6 +1237,10 @@ void QJsonArray::compact()
a = static_cast<QJsonPrivate::Array *>(d->header->root());
}
uint qHash(const QJsonArray &array, uint seed)
{
return qHashRange(array.begin(), array.end(), seed);
}
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
QDebug operator<<(QDebug dbg, const QJsonArray &a)

View File

@ -265,6 +265,8 @@ private:
Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonArray)
Q_CORE_EXPORT uint qHash(const QJsonArray &array, uint seed = 0);
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
#endif

View File

@ -1292,6 +1292,17 @@ void QJsonObject::setValueAt(int i, const QJsonValue &val)
insert(e->key(), val);
}
uint qHash(const QJsonObject &object, uint seed)
{
QtPrivate::QHashCombine hash;
for (auto it = object.begin(), end = object.end(); it != end; ++it) {
const QString key = it.key();
const QJsonValue value = it.value();
seed = hash(seed, std::pair<const QString&, const QJsonValue&>(key, value));
}
return seed;
}
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
QDebug operator<<(QDebug dbg, const QJsonObject &o)
{

View File

@ -262,6 +262,8 @@ private:
Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonObject)
Q_CORE_EXPORT uint qHash(const QJsonObject &object, uint seed = 0);
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
#endif

View File

@ -876,6 +876,28 @@ QJsonValue QJsonValueRef::toValue() const
return o->valueAt(index);
}
uint qHash(const QJsonValue &value, uint seed)
{
switch (value.type()) {
case QJsonValue::Null:
return qHash(nullptr, seed);
case QJsonValue::Bool:
return qHash(value.toBool(), seed);
case QJsonValue::Double:
return qHash(value.toDouble(), seed);
case QJsonValue::String:
return qHash(value.toString(), seed);
case QJsonValue::Array:
return qHash(value.toArray(), seed);
case QJsonValue::Object:
return qHash(value.toObject(), seed);
case QJsonValue::Undefined:
return seed;
}
Q_UNREACHABLE();
return 0;
}
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
QDebug operator<<(QDebug dbg, const QJsonValue &o)
{

View File

@ -247,6 +247,8 @@ public:
Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonValue)
Q_CORE_EXPORT uint qHash(const QJsonValue &value, uint seed = 0);
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
#endif

View File

@ -104,6 +104,11 @@ Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QBitArray &key, uint seed =
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QLatin1String key, uint seed = 0) Q_DECL_NOTHROW;
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) Q_DECL_NOTHROW;
Q_DECL_CONST_FUNCTION inline uint qHash(std::nullptr_t, uint seed = 0) Q_DECL_NOTHROW
{
return qHash(reinterpret_cast<quintptr>(nullptr), seed);
}
template <class T> inline uint qHash(const T *key, uint seed = 0) Q_DECL_NOTHROW
{
return qHash(reinterpret_cast<quintptr>(key), seed);