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

source code of qtbase/src/corelib/serialization/qcborarray.cpp