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