1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
45 #include <QtCore/qscopedpointer.h>
46 #include <QtCore/qiodevice.h>
47 #include <QtCore/qglobal.h>
50 #error qdatastream.h must be included before any header file that defines Status
62 template <typename T> class QList;
63 template <typename T> class QLinkedList;
64 template <typename T> class QVector;
65 template <typename T> class QSet;
66 template <class Key, class T> class QHash;
67 template <class Key, class T> class QMap;
69 #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED)
70 class QDataStreamPrivate;
71 class Q_CORE_EXPORT QDataStream
90 #if QT_VERSION >= 0x040900
91 #error Add the datastream version for this Qt version
97 BigEndian = QSysInfo::BigEndian,
98 LittleEndian = QSysInfo::LittleEndian
108 enum FloatingPointPrecision {
114 explicit QDataStream(QIODevice *);
116 QDataStream(QByteArray *, int mode);
118 QDataStream(QByteArray *, QIODevice::OpenMode flags);
119 QDataStream(const QByteArray &);
120 virtual ~QDataStream();
122 QIODevice *device() const;
123 void setDevice(QIODevice *);
128 inline QT3_SUPPORT bool eof() const { return atEnd(); }
131 Status status() const;
132 void setStatus(Status status);
135 FloatingPointPrecision floatingPointPrecision() const;
136 void setFloatingPointPrecision(FloatingPointPrecision precision);
138 ByteOrder byteOrder() const;
139 void setByteOrder(ByteOrder);
142 void setVersion(int);
144 QDataStream &operator>>(qint8 &i);
145 QDataStream &operator>>(quint8 &i);
146 QDataStream &operator>>(qint16 &i);
147 QDataStream &operator>>(quint16 &i);
148 QDataStream &operator>>(qint32 &i);
149 QDataStream &operator>>(quint32 &i);
150 QDataStream &operator>>(qint64 &i);
151 QDataStream &operator>>(quint64 &i);
153 QDataStream &operator>>(bool &i);
154 QDataStream &operator>>(float &f);
155 QDataStream &operator>>(double &f);
156 QDataStream &operator>>(char *&str);
158 QDataStream &operator<<(qint8 i);
159 QDataStream &operator<<(quint8 i);
160 QDataStream &operator<<(qint16 i);
161 QDataStream &operator<<(quint16 i);
162 QDataStream &operator<<(qint32 i);
163 QDataStream &operator<<(quint32 i);
164 QDataStream &operator<<(qint64 i);
165 QDataStream &operator<<(quint64 i);
166 QDataStream &operator<<(bool i);
167 QDataStream &operator<<(float f);
168 QDataStream &operator<<(double f);
169 QDataStream &operator<<(const char *str);
171 QDataStream &readBytes(char *&, uint &len);
172 int readRawData(char *, int len);
174 QDataStream &writeBytes(const char *, uint len);
175 int writeRawData(const char *, int len);
177 int skipRawData(int len);
180 inline QT3_SUPPORT QDataStream &readRawBytes(char *str, uint len)
181 { readRawData(str, static_cast<int>(len)); return *this; }
182 inline QT3_SUPPORT QDataStream &writeRawBytes(const char *str, uint len)
183 { writeRawData(str, static_cast<int>(len)); return *this; }
184 inline QT3_SUPPORT bool isPrintableData() const { return false; }
185 inline QT3_SUPPORT void setPrintableData(bool) {}
189 Q_DISABLE_COPY(QDataStream)
191 QScopedPointer<QDataStreamPrivate> d;
202 /*****************************************************************************
203 QDataStream inline functions
204 *****************************************************************************/
206 inline QIODevice *QDataStream::device() const
209 inline QDataStream::ByteOrder QDataStream::byteOrder() const
210 { return byteorder; }
212 inline int QDataStream::version() const
215 inline void QDataStream::setVersion(int v)
218 inline QDataStream &QDataStream::operator>>(quint8 &i)
219 { return *this >> reinterpret_cast<qint8&>(i); }
221 inline QDataStream &QDataStream::operator>>(quint16 &i)
222 { return *this >> reinterpret_cast<qint16&>(i); }
224 inline QDataStream &QDataStream::operator>>(quint32 &i)
225 { return *this >> reinterpret_cast<qint32&>(i); }
227 inline QDataStream &QDataStream::operator>>(quint64 &i)
228 { return *this >> reinterpret_cast<qint64&>(i); }
230 inline QDataStream &QDataStream::operator<<(quint8 i)
231 { return *this << qint8(i); }
233 inline QDataStream &QDataStream::operator<<(quint16 i)
234 { return *this << qint16(i); }
236 inline QDataStream &QDataStream::operator<<(quint32 i)
237 { return *this << qint32(i); }
239 inline QDataStream &QDataStream::operator<<(quint64 i)
240 { return *this << qint64(i); }
242 template <typename T>
243 QDataStream& operator>>(QDataStream& s, QList<T>& l)
249 for(quint32 i = 0; i < c; ++i)
260 template <typename T>
261 QDataStream& operator<<(QDataStream& s, const QList<T>& l)
263 s << quint32(l.size());
264 for (int i = 0; i < l.size(); ++i)
269 template <typename T>
270 QDataStream& operator>>(QDataStream& s, QLinkedList<T>& l)
275 for(quint32 i = 0; i < c; ++i)
286 template <typename T>
287 QDataStream& operator<<(QDataStream& s, const QLinkedList<T>& l)
289 s << quint32(l.size());
290 typename QLinkedList<T>::ConstIterator it = l.constBegin();
291 for(; it != l.constEnd(); ++it)
297 QDataStream& operator>>(QDataStream& s, QVector<T>& v)
303 for(quint32 i = 0; i < c; ++i) {
312 QDataStream& operator<<(QDataStream& s, const QVector<T>& v)
314 s << quint32(v.size());
315 for (typename QVector<T>::const_iterator it = v.begin(); it != v.end(); ++it)
320 template <typename T>
321 QDataStream &operator>>(QDataStream &in, QSet<T> &set)
326 for (quint32 i = 0; i < c; ++i) {
336 template <typename T>
337 QDataStream& operator<<(QDataStream &out, const QSet<T> &set)
339 out << quint32(set.size());
340 typename QSet<T>::const_iterator i = set.constBegin();
341 while (i != set.constEnd()) {
348 template <class Key, class T>
349 Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash)
351 QDataStream::Status oldStatus = in.status();
358 for (quint32 i = 0; i < n; ++i) {
359 if (in.status() != QDataStream::Ok)
365 hash.insertMulti(k, t);
368 if (in.status() != QDataStream::Ok)
370 if (oldStatus != QDataStream::Ok)
371 in.setStatus(oldStatus);
375 template <class Key, class T>
376 Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QHash<Key, T>& hash)
378 out << quint32(hash.size());
379 typename QHash<Key, T>::ConstIterator it = hash.end();
380 typename QHash<Key, T>::ConstIterator begin = hash.begin();
381 while (it != begin) {
383 out << it.key() << it.value();
388 template <class Key, class T>
389 Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<Key, T> &map)
391 template <class aKey, class aT>
392 Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<aKey, aT> &map)
395 QDataStream::Status oldStatus = in.status();
403 map.setInsertInOrder(true);
404 for (quint32 i = 0; i < n; ++i) {
405 if (in.status() != QDataStream::Ok)
411 map.insertMulti(key, value);
413 map.setInsertInOrder(false);
414 if (in.status() != QDataStream::Ok)
416 if (oldStatus != QDataStream::Ok)
417 in.setStatus(oldStatus);
421 template <class Key, class T>
422 Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QMap<Key, T> &map)
424 out << quint32(map.size());
425 typename QMap<Key, T>::ConstIterator it = map.end();
426 typename QMap<Key, T>::ConstIterator begin = map.begin();
427 while (it != begin) {
429 out << it.key() << it.value();
434 #endif // QT_NO_DATASTREAM
440 #endif // QDATASTREAM_H