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