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
qiterable.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/qiterable.h>
6#include <QtCore/qloggingcategory.h>
7
9
10Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg);
11
12/*!
13 \class QBaseIterator
14 \inmodule QtCore
15 QBaseIterator<Container> forms the common base class for all iterators
16 operating on subclasses of QIterable, where \a Container is the meta-type
17 descriptor.
18*/
19
20/*!
21 \fn template<class Container> QBaseIterator<Container>::QBaseIterator(const QIterable<Container> *iterable, void *iterator)
22
23 \internal
24 Creates a const QBaseIterator from an \a iterable and an \a iterator.
25 */
26
27/*!
28 \fn template<class Container> QBaseIterator<Container>::QBaseIterator(QIterable<Container> *iterable, void *iterator)
29
30 \internal
31 Creates a non-const QBaseIterator from an \a iterable and an \a iterator.
32 */
33
34/*!
35 \fn template<class Container> QBaseIterator<Container>::QBaseIterator(QBaseIterator<Container> &&other)
36
37 \internal
38 Move-constructs a QBaseIterator from \a other, preserving its const-ness.
39 */
40
41/*!
42 \fn template<class Container> QBaseIterator<Container>::QBaseIterator(const QBaseIterator<Container> &other)
43
44 \internal
45 Copy-constructs a QBaseIterator from \a other, preserving its const-ness.
46 */
47
48/*!
49 \fn template<class Container> QBaseIterator<Container>::~QBaseIterator()
50
51 \internal
52 Destroys a QBaseIterator.
53 */
54
55/*!
56 \fn template<class Container> QBaseIterator<Container> &QBaseIterator<Container>::operator=(const QBaseIterator<Container> &other)
57
58 \internal
59 Copy-assigns a QBaseIterator from \a other, preserving its const-ness.
60 */
61
62/*!
63 \fn template<class Container> void QBaseIterator<Container>::initIterator(const void *copy)
64
65 \internal
66 Initializes the internal native iterator by duplicating \a copy, if given.
67 */
68
69/*!
70 \fn template<class Container> void QBaseIterator<Container>::clearIterator()
71
72 \internal
73 Destroys the internal native iterator.
74 */
75
76
77/*!
78 \fn QMetaContainer QBaseIterator<Container>::metaContainer() const
79
80 \internal
81 Returns the meta sequence.
82 */
83
84/*!
85 \fn template<class Container> QIterable *QBaseIterator<Container>::mutableIterable() const
86
87 \internal
88 Returns a non-const pointer to the iterable, if the original iterable was
89 non-const. Otherwise returns nullptr.
90 */
91
92/*!
93 \fn template<class Container> const QIterable *QBaseIterator<Container>::constIterable() const
94
95 \internal
96 Returns a const pointer to the iterable.
97 */
98
99/*!
100 \fn template<class Container> void *QBaseIterator<Container>::mutableIterator()
101
102 Returns a non-const pointer to the internal native iterator.
103 */
104
105/*!
106 \fn template<class Container> const void *QBaseIterator<Container>::constIterator() const
107
108 Returns a const pointer to the internal native iterator.
109 */
110
111/*!
112 \fn template<class Container> QBaseIterator &QBaseIterator<Container>::operator=(QBaseIterator<Container> &&other)
113
114 \internal
115 Move-assigns a QBaseIterator from \a other, preserving its const-ness.
116 */
117
118/*!
119 \class QIterator
120 \since 6.0
121 \inmodule QtCore
122 \brief The QIterator is a template class that allows iteration over a container in a QVariant.
123
124 QIterator<Container> provides mutable iteration over a container, where
125 \a Container is the meta-type descriptor (either QMetaSequence or
126 QMetaAssociation) that defines the container's iteration capabilities.
127
128 A QIterator can only be created by a QIterable instance, and can be used
129 in a way similar to other stl-style iterators. Generally, QIterator should
130 not be used directly, but through its derived classes provided by
131 QMetaSequence::Iterable and QMetaAssociation::Iterable.
132
133 \sa QIterable
134*/
135
136/*!
137 \fn template<class Container> QIterator<Container>::QIterator(QIterable<Container> *iterable, void *iterator)
138
139 Creates an iterator from an \a iterable and a pointer to a native \a iterator.
140 */
141
142/*!
143 \fn template<class Container> bool QIterator<Container>::operator==(const QIterator<Container> &other) const
144
145 Returns \c true if \a other points to the same item as this
146 iterator; otherwise returns \c false.
147
148 \sa operator!=()
149*/
150
151/*!
152 \fn template<class Container> bool QIterator<Container>::operator!=(const QIterator<Container> &other) const
153
154 Returns \c true if \a other points to a different item than this
155 iterator; otherwise returns \c false.
156
157 \sa operator==()
158*/
159
160/*!
161 \fn template<class Container> QIterator<Container> &QIterator<Container>::operator++()
162
163 The prefix \c{++} operator (\c{++it}) advances the iterator to the
164 next item in the container and returns an iterator to the new current
165 item.
166
167 Calling this function on QMetaSequence::Iterable::constEnd() leads to undefined results.
168
169 \sa operator--()
170*/
171
172/*!
173 \fn template<class Container> QIterator<Container> QIterator<Container>::operator++(int)
174 \overload
175
176 The postfix \c{++} operator (\c{it++}) advances the iterator to the
177 next item in the container and returns an iterator to the previously
178 current item.
179*/
180
181
182/*!
183 \fn template<class Container> QIterator<Container> &QIterator<Container>::operator--()
184
185 The prefix \c{--} operator (\c{--it}) makes the preceding item
186 current and returns an iterator to the new current item.
187
188 Calling this function on QMetaSequence::Iterable::constBegin() leads to undefined results.
189
190 If the container in the QVariant does not support bi-directional iteration, calling this function
191 leads to undefined results.
192
193 \sa operator++(), QIterable::canReverseIterate()
194*/
195
196/*!
197 \fn template<class Container> QIterator<Container> QIterator<Container>::operator--(int)
198
199 \overload
200
201 The postfix \c{--} operator (\c{it--}) makes the preceding item
202 current and returns an iterator to the previously current item.
203
204 If the container in the QVariant does not support bi-directional iteration, calling this function
205 leads to undefined results.
206
207 \sa QIterable::canReverseIterate()
208*/
209
210/*!
211 \fn template<class Container> QIterator<Container> &QIterator<Container>::operator+=(qsizetype j)
212
213 Advances the iterator by \a j items.
214
215 \sa operator-=(), operator+()
216*/
217
218/*!
219 \fn template<class Container> QIterator<Container> &QIterator<Container>::operator-=(qsizetype j)
220
221 Makes the iterator go back by \a j items.
222
223 If the container in the QVariant does not support bi-directional iteration, calling this function
224 leads to undefined results.
225
226 \sa operator+=(), operator-(), QIterable::canReverseIterate()
227*/
228
229/*!
230 \fn template<class Container> QIterator<Container> QIterator<Container>::operator+(qsizetype j) const
231
232 Returns an iterator to the item at \a j positions forward from
233 this iterator.
234
235 \sa operator-(), operator+=()
236*/
237
238/*!
239 \fn template<class Container> QIterator<Container> QIterator<Container>::operator-(qsizetype j) const
240
241 Returns an iterator to the item at \a j positions backward from
242 this iterator.
243
244 If the container in the QVariant does not support bi-directional iteration, calling this function
245 leads to undefined results.
246
247 \sa operator+(), operator-=(), QIterable::canReverseIterate()
248*/
249
250/*!
251 \fn template<class Container> qsizetype QIterator<Container>::operator-(const QIterator<Container> &j) const
252 \overload
253
254 Returns the distance between the two iterators.
255
256 \sa operator+(), operator-=(), QIterable::canReverseIterate()
257 */
258
259/*!
260 \fn template <class Container> QIterator<Container> QIterator<Container>::operator+(qsizetype j, const QIterator<Container> &k)
261
262 Returns an iterator to the item at \a j positions forward from iterator \a k.
263*/
264
265/*!
266 \struct QConstIterator
267 \since 6.0
268 \inmodule QtCore
269 \brief The QConstIterator allows iteration over a container in a QVariant.
270
271 QConstIterator<Container> provides const iteration over a container, where
272 \a Container is the meta-type descriptor (either QMetaSequence or
273 QMetaAssociation) that defines the container's iteration capabilities.
274
275 \sa QIterator, QIterable
276*/
277
278/*!
279 \fn template <class Container> QConstIterator<Container>::QConstIterator(const QIterable<Container> *iterable, void *iterator)
280
281 Creates a QConstIterator to wrap \a iterator, operating on \a iterable.
282 */
283
284/*!
285 \fn template<class Container> bool QConstIterator<Container>::operator==(const QConstIterator<Container> &other) const
286
287 Returns \c true if \a other points to the same item as this
288 iterator; otherwise returns \c false.
289
290 \sa operator!=()
291*/
292
293/*!
294 \fn template<class Container> bool QConstIterator<Container>::operator!=(const QConstIterator<Container> &other) const
295
296 Returns \c true if \a other points to a different item than this
297 iterator; otherwise returns \c false.
298
299 \sa operator==()
300*/
301
302/*!
303 \fn template<class Container> QConstIterator<Container> &QConstIterator<Container>::operator++()
304
305 The prefix \c{++} operator (\c{++it}) advances the iterator to the
306 next item in the container and returns an iterator to the new current
307 item.
308
309 Calling this function on QIterable<Container>::end() leads to undefined results.
310
311 \sa operator--()
312*/
313
314/*!
315 \fn template<class Container> QConstIterator<Container> QConstIterator<Container>::operator++(int)
316
317 \overload
318
319 The postfix \c{++} operator (\c{it++}) advances the iterator to the
320 next item in the container and returns an iterator to the previously
321 current item.
322*/
323
324/*!
325 \fn template<class Container> QConstIterator<Container> &QConstIterator<Container>::operator--()
326
327 The prefix \c{--} operator (\c{--it}) makes the preceding item
328 current and returns an iterator to the new current item.
329
330 Calling this function on QIterable<Container>::begin() leads to undefined results.
331
332 If the container in the QVariant does not support bi-directional iteration, calling this function
333 leads to undefined results.
334
335 \sa operator++(), QIterable::canReverseIterate()
336*/
337
338/*!
339 \fn template<class Container> QConstIterator<Container> QConstIterator<Container>::operator--(int)
340
341 \overload
342
343 The postfix \c{--} operator (\c{it--}) makes the preceding item
344 current and returns an iterator to the previously current item.
345
346 If the container in the QVariant does not support bi-directional iteration, calling this function
347 leads to undefined results.
348
349 \sa QIterable::canReverseIterate()
350*/
351
352/*!
353 \fn template<class Container> QConstIterator<Container> &QConstIterator<Container>::operator+=(qsizetype j)
354
355 Advances the iterator by \a j items.
356
357 \sa operator-=(), operator+()
358*/
359
360/*!
361 \fn template<class Container> QConstIterator<Container> &QConstIterator<Container>::operator-=(qsizetype j)
362
363 Makes the iterator go back by \a j items.
364
365 If the container in the QVariant does not support bi-directional iteration, calling this function
366 leads to undefined results.
367
368 \sa operator+=(), operator-(), QIterable::canReverseIterate()
369*/
370
371/*!
372 \fn template<class Container> QConstIterator<Container> QConstIterator<Container>::operator+(qsizetype j) const
373
374 Returns an iterator to the item at \a j positions forward from
375 this iterator.
376
377 \sa operator-(), operator+=()
378*/
379
380/*!
381 \fn template<class Container> QConstIterator<Container> QConstIterator<Container>::operator-(qsizetype j) const
382
383 Returns an iterator to the item at \a j positions backward from
384 this iterator.
385
386 If the container in the QVariant does not support bi-directional iteration, calling this function
387 leads to undefined results.
388
389 \sa operator+(), operator-=(), QIterable::canReverseIterate()
390*/
391
392/*!
393 \fn template <class Container> qsizetype QConstIterator<Container>::operator-(const QConstIterator<Container> &j) const
394
395 \overload
396
397 Returns the distance between the two iterators.
398
399 \sa operator+(), operator-=(), QIterable::canReverseIterate()
400 */
401
402/*!
403 \class QIterable
404 \inmodule QtCore
405 \since 6.0
406 \brief QIterable is a template class that is the base class for QMetaSequence::Iterable and QMetaAssociation::Iterable.
407
408 QIterable<Container> provides a common interface for iterating over
409 containers through QVariant, where \a Container is the meta-type descriptor
410 (either QMetaSequence or QMetaAssociation) that defines the container's
411 iteration capabilities.
412*/
413
414/*!
415 \fn template <class Container> bool QIterable<Container>::canInputIterate() const
416
417 Returns whether the container has an input iterator. This corresponds to
418 the std::input_iterator_tag iterator trait of the iterator and
419 const_iterator of the container.
420*/
421
422/*!
423 \fn template<class Container> bool QIterable<Container>::canForwardIterate() const
424
425 Returns whether it is possible to iterate over the container in forward
426 direction. This corresponds to the std::forward_iterator_tag iterator trait
427 of the iterator and const_iterator of the container.
428*/
429
430/*!
431 \fn template<class Container> bool QIterable<Container>::canReverseIterate() const
432
433 Returns whether it is possible to iterate over the container in reverse. This
434 corresponds to the std::bidirectional_iterator_tag iterator trait of the
435 const_iterator of the container.
436*/
437
438/*!
439 \fn template<class Container> bool QIterable<Container>::canRandomAccessIterate() const
440
441 Returns whether it is possible to efficiently skip over multiple values
442 using and iterator. This corresponds to the std::random_access_iterator_tag
443 iterator trait of the iterator and const_iterator of the container.
444*/
445
446/*!
447 \fn template<class Container> QConstIterator<Container> QIterable<Container>::constBegin() const
448
449 Returns a QConstIterator for the beginning of the container. This
450 can be used in stl-style iteration.
451
452 \sa constEnd(), mutableBegin()
453*/
454
455/*!
456 \fn template<class Container> QConstIterator<Container> QIterable<Container>::constEnd() const
457
458 Returns a Qterable::QConstIterator for the end of the container. This
459 can be used in stl-style iteration.
460
461 \sa constBegin(), mutableEnd()
462*/
463
464/*!
465 \fn template<class Container> QIterator<Container> QIterable<Container>::mutableBegin()
466
467 Returns a QIterator for the beginning of the container. This
468 can be used in stl-style iteration.
469
470 \sa mutableEnd(), constBegin()
471*/
472
473/*!
474 \fn template<class Container> QIterator<Container> QIterable<Container>::mutableEnd()
475
476 Returns a QMetaSequence::Iterable::iterator for the end of the container. This
477 can be used in stl-style iteration.
478
479 \sa mutableBegin(), constEnd()
480*/
481
482/*!
483 \fn template<class Container> qsizetype QIterable<Container>::size() const
484
485 Returns the number of values in the container.
486
487 \note If the underlying container does not provide a native way to query
488 the size, this method will synthesize the access using iterators.
489 This behavior is deprecated and will be removed in a future version
490 of Qt.
491*/
492
493/*!
494 \fn template<class Container> void QIterable<Container>::clear()
495
496 Clears the container.
497*/
498
499/*!
500 \class QTaggedIterator
501 \since 6.0
502 \inmodule QtCore
503 \brief QTaggedIterator is a template class that wraps an iterator and exposes standard iterator traits.
504
505 QTaggedIterator<Iterator, IteratorCategory> wraps an iterator, where
506 \a Iterator is the underlying iterator type (such as QIterator or
507 QConstIterator) and \a IteratorCategory is a standard iterator category tag
508 (such as std::forward_iterator_tag or std::random_access_iterator_tag).
509
510 In order to use an iterator any of the standard algorithms, its iterator
511 traits need to be known. As QMetaSequence::Iterable can work with many different
512 kinds of containers, we cannot declare the traits in the iterator classes
513 themselves. A QTaggedIterator gives you a way to explicitly declare a trait for
514 a concrete instance of an iterator or QConstIterator.
515*/
516
517/*!
518 \fn template<class Iterator, typename IteratorCategory> QTaggedIterator<Iterator, IteratorCategory>::QTaggedIterator(Iterator &&it)
519
520 Constructs a QTaggedIterator from an iterator or QConstIterator \a it. Checks
521 whether the IteratorCategory passed as template argument matches the run
522 time capabilities of \a it; if there's no match, \a it is refused.
523*/
524
525/*!
526 \fn template<class Iterator, typename IteratorCategory> bool QTaggedIterator<Iterator, IteratorCategory>::operator==(const QTaggedIterator<Iterator, IteratorCategory> &other) const
527
528 Returns \c true if \a other points to the same item as this
529 iterator; otherwise returns \c false.
530
531 \sa operator!=()
532*/
533
534/*!
535 \fn template<class Iterator, typename IteratorCategory> bool QTaggedIterator<Iterator, IteratorCategory>::operator!=(const QTaggedIterator<Iterator, IteratorCategory> &other) const
536
537 Returns \c true if \a other points to a different item than this
538 iterator; otherwise returns \c false.
539
540 \sa operator==()
541*/
542
543/*!
544 \fn template<class Iterator, typename IteratorCategory> QTaggedIterator<Iterator, IteratorCategory> &QTaggedIterator<Iterator, IteratorCategory>::operator++()
545
546 The prefix \c{++} operator (\c{++it}) advances the iterator to the
547 next item in the container and returns an iterator to the new current
548 item.
549
550 Calling this function on QMetaSequence::Iterable::constEnd() leads to undefined results.
551
552 \sa operator--()
553*/
554
555/*!
556 \fn template<class Iterator, typename IteratorCategory> QTaggedIterator<Iterator, IteratorCategory> QTaggedIterator<Iterator, IteratorCategory>::operator++(int)
557 \overload
558
559 The postfix \c{++} operator (\c{it++}) advances the iterator to the
560 next item in the container and returns an iterator to the previously
561 current item.
562*/
563
564
565/*!
566 \fn template<class Iterator, typename IteratorCategory> QTaggedIterator<Iterator, IteratorCategory> &QTaggedIterator<Iterator, IteratorCategory>::operator--()
567
568 The prefix \c{--} operator (\c{--it}) makes the preceding item
569 current and returns an iterator to the new current item.
570
571 Calling this function on QMetaSequence::Iterable::constBegin() leads to undefined results.
572
573 If the container in the QVariant does not support bi-directional iteration, calling this function
574 leads to undefined results.
575
576 \sa operator++(), QIterable::canReverseIterate()
577*/
578
579/*!
580 \fn template<class Iterator, typename IteratorCategory> QTaggedIterator<Iterator, IteratorCategory> QTaggedIterator<Iterator, IteratorCategory>::operator--(int)
581 \overload
582
583 The postfix \c{--} operator (\c{it--}) makes the preceding item
584 current and returns an iterator to the previously current item.
585
586 If the container in the QVariant does not support bi-directional iteration, calling this function
587 leads to undefined results.
588
589 \sa QIterable::canReverseIterate()
590*/
591
592
593/*!
594 \fn template<class Iterator, typename IteratorCategory> QTaggedIterator<Iterator, IteratorCategory> &QTaggedIterator<Iterator, IteratorCategory>::operator+=(qsizetype j)
595
596 Advances the iterator by \a j items.
597
598 \sa operator-=(), operator+()
599*/
600
601/*!
602 \fn template<class Iterator, typename IteratorCategory> QTaggedIterator<Iterator, IteratorCategory> &QTaggedIterator<Iterator, IteratorCategory>::operator-=(qsizetype j)
603
604 Makes the iterator go back by \a j items.
605
606 If the container in the QVariant does not support bi-directional iteration, calling this function
607 leads to undefined results.
608
609 \sa operator+=(), operator-(), QIterable::canReverseIterate()
610*/
611
612/*!
613 \fn template<class Iterator, typename IteratorCategory> QTaggedIterator<Iterator, IteratorCategory> QTaggedIterator<Iterator, IteratorCategory>::operator+(qsizetype j) const
614
615 Returns an iterator to the item at \a j positions forward from
616 this iterator.
617
618 \sa operator-(), operator+=()
619*/
620
621/*!
622 \fn template<class Iterator, typename IteratorCategory> QTaggedIterator<Iterator, IteratorCategory> QTaggedIterator<Iterator, IteratorCategory>::operator+(qsizetype j, const QTaggedIterator &k)
623
624 Returns an iterator to the item at \a j positions forward from iterator \a k.
625*/
626
627/*!
628 \fn template<class Iterator, typename IteratorCategory> QTaggedIterator<Iterator, IteratorCategory> QTaggedIterator<Iterator, IteratorCategory>::operator-(qsizetype j) const
629
630 Returns an iterator to the item at \a j positions backward from
631 this iterator.
632
633 If the container in the QVariant does not support bi-directional iteration, calling this function
634 leads to undefined results.
635
636 \sa operator+(), operator-=(), QIterable::canReverseIterate()
637*/
638
639/*!
640 \fn template <class Iterator, typename IteratorCategory> qsizetype QTaggedIterator<Iterator, IteratorCategory>::operator-(const QTaggedIterator<Iterator, IteratorCategory> &j) const
641
642 Returns the distance between this iterator and \a j.
643
644 \sa operator+(), operator-=(), QIterable::canReverseIterate()
645*/
646
647/*!
648 \internal
649 */
650void QtPrivate::warnSynthesizedIterableAccess(QtPrivate::SynthesizedAccessFunction function)
651{
652 switch (function) {
653 case QtPrivate::SynthesizedAccessFunction::IterableSize:
654 qCWarning(lcSynthesizedIterableAccess,
655 "size() called on an iterable without native size accessor. This is slow");
656 break;
657 case QtPrivate::SynthesizedAccessFunction::SequenceAt:
658 qCWarning(lcSynthesizedIterableAccess,
659 "at() called on an iterable without native indexed accessors. This is slow");
660 break;
661 }
662}
663
664QT_END_NAMESPACE
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)