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