Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qassociativeiterable.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtCore/qassociativeiterable.h>
5#include <QtCore/qiterable_impl.h>
6#include <QtCore/qvariant.h>
7
9
10/*!
11 \class QAssociativeIterator
12 \internal
13 */
14
15/*!
16 Returns the key this iterator points to.
17*/
18QVariant QAssociativeIterator::key() const
19{
20 return QIterablePrivate::retrieveElement(
21 metaContainer().keyMetaType(), [this](void *dataPtr) {
22 metaContainer().keyAtIterator(constIterator(), dataPtr);
23 });
24}
25
26/*!
27 Returns the mapped value this iterator points to. If the container does not
28 provide a mapped value (for example a set), returns an invalid QVariantRef.
29*/
30QVariantRef<QAssociativeIterator> QAssociativeIterator::value() const
31{
32 const QMetaType mappedMetaType(metaContainer().mappedMetaType());
33 return QVariantRef<QAssociativeIterator>(mappedMetaType.isValid() ? this : nullptr);
34}
35
36/*!
37 Returns the current item, converted to a QVariantRef. The resulting
38 QVariantRef resolves to the mapped value if there is one, or to the key
39 value if not.
40*/
41QVariantRef<QAssociativeIterator> QAssociativeIterator::operator*() const
42{
43 return QVariantRef<QAssociativeIterator>(this);
44}
45
46/*!
47 Returns the current item, converted to a QVariantPointer. The resulting
48 QVariantPointer resolves to the mapped value if there is one, or to the key
49 value if not.
50*/
51QVariantPointer<QAssociativeIterator> QAssociativeIterator::operator->() const
52{
53 return QVariantPointer<QAssociativeIterator>(this);
54}
55
56/*!
57 \class QAssociativeConstIterator
58 \internal
59 */
60
61/*!
62 Returns the key this iterator points to.
63*/
64QVariant QAssociativeConstIterator::key() const
65{
66 return QIterablePrivate::retrieveElement(
67 metaContainer().keyMetaType(), [this](void *dataPtr) {
68 metaContainer().keyAtConstIterator(constIterator(), dataPtr);
69 });
70}
71
72/*!
73 Returns the mapped value this iterator points to, or an invalid QVariant if
74 there is no mapped value.
75*/
76QVariant QAssociativeConstIterator::value() const
77{
78 return QIterablePrivate::retrieveElement(
79 metaContainer().mappedMetaType(), [this](void *dataPtr) {
80 metaContainer().mappedAtConstIterator(constIterator(), dataPtr);
81 });
82}
83
84/*!
85 Returns the current item, converted to a QVariant. The returned value is the
86 mapped value at the current iterator if there is one, or otherwise the key.
87*/
88QVariant QAssociativeConstIterator::operator*() const
89{
90 const QMetaType mappedMetaType(metaContainer().mappedMetaType());
91 return mappedMetaType.isValid() ? value() : key();
92}
93
94/*!
95 Returns the current item, converted to a QVariantConstPointer. The
96 QVariantConstPointer will resolve to the mapped value at the current
97 iterator if there is one, or otherwise the key.
98*/
99QVariantConstPointer QAssociativeConstIterator::operator->() const
100{
101 return QVariantConstPointer(operator*());
102}
103
104/*!
105 \class QAssociativeIterable
106 \since 5.2
107 \inmodule QtCore
108 \brief The QAssociativeIterable class is an iterable interface for an associative container in a QVariant.
109
110 This class allows several methods of accessing the elements of an associative container held within
111 a QVariant. An instance of QAssociativeIterable can be extracted from a QVariant if it can
112 be converted to a QVariantHash or QVariantMap or if a custom mutable view has been registered.
113
114 \snippet code/src_corelib_kernel_qvariant.cpp 10
115
116 The container itself is not copied before iterating over it.
117
118 \sa QVariant
119*/
120
121/*!
122 \typedef QAssociativeIterable::RandomAccessIterator
123 Exposes an iterator using std::random_access_iterator_tag.
124*/
125
126/*!
127 \typedef QAssociativeIterable::BidirectionalIterator
128 Exposes an iterator using std::bidirectional_iterator_tag.
129*/
130
131/*!
132 \typedef QAssociativeIterable::ForwardIterator
133 Exposes an iterator using std::forward_iterator_tag.
134*/
135
136/*!
137 \typedef QAssociativeIterable::InputIterator
138 Exposes an iterator using std::input_iterator_tag.
139*/
140
141/*!
142 \typedef QAssociativeIterable::RandomAccessConstIterator
143 Exposes a const_iterator using std::random_access_iterator_tag.
144*/
145
146/*!
147 \typedef QAssociativeIterable::BidirectionalConstIterator
148 Exposes a const_iterator using std::bidirectional_iterator_tag.
149*/
150
151/*!
152 \typedef QAssociativeIterable::ForwardConstIterator
153 Exposes a const_iterator using std::forward_iterator_tag.
154*/
155
156/*!
157 \typedef QAssociativeIterable::InputConstIterator
158 Exposes a const_iterator using std::input_iterator_tag.
159*/
160
161/*!
162 Retrieves a const_iterator pointing to the element at the given \a key, or
163 the end of the container if that key does not exist. If the \a key isn't
164 convertible to the expected type, the end of the container is returned.
165 */
166QAssociativeIterable::const_iterator QAssociativeIterable::find(const QVariant &key) const
167{
168 const QMetaAssociation meta = metaContainer();
169 QtPrivate::QVariantTypeCoercer coercer;
170 if (const void *keyData = coercer.convert(key, meta.keyMetaType())) {
171 return const_iterator(QConstIterator(this, meta.createConstIteratorAtKey(
172 constIterable(), keyData)));
173 }
174 return constEnd();
175}
176
177/*!
178 Retrieves an iterator pointing to the element at the given \a key, or
179 the end of the container if that key does not exist. If the \a key isn't
180 convertible to the expected type, the end of the container is returned.
181 */
182QAssociativeIterable::iterator QAssociativeIterable::mutableFind(const QVariant &key)
183{
184 const QMetaAssociation meta = metaContainer();
185 QtPrivate::QVariantTypeCoercer coercer;
186 if (const void *keyData = coercer.convert(key, meta.keyMetaType()))
187 return iterator(QIterator(this, meta.createIteratorAtKey(mutableIterable(), keyData)));
188 return mutableEnd();
189}
190
191/*!
192 Returns \c true if the container has an entry with the given \a key, or
193 \c false otherwise. If the \a key isn't convertible to the expected type,
194 \c false is returned.
195 */
196bool QAssociativeIterable::containsKey(const QVariant &key)
197{
198 QtPrivate::QVariantTypeCoercer keyCoercer;
199 QMetaAssociation meta = metaContainer();
200 if (const void *keyData = keyCoercer.convert(key, meta.keyMetaType()))
201 return meta.containsKey(constIterable(), keyData);
202 return false;
203}
204
205/*!
206 Inserts a new entry with the given \a key, or resets the mapped value of
207 any existing entry with the given \a key to the default constructed
208 mapped value. The \a key is coerced to the expected type: If it isn't
209 convertible, a default value is inserted.
210 */
211void QAssociativeIterable::insertKey(const QVariant &key)
212{
213 QMetaAssociation meta = metaContainer();
214 QtPrivate::QVariantTypeCoercer keyCoercer;
215 meta.insertKey(mutableIterable(), keyCoercer.coerce(key, meta.keyMetaType()));
216}
217
218/*!
219 Removes the entry with the given \a key from the container. The \a key is
220 coerced to the expected type: If it isn't convertible, the default value
221 is removed.
222 */
223void QAssociativeIterable::removeKey(const QVariant &key)
224{
225 QMetaAssociation meta = metaContainer();
226 QtPrivate::QVariantTypeCoercer keyCoercer;
227 meta.removeKey(mutableIterable(), keyCoercer.coerce(key, meta.keyMetaType()));
228}
229
230
231/*!
232 Retrieves the mapped value at the given \a key, or a default-constructed
233 QVariant of the mapped type, if the key does not exist. If the \a key is not
234 convertible to the key type, the mapped value associated with the
235 default-constructed key is returned.
236 */
237QVariant QAssociativeIterable::value(const QVariant &key) const
238{
239 const QMetaAssociation meta = metaContainer();
240 const QMetaType mappedMetaType = meta.mappedMetaType();
241
242 QtPrivate::QVariantTypeCoercer coercer;
243 const void *keyData = coercer.coerce(key, meta.keyMetaType());
244
245 if (mappedMetaType == QMetaType::fromType<QVariant>()) {
246 QVariant result;
247 meta.mappedAtKey(constIterable(), keyData, &result);
248 return result;
249 }
250
251 QVariant result(mappedMetaType);
252 meta.mappedAtKey(constIterable(), keyData, result.data());
253 return result;
254}
255
256/*!
257 Sets the mapped value associated with \a key to \a mapped, if possible.
258 Inserts a new entry if none exists yet, for the given \a key. If the \a key
259 is not convertible to the key type, the value for the default-constructed
260 key type is overwritten.
261 */
262void QAssociativeIterable::setValue(const QVariant &key, const QVariant &mapped)
263{
264 QtPrivate::QVariantTypeCoercer keyCoercer;
265 QtPrivate::QVariantTypeCoercer mappedCoercer;
266 QMetaAssociation meta = metaContainer();
267 meta.setMappedAtKey(mutableIterable(), keyCoercer.coerce(key, meta.keyMetaType()),
268 mappedCoercer.coerce(mapped, meta.mappedMetaType()));
269}
270
271/*!
272 \typealias QAssociativeIterable::const_iterator
273 \inmodule QtCore
274 \brief The QAssociativeIterable::const_iterator allows iteration over a container in a QVariant.
275
276 A QAssociativeIterable::const_iterator can only be created by a QAssociativeIterable instance,
277 and can be used in a way similar to other stl-style iterators.
278
279 \snippet code/src_corelib_kernel_qvariant.cpp 10
280
281 \sa QAssociativeIterable
282*/
283
284/*!
285 \typealias QAssociativeIterable::iterator
286 \since 6.0
287 \inmodule QtCore
288 \brief The QAssociativeIterable::iterator allows iteration over a container in a QVariant.
289
290 A QAssociativeIterable::iterator can only be created by a QAssociativeIterable instance,
291 and can be used in a way similar to other stl-style iterators.
292
293 \sa QAssociativeIterable
294*/
295
296QT_END_NAMESPACE
\inmodule QtCore
Definition qvariant.h:79