1// Copyright (C) 2018 Intel Corporation.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:data-parser
4
5#ifndef QCBORSTREAMWRITER_H
6#define QCBORSTREAMWRITER_H
7
8#include <QtCore/qbytearray.h>
9#include <QtCore/qcborcommon.h>
10#include <QtCore/qscopedpointer.h>
11#include <QtCore/qstring.h>
12#include <QtCore/qstringview.h>
13#ifndef QT_BOOTSTRAPPED
14#include <QtCore/qfloat16.h>
15#endif
16
17#include <memory>
18
19QT_REQUIRE_CONFIG(cborstreamwriter);
20
21/* X11 headers use these values too, but as defines */
22#if defined(False) && defined(True)
23# undef True
24# undef False
25#endif
26
27QT_BEGIN_NAMESPACE
28
29class QIODevice;
30
31class QCborStreamWriterPrivate;
32class Q_CORE_EXPORT QCborStreamWriter
33{
34public:
35 explicit QCborStreamWriter(QIODevice *device);
36 explicit QCborStreamWriter(QByteArray *data);
37 ~QCborStreamWriter();
38 Q_DISABLE_COPY(QCborStreamWriter)
39
40 void setDevice(QIODevice *device);
41 QIODevice *device() const;
42
43 void append(quint64 u);
44 void append(qint64 i);
45 void append(QCborNegativeInteger n);
46#if !QT_CORE_REMOVED_SINCE(6, 10) // wasn't a template until 6.10
47 Q_WEAK_OVERLOAD
48#endif
49 void append(const QByteArray &ba) { appendByteString(data: ba.constData(), len: ba.size()); }
50 void append(QByteArrayView ba) { appendByteString(data: ba.data(), len: ba.size()); }
51 void append(QLatin1StringView str);
52 void append(QStringView str);
53 void append(QUtf8StringView str) { appendTextString(utf8: str.data(), len: str.size()); }
54 void append(QCborTag tag);
55 void append(QCborKnownTags tag) { append(tag: QCborTag(tag)); }
56 void append(QCborSimpleType st);
57 void append(std::nullptr_t) { append(st: QCborSimpleType::Null); }
58#ifndef QT_BOOTSTRAPPED
59 void append(qfloat16 f);
60#endif
61 void append(float f);
62 void append(double d);
63
64 void appendByteString(const char *data, qsizetype len);
65 void appendTextString(const char *utf8, qsizetype len);
66
67 // convenience
68 void append(bool b) { append(st: b ? QCborSimpleType::True : QCborSimpleType::False); }
69 void appendNull() { append(st: QCborSimpleType::Null); }
70 void appendUndefined() { append(st: QCborSimpleType::Undefined); }
71
72#ifndef Q_QDOC
73 // overloads to make normal code not complain
74 void append(int i) { append(i: qint64(i)); }
75 void append(uint u) { append(u: quint64(u)); }
76#endif
77#ifndef QT_NO_CAST_FROM_ASCII
78 void append(const char *str, qsizetype size = -1)
79 { appendTextString(utf8: str, len: (str && size == -1) ? int(strlen(s: str)) : size); }
80#endif
81
82 void startArray();
83 void startArray(quint64 count);
84 bool endArray();
85 void startMap();
86 void startMap(quint64 count);
87 bool endMap();
88
89 // no API for encoding chunked strings
90
91private:
92 std::unique_ptr<QCborStreamWriterPrivate> d;
93};
94
95QT_END_NAMESPACE
96
97#endif // QCBORSTREAMWRITER_H
98

source code of qtbase/src/corelib/serialization/qcborstreamwriter.h