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
9
QT_BEGIN_NAMESPACE
10
11
#
if
QT_DEPRECATED_SINCE
(
6
,
15
)
12
QT_WARNING_PUSH
13
QT_WARNING_DISABLE_DEPRECATED
14
15
/*!
16
\class QAssociativeIterator
17
\internal
18
*/
19
20
/*!
21
Returns the key this iterator points to.
22
*/
23
QVariant
QAssociativeIterator
::
key
()
const
24
{
25
return
QtIterablePrivate
::
retrieveElement
(
26
metaContainer
().
keyMetaType
(), [
this
](
void
*
dataPtr
) {
27
metaContainer
().
keyAtIterator
(
constIterator
(),
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
*/
35
QVariantRef
<
QAssociativeIterator
>
QAssociativeIterator
::
value
()
const
36
{
37
const
QMetaType
mappedMetaType
(
metaContainer
().
mappedMetaType
());
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
*/
46
QVariantRef
<
QAssociativeIterator
>
QAssociativeIterator
::
operator
*()
const
47
{
48
return
QVariantRef
<
QAssociativeIterator
>(
this
);
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
*/
56
QVariantPointer
<
QAssociativeIterator
>
QAssociativeIterator
::
operator
->()
const
57
{
58
return
QVariantPointer
<
QAssociativeIterator
>(
this
);
59
}
60
61
/*!
62
\class QAssociativeConstIterator
63
\internal
64
*/
65
66
/*!
67
Returns the key this iterator points to.
68
*/
69
QVariant
QAssociativeConstIterator
::
key
()
const
70
{
71
return
QtIterablePrivate
::
retrieveElement
(
72
metaContainer
().
keyMetaType
(), [
this
](
void
*
dataPtr
) {
73
metaContainer
().
keyAtConstIterator
(
constIterator
(),
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
*/
81
QVariant
QAssociativeConstIterator
::
value
()
const
82
{
83
return
QtIterablePrivate
::
retrieveElement
(
84
metaContainer
().
mappedMetaType
(), [
this
](
void
*
dataPtr
) {
85
metaContainer
().
mappedAtConstIterator
(
constIterator
(),
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
*/
93
QVariant
QAssociativeConstIterator
::
operator
*()
const
94
{
95
const
QMetaType
mappedMetaType
(
metaContainer
().
mappedMetaType
());
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
*/
104
QVariantConstPointer
QAssociativeConstIterator
::
operator
->()
const
105
{
106
return
QVariantConstPointer
(
operator
*());
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
*/
170
QAssociativeIterable
::
const_iterator
QAssociativeIterable
::
find
(
const
QVariant
&
key
)
const
171
{
172
const
QMetaAssociation
meta
=
metaContainer
();
173
QtPrivate
::
QVariantTypeCoercer
coercer
;
174
if
(
const
void
*
keyData
=
coercer
.
convert
(
key
,
meta
.
keyMetaType
())) {
175
return
const_iterator
(
QConstIterator
(
this
,
meta
.
createConstIteratorAtKey
(
176
constIterable
(),
keyData
)));
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
*/
186
QAssociativeIterable
::
iterator
QAssociativeIterable
::
mutableFind
(
const
QVariant
&
key
)
187
{
188
const
QMetaAssociation
meta
=
metaContainer
();
189
QtPrivate
::
QVariantTypeCoercer
coercer
;
190
if
(
const
void
*
keyData
=
coercer
.
convert
(
key
,
meta
.
keyMetaType
()))
191
return
iterator
(
QIterator
(
this
,
meta
.
createIteratorAtKey
(
mutableIterable
(),
keyData
)));
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
*/
200
bool
QAssociativeIterable
::
containsKey
(
const
QVariant
&
key
)
201
{
202
QtPrivate
::
QVariantTypeCoercer
keyCoercer
;
203
QMetaAssociation
meta
=
metaContainer
();
204
if
(
const
void
*
keyData
=
keyCoercer
.
convert
(
key
,
meta
.
keyMetaType
()))
205
return
meta
.
containsKey
(
constIterable
(),
keyData
);
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
*/
215
void
QAssociativeIterable
::
insertKey
(
const
QVariant
&
key
)
216
{
217
QMetaAssociation
meta
=
metaContainer
();
218
QtPrivate
::
QVariantTypeCoercer
keyCoercer
;
219
meta
.
insertKey
(
mutableIterable
(),
keyCoercer
.
coerce
(
key
,
meta
.
keyMetaType
()));
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
*/
227
void
QAssociativeIterable
::
removeKey
(
const
QVariant
&
key
)
228
{
229
QMetaAssociation
meta
=
metaContainer
();
230
QtPrivate
::
QVariantTypeCoercer
keyCoercer
;
231
meta
.
removeKey
(
mutableIterable
(),
keyCoercer
.
coerce
(
key
,
meta
.
keyMetaType
()));
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
*/
241
QVariant
QAssociativeIterable
::
value
(
const
QVariant
&
key
)
const
242
{
243
const
QMetaAssociation
meta
=
metaContainer
();
244
const
QMetaType
mappedMetaType
=
meta
.
mappedMetaType
();
245
246
QtPrivate
::
QVariantTypeCoercer
coercer
;
247
const
void
*
keyData
=
coercer
.
coerce
(
key
,
meta
.
keyMetaType
());
248
249
if
(
mappedMetaType
==
QMetaType
::
fromType
<
QVariant
>()) {
250
QVariant
result
;
251
meta
.
mappedAtKey
(
constIterable
(),
keyData
, &
result
);
252
return
result
;
253
}
254
255
QVariant
result
(
mappedMetaType
);
256
meta
.
mappedAtKey
(
constIterable
(),
keyData
,
result
.
data
());
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
*/
266
void
QAssociativeIterable
::
setValue
(
const
QVariant
&
key
,
const
QVariant
&
mapped
)
267
{
268
QtPrivate
::
QVariantTypeCoercer
keyCoercer
;
269
QtPrivate
::
QVariantTypeCoercer
mappedCoercer
;
270
QMetaAssociation
meta
=
metaContainer
();
271
meta
.
setMappedAtKey
(
mutableIterable
(),
keyCoercer
.
coerce
(
key
,
meta
.
keyMetaType
()),
272
mappedCoercer
.
coerce
(
mapped
,
meta
.
mappedMetaType
()));
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
300
QT_WARNING_POP
301
#
endif
// QT_DEPRECATED_SINCE(6, 15)
302
303
QT_END_NAMESPACE
QSqlRelationalDelegate
\inmodule QtSql
qtbase
src
corelib
kernel
qassociativeiterable.cpp
Generated on
for Qt by
1.16.1