QtBase  v6.3.1
qdatastream.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2021 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/qiodevicebase.h>
45 #include <QtCore/qcontainerfwd.h>
46 #include <QtCore/qnamespace.h>
47 
48 #ifdef Status
49 #error qdatastream.h must be included before any header file that defines Status
50 #endif
51 
53 
54 #if QT_CORE_REMOVED_SINCE(6, 3)
55 class qfloat16;
56 #endif
57 class QByteArray;
58 class QIODevice;
59 
60 #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED)
61 class QDataStreamPrivate;
62 namespace QtPrivate {
63 class StreamStateSaver;
64 }
65 class Q_CORE_EXPORT QDataStream : public QIODeviceBase
66 {
67 public:
68  enum Version {
69  Qt_1_0 = 1,
70  Qt_2_0 = 2,
71  Qt_2_1 = 3,
72  Qt_3_0 = 4,
73  Qt_3_1 = 5,
74  Qt_3_3 = 6,
75  Qt_4_0 = 7,
76  Qt_4_1 = Qt_4_0,
77  Qt_4_2 = 8,
78  Qt_4_3 = 9,
79  Qt_4_4 = 10,
80  Qt_4_5 = 11,
81  Qt_4_6 = 12,
82  Qt_4_7 = Qt_4_6,
83  Qt_4_8 = Qt_4_7,
84  Qt_4_9 = Qt_4_8,
85  Qt_5_0 = 13,
86  Qt_5_1 = 14,
87  Qt_5_2 = 15,
88  Qt_5_3 = Qt_5_2,
89  Qt_5_4 = 16,
90  Qt_5_5 = Qt_5_4,
91  Qt_5_6 = 17,
92  Qt_5_7 = Qt_5_6,
93  Qt_5_8 = Qt_5_7,
94  Qt_5_9 = Qt_5_8,
95  Qt_5_10 = Qt_5_9,
96  Qt_5_11 = Qt_5_10,
97  Qt_5_12 = 18,
98  Qt_5_13 = 19,
99  Qt_5_14 = Qt_5_13,
100  Qt_5_15 = Qt_5_14,
101  Qt_6_0 = 20,
102  Qt_6_1 = Qt_6_0,
103  Qt_6_2 = Qt_6_0,
104  Qt_6_3 = Qt_6_0,
105  Qt_DefaultCompiledVersion = Qt_6_3
106 #if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
107 #error Add the datastream version for this Qt version and update Qt_DefaultCompiledVersion
108 #endif
109  };
110 
111  enum ByteOrder {
112  BigEndian = QSysInfo::BigEndian,
113  LittleEndian = QSysInfo::LittleEndian
114  };
115 
116  enum Status {
117  Ok,
120  WriteFailed
121  };
122 
125  DoublePrecision
126  };
127 
128  QDataStream();
129  explicit QDataStream(QIODevice *);
130  QDataStream(QByteArray *, OpenMode flags);
131  QDataStream(const QByteArray &);
132  ~QDataStream();
133 
134  QIODevice *device() const;
135  void setDevice(QIODevice *);
136 
137  bool atEnd() const;
138 
139  Status status() const;
140  void setStatus(Status status);
141  void resetStatus();
142 
143  FloatingPointPrecision floatingPointPrecision() const;
144  void setFloatingPointPrecision(FloatingPointPrecision precision);
145 
146  ByteOrder byteOrder() const;
147  void setByteOrder(ByteOrder);
148 
149  int version() const;
150  void setVersion(int);
151 
152  QDataStream &operator>>(char &i);
158  inline QDataStream &operator>>(quint32 &i);
161  QDataStream &operator>>(std::nullptr_t &ptr) { ptr = nullptr; return *this; }
162 
163  QDataStream &operator>>(bool &i);
164 #if QT_CORE_REMOVED_SINCE(6, 3)
166 #endif
167  QDataStream &operator>>(float &f);
168  QDataStream &operator>>(double &f);
169  QDataStream &operator>>(char *&str);
170  QDataStream &operator>>(char16_t &c);
171  QDataStream &operator>>(char32_t &c);
172 
173  QDataStream &operator<<(char i);
179  inline QDataStream &operator<<(quint32 i);
182  QDataStream &operator<<(std::nullptr_t) { return *this; }
183  QDataStream &operator<<(bool i);
184 #if QT_CORE_REMOVED_SINCE(6, 3)
186 #endif
187  QDataStream &operator<<(float f);
188  QDataStream &operator<<(double f);
189  QDataStream &operator<<(const char *str);
190  QDataStream &operator<<(char16_t c);
191  QDataStream &operator<<(char32_t c);
192 
193 
194  QDataStream &readBytes(char *&, uint &len);
195  int readRawData(char *, int len);
196 
197  QDataStream &writeBytes(const char *, uint len);
198  int writeRawData(const char *, int len);
199 
200  int skipRawData(int len);
201 
202  void startTransaction();
203  bool commitTransaction();
204  void rollbackTransaction();
205  void abortTransaction();
206 
207  bool isDeviceTransactionStarted() const;
208 private:
210 
212 
213  QIODevice *dev;
214  bool owndev;
215  bool noswap;
216  ByteOrder byteorder;
217  int ver;
218  Status q_status;
219 
220  int readBlock(char *data, int len);
222 };
223 
224 namespace QtPrivate {
225 
227 {
228 public:
229  inline StreamStateSaver(QDataStream *s) : stream(s), oldStatus(s->status())
230  {
231  if (!stream->isDeviceTransactionStarted())
232  stream->resetStatus();
233  }
235  {
236  if (oldStatus != QDataStream::Ok) {
237  stream->resetStatus();
238  stream->setStatus(oldStatus);
239  }
240  }
241 
242 private:
244  QDataStream::Status oldStatus;
245 };
246 
247 template <typename Container>
249 {
250  StreamStateSaver stateSaver(&s);
251 
252  c.clear();
253  quint32 n;
254  s >> n;
255  c.reserve(n);
256  for (quint32 i = 0; i < n; ++i) {
257  typename Container::value_type t;
258  s >> t;
259  if (s.status() != QDataStream::Ok) {
260  c.clear();
261  break;
262  }
263  c.append(t);
264  }
265 
266  return s;
267 }
268 
269 template <typename Container>
271 {
272  StreamStateSaver stateSaver(&s);
273 
274  c.clear();
275  quint32 n;
276  s >> n;
277  for (quint32 i = 0; i < n; ++i) {
278  typename Container::value_type t;
279  s >> t;
280  if (s.status() != QDataStream::Ok) {
281  c.clear();
282  break;
283  }
284  c << t;
285  }
286 
287  return s;
288 }
289 
290 template <typename Container>
292 {
293  StreamStateSaver stateSaver(&s);
294 
295  c.clear();
296  quint32 n;
297  s >> n;
298  for (quint32 i = 0; i < n; ++i) {
299  typename Container::key_type k;
300  typename Container::mapped_type t;
301  s >> k >> t;
302  if (s.status() != QDataStream::Ok) {
303  c.clear();
304  break;
305  }
306  c.insert(k, t);
307  }
308 
309  return s;
310 }
311 
312 template <typename Container>
314 {
315  s << quint32(c.size());
316  for (const typename Container::value_type &t : c)
317  s << t;
318 
319  return s;
320 }
321 
322 template <typename Container>
324 {
325  s << quint32(c.size());
326  auto it = c.constBegin();
327  auto end = c.constEnd();
328  while (it != end) {
329  s << it.key() << it.value();
330  ++it;
331  }
332 
333  return s;
334 }
335 
336 template <typename Container>
338 {
339  s << quint32(c.size());
340  auto it = c.constBegin();
341  auto end = c.constEnd();
342  while (it != end) {
343  const auto rangeStart = it++;
344  while (it != end && rangeStart.key() == it.key())
345  ++it;
346  const qint64 last = std::distance(rangeStart, it) - 1;
347  for (qint64 i = last; i >= 0; --i) {
348  auto next = std::next(rangeStart, i);
349  s << next.key() << next.value();
350  }
351  }
352 
353  return s;
354 }
355 
356 } // QtPrivate namespace
357 
358 template<typename ...T>
360  std::enable_if_t<std::conjunction_v<QTypeTraits::has_ostream_operator<QDataStream, T>...>, QDataStream &>;
361 template<typename Container, typename ...T>
363  std::enable_if_t<std::conjunction_v<QTypeTraits::has_ostream_operator_container<QDataStream, Container, T>...>, QDataStream &>;
364 
365 template<typename ...T>
367  std::enable_if_t<std::conjunction_v<QTypeTraits::has_istream_operator<QDataStream, T>...>, QDataStream &>;
368 template<typename Container, typename ...T>
370  std::enable_if_t<std::conjunction_v<QTypeTraits::has_istream_operator_container<QDataStream, Container, T>...>, QDataStream &>;
371 
372 /*****************************************************************************
373  QDataStream inline functions
374  *****************************************************************************/
375 
377 { return dev; }
378 
380 { return byteorder; }
381 
382 inline int QDataStream::version() const
383 { return ver; }
384 
385 inline void QDataStream::setVersion(int v)
386 { ver = v; }
387 
389 { return *this >> reinterpret_cast<qint8&>(i); }
390 
392 { return *this >> reinterpret_cast<qint8&>(i); }
393 
395 { return *this >> reinterpret_cast<qint16&>(i); }
396 
398 { return *this >> reinterpret_cast<qint32&>(i); }
399 
401 { return *this >> reinterpret_cast<qint64&>(i); }
402 
404 { return *this << qint8(i); }
405 
407 { return *this << qint8(i); }
408 
410 { return *this << qint16(i); }
411 
413 { return *this << qint32(i); }
414 
416 { return *this << qint64(i); }
417 
418 template <typename Enum>
421 
422 template <typename Enum>
424 {
425  typename QFlags<Enum>::Int i;
426  s >> i;
427  e = QFlag(i);
428  return s;
429 }
430 
431 template <typename T>
433 operator<<(QDataStream &s, const T &t)
435 
436 template <typename T>
439 { return s >> reinterpret_cast<typename std::underlying_type<T>::type &>(t); }
440 
441 #ifndef Q_CLANG_QDOC
442 
443 template<typename T>
445 {
447 }
448 
449 template<typename T>
451 {
453 }
454 
455 template <typename T>
457 {
459 }
460 
461 template <typename T>
463 {
465 }
466 
467 template <class Key, class T>
469 {
471 }
472 
473 template <class Key, class T>
474 
476 {
478 }
479 
480 template <class Key, class T>
482 {
484 }
485 
486 template <class Key, class T>
488 {
490 }
491 
492 template <class Key, class T>
494 {
496 }
497 
498 template <class Key, class T>
500 {
502 }
503 
504 template <class Key, class T>
506 {
508 }
509 
510 template <class Key, class T>
512 {
514 }
515 
516 template <class T1, class T2>
518 {
519  s >> p.first >> p.second;
520  return s;
521 }
522 
523 template <class T1, class T2>
524 inline QDataStreamIfHasOStreamOperators<T1, T2> operator<<(QDataStream& s, const std::pair<T1, T2> &p)
525 {
526  s << p.first << p.second;
527  return s;
528 }
529 
530 #else
531 
532 template <class T>
534 
535 template <class T>
537 
538 template <class T>
540 
541 template <class T>
543 
544 template <class Key, class T>
546 
547 template <class Key, class T>
549 
550 template <class Key, class T>
552 
553 template <class Key, class T>
555 
556 template <class Key, class T>
558 
559 template <class Key, class T>
561 
562 template <class Key, class T>
564 
565 template <class Key, class T>
567 
568 template <class T1, class T2>
569 QDataStream &operator>>(QDataStream& s, std::pair<T1, T2> &p);
570 
571 template <class T1, class T2>
572 QDataStream &operator<<(QDataStream& s, const std::pair<T1, T2> &p);
573 
574 #endif // Q_CLANG_QDOC
575 
577 {
578  int combined;
579  s >> combined;
580  combination = QKeyCombination::fromCombined(combined);
581  return s;
582 }
583 
585 {
586  return s << combination.toCombined();
587 }
588 
589 #endif // QT_NO_DATASTREAM
590 
592 
593 #endif // QDATASTREAM_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
#define value
[5]
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
QDataStream & operator>>(char &i)
Definition: qdatastream.h:388
QDataStream & operator>>(std::nullptr_t &ptr)
Definition: qdatastream.h:161
operator>>(QDataStream &ds, qfloat16 &f)
Definition: qfloat16.cpp:344
QDataStream & operator<<(std::nullptr_t)
Definition: qdatastream.h:182
ByteOrder byteOrder() const
Definition: qdatastream.h:379
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
QIODevice * device() const
Definition: qdatastream.h:376
int version() const
Definition: qdatastream.h:382
QDataStream & operator<<(char i)
Definition: qdatastream.h:403
void setVersion(int)
Definition: qdatastream.h:385
The QFlag class is a helper data type for QFlags.
Definition: qflags.h:53
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
Definition: qflags.h:89
std::conditional< std::is_unsigned< typename std::underlying_type< Enum >::type >::value, unsigned int, signed int >::type Int
Definition: qflags.h:92
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qhash.h:773
Base class for QIODevice that provides flags describing the mode in which a device is opened.
Definition: qiodevicebase.h:48
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:70
static constexpr QKeyCombination fromCombined(int combined)
Definition: qnamespace.h:1897
constexpr int toCombined() const noexcept
Definition: qnamespace.h:1904
Definition: qlist.h:108
Definition: qmap.h:222
The QMultiHash class is a convenience QHash subclass that provides multi-valued hashes.
Definition: qhash.h:1293
Definition: qset.h:54
@ BigEndian
Definition: qsysinfo.h:61
@ LittleEndian
Definition: qsysinfo.h:62
StreamStateSaver(QDataStream *s)
Definition: qdatastream.h:229
Provides 16-bit floating point support.
Definition: qfloat16.h:75
format setVersion(3, 2)
QHash< int, QWidget * > hash
[35multi]
QMap< QString, QString > map
[6]
QString str
[2]
double e
int const char * version
Definition: zlib.h:814
short next
Definition: keywords.cpp:454
typename C::value_type value_type
typename C::key_type key_type
typename C::mapped_type mapped_type
QDataStream & readListBasedContainer(QDataStream &s, Container &c)
Definition: qdatastream.h:270
QDataStream & writeAssociativeMultiContainer(QDataStream &s, const Container &c)
Definition: qdatastream.h:337
QDataStream & writeAssociativeContainer(QDataStream &s, const Container &c)
Definition: qdatastream.h:323
QDataStream & readAssociativeContainer(QDataStream &s, Container &c)
Definition: qdatastream.h:291
QDataStream & readArrayBasedContainer(QDataStream &s, Container &c)
Definition: qdatastream.h:248
QDataStream & writeSequentialContainer(QDataStream &s, const Container &c)
Definition: qdatastream.h:313
int distance(TestIterator &a, TestIterator &b)
std::enable_if_t< std::conjunction_v< QTypeTraits::has_istream_operator< QDataStream, T >... >, QDataStream & > QDataStreamIfHasIStreamOperators
Definition: qdatastream.h:367
std::enable_if_t< std::conjunction_v< QTypeTraits::has_ostream_operator_container< QDataStream, Container, T >... >, QDataStream & > QDataStreamIfHasOStreamOperatorsContainer
Definition: qdatastream.h:363
std::enable_if_t< std::conjunction_v< QTypeTraits::has_ostream_operator< QDataStream, T >... >, QDataStream & > QDataStreamIfHasOStreamOperators
Definition: qdatastream.h:360
QDataStream & operator<<(QDataStream &s, QFlags< Enum > e)
Definition: qdatastream.h:419
std::enable_if_t< std::conjunction_v< QTypeTraits::has_istream_operator_container< QDataStream, Container, T >... >, QDataStream & > QDataStreamIfHasIStreamOperatorsContainer
Definition: qdatastream.h:370
QDataStream & operator>>(QDataStream &s, QFlags< Enum > &e)
Definition: qdatastream.h:423
EGLStreamKHR stream
unsigned int quint32
Definition: qglobal.h:288
short qint16
Definition: qglobal.h:285
unsigned short quint16
Definition: qglobal.h:286
int qint32
Definition: qglobal.h:287
unsigned long long quint64
Definition: qglobal.h:299
unsigned int uint
Definition: qglobal.h:334
long long qint64
Definition: qglobal.h:298
#define Q_DISABLE_COPY(Class)
Definition: qglobal.h:515
QT_BEGIN_NAMESPACE typedef signed char qint8
Definition: qglobal.h:283
unsigned char quint8
Definition: qglobal.h:284
GLenum type
Definition: qopengl.h:270
GLsizei const GLfloat * v
[13]
GLuint GLuint end
GLfloat GLfloat f
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat n
const GLubyte * c
Definition: qopenglext.h:12701
GLenum GLsizei len
Definition: qopenglext.h:3292
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
GLdouble s
[6]
Definition: qopenglext.h:235
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
GLenum GLint GLint * precision
Definition: qopenglext.h:1890
QFuture< QSet< QChar > > set
[10]
QStringList::Iterator it
Definition: main.cpp:38