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