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