diff options
Diffstat (limited to 'src/corelib/kernel/qassociativeiterable.cpp')
-rw-r--r-- | src/corelib/kernel/qassociativeiterable.cpp | 144 |
1 files changed, 89 insertions, 55 deletions
diff --git a/src/corelib/kernel/qassociativeiterable.cpp b/src/corelib/kernel/qassociativeiterable.cpp index 780ae35a42..db17c392a2 100644 --- a/src/corelib/kernel/qassociativeiterable.cpp +++ b/src/corelib/kernel/qassociativeiterable.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include <QtCore/qassociativeiterable.h> #include <QtCore/qvariant.h> @@ -187,46 +151,116 @@ 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. + 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 + 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()); - QVariant result(QMetaType(meta.mappedMetaType())); + const QMetaType mappedMetaType = meta.mappedMetaType(); + + QtPrivate::QVariantTypeCoercer coercer; + const void *keyData = coercer.coerce(key, meta.keyMetaType()); + + if (mappedMetaType == QMetaType::fromType<QVariant>()) { + QVariant result; + meta.mappedAtKey(constIterable(), keyData, &result); + return result; + } + + QVariant result(mappedMetaType); meta.mappedAtKey(constIterable(), keyData, result.data()); return result; } /*! - \class QAssociativeIterable::const_iterator - \since 5.2 + 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())); +} + +/*! + \typealias QAssociativeIterable::const_iterator \inmodule QtCore \brief The QAssociativeIterable::const_iterator allows iteration over a container in a QVariant. @@ -239,7 +273,7 @@ QVariant QAssociativeIterable::value(const QVariant &key) const */ /*! - \class QAssociativeIterable::iterator + \typealias QAssociativeIterable::iterator \since 6.0 \inmodule QtCore \brief The QAssociativeIterable::iterator allows iteration over a container in a QVariant. |