diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-10-19 10:07:29 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-10-19 22:11:50 +0200 |
commit | 37c7ef4f4a8478e94eaf0af5b40c279c476fa561 (patch) | |
tree | 76cdeea2afb6785eeea0651054bd5787d9541810 /src/corelib/kernel/qassociativeiterable.cpp | |
parent | 52083e4da505bfe6abeb9e36ffd68ecefbbf6964 (diff) |
QMetaContainer: Consistently coerce types
The high-level iterable interfaces should coerce the types of most
QVariants passed to the expected ones. To do this, move the type
coercion code into qvariant.{h|cpp} so that it is available to the
QVariantRef specializations.
The exception are variants passed to the find() functions of associative
iterables. Here, we should not coerce values we cannot convert to the
default-constructed keys. Instead we return end() in such cases.
Fixes: QTBUG-87687
Change-Id: I0bd4e5c4e4e270dd3bf36cb3fb115794828077f2
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib/kernel/qassociativeiterable.cpp')
-rw-r--r-- | src/corelib/kernel/qassociativeiterable.cpp | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/src/corelib/kernel/qassociativeiterable.cpp b/src/corelib/kernel/qassociativeiterable.cpp index 780ae35a42..5e30ae291e 100644 --- a/src/corelib/kernel/qassociativeiterable.cpp +++ b/src/corelib/kernel/qassociativeiterable.cpp @@ -187,40 +187,46 @@ QVariantConstPointer QAssociativeConstIterator::operator->() const /*! Retrieves a const_iterator pointing to the element at the given \a key, or - the end of the container if that key does not exist. + the end of the container if that key does not exist. If the \a key isn't + convertible to the expected type, the end of the container is returned. */ QAssociativeIterable::const_iterator QAssociativeIterable::find(const QVariant &key) const { const QMetaAssociation meta = metaContainer(); - QVariant converted = key; - const void *keyData = QIterablePrivate::coerceType(converted, meta.keyMetaType()); - return const_iterator(QConstIterator( - this, meta.createConstIteratorAtKey(constIterable(), keyData))); + QtPrivate::QVariantTypeCoercer coercer; + if (const void *keyData = coercer.convert(key, meta.keyMetaType())) { + return const_iterator(QConstIterator(this, meta.createConstIteratorAtKey( + constIterable(), keyData))); + } + return constEnd(); } /*! Retrieves an iterator pointing to the element at the given \a key, or - the end of the container if that key does not exist. + the end of the container if that key does not exist. If the \a key isn't + convertible to the expected type, the end of the container is returned. */ QAssociativeIterable::iterator QAssociativeIterable::mutableFind(const QVariant &key) { const QMetaAssociation meta = metaContainer(); - QVariant converted = key; - const void *keyData = QIterablePrivate::coerceType(converted, meta.keyMetaType()); - return iterator(QIterator(this, meta.createIteratorAtKey(mutableIterable(), keyData))); + QtPrivate::QVariantTypeCoercer coercer; + if (const void *keyData = coercer.convert(key, meta.keyMetaType())) + return iterator(QIterator(this, meta.createIteratorAtKey(mutableIterable(), keyData))); + return mutableEnd(); } /*! - Retrieves the mapped value at the given \a key, or an invalid QVariant - if the key does not exist. + 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 + default-constructed key is returned. */ QVariant QAssociativeIterable::value(const QVariant &key) const { const QMetaAssociation meta = metaContainer(); - QVariant converted = key; - const void *keyData = QIterablePrivate::coerceType(converted, meta.keyMetaType()); + QtPrivate::QVariantTypeCoercer coercer; QVariant result(QMetaType(meta.mappedMetaType())); - meta.mappedAtKey(constIterable(), keyData, result.data()); + meta.mappedAtKey(constIterable(), coercer.coerce(key, meta.keyMetaType()), result.data()); return result; } |