7#define CBOR_NO_PARSER_API
8#include <private/qcborcommon_p.h>
10#include <private/qnumeric_p.h>
11#include <private/qstringconverter_p.h>
15#include <qvarlengtharray.h>
20#define CBOR_ENCODER_WRITER_CONTROL 1
21#define CBOR_ENCODER_WRITE_FUNCTION qt_cbor_encoder_write_callback
22#define CBOR_ENCODER_NO_CHECK_USER
25QT_WARNING_DISABLE_MSVC(4334)
27#include <cborencoder.c>
28#include <cborencoder_close_container_checked.c>
29#include <cborencoder_float.c>
33Q_DECLARE_TYPEINFO(CborEncoder, Q_PRIMITIVE_TYPE);
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
187 cbor_encoder_init_writer(&encoder, qt_cbor_encoder_write_callback,
this);
196 template <
typename... Args>
void executeAppend(CborError (*f)(CborEncoder *, Args...), Args... args)
198 f(&encoder,
std::forward<Args>(args)...);
201 void createContainer(CborError (*f)(CborEncoder *, CborEncoder *, size_t), quint64 len = IndefiniteLength)
203 static_assert(size_t(IndefiniteLength) == CborIndefiniteLength);
204 if (
sizeof(len) !=
sizeof(size_t) && len != IndefiniteLength) {
205 if (Q_UNLIKELY(len >= CborIndefiniteLength)) {
207 qWarning(
"QCborStreamWriter: container of size %llu is too big for a 32-bit build; "
208 "will use indeterminate length instead", len);
209 len = CborIndefiniteLength;
213 containerStack.push(encoder);
214 f(&containerStack.top(), &encoder, len);
219 if (containerStack.isEmpty()) {
220 qWarning(
"QCborStreamWriter: closing map or array that wasn't open");
224 CborEncoder container = containerStack.pop();
225 CborError err = cbor_encoder_close_container(&container, &encoder);
228 if (Q_UNLIKELY(err)) {
229 if (err == CborErrorTooFewItems)
230 qWarning(
"QCborStreamWriter: not enough items added to array or map");
231 else if (err == CborErrorTooManyItems)
232 qWarning(
"QCborStreamWriter: too many items added to array or map");
245 qint64 written = that
->device->write(
static_cast<
const char *>(data), len);
246 return (written == qsizetype(len) ? CborNoError : CborErrorIO);
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266QCborStreamWriter::QCborStreamWriter(QIODevice *device)
267 : d(
new QCborStreamWriterPrivate(device))
271#ifndef QT_BOOTSTRAPPED
273
274
275
276
277
278
279
280
281
282
283
284QCborStreamWriter::QCborStreamWriter(QByteArray *data)
285 : d(
new QCborStreamWriterPrivate(
new QBuffer(data)))
287 d->deleteDevice =
true;
288 d->device->open(QIODevice::WriteOnly | QIODevice::Unbuffered);
293
294
295
296
297
298
299QCborStreamWriter::~QCborStreamWriter()
304
305
306
307
308
309void QCborStreamWriter::setDevice(QIODevice *device)
314 d->deleteDevice =
false;
318
319
320
321
322
323
324
325
326
327QIODevice *QCborStreamWriter::device()
const
333
334
335
336
337
338
339
340
341
342
343void QCborStreamWriter::append(quint64 u)
345 d->executeAppend(cbor_encode_uint, uint64_t(u));
349
350
351
352
353
354
355
356
357
358
359
360void QCborStreamWriter::append(qint64 i)
362 d->executeAppend(cbor_encode_int, int64_t(i));
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384void QCborStreamWriter::append(QCborNegativeInteger n)
386 d->executeAppend(cbor_encode_negative_int, uint64_t(n));
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
414
415
416
417
418
419
420
421
422
423
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445void QCborStreamWriter::append(QLatin1StringView str)
449 if (QtPrivate::isAscii(str)) {
451 appendTextString(str.latin1(), str.size());
454 QVarLengthArray<
char> utf8(str.size() * 2);
455 const qsizetype written = QUtf8::convertFromLatin1(utf8.data(), str) - utf8.data();
456 appendTextString(utf8.data(), written);
461
462
463
464
465
466
467
468
469
470
471
472
473void QCborStreamWriter::append(QStringView str)
475 QByteArray utf8 = str.toUtf8();
476 appendTextString(utf8.constData(), utf8.size());
480
481
482
483
484
485
486
487
488
489
490
491
492void QCborStreamWriter::append(QCborTag tag)
494 d->executeAppend(cbor_encode_tag, CborTag(tag));
498
499
500
501
502
503
504
505
506
507
508
509
510
511
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528void QCborStreamWriter::append(QCborSimpleType st)
530 d->executeAppend(cbor_encode_simple_value, uint8_t(st));
533#ifndef QT_BOOTSTRAPPED
535
536
537
538
539
540
541
542
543
544
545
546void QCborStreamWriter::append(qfloat16 f)
548 d->executeAppend(cbor_encode_half_float,
static_cast<
const void *>(&f));
553
554
555
556
557
558
559
560
561
562
563
564void QCborStreamWriter::append(
float f)
566 d->executeAppend(cbor_encode_float, f);
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589void QCborStreamWriter::append(
double d)
591 this->d->executeAppend(cbor_encode_double, d);
595
596
597
598
599
600
601
602void QCborStreamWriter::appendByteString(
const char *data, qsizetype len)
604 d->executeAppend(cbor_encode_byte_string,
reinterpret_cast<
const uint8_t *>(data), size_t(len));
608
609
610
611
612
613
614
615
616
617
618void QCborStreamWriter::appendTextString(
const char *utf8, qsizetype len)
620 d->executeAppend(cbor_encode_text_string, utf8, size_t(len));
624
625
626
627
628
629
630
631
632
633
634
635
636
637
640
641
642
643
644
645
646
647
648
649
650
651
654
655
656
657
658
659
660
661
662
663
666
667
668
669
670
671
672
673
674
677
678
679
680
681
682
683
684
685
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704void QCborStreamWriter::startArray()
706 d->createContainer(cbor_encoder_create_array);
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736void QCborStreamWriter::startArray(quint64 count)
738 d->createContainer(cbor_encoder_create_array, count);
742
743
744
745
746
747
748
749
750
751
752
753
754
755bool QCborStreamWriter::endArray()
757 return d->closeContainer();
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778void QCborStreamWriter::startMap()
780 d->createContainer(cbor_encoder_create_map);
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810void QCborStreamWriter::startMap(quint64 count)
812 d->createContainer(cbor_encoder_create_map, count);
816
817
818
819
820
821
822
823
824
825
826
827
828
829bool QCborStreamWriter::endMap()
831 return d->closeContainer();
836#undef CBOR_ENCODER_WRITER_CONTROL
837#undef CBOR_ENCODER_WRITE_FUNCTION
838#undef CBOR_ENCODER_NO_CHECK_USER
static constexpr quint64 IndefiniteLength
~QCborStreamWriterPrivate()
QStack< CborEncoder > containerStack
void executeAppend(CborError(*f)(CborEncoder *, Args...), Args... args)
QCborStreamWriterPrivate(QIODevice *device)
void createContainer(CborError(*f)(CborEncoder *, CborEncoder *, size_t), quint64 len=IndefiniteLength)
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE CborError qt_cbor_encoder_write_callback(void *token, const void *data, size_t len, CborEncoderAppendType)