2015-12-10 05:06:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
2019-06-03 13:38:02 +00:00
|
|
|
** Copyright (C) 2016-2019 Ivailo Monev
|
2015-12-10 05:06:13 +02:00
|
|
|
**
|
2019-07-02 18:13:44 +00:00
|
|
|
** This file is part of the QtCore module of the Katie Toolkit.
|
2015-12-10 05:06:13 +02:00
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
**
|
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef QDATASTREAM_H
|
|
|
|
#define QDATASTREAM_H
|
|
|
|
|
|
|
|
#include <QtCore/qiodevice.h>
|
|
|
|
|
|
|
|
QT_BEGIN_HEADER
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
|
|
|
|
class QByteArray;
|
|
|
|
|
|
|
|
template <typename T> class QList;
|
|
|
|
template <typename T> class QLinkedList;
|
|
|
|
template <typename T> class QVector;
|
|
|
|
template <typename T> class QSet;
|
|
|
|
template <class Key, class T> class QHash;
|
|
|
|
template <class Key, class T> class QMap;
|
|
|
|
|
2016-10-19 18:43:23 +00:00
|
|
|
#if !defined(QT_NO_DATASTREAM)
|
2015-12-10 05:06:13 +02:00
|
|
|
class QDataStreamPrivate;
|
|
|
|
class Q_CORE_EXPORT QDataStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Version {
|
2019-06-12 15:12:21 +00:00
|
|
|
Qt_4_6 = 12,
|
2015-12-10 05:06:13 +02:00
|
|
|
Qt_4_7 = Qt_4_6,
|
2016-01-24 21:53:43 +02:00
|
|
|
Qt_4_8 = Qt_4_7,
|
2016-02-12 00:18:41 +02:00
|
|
|
Qt_4_9 = Qt_4_8,
|
|
|
|
#if QT_VERSION >= 0x041000
|
2015-12-10 05:06:13 +02:00
|
|
|
#error Add the datastream version for this Qt version
|
2016-02-12 00:18:41 +02:00
|
|
|
Qt_4_10 = Qt_4_9
|
2015-12-10 05:06:13 +02:00
|
|
|
#endif
|
2016-02-12 00:18:41 +02:00
|
|
|
Qt_Default = Qt_4_9
|
2015-12-10 05:06:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ByteOrder {
|
|
|
|
BigEndian = QSysInfo::BigEndian,
|
|
|
|
LittleEndian = QSysInfo::LittleEndian
|
|
|
|
};
|
|
|
|
|
2017-08-09 11:51:23 +00:00
|
|
|
enum DataStatus {
|
2015-12-10 05:06:13 +02:00
|
|
|
Ok,
|
|
|
|
ReadPastEnd,
|
|
|
|
ReadCorruptData,
|
|
|
|
WriteFailed
|
|
|
|
};
|
|
|
|
|
|
|
|
enum FloatingPointPrecision {
|
|
|
|
SinglePrecision,
|
|
|
|
DoublePrecision
|
|
|
|
};
|
|
|
|
|
|
|
|
QDataStream();
|
|
|
|
explicit QDataStream(QIODevice *);
|
|
|
|
QDataStream(QByteArray *, QIODevice::OpenMode flags);
|
|
|
|
QDataStream(const QByteArray &);
|
|
|
|
virtual ~QDataStream();
|
|
|
|
|
|
|
|
QIODevice *device() const;
|
|
|
|
void setDevice(QIODevice *);
|
|
|
|
|
|
|
|
bool atEnd() const;
|
|
|
|
|
2017-08-09 11:51:23 +00:00
|
|
|
DataStatus status() const;
|
|
|
|
void setStatus(DataStatus status);
|
2015-12-10 05:06:13 +02:00
|
|
|
void resetStatus();
|
|
|
|
|
|
|
|
FloatingPointPrecision floatingPointPrecision() const;
|
|
|
|
void setFloatingPointPrecision(FloatingPointPrecision precision);
|
|
|
|
|
|
|
|
ByteOrder byteOrder() const;
|
|
|
|
void setByteOrder(ByteOrder);
|
|
|
|
|
2019-06-12 15:12:21 +00:00
|
|
|
Version version() const;
|
|
|
|
void setVersion(Version);
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
QDataStream &operator>>(qint8 &i);
|
|
|
|
QDataStream &operator>>(quint8 &i);
|
|
|
|
QDataStream &operator>>(qint16 &i);
|
|
|
|
QDataStream &operator>>(quint16 &i);
|
|
|
|
QDataStream &operator>>(qint32 &i);
|
|
|
|
QDataStream &operator>>(quint32 &i);
|
|
|
|
QDataStream &operator>>(qint64 &i);
|
|
|
|
QDataStream &operator>>(quint64 &i);
|
|
|
|
|
|
|
|
QDataStream &operator>>(bool &i);
|
|
|
|
QDataStream &operator>>(float &f);
|
|
|
|
QDataStream &operator>>(double &f);
|
|
|
|
QDataStream &operator>>(char *&str);
|
|
|
|
|
|
|
|
QDataStream &operator<<(qint8 i);
|
|
|
|
QDataStream &operator<<(quint8 i);
|
|
|
|
QDataStream &operator<<(qint16 i);
|
|
|
|
QDataStream &operator<<(quint16 i);
|
|
|
|
QDataStream &operator<<(qint32 i);
|
|
|
|
QDataStream &operator<<(quint32 i);
|
|
|
|
QDataStream &operator<<(qint64 i);
|
|
|
|
QDataStream &operator<<(quint64 i);
|
|
|
|
QDataStream &operator<<(bool i);
|
|
|
|
QDataStream &operator<<(float f);
|
|
|
|
QDataStream &operator<<(double f);
|
|
|
|
QDataStream &operator<<(const char *str);
|
|
|
|
|
|
|
|
QDataStream &readBytes(char *&, uint &len);
|
|
|
|
int readRawData(char *, int len);
|
|
|
|
|
|
|
|
QDataStream &writeBytes(const char *, uint len);
|
|
|
|
int writeRawData(const char *, int len);
|
|
|
|
|
|
|
|
int skipRawData(int len);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(QDataStream)
|
|
|
|
|
2016-11-01 17:38:09 +00:00
|
|
|
QDataStreamPrivate* d;
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
QIODevice *dev;
|
|
|
|
bool owndev;
|
|
|
|
bool noswap;
|
|
|
|
ByteOrder byteorder;
|
2019-06-12 15:12:21 +00:00
|
|
|
Version ver;
|
2017-08-09 11:51:23 +00:00
|
|
|
DataStatus q_status;
|
2015-12-10 05:06:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
QDataStream inline functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
inline QIODevice *QDataStream::device() const
|
|
|
|
{ return dev; }
|
|
|
|
|
|
|
|
inline QDataStream::ByteOrder QDataStream::byteOrder() const
|
|
|
|
{ return byteorder; }
|
|
|
|
|
2019-06-12 15:12:21 +00:00
|
|
|
inline QDataStream::Version QDataStream::version() const
|
2015-12-10 05:06:13 +02:00
|
|
|
{ return ver; }
|
|
|
|
|
2019-06-12 15:12:21 +00:00
|
|
|
inline void QDataStream::setVersion(QDataStream::Version v)
|
2015-12-10 05:06:13 +02:00
|
|
|
{ ver = v; }
|
|
|
|
|
|
|
|
inline QDataStream &QDataStream::operator>>(quint8 &i)
|
|
|
|
{ return *this >> reinterpret_cast<qint8&>(i); }
|
|
|
|
|
|
|
|
inline QDataStream &QDataStream::operator>>(quint16 &i)
|
|
|
|
{ return *this >> reinterpret_cast<qint16&>(i); }
|
|
|
|
|
|
|
|
inline QDataStream &QDataStream::operator>>(quint32 &i)
|
|
|
|
{ return *this >> reinterpret_cast<qint32&>(i); }
|
|
|
|
|
|
|
|
inline QDataStream &QDataStream::operator>>(quint64 &i)
|
|
|
|
{ return *this >> reinterpret_cast<qint64&>(i); }
|
|
|
|
|
|
|
|
inline QDataStream &QDataStream::operator<<(quint8 i)
|
|
|
|
{ return *this << qint8(i); }
|
|
|
|
|
|
|
|
inline QDataStream &QDataStream::operator<<(quint16 i)
|
|
|
|
{ return *this << qint16(i); }
|
|
|
|
|
|
|
|
inline QDataStream &QDataStream::operator<<(quint32 i)
|
|
|
|
{ return *this << qint32(i); }
|
|
|
|
|
|
|
|
inline QDataStream &QDataStream::operator<<(quint64 i)
|
|
|
|
{ return *this << qint64(i); }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
QDataStream& operator>>(QDataStream& s, QList<T>& l)
|
|
|
|
{
|
|
|
|
l.clear();
|
|
|
|
quint32 c;
|
|
|
|
s >> c;
|
|
|
|
l.reserve(c);
|
|
|
|
for(quint32 i = 0; i < c; ++i)
|
|
|
|
{
|
|
|
|
T t;
|
|
|
|
s >> t;
|
|
|
|
l.append(t);
|
|
|
|
if (s.atEnd())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
QDataStream& operator<<(QDataStream& s, const QList<T>& l)
|
|
|
|
{
|
|
|
|
s << quint32(l.size());
|
|
|
|
for (int i = 0; i < l.size(); ++i)
|
|
|
|
s << l.at(i);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
QDataStream& operator>>(QDataStream& s, QLinkedList<T>& l)
|
|
|
|
{
|
|
|
|
l.clear();
|
|
|
|
quint32 c;
|
|
|
|
s >> c;
|
|
|
|
for(quint32 i = 0; i < c; ++i)
|
|
|
|
{
|
|
|
|
T t;
|
|
|
|
s >> t;
|
|
|
|
l.append(t);
|
|
|
|
if (s.atEnd())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
QDataStream& operator<<(QDataStream& s, const QLinkedList<T>& l)
|
|
|
|
{
|
|
|
|
s << quint32(l.size());
|
2019-06-12 15:12:21 +00:00
|
|
|
typename QLinkedList<T>::const_iterator it = l.constBegin();
|
2015-12-10 05:06:13 +02:00
|
|
|
for(; it != l.constEnd(); ++it)
|
|
|
|
s << *it;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
QDataStream& operator>>(QDataStream& s, QVector<T>& v)
|
|
|
|
{
|
|
|
|
v.clear();
|
|
|
|
quint32 c;
|
|
|
|
s >> c;
|
|
|
|
v.resize(c);
|
|
|
|
for(quint32 i = 0; i < c; ++i) {
|
|
|
|
T t;
|
|
|
|
s >> t;
|
|
|
|
v[i] = t;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
QDataStream& operator<<(QDataStream& s, const QVector<T>& v)
|
|
|
|
{
|
|
|
|
s << quint32(v.size());
|
2019-06-15 14:05:44 +00:00
|
|
|
for (typename QVector<T>::const_iterator it = v.constBegin(); it != v.constEnd(); ++it)
|
2015-12-10 05:06:13 +02:00
|
|
|
s << *it;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
QDataStream &operator>>(QDataStream &in, QSet<T> &set)
|
|
|
|
{
|
|
|
|
set.clear();
|
|
|
|
quint32 c;
|
|
|
|
in >> c;
|
|
|
|
for (quint32 i = 0; i < c; ++i) {
|
|
|
|
T t;
|
|
|
|
in >> t;
|
|
|
|
set << t;
|
|
|
|
if (in.atEnd())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
QDataStream& operator<<(QDataStream &out, const QSet<T> &set)
|
|
|
|
{
|
|
|
|
out << quint32(set.size());
|
2019-06-15 14:05:44 +00:00
|
|
|
for (typename QSet<T>::const_iterator it = set.constBegin(); it != set.constEnd(); ++it)
|
2019-06-12 15:12:21 +00:00
|
|
|
out << *it;
|
2015-12-10 05:06:13 +02:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash)
|
|
|
|
{
|
2017-08-09 11:51:23 +00:00
|
|
|
QDataStream::DataStatus oldStatus = in.status();
|
2015-12-10 05:06:13 +02:00
|
|
|
in.resetStatus();
|
|
|
|
hash.clear();
|
|
|
|
|
|
|
|
quint32 n;
|
|
|
|
in >> n;
|
|
|
|
|
|
|
|
for (quint32 i = 0; i < n; ++i) {
|
|
|
|
if (in.status() != QDataStream::Ok)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Key k;
|
|
|
|
T t;
|
|
|
|
in >> k >> t;
|
|
|
|
hash.insertMulti(k, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in.status() != QDataStream::Ok)
|
|
|
|
hash.clear();
|
|
|
|
if (oldStatus != QDataStream::Ok)
|
|
|
|
in.setStatus(oldStatus);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QHash<Key, T>& hash)
|
|
|
|
{
|
|
|
|
out << quint32(hash.size());
|
2019-06-15 14:05:44 +00:00
|
|
|
typename QHash<Key, T>::const_iterator it = hash.constEnd();
|
|
|
|
typename QHash<Key, T>::const_iterator begin = hash.constBegin();
|
2015-12-10 05:06:13 +02:00
|
|
|
while (it != begin) {
|
|
|
|
--it;
|
|
|
|
out << it.key() << it.value();
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
template <class aKey, class aT>
|
|
|
|
Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<aKey, aT> &map)
|
|
|
|
{
|
2017-08-09 11:51:23 +00:00
|
|
|
QDataStream::DataStatus oldStatus = in.status();
|
2015-12-10 05:06:13 +02:00
|
|
|
in.resetStatus();
|
|
|
|
map.clear();
|
|
|
|
|
|
|
|
quint32 n;
|
|
|
|
in >> n;
|
|
|
|
|
|
|
|
map.detach();
|
|
|
|
map.setInsertInOrder(true);
|
|
|
|
for (quint32 i = 0; i < n; ++i) {
|
|
|
|
if (in.status() != QDataStream::Ok)
|
|
|
|
break;
|
|
|
|
|
|
|
|
aKey key;
|
|
|
|
aT value;
|
|
|
|
in >> key >> value;
|
|
|
|
map.insertMulti(key, value);
|
|
|
|
}
|
|
|
|
map.setInsertInOrder(false);
|
|
|
|
if (in.status() != QDataStream::Ok)
|
|
|
|
map.clear();
|
|
|
|
if (oldStatus != QDataStream::Ok)
|
|
|
|
in.setStatus(oldStatus);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QMap<Key, T> &map)
|
|
|
|
{
|
|
|
|
out << quint32(map.size());
|
2019-06-15 14:05:44 +00:00
|
|
|
typename QMap<Key, T>::const_iterator it = map.constEnd();
|
|
|
|
typename QMap<Key, T>::const_iterator begin = map.constBegin();
|
2015-12-10 05:06:13 +02:00
|
|
|
while (it != begin) {
|
|
|
|
--it;
|
|
|
|
out << it.key() << it.value();
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // QT_NO_DATASTREAM
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
QT_END_HEADER
|
|
|
|
|
|
|
|
#endif // QDATASTREAM_H
|