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