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#if QT_DEPRECATED_SINCE(6, 15)
13
14/*!
15 \class QAssociativeIterator
16 \internal
17 */
18
19/*!
20 Returns the key this iterator points to.
21*/
23{
25 metaContainer().keyMetaType(), [this](void *dataPtr) {
27 });
28}
29
30/*!
31 Returns the mapped value this iterator points to. If the container does not
32 provide a mapped value (for example a set), returns an invalid QVariantRef.
33*/
35{
37 return QVariantRef<QAssociativeIterator>(mappedMetaType.isValid() ? this : nullptr);
38}
39
40/*!
41 Returns the current item, converted to a QVariantRef. The resulting
42 QVariantRef resolves to the mapped value if there is one, or to the key
43 value if not.
44*/
46{
48}
49
50/*!
51 Returns the current item, converted to a QVariantPointer. The resulting
52 QVariantPointer resolves to the mapped value if there is one, or to the key
53 value if not.
54*/
56{
58}
59
60/*!
61 \class QAssociativeConstIterator
62 \internal
63 */
64
65/*!
66 Returns the key this iterator points to.
67*/
69{
71 metaContainer().keyMetaType(), [this](void *dataPtr) {
73 });
74}
75
76/*!
77 Returns the mapped value this iterator points to, or an invalid QVariant if
78 there is no mapped value.
79*/
81{
83 metaContainer().mappedMetaType(), [this](void *dataPtr) {
85 });
86}
87
88/*!
89 Returns the current item, converted to a QVariant. The returned value is the
90 mapped value at the current iterator if there is one, or otherwise the key.
91*/
93{
95 return mappedMetaType.isValid() ? value() : key();
96}
97
98/*!
99 Returns the current item, converted to a QVariantConstPointer. The
100 QVariantConstPointer will resolve to the mapped value at the current
101 iterator if there is one, or otherwise the key.
102*/
104{
106}
107
108/*!
109 \class QAssociativeIterable
110 \deprecated [6.15] Use QMetaAssociation::Iterable instead.
111 \since 5.2
112 \inmodule QtCore
113 \brief The QAssociativeIterable class is an iterable interface for an associative container in a QVariant.
114
115 This class allows several methods of accessing the elements of an associative container held within
116 a QVariant. An instance of QAssociativeIterable can be extracted from a QVariant if it can
117 be converted to a QVariantHash or QVariantMap or if a custom mutable view has been registered.
118
119 The container itself is not copied before iterating over it.
120
121 \sa QVariant
122*/
123
124/*!
125 \typedef QAssociativeIterable::RandomAccessIterator
126 Exposes an iterator using std::random_access_iterator_tag.
127*/
128
129/*!
130 \typedef QAssociativeIterable::BidirectionalIterator
131 Exposes an iterator using std::bidirectional_iterator_tag.
132*/
133
134/*!
135 \typedef QAssociativeIterable::ForwardIterator
136 Exposes an iterator using std::forward_iterator_tag.
137*/
138
139/*!
140 \typedef QAssociativeIterable::InputIterator
141 Exposes an iterator using std::input_iterator_tag.
142*/
143
144/*!
145 \typedef QAssociativeIterable::RandomAccessConstIterator
146 Exposes a const_iterator using std::random_access_iterator_tag.
147*/
148
149/*!
150 \typedef QAssociativeIterable::BidirectionalConstIterator
151 Exposes a const_iterator using std::bidirectional_iterator_tag.
152*/
153
154/*!
155 \typedef QAssociativeIterable::ForwardConstIterator
156 Exposes a const_iterator using std::forward_iterator_tag.
157*/
158
159/*!
160 \typedef QAssociativeIterable::InputConstIterator
161 Exposes a const_iterator using std::input_iterator_tag.
162*/
163
164/*!
165 Retrieves a const_iterator pointing to the element at the given \a key, or
166 the end of the container if that key does not exist. If the \a key isn't
167 convertible to the expected type, the end of the container is returned.
168 */
170{
173 if (const void *keyData = coercer.convert(key, meta.keyMetaType())) {
176 }
177 return constEnd();
178}
179
180/*!
181 Retrieves an iterator pointing to the element at the given \a key, or
182 the end of the container if that key does not exist. If the \a key isn't
183 convertible to the expected type, the end of the container is returned.
184 */
186{
189 if (const void *keyData = coercer.convert(key, meta.keyMetaType()))
191 return mutableEnd();
192}
193
194/*!
195 Returns \c true if the container has an entry with the given \a key, or
196 \c false otherwise. If the \a key isn't convertible to the expected type,
197 \c false is returned.
198 */
200{
203 if (const void *keyData = keyCoercer.convert(key, meta.keyMetaType()))
205 return false;
206}
207
208/*!
209 Inserts a new entry with the given \a key, or resets the mapped value of
210 any existing entry with the given \a key to the default constructed
211 mapped value. The \a key is coerced to the expected type: If it isn't
212 convertible, a default value is inserted.
213 */
215{
219}
220
221/*!
222 Removes the entry with the given \a key from the container. The \a key is
223 coerced to the expected type: If it isn't convertible, the default value
224 is removed.
225 */
227{
231}
232
233
234/*!
235 Retrieves the mapped value at the given \a key, or a default-constructed
236 QVariant of the mapped type, if the key does not exist. If the \a key is not
237 convertible to the key type, the mapped value associated with the
238 default-constructed key is returned.
239 */
241{
244
246 const void *keyData = coercer.coerce(key, meta.keyMetaType());
247
251 return result;
252 }
253
256 return result;
257}
258
259/*!
260 Sets the mapped value associated with \a key to \a mapped, if possible.
261 Inserts a new entry if none exists yet, for the given \a key. If the \a key
262 is not convertible to the key type, the value for the default-constructed
263 key type is overwritten.
264 */
266{
272}
273
274/*!
275 \typealias QAssociativeIterable::const_iterator
276 \deprecated [6.15] Use QMetaAssociation::Iterable::ConstIterator instead.
277 \inmodule QtCore
278 \brief The QAssociativeIterable::const_iterator allows iteration over a container in a QVariant.
279
280 A QAssociativeIterable::const_iterator can only be created by a QAssociativeIterable instance,
281 and can be used in a way similar to other stl-style iterators.
282
283 \sa QAssociativeIterable
284*/
285
286/*!
287 \typealias QAssociativeIterable::iterator
288 \since 6.0
289 \deprecated [6.15] Use QMetaAssociation::Iterable::Iterator instead.
290 \inmodule QtCore
291 \brief The QAssociativeIterable::iterator allows iteration over a container in a QVariant.
292
293 A QAssociativeIterable::iterator can only be created by a QAssociativeIterable instance,
294 and can be used in a way similar to other stl-style iterators.
295
296 \sa QAssociativeIterable
297*/
298
300#endif // QT_DEPRECATED_SINCE(6, 15)
301
\inmodule QtSql