summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-10-19 10:12:22 +0200
committerUlf Hermann <ulf.hermann@qt.io>2020-10-19 22:12:04 +0200
commit45c248a011152727a7d7737e04ed721a01ad04bb (patch)
tree423647762d72774e24d38419ca0f3b8672247d11 /src
parent37c7ef4f4a8478e94eaf0af5b40c279c476fa561 (diff)
QAssociativeIterable: Add methods to add/remove keys and values
This way we can actually modify the container. Previously the interface was rather useless. Change-Id: I278aae46999862ada115c9066a010d7de5cde4ff Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/kernel/qassociativeiterable.cpp55
-rw-r--r--src/corelib/kernel/qassociativeiterable.h5
2 files changed, 60 insertions, 0 deletions
diff --git a/src/corelib/kernel/qassociativeiterable.cpp b/src/corelib/kernel/qassociativeiterable.cpp
index 5e30ae291e..41db3485f0 100644
--- a/src/corelib/kernel/qassociativeiterable.cpp
+++ b/src/corelib/kernel/qassociativeiterable.cpp
@@ -216,6 +216,46 @@ QAssociativeIterable::iterator QAssociativeIterable::mutableFind(const QVariant
}
/*!
+ Returns \c true if the container has an entry with the given \a key, or
+ \c false otherwise. If the \a key isn't convertible to the expected type,
+ \c false is returned.
+ */
+bool QAssociativeIterable::containsKey(const QVariant &key)
+{
+ QtPrivate::QVariantTypeCoercer keyCoercer;
+ QMetaAssociation meta = metaContainer();
+ if (const void *keyData = keyCoercer.convert(key, meta.keyMetaType()))
+ return meta.containsKey(constIterable(), keyData);
+ return false;
+}
+
+/*!
+ Inserts a new entry with the given \a key, or resets the mapped value of
+ any existing entry with the given \a key to the default constructed
+ mapped value. The \a key is coerced to the expected type: If it isn't
+ convertible, a default value is inserted.
+ */
+void QAssociativeIterable::insertKey(const QVariant &key)
+{
+ QMetaAssociation meta = metaContainer();
+ QtPrivate::QVariantTypeCoercer keyCoercer;
+ meta.insertKey(mutableIterable(), keyCoercer.coerce(key, meta.keyMetaType()));
+}
+
+/*!
+ Removes the entry with the given \a key from the container. The \a key is
+ coerced to the expected type: If it isn't convertible, the default value
+ is removed.
+ */
+void QAssociativeIterable::removeKey(const QVariant &key)
+{
+ QMetaAssociation meta = metaContainer();
+ QtPrivate::QVariantTypeCoercer keyCoercer;
+ meta.removeKey(mutableIterable(), keyCoercer.coerce(key, meta.keyMetaType()));
+}
+
+
+/*!
Retrieves the mapped value at the given \a key, or a default-constructed
QVariant of the mapped type, if the key does not exist. If the \a key is not
convertible to the key type, the mapped value associated with the
@@ -231,6 +271,21 @@ QVariant QAssociativeIterable::value(const QVariant &key) const
}
/*!
+ Sets the mapped value associated with \a key to \a mapped, if possible.
+ Inserts a new entry if none exists yet, for the given \a key. If the \a key
+ is not convertible to the key type, the value for the default-constructed
+ key type is overwritten.
+ */
+void QAssociativeIterable::setValue(const QVariant &key, const QVariant &mapped)
+{
+ QtPrivate::QVariantTypeCoercer keyCoercer;
+ QtPrivate::QVariantTypeCoercer mappedCoercer;
+ QMetaAssociation meta = metaContainer();
+ meta.setMappedAtKey(mutableIterable(), keyCoercer.coerce(key, meta.keyMetaType()),
+ mappedCoercer.coerce(mapped, meta.mappedMetaType()));
+}
+
+/*!
\class QAssociativeIterable::const_iterator
\since 5.2
\inmodule QtCore
diff --git a/src/corelib/kernel/qassociativeiterable.h b/src/corelib/kernel/qassociativeiterable.h
index ba20af0327..4a1942fbc9 100644
--- a/src/corelib/kernel/qassociativeiterable.h
+++ b/src/corelib/kernel/qassociativeiterable.h
@@ -155,7 +155,12 @@ public:
const_iterator constFind(const QVariant &key) const { return find(key); }
iterator mutableFind(const QVariant &key);
+ bool containsKey(const QVariant &key);
+ void insertKey(const QVariant &key);
+ void removeKey(const QVariant &key);
+
QVariant value(const QVariant &key) const;
+ void setValue(const QVariant &key, const QVariant &mapped);
};
template<>