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.h | |
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.h')
-rw-r--r-- | src/corelib/kernel/qassociativeiterable.h | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/src/corelib/kernel/qassociativeiterable.h b/src/corelib/kernel/qassociativeiterable.h index 8412dc86fd..ba20af0327 100644 --- a/src/corelib/kernel/qassociativeiterable.h +++ b/src/corelib/kernel/qassociativeiterable.h @@ -163,14 +163,15 @@ inline QVariantRef<QAssociativeIterator>::operator QVariant() const { if (m_pointer == nullptr) return QVariant(); + const auto metaAssociation = m_pointer->metaContainer(); - QMetaType metaType(metaAssociation.mappedMetaType()); + const QMetaType metaType(metaAssociation.mappedMetaType()); if (!metaType.isValid()) return m_pointer->key(); QVariant v(metaType); - void *dataPtr = metaType == QMetaType::fromType<QVariant>() ? &v : v.data(); - metaAssociation.mappedAtIterator(m_pointer->constIterator(), dataPtr); + metaAssociation.mappedAtIterator(m_pointer->constIterator(), + metaType == QMetaType::fromType<QVariant>() ? &v : v.data()); return v; } @@ -180,11 +181,15 @@ inline QVariantRef<QAssociativeIterator> &QVariantRef<QAssociativeIterator>::ope { if (m_pointer == nullptr) return *this; - const QMetaType metaType(m_pointer->metaContainer().mappedMetaType()); - const void *dataPtr = metaType == QMetaType::fromType<QVariant>() - ? &value - : value.constData(); - m_pointer->metaContainer().setMappedAtIterator(m_pointer->constIterator(), dataPtr); + + const auto metaAssociation = m_pointer->metaContainer(); + const QMetaType metaType(metaAssociation.mappedMetaType()); + if (metaType.isValid()) { + QtPrivate::QVariantTypeCoercer coercer; + metaAssociation.setMappedAtIterator(m_pointer->constIterator(), + coercer.coerce(value, metaType)); + } + return *this; } |