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
qmetaassociation.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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/qmetaassociation.h>
5#include <QtCore/qmetatype.h>
6
8
9/*!
10 \class QMetaAssociation
11 \inmodule QtCore
12 \since 6.0
13 \brief The QMetaAssociation class allows type erased access to associative containers.
14
15 \ingroup objectmodel
16
17 \compares equality
18
19 The class provides a number of primitive container operations, using void*
20 as operands. This way, you can manipulate a generic container retrieved from
21 a Variant without knowing its type.
22
23 QMetaAssociation covers both, containers with mapped values such as QMap or
24 QHash, and containers that only hold keys such as QSet.
25
26 The void* arguments to the various methods are typically created by using
27 a \l QVariant of the respective container or value type, and calling
28 its \l QVariant::data() or \l QVariant::constData() methods. However, you
29 can also pass plain pointers to objects of the container or value type.
30
31 Iterator invalidation follows the rules given by the underlying containers
32 and is not expressed in the API. Therefore, for a truly generic container,
33 any iterators should be considered invalid after any write operation.
34
35 \sa QMetaContainer, QMetaSequence, QIterable, QIterator
36*/
37
38/*!
39 \fn template<typename C> QMetaAssociation QMetaAssociation::fromContainer()
40 \since 6.0
41
42 Returns the QMetaAssociation corresponding to the type given as template parameter.
43*/
44
45/*!
46 Returns the meta type for keys in the container.
47 */
48QMetaType QMetaAssociation::keyMetaType() const
49{
50 if (auto iface = d())
51 return QMetaType(iface->keyMetaType);
52 return QMetaType();
53}
54
55/*!
56 Returns the meta type for mapped values in the container.
57 */
58QMetaType QMetaAssociation::mappedMetaType() const
59{
60 if (auto iface = d())
61 return QMetaType(iface->mappedMetaType);
62 return QMetaType();
63}
64
65/*!
66 \fn bool QMetaAssociation::canInsertKey() const
67
68 Returns \c true if keys can be added to the container using \l insertKey(),
69 otherwise returns \c false.
70
71 \sa insertKey()
72 */
73
74/*!
75 \fn void QMetaAssociation::insertKey(void *container, const void *key) const
76
77 Inserts the \a key into the \a container if possible. If the container has
78 mapped values a default-create mapped value is associated with the \a key.
79
80 \sa canInsertKey()
81 */
82
83/*!
84 \fn bool QMetaAssociation::canRemoveKey() const
85
86 Returns \c true if keys can be removed from the container using
87 \l removeKey(), otherwise returns \c false.
88
89 \sa removeKey()
90 */
91
92/*!
93 \fn void QMetaAssociation::removeKey(void *container, const void *key) const
94
95 Removes the \a key and its associated mapped value from the \a container if
96 possible.
97
98 \sa canRemoveKey()
99 */
100
101/*!
102 \fn bool QMetaAssociation::canContainsKey() const
103
104 Returns \c true if the container can be queried for keys using
105 \l containsKey(), otherwise returns \c false.
106 */
107
108/*!
109 \fn bool QMetaAssociation::containsKey(const void *container, const void *key) const
110
111 Returns \c true if the \a container can be queried for keys and contains the
112 \a key, otherwise returns \c false.
113
114 \sa canContainsKey()
115 */
116
117/*!
118 \fn bool QMetaAssociation::canGetMappedAtKey() const
119
120 Returns \c true if the container can be queried for values using
121 \l mappedAtKey(), otherwise returns \c false.
122 */
123
124/*!
125 \fn void QMetaAssociation::mappedAtKey(const void *container, const void *key, void *mapped) const
126
127 Retrieves the mapped value associated with the \a key in the \a container
128 and places it in the memory location pointed to by \a mapped, if that is
129 possible.
130
131 \sa canGetMappedAtKey()
132 */
133
134/*!
135 \fn bool QMetaAssociation::canSetMappedAtKey() const
136
137 Returns \c true if mapped values can be modified in the container using
138 \l setMappedAtKey(), otherwise returns \c false.
139
140 \sa setMappedAtKey()
141 */
142
143/*!
144 \fn void QMetaAssociation::setMappedAtKey(void *container, const void *key, const void *mapped) const
145
146 Overwrites the value associated with the \a key in the \a container using
147 the \a mapped value passed as argument if that is possible.
148
149 \sa canSetMappedAtKey()
150 */
151
152/*!
153 \fn bool QMetaAssociation::canGetKeyAtIterator() const
154
155 Returns \c true if a key can be retrieved from a non-const iterator using
156 \l keyAtIterator(), otherwise returns \c false.
157
158 \sa keyAtIterator()
159 */
160
161/*!
162 \fn void QMetaAssociation::keyAtIterator(const void *iterator, void *key) const
163
164 Retrieves the key pointed to by the non-const \a iterator and stores it
165 in the memory location pointed to by \a key, if possible.
166
167 \sa canGetKeyAtIterator(), begin(), end(), createIteratorAtKey()
168 */
169
170/*!
171 \fn bool QMetaAssociation::canGetKeyAtConstIterator() const
172
173 Returns \c true if a key can be retrieved from a const iterator using
174 \l keyAtConstIterator(), otherwise returns \c false.
175
176 \sa keyAtConstIterator()
177 */
178
179/*!
180 \fn void QMetaAssociation::keyAtConstIterator(const void *iterator, void *key) const
181
182 Retrieves the key pointed to by the const \a iterator and stores it
183 in the memory location pointed to by \a key, if possible.
184
185 \sa canGetKeyAtConstIterator(), constBegin(), constEnd(), createConstIteratorAtKey()
186 */
187
188/*!
189 \fn bool QMetaAssociation::canGetMappedAtIterator() const
190
191 Returns \c true if a mapped value can be retrieved from a non-const
192 iterator using \l mappedAtIterator(), otherwise returns \c false.
193
194 \sa mappedAtIterator()
195 */
196
197/*!
198 \fn void QMetaAssociation::mappedAtIterator(const void *iterator, void *mapped) const
199
200 Retrieves the mapped value pointed to by the non-const \a iterator and
201 stores it in the memory location pointed to by \a mapped, if possible.
202
203 \sa canGetMappedAtIterator(), begin(), end(), createIteratorAtKey()
204 */
205
206/*!
207 \fn bool QMetaAssociation::canGetMappedAtConstIterator() const
208
209 Returns \c true if a mapped value can be retrieved from a const iterator
210 using \l mappedAtConstIterator(), otherwise returns \c false.
211
212 \sa mappedAtConstIterator()
213 */
214
215/*!
216 \fn void QMetaAssociation::mappedAtConstIterator(const void *iterator, void *mapped) const
217
218 Retrieves the mapped value pointed to by the const \a iterator and
219 stores it in the memory location pointed to by \a mapped, if possible.
220
221 \sa canGetMappedAtConstIterator(), constBegin(), constEnd(), createConstIteratorAtKey()
222 */
223
224/*!
225 \fn bool QMetaAssociation::canSetMappedAtIterator() const
226
227 Returns \c true if a mapped value can be set via a non-const iterator using
228 \l setMappedAtIterator(), otherwise returns \c false.
229
230 \sa setMappedAtIterator()
231 */
232
233/*!
234 \fn void QMetaAssociation::setMappedAtIterator(const void *iterator, const void *mapped) const
235
236 Writes the \a mapped value to the container location pointed to by the
237 non-const \a iterator, if possible.
238
239 \sa canSetMappedAtIterator(), begin(), end(), createIteratorAtKey()
240 */
241
242/*!
243 \fn bool QMetaAssociation::canCreateIteratorAtKey() const
244
245 Returns \c true if an iterator pointing to an entry in the container can be
246 created using \l createIteratorAtKey(), otherwise returns false.
247
248 \sa createIteratorAtKey()
249 */
250
251/*!
252 \fn void *QMetaAssociation::createIteratorAtKey(void *container, const void *key) const
253
254 Returns a non-const iterator pointing to the entry of \a key in the
255 \a container, if possible. If the entry doesn't exist, creates a non-const
256 iterator pointing to the end of the \a container. If no non-const iterator
257 can be created, returns \c nullptr.
258
259 The non-const iterator has to be destroyed using destroyIterator().
260
261 \sa canCreateIteratorAtKey(), begin(), end(), destroyIterator()
262 */
263
264/*!
265 \fn bool QMetaAssociation::canCreateConstIteratorAtKey() const
266
267 Returns \c true if a const iterator pointing to an entry in the container
268 can be created using \l createConstIteratorAtKey(), otherwise returns false.
269 */
270
271/*!
272 \fn void *QMetaAssociation::createConstIteratorAtKey(const void *container, const void *key) const
273
274 Returns a const iterator pointing to the entry of \a key in the
275 \a container, if possible. If the entry doesn't exist, creates a const
276 iterator pointing to the end of the \a container. If no const iterator can
277 be created, returns \c nullptr.
278
279 The const iterator has to be destroyed using destroyConstIterator().
280
281 \sa canCreateConstIteratorAtKey(), constBegin(), constEnd(), destroyConstIterator()
282 */
283
284/*!
285 \fn bool QMetaAssociation::operator==(const QMetaAssociation &lhs, const QMetaAssociation &rhs)
286
287 Returns \c true if the QMetaAssociation \a lhs represents the same container type
288 as the QMetaAssociation \a rhs, otherwise returns \c false.
289*/
290/*!
291 \fn bool QMetaAssociation::operator!=(const QMetaAssociation &lhs, const QMetaAssociation &rhs)
292
293 Returns \c true if the QMetaAssociation \a lhs represents a different container
294 type than the QMetaAssociation \a rhs, otherwise returns \c false.
295*/
296
297/*!
298 \class QMetaAssociation::Iterable
299 \inherits QIterable
300 \since 6.11
301 \inmodule QtCore
302 \brief QMetaAssociation::Iterable is an iterable interface for an associative container in a QVariant.
303
304 This class allows several methods of accessing the elements of an
305 associative container held within a QVariant. An instance of
306 QMetaAssociation::Iterable can be extracted from a QVariant if it can be
307 converted to a QVariantHash or QVariantMap or if a custom mutable view has
308 been registered.
309
310 \snippet code/src_corelib_kernel_qvariant.cpp 10
311
312 The container itself is not copied before iterating over it.
313
314 \sa QVariant
315*/
316
317/*!
318 \typealias QMetaAssociation::Iterable::RandomAccessIterator
319 Exposes an iterator using std::random_access_iterator_tag.
320*/
321
322/*!
323 \typealias QMetaAssociation::Iterable::BidirectionalIterator
324 Exposes an iterator using std::bidirectional_iterator_tag.
325*/
326
327/*!
328 \typealias QMetaAssociation::Iterable::ForwardIterator
329 Exposes an iterator using std::forward_iterator_tag.
330*/
331
332/*!
333 \typealias QMetaAssociation::Iterable::InputIterator
334 Exposes an iterator using std::input_iterator_tag.
335*/
336
337/*!
338 \typealias QMetaAssociation::Iterable::RandomAccessConstIterator
339 Exposes a const_iterator using std::random_access_iterator_tag.
340*/
341
342/*!
343 \typealias QMetaAssociation::Iterable::BidirectionalConstIterator
344 Exposes a const_iterator using std::bidirectional_iterator_tag.
345*/
346
347/*!
348 \typealias QMetaAssociation::Iterable::ForwardConstIterator
349 Exposes a const_iterator using std::forward_iterator_tag.
350*/
351
352/*!
353 \typealias QMetaAssociation::Iterable::InputConstIterator
354 Exposes a const_iterator using std::input_iterator_tag.
355*/
356
357/*!
358 \class QMetaAssociation::Iterable::ConstIterator
359 \inherits QConstIterator
360 \since 6.11
361 \inmodule QtCore
362 \brief QMetaAssociation::Iterable::ConstIterator allows iteration over a container in a QVariant.
363
364 A QMetaAssociation::Iterable::ConstIterator can only be created by a
365 QMetaAssociation::Iterable instance, and can be used in a way similar to
366 other stl-style iterators.
367
368 \snippet code/src_corelib_kernel_qvariant.cpp 10
369
370 \sa QMetaAssociation::Iterable
371*/
372
373/*!
374 \class QMetaAssociation::Iterable::Iterator
375 \inherits QIterator
376 \since 6.11
377 \inmodule QtCore
378 \brief The QMetaAssociation::Iterable::Iterator allows iteration over a container in a QVariant.
379
380 A QMetaAssociation::Iterable::Iterator can only be created by a
381 QMetaAssociation::Iterable instance, and can be used in a way similar to
382 other stl-style iterators.
383
384 \sa QMetaAssociation::Iterable
385*/
386
387/*!
388 \fn QMetaAssociation::Iterable::ConstIterator QMetaAssociation::Iterable::find(const QVariant &key) const
389 Retrieves a ConstIterator pointing to the element at the given \a key, or
390 the end of the container if that key does not exist. If the \a key isn't
391 convertible to the expected type, the end of the container is returned.
392 */
393
394/*!
395 \fn QMetaAssociation::Iterable::Iterator QMetaAssociation::Iterable::mutableFind(const QVariant &key)
396 Retrieves an iterator pointing to the element at the given \a key, or
397 the end of the container if that key does not exist. If the \a key isn't
398 convertible to the expected type, the end of the container is returned.
399 */
400
401/*!
402 \fn bool QMetaAssociation::Iterable::containsKey(const QVariant &key) const
403 Returns \c true if the container has an entry with the given \a key, or
404 \c false otherwise. If the \a key isn't convertible to the expected type,
405 \c false is returned.
406 */
407
408/*!
409 \fn void QMetaAssociation::Iterable::insertKey(const QVariant &key)
410 Inserts a new entry with the given \a key, or resets the mapped value of
411 any existing entry with the given \a key to the default constructed
412 mapped value. The \a key is coerced to the expected type: If it isn't
413 convertible, a default value is inserted.
414 */
415
416/*!
417 \fn void QMetaAssociation::Iterable::removeKey(const QVariant &key)
418 Removes the entry with the given \a key from the container. The \a key is
419 coerced to the expected type: If it isn't convertible, the default value
420 is removed.
421 */
422
423/*!
424 \fn QVariant QMetaAssociation::Iterable::value(const QVariant &key) const
425 Retrieves the mapped value at the given \a key, or a QVariant of a
426 default-constructed instance of the mapped type, if the key does not
427 exist. If the \a key is not convertible to the key type, the mapped value
428 associated with the default-constructed key is returned.
429 */
430
431/*!
432 \fn void QMetaAssociation::Iterable::setValue(const QVariant &key, const QVariant &mapped)
433 Sets the mapped value associated with \a key to \a mapped, if possible.
434 Inserts a new entry if none exists yet, for the given \a key. If the
435 \a key is not convertible to the key type, the value for the
436 default-constructed key type is overwritten.
437 */
438
439
440/*!
441 \fn QVariant QMetaAssociation::Iterable::Iterator::key() const
442 Returns the key this iterator points to.
443*/
444
445/*!
446 \fn QVariant::Reference<QMetaAssociation::Iterable::Iterator> QMetaAssociation::Iterable::Iterator::value() const
447 Returns the mapped value this iterator points to. If the container does not
448 provide a mapped value (for example a set), returns an invalid
449 QVariant::Reference.
450*/
451
452/*!
453 \fn QVariant::Reference<QMetaAssociation::Iterable::Iterator> QMetaAssociation::Iterable::Iterator::operator*() const
454 Returns the current item, converted to a QVariant::Reference. The resulting
455 QVariant::Reference resolves to the mapped value if there is one, or to the
456 key value if not.
457*/
458
459/*!
460 \fn QVariant::Pointer<QMetaAssociation::Iterable::Iterator> QMetaAssociation::Iterable::Iterator::operator->() const
461 Returns the current item, converted to a QVariant::Pointer. The resulting
462 QVariant::Pointer resolves to the mapped value if there is one, or to the
463 key value if not.
464*/
465
466/*!
467 \fn QVariant QMetaAssociation::Iterable::ConstIterator::key() const
468 Returns the key this iterator points to.
469*/
470
471/*!
472 \fn QVariant QMetaAssociation::Iterable::ConstIterator::value() const
473 Returns the mapped value this iterator points to, or an invalid QVariant if
474 there is no mapped value.
475*/
476
477/*!
478 \fn QVariant QMetaAssociation::Iterable::ConstIterator::operator*() const
479 Returns the current item, converted to a QVariant. The returned value is the
480 mapped value at the current iterator if there is one, or otherwise the key.
481*/
482
483/*!
484 \fn QVariant::ConstPointer<QMetaAssociation::Iterable::ConstIterator> QMetaAssociation::Iterable::ConstIterator::operator->() const
485 Returns the current item, converted to a QVariant::ConstPointer. The
486 QVariant::ConstPointer will resolve to the mapped value at the current
487 iterator if there is one, or otherwise the key.
488*/
489
490QT_END_NAMESPACE
\inmodule QtCore
Definition qmetatype.h:339