Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_corelib_serialization_qcborstream.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4using namespace Qt::StringLiterals;
5
7 QCborValue(QCborTag(2), QByteArray("\x01\0\0\0\0\0\0\0\0", 9));
9
11 writer.startMap(4); // 4 elements in the map
12
13 writer.append("label"_L1);
14 writer.append("journald"_L1);
15
16 writer.append("autoDetect"_L1);
17 writer.append(false);
18
19 writer.append("condition"_L1);
20 writer.append("libs.journald"_L1);
21
22 writer.append("output"_L1);
23 writer.startArray(1);
24 writer.append("privateFeature"_L1);
25 writer.endArray();
26
27 writer.endMap();
29
31 QFile f("output", QIODevice::WriteOnly);
33 writer.startMap(0);
34 writer.endMap();
36
39 {
41 QCborStreamWriter writer(&ba);
42 writer.append(value);
43 return ba;
44 }
46
48 writer.append(0U);
49 writer.append(Q_UINT64_C(4294967296));
50 writer.append(std::numeric_limits<quint64>::max());
52
54 writer.append(0);
55 writer.append(-1);
56 writer.append(Q_INT64_C(4294967296));
57 writer.append(std::numeric_limits<qint64>::max());
59
63 writer.append(QCborNegativeInteger(-quint64(std::numeric_limits<qint64>::min())));
65
68 {
70 if (f.open(QIODevice::ReadOnly))
71 writer.append(f.readAll());
72 }
74
76 writer.append("Hello, World"_L1);
78
81 {
82 writer.append(str);
83 }
85
88 {
89 writer.append(QCborTag(36));
90 writer.append(rx.pattern());
91 }
93
96 {
97 writer.append(QCborKnownTags::UnixTime_t);
98 writer.append(time(nullptr));
99 }
101
103 writer.append(QCborSimpleType::Null);
104 writer.append(QCborSimpleType(32));
106
108 void writeFloat(QCborStreamWriter &writer, float f)
109 {
110 qfloat16 f16 = f;
111 if (qIsNaN(f) || f16 == f)
112 writer.append(f16);
113 else
114 writer.append(f);
115 }
117
119 void writeFloat(QCborStreamWriter &writer, double d)
120 {
121 float f = d;
122 if (qIsNaN(d) || d == f)
123 writer.append(f);
124 else
125 writer.append(d);
126 }
128
130 void writeDouble(QCborStreamWriter &writer, double d)
131 {
132 float f;
133 if (qIsNaN(d)) {
134 writer.append(qfloat16(qQNaN()));
135 } else if (qIsInf(d)) {
136 writer.append(d < 0 ? -qInf() : qInf());
137 } else if ((f = d) == d) {
138 qfloat16 f16 = f;
139 if (f16 == f)
140 writer.append(f16);
141 else
142 writer.append(f);
143 } else {
144 writer.append(d);
145 }
146 }
148
152
154 writer.append(QCborSimpleType::Null);
156
158 writer.append(QCborSimpleType::Null);
160
164
166 void appendList(QCborStreamWriter &writer, const QList<QString> &values)
167 {
168 writer.startArray();
169 for (const QString &s : values)
170 writer.append(s);
171 writer.endArray();
172 }
174
177 {
178 writer.startArray(list.size());
179 for (const QString &s : list)
180 writer.append(s);
181 writer.endArray();
182 }
184
186 void appendMap(QCborStreamWriter &writer, const QList<std::pair<int, QString>> &values)
187 {
188 writer.startMap();
189 for (const auto pair : values) {
190 writer.append(pair.first)
191 writer.append(pair.second);
192 }
193 writer.endMap();
194 }
196
198 void appendMap(QCborStreamWriter &writer, const QMap<int, QString> &map)
199 {
200 writer.startMap(map.size());
201 for (auto it = map.cbegin(), end = map.cend(); it != end; ++it) {
202 writer.append(it.key());
203 writer.append(it.value());
204 }
205 writer.endMap();
206 }
208
211 {
212 switch (reader.type())
219 handleFixedWidth(reader);
220 reader.next();
221 break;
224 handleString(reader);
225 break;
228 reader.enterContainer();
229 while (reader.lastError() == QCborError::NoError)
230 handleStream(reader);
231 if (reader.lastError() == QCborError::NoError)
232 reader.leaveContainer();
233 }
234 }
236
239 {
241 if (reader.isLengthKnown())
242 list.reserve(reader.length());
243
244 reader.enterContainer();
245 while (reader.lastError() == QCborError::NoError && reader.hasNext())
246 list.append(readOneElement(reader));
247 if (reader.lastError() == QCborError::NoError)
248 reader.leaveContainer();
249 }
251
254 {
256 if (reader.isLengthKnown())
257 map.reserve(reader.length());
258
259 reader.enterContainer();
260 while (reader.lastError() == QCborError::NoError && reader.hasNext()) {
261 QString key = readElementAsString(reader);
262 map.insert(key, readOneElement(reader));
263 }
264 if (reader.lastError() == QCborError::NoError)
265 reader.leaveContainer();
266 }
268
271 {
273 auto r = reader.readString();
274 while (r.status == QCborStreamReader::Ok) {
275 result += r.data;
276 r = reader.readString();
277 }
278
279 if (r.status == QCborStreamReader::Error) {
280 // handle error condition
281 result.clear();
282 }
283 return result;
284 }
286
289 {
290 QBytearray result;
291 auto r = reader.readBytearray();
292 while (r.status == QCborStreamReader::Ok) {
293 result += r.data;
294 r = reader.readByteArray();
295 }
296
297 if (r.status == QCborStreamReader::Error) {
298 // handle error condition
299 result.clear();
300 }
301 return result;
302 }
304
306 QCborStreamReader<qsizetype> result;
307 do {
308 qsizetype size = reader.currentStringChunkSize();
310 buffer.resize(oldsize + size);
311 result = reader.readStringChunk(buffer.data() + oldsize, size);
312 } while (result.status() == QCborStreamReader::Ok);
\inmodule QtCore
Definition qbytearray.h:57
QByteArray & append(char c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore
Definition qfile.h:93
Definition qlist.h:75
qsizetype size() const noexcept
Definition qlist.h:397
void reserve(qsizetype size)
Definition qlist.h:753
void append(parameter_type t)
Definition qlist.h:458
iterator insert(const Key &key, const T &value)
Definition qmap.h:688
const_iterator cend() const
Definition qmap.h:605
const_iterator cbegin() const
Definition qmap.h:601
size_type size() const
Definition qmap.h:267
\inmodule QtCore \reentrant
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:47
QString str
[2]
QMap< QString, QString > map
[6]
QSet< QString >::iterator it
QImageReader reader("image.png")
[1]
QCborTag
Definition qcborcommon.h:30
QCborSimpleType
Definition qcborcommon.h:23
QCborNegativeInteger
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
bool qIsNaN(qfloat16 f) noexcept
Definition qfloat16.h:284
bool qIsInf(qfloat16 f) noexcept
Definition qfloat16.h:283
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf()
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qQNaN()
GLenum GLsizei GLsizei GLint * values
[15]
GLboolean GLboolean GLboolean b
GLuint64 key
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLboolean r
[2]
GLuint GLuint end
GLfloat GLfloat f
GLenum GLuint buffer
GLdouble s
[6]
Definition qopenglext.h:235
GLuint64EXT * result
[6]
#define Q_UINT64_C(c)
Definition qtypes.h:58
unsigned long long quint64
Definition qtypes.h:61
ptrdiff_t qsizetype
Definition qtypes.h:165
long long qint64
Definition qtypes.h:60
#define Q_INT64_C(c)
Definition qtypes.h:57
QList< int > list
[14]
QByteArray ba
[0]
void writeCurrentTime(QCborStreamWriter &writer)
[10]
void writeFloat(QCborStreamWriter &writer, float f)
[12]
QFile f("output", QIODevice::WriteOnly)
[1]
void writeDouble(QCborStreamWriter &writer, double d)
[14]
QBytearray decodeBytearray(QCborStreamReader &reader)
[27]
void writeString(QCborStreamWriter &writer, const QString &str)
[8]
void writeFile(QCborStreamWriter &writer, const QString &fileName)
[6]
QCborStreamReader< qsizetype > result
[28]
QByteArray encodedNumber(qint64 value)
[2]
void writeRxPattern(QCborStreamWriter &writer, const QRegularExpression &rx)
[9]
QVariantList populateFromCbor(QCborStreamReader &reader)
[24]
QString decodeString(QCborStreamReader &reader)
[26]
void appendList(QCborStreamWriter &writer, const QList< QString > &values)
[19]
void appendMap(QCborStreamWriter &writer, const QList< std::pair< int, QString > > &values)
[21]
QCborValue(QCborTag(2), QByteArray("\x01\0\0\0\0\0\0\0\0", 9))
[0]
void handleStream(QCborStreamReader &reader)
[23]
p rx()++