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
qcborarray.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 Intel Corporation.
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:critical reason:data-parser
4
5#include "qcborarray.h"
6#include "qcborvalue_p.h"
7#include "qdatastream.h"
8
10
11using namespace QtCbor;
12
13/*!
14 \class QCborArray
15 \inmodule QtCore
16 \ingroup cbor
17 \ingroup qtserialization
18 \reentrant
19 \since 5.12
20
21 \brief The QCborArray class is used to hold an array of CBOR elements.
22
23 \compares strong
24 \compareswith strong QCborValueConstRef
25 \endcompareswith
26
27 This class can be used to hold one sequential container in CBOR (an array).
28 CBOR is the Concise Binary Object Representation, a very compact form of
29 binary data encoding that is a superset of JSON. It was created by the IETF
30 Constrained RESTful Environments (CoRE) WG, which has used it in many new
31 RFCs. It is meant to be used alongside the
32 \l{RFC 7252}{CoAP protocol}.
33
34 QCborArray is very similar to \l QVariantList and \l QJsonArray and its API
35 is almost identical to those two classes. It can also be converted to and
36 from those two, though there may be loss of information in some
37 conversions.
38
39 \sa QCborValue, QCborMap, QJsonArray, QList, {Parsing and displaying CBOR data},
40 {Serialization Converter}, {Saving and Loading a Game}
41 */
42
43/*!
44 \typedef QCborArray::size_type
45
46 A typedef to qsizetype.
47 */
48
49/*!
50 \typedef QCborArray::difference_type
51
52 A typedef to qsizetype.
53 */
54
55/*!
56 \typedef QCborArray::value_type
57
58 The type of values that can be held in a QCborArray: that is, QCborValue.
59 */
60
61/*!
62 \typedef QCborArray::pointer
63
64 A typedef to \c{QCborValue *}, for compatibility with generic algorithms.
65 */
66
67/*!
68 \typedef QCborArray::const_pointer
69
70 A typedef to \c{const QCborValue *}, for compatibility with generic algorithms.
71 */
72
73/*!
74 \typedef QCborArray::reference
75
76 A typedef to \c{QCborValue &}, for compatibility with generic algorithms.
77 */
78
79/*!
80 \typedef QCborArray::const_reference
81
82 A typedef to \c{const QCborValue &}, for compatibility with generic algorithms.
83 */
84
85/*!
86 Constructs an empty QCborArray.
87 */
88QCborArray::QCborArray() noexcept
89 : d(nullptr)
90{
91}
92
93/*!
94 Copies the contents of \a other into this object.
95 */
96QCborArray::QCborArray(const QCborArray &other) noexcept
97 : d(other.d)
98{
99}
100
101/*!
102 \fn QCborArray::QCborArray(QCborArray &&other)
103 \since 6.10
104
105 Move-constructor.
106
107 The moved-from object \a other is placed in the default-constructed state.
108
109 \sa QCborArray::QCborArray()
110*/
111
112/*!
113 \fn QCborArray::QCborArray(std::initializer_list<QCborValue> args)
114
115 Initializes this QCborArray from the C++ brace-enclosed list found in \a
116 args, as in the following example:
117
118 \code
119 QCborArray a = { null, 0, 1, 1.5, 2, "Hello", QByteArray("World") };
120 \endcode
121 */
122
123/*!
124 Destroys this QCborArray and frees any associated resources.
125 */
126QCborArray::~QCborArray()
127{
128}
129
130/*!
131 Replaces the contents of this array with that found in \a other, then
132 returns a reference to this object.
133 */
134QCborArray &QCborArray::operator=(const QCborArray &other) noexcept
135{
136 d = other.d;
137 return *this;
138}
139
140/*!
141 \fn QCborArray &QCborArray::operator=(QCborArray &&other)
142 \since 6.10
143
144 Move-assignment operator.
145
146 The moved-from object \a other is placed in a valid, but unspecified state.
147*/
148
149/*!
150 \fn void QCborArray::swap(QCborArray &other)
151 \memberswap{array}
152 */
153
154/*!
155 \fn QCborValue QCborArray::toCborValue() const
156
157 Explicitly construcuts a \l QCborValue object that represents this array.
158 This function is usually not necessary since QCborValue has a constructor
159 for QCborArray, so the conversion is implicit.
160
161 Converting QCborArray to QCborValue allows it to be used in any context
162 where QCborValues can be used, including as items in QCborArrays and as keys
163 and mapped types in QCborMap. Converting an array to QCborValue allows
164 access to QCborValue::toCbor().
165
166 \sa QCborValue::QCborValue(const QCborArray &)
167 */
168
169/*!
170 Returns the size of this array.
171
172 \sa isEmpty()
173 */
174qsizetype QCborArray::size() const noexcept
175{
176 return d ? d->elements.size() : 0;
177}
178
179/*!
180 Empties this array.
181
182 \sa isEmpty()
183 */
184void QCborArray::clear()
185{
186 d.reset();
187}
188
189/*!
190 \fn bool QCborArray::isEmpty() const
191
192 Returns true if this QCborArray is empty (that is if size() is 0).
193
194 \sa size(), clear()
195 */
196
197/*!
198 Returns the QCborValue element at position \a i in the array.
199
200 If the array is smaller than \a i elements, this function returns a
201 QCborValue containing an undefined value. For that reason, it is not
202 possible with this function to tell apart the situation where the array is
203 not large enough from the case where the array starts with an undefined
204 value.
205
206 \sa operator[](), first(), last(), insert(), prepend(), append(),
207 removeAt(), takeAt()
208 */
209QCborValue QCborArray::at(qsizetype i) const
210{
211 if (!d || size_t(i) >= size_t(size()))
212 return QCborValue();
213 return d->valueAt(i);
214}
215
216/*!
217 \fn QCborValue QCborArray::first() const
218
219 Returns the first QCborValue of this array.
220
221 If the array is empty, this function returns a QCborValue containing an
222 undefined value. For that reason, it is not possible with this function to
223 tell apart the situation where the array is not large enough from the case
224 where the array ends with an undefined value.
225
226 \sa operator[](), at(), last(), insert(), prepend(), append(),
227 removeAt(), takeAt()
228 */
229
230/*!
231 \fn QCborValue QCborArray::last() const
232
233 Returns the last QCborValue of this array.
234
235 If the array is empty, this function returns a QCborValue containing an
236 undefined value. For that reason, it is not possible with this function to
237 tell apart the situation where the array is not large enough from the case
238 where the array ends with an undefined value.
239
240 \sa operator[](), at(), first(), insert(), prepend(), append(),
241 removeAt(), takeAt()
242 */
243
244/*!
245 \fn QCborValue QCborArray::operator[](qsizetype i) const
246
247 Returns the QCborValue element at position \a i in the array.
248
249 If the array is smaller than \a i elements, this function returns a
250 QCborValue containing an undefined value. For that reason, it is not
251 possible with this function to tell apart the situation where the array is
252 not large enough from the case where the array contains an undefined value
253 at position \a i.
254
255 \sa at(), first(), last(), insert(), prepend(), append(),
256 removeAt(), takeAt()
257 */
258
259/*!
260 \fn QCborValueRef QCborArray::first()
261
262 Returns a reference to the first QCborValue of this array. The array must
263 not be empty.
264
265 QCborValueRef has the exact same API as \l QCborValue, with one important
266 difference: if you assign new values to it, this array will be updated with
267 that new value.
268
269 \sa operator[](), at(), last(), insert(), prepend(), append(),
270 removeAt(), takeAt()
271 */
272
273/*!
274 \fn QCborValueRef QCborArray::last()
275
276 Returns a reference to the last QCborValue of this array. The array must
277 not be empty.
278
279 QCborValueRef has the exact same API as \l QCborValue, with one important
280 difference: if you assign new values to it, this array will be updated with
281 that new value.
282
283 \sa operator[](), at(), first(), insert(), prepend(), append(),
284 removeAt(), takeAt()
285 */
286
287/*!
288 \fn QCborValueRef QCborArray::operator[](qsizetype i)
289
290 Returns a reference to the QCborValue element at position \a i in the
291 array. Indices beyond the end of the array will grow the array, filling
292 with undefined entries, until it has an entry at the specified index.
293
294 QCborValueRef has the exact same API as \l QCborValue, with one important
295 difference: if you assign new values to it, this array will be updated with
296 that new value.
297
298 \sa at(), first(), last(), insert(), prepend(), append(),
299 removeAt(), takeAt()
300 */
301
302/*!
303 \fn void QCborArray::insert(qsizetype i, const QCborValue &value)
304 \fn void QCborArray::insert(qsizetype i, QCborValue &&value)
305
306 Inserts \a value into the array at position \a i in this array. If \a i is
307 -1, the entry is appended to the array. Pads the array with invalid entries
308 if \a i is greater than the prior size of the array.
309
310 \sa at(), operator[](), first(), last(), prepend(), append(),
311 removeAt(), takeAt(), extract()
312 */
313void QCborArray::insert(qsizetype i, const QCborValue &value)
314{
315 if (i < 0) {
316 Q_ASSERT(i == -1);
317 i = size();
318 detach(i + 1);
319 } else {
320 d = QCborContainerPrivate::grow(d.data(), i); // detaches
321 }
322 d->insertAt(i, value);
323}
324
325void QCborArray::insert(qsizetype i, QCborValue &&value)
326{
327 if (i < 0) {
328 Q_ASSERT(i == -1);
329 i = size();
330 detach(i + 1);
331 } else {
332 d = QCborContainerPrivate::grow(d.data(), i); // detaches
333 }
334 d->insertAt(i, value, QCborContainerPrivate::MoveContainer);
335 QCborContainerPrivate::resetValue(value);
336}
337
338/*!
339 \fn QCborValue QCborArray::extract(Iterator it)
340 \fn QCborValue QCborArray::extract(ConstIterator it)
341
342 Extracts a value from the array at the position indicated by iterator \a it
343 and returns the value so extracted.
344
345 \sa insert(), erase(), takeAt(), removeAt()
346 */
347QCborValue QCborArray::extract(iterator it)
348{
349 detach();
350
351 QCborValue v = d->extractAt(it.item.i);
352 d->removeAt(it.item.i);
353 return v;
354}
355
356/*!
357 \fn void QCborArray::prepend(const QCborValue &value)
358 \fn void QCborArray::prepend(QCborValue &&value)
359
360 Prepends \a value into the array before any other elements it may already
361 contain.
362
363 \sa at(), operator[](), first(), last(), insert(), append(),
364 removeAt(), takeAt()
365 */
366
367/*!
368 \fn void QCborArray::append(const QCborValue &value)
369 \fn void QCborArray::append(QCborValue &&value)
370
371 Appends \a value into the array after all other elements it may already
372 contain.
373
374 \sa at(), operator[](), first(), last(), insert(), prepend(),
375 removeAt(), takeAt()
376 */
377
378/*!
379 Removes the item at position \a i from the array. The array must have more
380 than \a i elements before the removal.
381
382 \sa takeAt(), removeFirst(), removeLast(), at(), operator[](), insert(),
383 prepend(), append()
384 */
385void QCborArray::removeAt(qsizetype i)
386{
387 detach(size());
388 d->removeAt(i);
389}
390
391/*!
392 \fn QCborValue QCborArray::takeAt(qsizetype i)
393
394 Removes the item at position \a i from the array and returns it. The array
395 must have more than \a i elements before the removal.
396
397 \sa removeAt(), removeFirst(), removeLast(), at(), operator[](), insert(),
398 prepend(), append()
399 */
400
401/*!
402 \fn void QCborArray::removeFirst()
403
404 Removes the first item in the array, making the second element become the
405 first. The array must not be empty before this call.
406
407 \sa removeAt(), takeFirst(), removeLast(), at(), operator[](), insert(),
408 prepend(), append()
409 */
410
411/*!
412 \fn void QCborArray::removeLast()
413
414 Removes the last item in the array. The array must not be empty before this
415 call.
416
417 \sa removeAt(), takeLast(), removeFirst(), at(), operator[](), insert(),
418 prepend(), append()
419 */
420
421/*!
422 \fn void QCborArray::takeFirst()
423
424 Removes the first item in the array and returns it, making the second
425 element become the first. The array must not be empty before this call.
426
427 \sa takeAt(), removeFirst(), removeLast(), at(), operator[](), insert(),
428 prepend(), append()
429 */
430
431/*!
432 \fn void QCborArray::takeLast()
433
434 Removes the last item in the array and returns it. The array must not be
435 empty before this call.
436
437 \sa takeAt(), removeLast(), removeFirst(), at(), operator[](), insert(),
438 prepend(), append()
439 */
440
441/*!
442 Returns true if this array contains an element that is equal to \a value.
443 */
444bool QCborArray::contains(const QCborValue &value) const
445{
446 for (qsizetype i = 0; i < size(); ++i) {
447 int cmp = d->compareElement(i, value, Comparison::ForEquality);
448 if (cmp == 0)
449 return true;
450 }
451 return false;
452}
453
454/*!
455 \fn int QCborArray::compare(const QCborArray &other) const
456
457 Compares this array and \a other, comparing each element in sequence, and
458 returns an integer that indicates whether this array should be sorted
459 before (if the result is negative) or after \a other (if the result is
460 positive). If this function returns 0, the two arrays are equal and contain
461 the same elements.
462
463 For more information on CBOR sorting order, see QCborValue::compare().
464
465 \sa QCborValue::compare(), QCborMap::compare(), operator==()
466 */
467
468/*!
469 \fn bool QCborArray::operator==(const QCborArray &lhs, const QCborArray &rhs)
470
471 Compares \a lhs and \a rhs arrays, comparing each element in sequence, and
472 returns true if both arrays contains the same elements, false otherwise.
473
474 For more information on CBOR equality in Qt, see, QCborValue::compare().
475
476 \sa compare(), QCborValue::operator==(), QCborMap::operator==(),
477 operator!=(), operator<()
478 */
479
480/*!
481 \fn bool QCborArray::operator!=(const QCborArray &lhs, const QCborArray &rhs)
482
483 Compares \a lhs and \a rhs arrays, comparing each element in sequence, and
484 returns true if the two arrays' contents are different, false otherwise.
485
486 For more information on CBOR equality in Qt, see, QCborValue::compare().
487
488 \sa compare(), QCborValue::operator==(), QCborMap::operator==(),
489 operator==(), operator<()
490 */
491
492/*!
493 \fn bool QCborArray::operator<(const QCborArray &lhs, const QCborArray &rhs)
494
495 Compares \a lhs and \a rhs arrays, comparing each element in sequence, and
496 returns true if \a lhs array should be sorted before \a rhs, false
497 otherwise.
498
499 For more information on CBOR sorting order, see QCborValue::compare().
500
501 \sa compare(), QCborValue::operator==(), QCborMap::operator==(),
502 operator==(), operator!=(), operator<=()
503 */
504
505/*!
506 \fn bool QCborArray::operator<=(const QCborArray &lhs, const QCborArray &rhs)
507
508 Compares \a lhs and \a rhs arrays, comparing each element in sequence, and
509 returns true if \a lhs array should be sorted before \a rhs, or if both
510 arrays contains the same elements, false otherwise.
511
512 For more information on CBOR sorting order, see QCborValue::compare().
513
514 \sa compare(), QCborValue::operator==(), QCborMap::operator==(),
515 operator==(), operator!=(), operator<()
516*/
517
518/*!
519 \fn bool QCborArray::operator>(const QCborArray &lhs, const QCborArray &rhs)
520
521 Compares \a lhs and \a rhs arrays, comparing each element in sequence, and
522 returns true if \a lhs array should be sorted after \a rhs, false
523 otherwise.
524
525 For more information on CBOR sorting order, see QCborValue::compare().
526
527 \sa compare(), QCborValue::operator==(), QCborMap::operator==(),
528 operator==(), operator!=(), operator>=()
529*/
530
531/*!
532 \fn bool QCborArray::operator>=(const QCborArray &lhs, const QCborArray &rhs)
533
534 Compares \a lhs and \a rhs arrays, comparing each element in sequence, and
535 returns true if \a lhs array should be sorted after \a rhs, or if both
536 arrays contains the same elements, false otherwise.
537
538 For more information on CBOR sorting order, see QCborValue::compare().
539
540 \sa compare(), QCborValue::operator==(), QCborMap::operator==(),
541 operator==(), operator!=(), operator>()
542*/
543
544/*!
545 \typedef QCborArray::iterator
546
547 A synonym to QCborArray::Iterator.
548 */
549
550/*!
551 \typedef QCborArray::const_iterator
552
553 A synonym to QCborArray::ConstIterator.
554 */
555
556/*!
557 \fn QCborArray::iterator QCborArray::begin()
558
559 Returns an array iterator pointing to the first item in this array. If the
560 array is empty, then this function returns the same as end().
561
562 \sa constBegin(), end()
563 */
564
565/*!
566 \fn QCborArray::const_iterator QCborArray::begin() const
567
568 Returns an array iterator pointing to the first item in this array. If the
569 array is empty, then this function returns the same as end().
570
571 \sa constBegin(), constEnd()
572 */
573
574/*!
575 \fn QCborArray::const_iterator QCborArray::cbegin() const
576
577 Returns an array iterator pointing to the first item in this array. If the
578 array is empty, then this function returns the same as end().
579
580 \sa constBegin(), constEnd()
581 */
582
583/*!
584 \fn QCborArray::const_iterator QCborArray::constBegin() const
585
586 Returns an array iterator pointing to the first item in this array. If the
587 array is empty, then this function returns the same as end().
588
589 \sa begin(), constEnd()
590 */
591
592/*!
593 \fn QCborArray::iterator QCborArray::end()
594
595 Returns an array iterator pointing to just after the last element in this
596 array.
597
598 \sa begin(), constEnd()
599 */
600
601/*!
602 \fn QCborArray::const_iterator QCborArray::end() const
603
604 Returns an array iterator pointing to just after the last element in this
605 array.
606
607 \sa constBegin(), constEnd()
608 */
609
610/*!
611 \fn QCborArray::const_iterator QCborArray::cend() const
612
613 Returns an array iterator pointing to just after the last element in this
614 array.
615
616 \sa constBegin(), constEnd()
617 */
618
619/*!
620 \fn QCborArray::const_iterator QCborArray::constEnd() const
621
622 Returns an array iterator pointing to just after the last element in this
623 array.
624
625 \sa constBegin(), end()
626 */
627
628/*!
629 \fn QCborArray::iterator QCborArray::insert(iterator before, const QCborValue &value)
630 \fn QCborArray::iterator QCborArray::insert(const_iterator before, const QCborValue &value)
631 \overload
632
633 Inserts \a value into this array before element \a before and returns an
634 array iterator pointing to the just-inserted element.
635
636 \sa erase(), removeAt(), prepend(), append()
637 */
638
639/*!
640 \fn QCborArray::iterator QCborArray::erase(iterator it)
641 \fn QCborArray::iterator QCborArray::erase(const_iterator it)
642
643 Removes the element pointed to by the array iterator \a it from this array,
644 then returns an iterator to the next element (the one that took the same
645 position in the array that \a it used to occupy).
646
647 \sa insert(), removeAt(), takeAt(), takeFirst(), takeLast()
648 */
649
650/*!
651 \fn void QCborArray::push_back(const QCborValue &t)
652
653 Synonym for append(). This function is provided for compatibility with
654 generic code that uses the Standard Library API.
655
656 Appends the element \a t to this array.
657
658 \sa append(), push_front(), pop_back(), prepend(), insert()
659 */
660
661/*!
662 \fn void QCborArray::push_front(const QCborValue &t)
663
664 Synonym for prepend(). This function is provided for compatibility with
665 generic code that uses the Standard Library API.
666
667 Prepends the element \a t to this array.
668
669 \sa prepend(), push_back(), pop_front(), append(), insert()
670 */
671
672/*!
673 \fn void QCborArray::pop_front()
674
675 Synonym for removeFirst(). This function is provided for compatibility with
676 generic code that uses the Standard Library API.
677
678 Removes the first element of this array. The array must not be empty before
679 the removal
680
681 \sa removeFirst(), takeFirst(), pop_back(), push_front(), prepend(), insert()
682 */
683
684/*!
685 \fn void QCborArray::pop_back()
686
687 Synonym for removeLast(). This function is provided for compatibility with
688 generic code that uses the Standard Library API.
689
690 Removes the last element of this array. The array must not be empty before
691 the removal
692
693 \sa removeLast(), takeLast(), pop_front(), push_back(), append(), insert()
694 */
695
696/*!
697 \fn bool QCborArray::empty() const
698
699 Synonym for isEmpty(). This function is provided for compatibility with
700 generic code that uses the Standard Library API.
701
702 Returns true if this array is empty (size() == 0).
703
704 \sa isEmpty(), size()
705 */
706
707/*!
708 \fn QCborArray QCborArray::operator+(const QCborValue &v) const
709
710 Returns a new QCborArray containing the same elements as this array, plus
711 \a v appended as the last element.
712
713 \sa operator+=(), operator<<(), append()
714 */
715
716/*!
717 \fn QCborArray &QCborArray::operator+=(const QCborValue &v)
718
719 Appends \a v to this array and returns a reference to this array.
720
721 \sa append(), insert(), operator+(), operator<<()
722 */
723
724/*!
725 \fn QCborArray &QCborArray::operator<<(const QCborValue &v)
726
727 Appends \a v to this array and returns a reference to this array.
728
729 \sa append(), insert(), operator+(), operator+=()
730 */
731
732void QCborArray::detach(qsizetype reserved)
733{
734 d = QCborContainerPrivate::detach(d.data(), reserved ? reserved : size());
735}
736
737/*!
738 \class QCborArray::Iterator
739 \inmodule QtCore
740 \ingroup cbor
741 \since 5.12
742
743 \brief The QCborArray::Iterator class provides an STL-style non-const iterator for QCborArray.
744
745 \compares strong
746 \compareswith strong QCborArray::ConstIterator
747 \endcompareswith
748
749 QCborArray::Iterator allows you to iterate over a QCborArray and to modify
750 the array item associated with the iterator. If you want to iterate over a
751 const QCborArray, use QCborArray::ConstIterator instead. It is generally a
752 good practice to use QCborArray::ConstIterator on a non-const QCborArray as
753 well, unless you need to change the QCborArray through the iterator. Const
754 iterators are slightly faster and improve code readability.
755
756 Iterators are initialized by using a QCborArray function like
757 QCborArray::begin(), QCborArray::end(), or QCborArray::insert(). Iteration
758 is only possible after that.
759
760 Most QCborArray functions accept an integer index rather than an iterator.
761 For that reason, iterators are rarely useful in connection with QCborArray.
762 One place where STL-style iterators do make sense is as arguments to
763 \l{generic algorithms}.
764
765 Multiple iterators can be used on the same array. However, be aware that
766 any non-const function call performed on the QCborArray will render all
767 existing iterators undefined.
768
769 \sa QCborArray::ConstIterator
770*/
771
772/*!
773 \typedef QCborArray::Iterator::iterator_category
774
775 A synonym for \e {std::random_access_iterator_tag} indicating this iterator
776 is a random access iterator.
777 */
778
779/*!
780 \typedef QCborArray::Iterator::difference_type
781 \internal
782*/
783
784/*!
785 \typedef QCborArray::Iterator::value_type
786 \internal
787*/
788
789/*!
790 \typedef QCborArray::Iterator::reference
791 \internal
792*/
793
794/*!
795 \typedef QCborArray::Iterator::pointer
796 \internal
797*/
798
799/*!
800 \fn QCborArray::Iterator::Iterator()
801
802 Constructs an uninitialized iterator.
803
804 Functions like operator*() and operator++() should not be called on an
805 uninitialized iterator. Use operator=() to assign a value to it before
806 using it.
807
808 \sa QCborArray::begin(), QCborArray::end()
809*/
810
811/*!
812 \fn QCborArray::Iterator::Iterator(const Iterator &other)
813
814 Makes a copy of \a other.
815 */
816
817/*!
818 \fn QCborArray::Iterator &QCborArray::Iterator::operator=(const Iterator &other)
819
820 Makes this iterator a copy of \a other and returns a reference to this iterator.
821 */
822
823/*!
824 \fn QCborValueRef QCborArray::Iterator::operator*() const
825
826 Returns a modifiable reference to the current item.
827
828 You can change the value of an item by using operator*() on the left side
829 of an assignment.
830
831 The return value is of type QCborValueRef, a helper class for QCborArray
832 and QCborMap. When you get an object of type QCborValueRef, you can use it
833 as if it were a reference to a QCborValue. If you assign to it, the
834 assignment will apply to the element in the QCborArray or QCborMap from
835 which you got the reference.
836*/
837
838/*!
839 \fn QCborValueRef *QCborArray::Iterator::operator->() const
840
841 Returns a pointer to a modifiable reference to the current item.
842*/
843
844/*!
845 \fn QCborValueRef QCborArray::Iterator::operator[](qsizetype j) const
846
847 Returns a modifiable reference to the item at a position \a j steps forward
848 from the item pointed to by this iterator.
849
850 This function is provided to make QCborArray iterators behave like C++
851 pointers.
852
853 The return value is of type QCborValueRef, a helper class for QCborArray
854 and QCborMap. When you get an object of type QCborValueRef, you can use it
855 as if it were a reference to a QCborValue. If you assign to it, the
856 assignment will apply to the element in the QCborArray or QCborMap from
857 which you got the reference.
858
859 \sa operator+()
860*/
861
862/*!
863 \fn bool QCborArray::Iterator::operator==(const Iterator &lhs, const Iterator &rhs)
864 \fn bool QCborArray::Iterator::operator==(const Iterator &lhs, const ConstIterator &rhs)
865
866 Returns \c true if \a lhs points to the same entry in the array as \a rhs
867 iterator; otherwise returns \c false.
868
869 \sa operator!=()
870*/
871
872/*!
873 \fn bool QCborArray::Iterator::operator!=(const Iterator &lhs, const Iterator &rhs)
874 \fn bool QCborArray::Iterator::operator!=(const Iterator &lhs, const ConstIterator &rhs)
875
876 Returns \c true if \a lhs points to a different entry in the array than
877 \a rhs iterator; otherwise returns \c false.
878
879 \sa operator==()
880*/
881
882/*!
883 \fn bool QCborArray::Iterator::operator<(const Iterator &lhs, const Iterator &rhs)
884 \fn bool QCborArray::Iterator::operator<(const Iterator &lhs, const ConstIterator &rhs)
885
886 Returns \c true if the entry in the array pointed to by \a lhs iterator
887 occurs before the entry pointed to by the \a rhs iterator.
888*/
889
890/*!
891 \fn bool QCborArray::Iterator::operator<=(const Iterator &lhs, const Iterator &rhs)
892 \fn bool QCborArray::Iterator::operator<=(const Iterator &lhs, const ConstIterator &rhs)
893
894 Returns \c true if the entry in the array pointed to by \a lhs iterator
895 occurs before or is the same entry as is pointed to by the \a rhs
896 iterator.
897*/
898
899/*!
900 \fn bool QCborArray::Iterator::operator>(const Iterator &lhs, const Iterator &rhs)
901 \fn bool QCborArray::Iterator::operator>(const Iterator &lhs, const ConstIterator &rhs)
902
903 Returns \c true if the entry in the array pointed to by \a lhs iterator
904 occurs after the entry pointed to by the \a rhs iterator.
905 */
906
907/*!
908 \fn bool QCborArray::Iterator::operator>=(const Iterator &lhs, const Iterator &rhs)
909 \fn bool QCborArray::Iterator::operator>=(const Iterator &lhs, const ConstIterator &rhs)
910
911 Returns \c true if the entry in the array pointed to by \a lhs iterator
912 occurs after or is the same entry as is pointed to by the \a rhs
913 iterator.
914*/
915
916/*!
917 \fn QCborArray::Iterator &QCborArray::Iterator::operator++()
918
919 The prefix \c{++} operator, \c{++it}, advances the iterator to the next item in
920 the array and returns this iterator.
921
922 Calling this function on QCborArray::end() leads to undefined results.
923
924 \sa operator--()
925*/
926
927/*!
928 \fn QCborArray::Iterator QCborArray::Iterator::operator++(int)
929 \overload
930
931 The postfix \c{++} operator, \c{it++}, advances the iterator to the next item
932 in the array and returns an iterator to the previously current item.
933*/
934
935/*!
936 \fn QCborArray::Iterator &QCborArray::Iterator::operator--()
937
938 The prefix \c{--} operator, \c{--it}, makes the preceding item current and
939 returns this iterator.
940
941 Calling this function on QCborArray::begin() leads to undefined results.
942
943 \sa operator++()
944*/
945
946/*!
947 \fn QCborArray::Iterator QCborArray::Iterator::operator--(int)
948 \overload
949
950 The postfix \c{--} operator, \c{it--}, makes the preceding item current and
951 returns an iterator to the previously current item.
952*/
953
954/*!
955 \fn QCborArray::Iterator &QCborArray::Iterator::operator+=(qsizetype j)
956
957 Advances the iterator by \a j positions. If \a j is negative, the iterator
958 goes backward. Returns a reference to this iterator.
959
960 \sa operator-=(), operator+()
961*/
962
963/*!
964 \fn QCborArray::Iterator &QCborArray::Iterator::operator-=(qsizetype j)
965
966 Makes the iterator go back by \a j positions. If \a j is negative, the
967 iterator goes forward. Returns a reference to this iterator.
968
969 \sa operator+=(), operator-()
970*/
971
972/*!
973 \fn QCborArray::Iterator QCborArray::Iterator::operator+(qsizetype j) const
974
975 Returns an iterator to the item at position \a j steps forward from this
976 iterator. If \a j is negative, the iterator goes backward.
977
978 \sa operator-(), operator+=()
979*/
980
981/*!
982 \fn QCborArray::Iterator QCborArray::Iterator::operator-(qsizetype j) const
983
984 Returns an iterator to the item at position \a j steps backward from this
985 iterator. If \a j is negative, the iterator goes forward.
986
987 \sa operator+(), operator-=()
988*/
989
990/*!
991 \fn qsizetype QCborArray::Iterator::operator-(Iterator other) const
992
993 Returns the offset of this iterator relative to \a other.
994*/
995
996/*!
997 \class QCborArray::ConstIterator
998 \inmodule QtCore
999 \ingroup cbor
1000 \since 5.12
1001
1002 \brief The QCborArray::ConstIterator class provides an STL-style const iterator for QCborArray.
1003
1004 \compares strong
1005 \compareswith strong QCborArray::Iterator
1006 \endcompareswith
1007
1008 QCborArray::ConstIterator allows you to iterate over a QCborArray. If you
1009 want to modify the QCborArray as you iterate over it, use
1010 QCborArray::Iterator instead. It is generally good practice to use
1011 QCborArray::ConstIterator, even on a non-const QCborArray, when you don't
1012 need to change the QCborArray through the iterator. Const iterators are
1013 slightly faster and improves code readability.
1014
1015 Iterators are initialized by using a QCborArray function like
1016 QCborArray::begin() or QCborArray::end(). Iteration is only possible after
1017 that.
1018
1019 Most QCborArray functions accept an integer index rather than an iterator.
1020 For that reason, iterators are rarely useful in connection with QCborArray.
1021 One place where STL-style iterators do make sense is as arguments to
1022 \l{generic algorithms}.
1023
1024 Multiple iterators can be used on the same array. However, be aware that
1025 any non-const function call performed on the QCborArray will render all
1026 existing iterators undefined.
1027
1028 \sa QCborArray::Iterator
1029*/
1030
1031/*!
1032 \fn QCborArray::ConstIterator::ConstIterator()
1033
1034 Constructs an uninitialized iterator.
1035
1036 Functions like operator*() and operator++() should not be called on an
1037 uninitialized iterator. Use operator=() to assign a value to it before
1038 using it.
1039
1040 \sa QCborArray::constBegin(), QCborArray::constEnd()
1041*/
1042
1043/*!
1044 \fn QCborArray::ConstIterator &QCborArray::ConstIterator::operator=(const ConstIterator &other)
1045
1046 Makes this iterator a copy of \a other and returns a reference to this iterator.
1047*/
1048
1049/*!
1050 \typedef QCborArray::ConstIterator::iterator_category
1051
1052 A synonym for \e {std::random_access_iterator_tag} indicating this iterator
1053 is a random access iterator.
1054*/
1055
1056/*!
1057 \typedef QCborArray::ConstIterator::difference_type
1058 \internal
1059*/
1060
1061/*!
1062 \typedef QCborArray::ConstIterator::value_type
1063 \internal
1064*/
1065
1066/*!
1067 \typedef QCborArray::ConstIterator::reference
1068 \internal
1069*/
1070
1071/*!
1072 \typedef QCborArray::ConstIterator::pointer
1073 \internal
1074*/
1075
1076/*!
1077 \fn QCborArray::ConstIterator::ConstIterator(const ConstIterator &other)
1078
1079 Constructs a copy of \a other.
1080*/
1081
1082/*!
1083 \fn QCborValue QCborArray::ConstIterator::operator*() const
1084
1085 Returns the current item.
1086*/
1087
1088/*!
1089 \fn const QCborValue *QCborArray::ConstIterator::operator->() const
1090
1091 Returns a pointer to the current item.
1092*/
1093
1094/*!
1095 \fn QCborValueRef QCborArray::ConstIterator::operator[](qsizetype j) const
1096
1097 Returns the item at a position \a j steps forward from the item pointed to
1098 by this iterator.
1099
1100 This function is provided to make QCborArray iterators behave like C++
1101 pointers.
1102
1103 \sa operator+()
1104*/
1105
1106/*!
1107 \fn bool QCborArray::ConstIterator::operator==(const ConstIterator &lhs, const ConstIterator &rhs)
1108
1109 Returns \c true if \a lhs points to the same entry in the array as \a rhs
1110 iterator; otherwise returns \c false.
1111
1112 \sa operator!=()
1113*/
1114
1115/*!
1116 \fn bool QCborArray::ConstIterator::operator!=(const ConstIterator &lhs, const ConstIterator &rhs)
1117
1118 Returns \c true if \a lhs points to a different entry in the array than
1119 \a rhs iterator; otherwise returns \c false.
1120
1121 \sa operator==()
1122*/
1123
1124/*!
1125 \fn bool QCborArray::ConstIterator::operator<(const ConstIterator &lhs, const ConstIterator &rhs)
1126
1127 Returns \c true if the entry in the array pointed to by \a lhs iterator
1128 occurs before the entry pointed to by the \a rhs iterator.
1129*/
1130
1131/*!
1132 \fn bool QCborArray::ConstIterator::operator<=(const ConstIterator &lhs, const ConstIterator &rhs)
1133
1134 Returns \c true if the entry in the array pointed to by \a lhs iterator
1135 occurs before or is the same entry as is pointed to by the \a rhs
1136 iterator.
1137*/
1138
1139/*!
1140 \fn bool QCborArray::ConstIterator::operator>(const ConstIterator &lhs, const ConstIterator &rhs)
1141
1142 Returns \c true if the entry in the array pointed to by \a lhs iterator
1143 occurs after the entry pointed to by the \a rhs iterator.
1144*/
1145
1146/*!
1147 \fn bool QCborArray::ConstIterator::operator>=(const ConstIterator &lhs, const ConstIterator &rhs)
1148
1149 Returns \c true if the entry in the array pointed to by \a lhs iterator
1150 occurs after or is the same entry as is pointed to by the \a rhs
1151 iterator.
1152*/
1153
1154/*!
1155 \fn QCborArray::ConstIterator &QCborArray::ConstIterator::operator++()
1156
1157 The prefix \c{++} operator, \c{++it}, advances the iterator to the next item in
1158 the array and returns this iterator.
1159
1160 Calling this function on QCborArray::end() leads to undefined results.
1161
1162 \sa operator--()
1163*/
1164
1165/*!
1166 \fn QCborArray::ConstIterator QCborArray::ConstIterator::operator++(int)
1167 \overload
1168
1169 The postfix \c{++} operator, \c{it++}, advances the iterator to the next item
1170 in the array and returns an iterator to the previously current item.
1171*/
1172
1173/*!
1174 \fn QCborArray::ConstIterator &QCborArray::ConstIterator::operator--()
1175
1176 The prefix \c{--} operator, \c{--it}, makes the preceding item current and
1177 returns this iterator.
1178
1179 Calling this function on QCborArray::begin() leads to undefined results.
1180
1181 \sa operator++()
1182*/
1183
1184/*!
1185 \fn QCborArray::ConstIterator QCborArray::ConstIterator::operator--(int)
1186 \overload
1187
1188 The postfix \c{--} operator, \c{it--}, makes the preceding item current and
1189 returns an iterator to the previously current item.
1190*/
1191
1192/*!
1193 \fn QCborArray::ConstIterator &QCborArray::ConstIterator::operator+=(qsizetype j)
1194
1195 Advances the iterator by \a j positions. If \a j is negative, the iterator
1196 goes backward. Returns a reference to this iterator.
1197
1198 \sa operator-=(), operator+()
1199*/
1200
1201/*!
1202 \fn QCborArray::ConstIterator &QCborArray::ConstIterator::operator-=(qsizetype j)
1203
1204 Makes the iterator go back by \a j positions. If \a j is negative, the
1205 iterator goes forward. Returns a reference to this iterator.
1206
1207 \sa operator+=(), operator-()
1208*/
1209
1210/*!
1211 \fn QCborArray::ConstIterator QCborArray::ConstIterator::operator+(qsizetype j) const
1212
1213 Returns an iterator to the item at a position \a j steps forward from this
1214 iterator. If \a j is negative, the iterator goes backward.
1215
1216 \sa operator-(), operator+=()
1217*/
1218
1219/*!
1220 \fn QCborArray::ConstIterator QCborArray::ConstIterator::operator-(qsizetype j) const
1221
1222 Returns an iterator to the item at a position \a j steps backward from this
1223 iterator. If \a j is negative, the iterator goes forward.
1224
1225 \sa operator+(), operator-=()
1226*/
1227
1228/*!
1229 \fn qsizetype QCborArray::ConstIterator::operator-(ConstIterator other) const
1230
1231 Returns the offset of this iterator relative to \a other.
1232*/
1233
1234size_t qHash(const QCborArray &array, size_t seed)
1235{
1236 return qHashRange(array.begin(), array.end(), seed);
1237}
1238
1239#if !defined(QT_NO_DEBUG_STREAM)
1240QDebug operator<<(QDebug dbg, const QCborArray &a)
1241{
1242 QDebugStateSaver saver(dbg);
1243 dbg.nospace() << "QCborArray{";
1244 const char *comma = "";
1245 for (auto v : a) {
1246 dbg << comma << v;
1247 comma = ", ";
1248 }
1249 return dbg << '}';
1250}
1251#endif
1252
1253#ifndef QT_NO_DATASTREAM
1254#if QT_CONFIG(cborstreamwriter)
1255QDataStream &operator<<(QDataStream &stream, const QCborArray &value)
1256{
1257 stream << value.toCborValue().toCbor();
1258 return stream;
1259}
1260#endif
1261
1262QDataStream &operator>>(QDataStream &stream, QCborArray &value)
1263{
1264 QByteArray buffer;
1265 stream >> buffer;
1266 QCborParserError parseError{};
1267 value = QCborValue::fromCbor(buffer, &parseError).toArray();
1268 if (parseError.error)
1269 stream.setStatus(QDataStream::ReadCorruptData);
1270 return stream;
1271}
1272#endif
1273
1274QT_END_NAMESPACE
Combined button and popup list for selecting options.
QDebug operator<<(QDebug dbg, const QCborArray &a)
size_t qHash(const QCborArray &array, size_t seed)
QDataStream & operator>>(QDataStream &stream, QCborArray &value)
\inmodule QtCore\reentrant
Definition qcborvalue.h:38