From 45c248a011152727a7d7737e04ed721a01ad04bb Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Mon, 19 Oct 2020 10:12:22 +0200 Subject: 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 --- src/corelib/kernel/qassociativeiterable.cpp | 55 +++++++++++++++++++++++++++++ src/corelib/kernel/qassociativeiterable.h | 5 +++ 2 files changed, 60 insertions(+) (limited to 'src/corelib') 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 @@ -215,6 +215,46 @@ QAssociativeIterable::iterator QAssociativeIterable::mutableFind(const QVariant return mutableEnd(); } +/*! + 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 @@ -230,6 +270,21 @@ QVariant QAssociativeIterable::value(const QVariant &key) const return result; } +/*! + 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 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<> -- cgit v1.2.3