1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QDATASTREAM_H
41#define QDATASTREAM_H
42
43#include <QtCore/qscopedpointer.h>
44#include <QtCore/qiodevice.h>
45#include <QtCore/qpair.h>
46
47#ifdef Status
48#error qdatastream.h must be included before any header file that defines Status
49#endif
50
51QT_BEGIN_NAMESPACE
52
53class qfloat16;
54class QByteArray;
55class QIODevice;
56
57template <typename T> class QList;
58template <typename T> class QLinkedList;
59template <typename T> class QVector;
60template <typename T> class QSet;
61template <class Key, class T> class QHash;
62template <class Key, class T> class QMap;
63
64#if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED)
65class QDataStreamPrivate;
66namespace QtPrivate {
67class StreamStateSaver;
68}
69class Q_CORE_EXPORT QDataStream
70{
71public:
72 enum Version {
73 Qt_1_0 = 1,
74 Qt_2_0 = 2,
75 Qt_2_1 = 3,
76 Qt_3_0 = 4,
77 Qt_3_1 = 5,
78 Qt_3_3 = 6,
79 Qt_4_0 = 7,
80 Qt_4_1 = Qt_4_0,
81 Qt_4_2 = 8,
82 Qt_4_3 = 9,
83 Qt_4_4 = 10,
84 Qt_4_5 = 11,
85 Qt_4_6 = 12,
86 Qt_4_7 = Qt_4_6,
87 Qt_4_8 = Qt_4_7,
88 Qt_4_9 = Qt_4_8,
89 Qt_5_0 = 13,
90 Qt_5_1 = 14,
91 Qt_5_2 = 15,
92 Qt_5_3 = Qt_5_2,
93 Qt_5_4 = 16,
94 Qt_5_5 = Qt_5_4,
95 Qt_5_6 = 17,
96 Qt_5_7 = Qt_5_6,
97 Qt_5_8 = Qt_5_7,
98 Qt_5_9 = Qt_5_8,
99 Qt_5_10 = Qt_5_9,
100 Qt_5_11 = Qt_5_10,
101 Qt_5_12 = 18,
102 Qt_5_13 = 19,
103#if QT_VERSION >= 0x050e00
104#error Add the datastream version for this Qt version and update Qt_DefaultCompiledVersion
105#endif
106 Qt_DefaultCompiledVersion = Qt_5_13
107 };
108
109 enum ByteOrder {
110 BigEndian = QSysInfo::BigEndian,
111 LittleEndian = QSysInfo::LittleEndian
112 };
113
114 enum Status {
115 Ok,
116 ReadPastEnd,
117 ReadCorruptData,
118 WriteFailed
119 };
120
121 enum FloatingPointPrecision {
122 SinglePrecision,
123 DoublePrecision
124 };
125
126 QDataStream();
127 explicit QDataStream(QIODevice *);
128 QDataStream(QByteArray *, QIODevice::OpenMode flags);
129 QDataStream(const QByteArray &);
130 ~QDataStream();
131
132 QIODevice *device() const;
133 void setDevice(QIODevice *);
134#if QT_DEPRECATED_SINCE(5, 13)
135 QT_DEPRECATED_X("Use QDataStream::setDevice(nullptr) instead")
136 void unsetDevice();
137#endif
138
139 bool atEnd() const;
140
141 Status status() const;
142 void setStatus(Status status);
143 void resetStatus();
144
145 FloatingPointPrecision floatingPointPrecision() const;
146 void setFloatingPointPrecision(FloatingPointPrecision precision);
147
148 ByteOrder byteOrder() const;
149 void setByteOrder(ByteOrder);
150
151 int version() const;
152 void setVersion(int);
153
154 QDataStream &operator>>(qint8 &i);
155 QDataStream &operator>>(quint8 &i);
156 QDataStream &operator>>(qint16 &i);
157 QDataStream &operator>>(quint16 &i);
158 QDataStream &operator>>(qint32 &i);
159 inline QDataStream &operator>>(quint32 &i);
160 QDataStream &operator>>(qint64 &i);
161 QDataStream &operator>>(quint64 &i);
162 QDataStream &operator>>(std::nullptr_t &ptr) { ptr = nullptr; return *this; }
163
164 QDataStream &operator>>(bool &i);
165 QDataStream &operator>>(qfloat16 &f);
166 QDataStream &operator>>(float &f);
167 QDataStream &operator>>(double &f);
168 QDataStream &operator>>(char *&str);
169
170 QDataStream &operator<<(qint8 i);
171 QDataStream &operator<<(quint8 i);
172 QDataStream &operator<<(qint16 i);
173 QDataStream &operator<<(quint16 i);
174 QDataStream &operator<<(qint32 i);
175 inline QDataStream &operator<<(quint32 i);
176 QDataStream &operator<<(qint64 i);
177 QDataStream &operator<<(quint64 i);
178 QDataStream &operator<<(std::nullptr_t) { return *this; }
179 QDataStream &operator<<(bool i);
180 QDataStream &operator<<(qfloat16 f);
181 QDataStream &operator<<(float f);
182 QDataStream &operator<<(double f);
183 QDataStream &operator<<(const char *str);
184
185 QDataStream &readBytes(char *&, uint &len);
186 int readRawData(char *, int len);
187
188 QDataStream &writeBytes(const char *, uint len);
189 int writeRawData(const char *, int len);
190
191 int skipRawData(int len);
192
193 void startTransaction();
194 bool commitTransaction();
195 void rollbackTransaction();
196 void abortTransaction();
197
198private:
199 Q_DISABLE_COPY(QDataStream)
200
201 QScopedPointer<QDataStreamPrivate> d;
202
203 QIODevice *dev;
204 bool owndev;
205 bool noswap;
206 ByteOrder byteorder;
207 int ver;
208 Status q_status;
209
210 int readBlock(char *data, int len);
211 friend class QtPrivate::StreamStateSaver;
212};
213
214namespace QtPrivate {
215
216class StreamStateSaver
217{
218public:
219 inline StreamStateSaver(QDataStream *s) : stream(s), oldStatus(s->status())
220 {
221 if (!stream->dev || !stream->dev->isTransactionStarted())
222 stream->resetStatus();
223 }
224 inline ~StreamStateSaver()
225 {
226 if (oldStatus != QDataStream::Ok) {
227 stream->resetStatus();
228 stream->setStatus(oldStatus);
229 }
230 }
231
232private:
233 QDataStream *stream;
234 QDataStream::Status oldStatus;
235};
236
237template <typename Container>
238QDataStream &readArrayBasedContainer(QDataStream &s, Container &c)
239{
240 StreamStateSaver stateSaver(&s);
241
242 c.clear();
243 quint32 n;
244 s >> n;
245 c.reserve(n);
246 for (quint32 i = 0; i < n; ++i) {
247 typename Container::value_type t;
248 s >> t;
249 if (s.status() != QDataStream::Ok) {
250 c.clear();
251 break;
252 }
253 c.append(t);
254 }
255
256 return s;
257}
258
259template <typename Container>
260QDataStream &readListBasedContainer(QDataStream &s, Container &c)
261{
262 StreamStateSaver stateSaver(&s);
263
264 c.clear();
265 quint32 n;
266 s >> n;
267 for (quint32 i = 0; i < n; ++i) {
268 typename Container::value_type t;
269 s >> t;
270 if (s.status() != QDataStream::Ok) {
271 c.clear();
272 break;
273 }
274 c << t;
275 }
276
277 return s;
278}
279
280template <typename Container>
281QDataStream &readAssociativeContainer(QDataStream &s, Container &c)
282{
283 StreamStateSaver stateSaver(&s);
284
285 c.clear();
286 quint32 n;
287 s >> n;
288 for (quint32 i = 0; i < n; ++i) {
289 typename Container::key_type k;
290 typename Container::mapped_type t;
291 s >> k >> t;
292 if (s.status() != QDataStream::Ok) {
293 c.clear();
294 break;
295 }
296 c.insertMulti(k, t);
297 }
298
299 return s;
300}
301
302template <typename Container>
303QDataStream &writeSequentialContainer(QDataStream &s, const Container &c)
304{
305 s << quint32(c.size());
306 for (const typename Container::value_type &t : c)
307 s << t;
308
309 return s;
310}
311
312template <typename Container>
313QDataStream &writeAssociativeContainer(QDataStream &s, const Container &c)
314{
315 s << quint32(c.size());
316 // Deserialization should occur in the reverse order.
317 // Otherwise, value() will return the least recently inserted
318 // value instead of the most recently inserted one.
319 auto it = c.constEnd();
320 auto begin = c.constBegin();
321 while (it != begin) {
322 --it;
323 s << it.key() << it.value();
324 }
325
326 return s;
327}
328
329} // QtPrivate namespace
330
331/*****************************************************************************
332 QDataStream inline functions
333 *****************************************************************************/
334
335inline QIODevice *QDataStream::device() const
336{ return dev; }
337
338inline QDataStream::ByteOrder QDataStream::byteOrder() const
339{ return byteorder; }
340
341inline int QDataStream::version() const
342{ return ver; }
343
344inline void QDataStream::setVersion(int v)
345{ ver = v; }
346
347inline QDataStream &QDataStream::operator>>(quint8 &i)
348{ return *this >> reinterpret_cast<qint8&>(i); }
349
350inline QDataStream &QDataStream::operator>>(quint16 &i)
351{ return *this >> reinterpret_cast<qint16&>(i); }
352
353inline QDataStream &QDataStream::operator>>(quint32 &i)
354{ return *this >> reinterpret_cast<qint32&>(i); }
355
356inline QDataStream &QDataStream::operator>>(quint64 &i)
357{ return *this >> reinterpret_cast<qint64&>(i); }
358
359inline QDataStream &QDataStream::operator<<(quint8 i)
360{ return *this << qint8(i); }
361
362inline QDataStream &QDataStream::operator<<(quint16 i)
363{ return *this << qint16(i); }
364
365inline QDataStream &QDataStream::operator<<(quint32 i)
366{ return *this << qint32(i); }
367
368inline QDataStream &QDataStream::operator<<(quint64 i)
369{ return *this << qint64(i); }
370
371template <typename Enum>
372inline QDataStream &operator<<(QDataStream &s, QFlags<Enum> e)
373{ return s << e.i; }
374
375template <typename Enum>
376inline QDataStream &operator>>(QDataStream &s, QFlags<Enum> &e)
377{ return s >> e.i; }
378
379template <typename T>
380inline QDataStream &operator>>(QDataStream &s, QList<T> &l)
381{
382 return QtPrivate::readArrayBasedContainer(s, l);
383}
384
385template <typename T>
386inline QDataStream &operator<<(QDataStream &s, const QList<T> &l)
387{
388 return QtPrivate::writeSequentialContainer(s, l);
389}
390
391template <typename T>
392inline QDataStream &operator>>(QDataStream &s, QLinkedList<T> &l)
393{
394 return QtPrivate::readListBasedContainer(s, l);
395}
396
397template <typename T>
398inline QDataStream &operator<<(QDataStream &s, const QLinkedList<T> &l)
399{
400 return QtPrivate::writeSequentialContainer(s, l);
401}
402
403template<typename T>
404inline QDataStream &operator>>(QDataStream &s, QVector<T> &v)
405{
406 return QtPrivate::readArrayBasedContainer(s, v);
407}
408
409template<typename T>
410inline QDataStream &operator<<(QDataStream &s, const QVector<T> &v)
411{
412 return QtPrivate::writeSequentialContainer(s, v);
413}
414
415template <typename T>
416inline QDataStream &operator>>(QDataStream &s, QSet<T> &set)
417{
418 return QtPrivate::readListBasedContainer(s, set);
419}
420
421template <typename T>
422inline QDataStream &operator<<(QDataStream &s, const QSet<T> &set)
423{
424 return QtPrivate::writeSequentialContainer(s, set);
425}
426
427template <class Key, class T>
428inline QDataStream &operator>>(QDataStream &s, QHash<Key, T> &hash)
429{
430 return QtPrivate::readAssociativeContainer(s, hash);
431}
432
433template <class Key, class T>
434inline QDataStream &operator<<(QDataStream &s, const QHash<Key, T> &hash)
435{
436 return QtPrivate::writeAssociativeContainer(s, hash);
437}
438
439template <class Key, class T>
440inline QDataStream &operator>>(QDataStream &s, QMap<Key, T> &map)
441{
442 return QtPrivate::readAssociativeContainer(s, map);
443}
444
445template <class Key, class T>
446inline QDataStream &operator<<(QDataStream &s, const QMap<Key, T> &map)
447{
448 return QtPrivate::writeAssociativeContainer(s, map);
449}
450
451#ifndef QT_NO_DATASTREAM
452template <class T1, class T2>
453inline QDataStream& operator>>(QDataStream& s, QPair<T1, T2>& p)
454{
455 s >> p.first >> p.second;
456 return s;
457}
458
459template <class T1, class T2>
460inline QDataStream& operator<<(QDataStream& s, const QPair<T1, T2>& p)
461{
462 s << p.first << p.second;
463 return s;
464}
465#endif
466
467#endif // QT_NO_DATASTREAM
468
469QT_END_NAMESPACE
470
471#endif // QDATASTREAM_H
472