- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# qdatastream.h

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 **
    00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    00004 ** All rights reserved.
    00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
    00006 **
    00007 ** This file is part of the QtCore module of the Qt Toolkit.
    00008 **
    00009 ** $QT_BEGIN_LICENSE:LGPL$
    00010 ** Commercial Usage
    00011 ** Licensees holding valid Qt Commercial licenses may use this file in
    00012 ** accordance with the Qt Commercial License Agreement provided with the
    00013 ** Software or, alternatively, in accordance with the terms contained in
    00014 ** a written agreement between you and Nokia.
    00015 **
    00016 ** GNU Lesser General Public License Usage
    00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
    00018 ** General Public License version 2.1 as published by the Free Software
    00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
    00020 ** packaging of this file.  Please review the following information to
    00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
    00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    00023 **
    00024 ** In addition, as a special exception, Nokia gives you certain additional
    00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
    00027 **
    00028 ** GNU General Public License Usage
    00029 ** Alternatively, this file may be used under the terms of the GNU
    00030 ** General Public License version 3.0 as published by the Free Software
    00031 ** Foundation and appearing in the file LICENSE.GPL included in the
    00032 ** packaging of this file.  Please review the following information to
    00033 ** ensure the GNU General Public License version 3.0 requirements will be
    00034 ** met: http://www.gnu.org/copyleft/gpl.html.
    00035 **
    00036 ** If you have questions regarding the use of this file, please contact
    00037 ** Nokia at qt-info@nokia.com.
    00038 ** $QT_END_LICENSE$
    00039 **
    00040 ****************************************************************************/
    00041
    00042 #ifndef QDATASTREAM_H
    00043 #define QDATASTREAM_H
    00044
    00045 #include <QtCore/qscopedpointer.h>
    00046 #include <QtCore/qiodevice.h>
    00047 #include <QtCore/qglobal.h>
    00048
    00049 #ifdef Status
    00050 #error qdatastream.h must be included before any header file that defines Status
    00051 #endif
    00052
    00053 QT_BEGIN_HEADER
    00054
    00055 QT_BEGIN_NAMESPACE
    00056
    00057 QT_MODULE(Core)
    00058
    00059 class QByteArray;
    00060 class QIODevice;
    00061
    00062 template <typename T> class QList;
    00063 template <typename T> class QLinkedList;
    00064 template <typename T> class QVector;
    00065 template <typename T> class QSet;
    00066 template <class Key, class T> class QHash;
    00067 template <class Key, class T> class QMap;
    00068
    00069 #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED)
    00070 class QDataStreamPrivate;
    00071 class Q_CORE_EXPORT QDataStream
    00072 {
    00073 public:
    00074     enum Version {
    00075         Qt_1_0 = 1,
    00076         Qt_2_0 = 2,
    00077         Qt_2_1 = 3,
    00078         Qt_3_0 = 4,
    00079         Qt_3_1 = 5,
    00080         Qt_3_3 = 6,
    00081         Qt_4_0 = 7,
    00082         Qt_4_1 = Qt_4_0,
    00083         Qt_4_2 = 8,
    00084         Qt_4_3 = 9,
    00085         Qt_4_4 = 10,
    00086         Qt_4_5 = 11,
    00087         Qt_4_6 = 12,
    00088         Qt_4_7 = Qt_4_6
    00089 #if QT_VERSION >= 0x040800
    00090 #error Add the datastream version for this Qt version
    00091         Qt_4_8 = Qt_4_7
    00092 #endif
    00093     };
    00094
    00095     enum ByteOrder {
    00096         BigEndian = QSysInfo::BigEndian,
    00097         LittleEndian = QSysInfo::LittleEndian
    00098     };
    00099
    00100     enum Status {
    00101         Ok,
    00102         ReadPastEnd,
    00103     ReadCorruptData
    00104     };
    00105
    00106     enum FloatingPointPrecision {
    00107         SinglePrecision,
    00108         DoublePrecision
    00109     };
    00110
    00111     QDataStream();
    00112     explicit QDataStream(QIODevice *);
    00113 #ifdef QT3_SUPPORT
    00114     QDataStream(QByteArray *, int mode);
    00115 #endif
    00116     QDataStream(QByteArray *, QIODevice::OpenMode flags);
    00117     QDataStream(const QByteArray &);
    00118     virtual ~QDataStream();
    00119
    00120     QIODevice *device() const;
    00121     void setDevice(QIODevice *);
    00122     void unsetDevice();
    00123
    00124     bool atEnd() const;
    00125 #ifdef QT3_SUPPORT
    00126     inline QT3_SUPPORT bool eof() const { return atEnd(); }
    00127 #endif
    00128
    00129     Status status() const;
    00130     void setStatus(Status status);
    00131     void resetStatus();
    00132
    00133     FloatingPointPrecision floatingPointPrecision() const;
    00134     void setFloatingPointPrecision(FloatingPointPrecision precision);
    00135
    00136     ByteOrder byteOrder() const;
    00137     void setByteOrder(ByteOrder);
    00138
    00139     int version() const;
    00140     void setVersion(int);
    00141
    00142     QDataStream &operator>>(qint8 &i);
    00143     QDataStream &operator>>(quint8 &i);
    00144     QDataStream &operator>>(qint16 &i);
    00145     QDataStream &operator>>(quint16 &i);
    00146     QDataStream &operator>>(qint32 &i);
    00147     QDataStream &operator>>(quint32 &i);
    00148     QDataStream &operator>>(qint64 &i);
    00149     QDataStream &operator>>(quint64 &i);
    00150
    00151     QDataStream &operator>>(bool &i);
    00152     QDataStream &operator>>(float &f);
    00153     QDataStream &operator>>(double &f);
    00154     QDataStream &operator>>(char *&str);
    00155
    00156     QDataStream &operator<<(qint8 i);
    00157     QDataStream &operator<<(quint8 i);
    00158     QDataStream &operator<<(qint16 i);
    00159     QDataStream &operator<<(quint16 i);
    00160     QDataStream &operator<<(qint32 i);
    00161     QDataStream &operator<<(quint32 i);
    00162     QDataStream &operator<<(qint64 i);
    00163     QDataStream &operator<<(quint64 i);
    00164     QDataStream &operator<<(bool i);
    00165     QDataStream &operator<<(float f);
    00166     QDataStream &operator<<(double f);
    00167     QDataStream &operator<<(const char *str);
    00168
    00169     QDataStream &readBytes(char *&, uint &len);
    00170     int readRawData(char *, int len);
    00171
    00172     QDataStream &writeBytes(const char *, uint len);
    00173     int writeRawData(const char *, int len);
    00174
    00175     int skipRawData(int len);
    00176
    00177 #ifdef QT3_SUPPORT
    00178     inline QT3_SUPPORT QDataStream &readRawBytes(char *str, uint len)
    00179         { readRawData(str, static_cast<int>(len)); return *this; }
    00180     inline QT3_SUPPORT QDataStream &writeRawBytes(const char *str, uint len)
    00181         { writeRawData(str, static_cast<int>(len)); return *this; }
    00182     inline QT3_SUPPORT bool isPrintableData() const { return false; }
    00183     inline QT3_SUPPORT void setPrintableData(bool) {}
    00184 #endif
    00185
    00186 private:
    00187     Q_DISABLE_COPY(QDataStream)
    00188
    00189     QScopedPointer<QDataStreamPrivate> d;
    00190
    00191     QIODevice *dev;
    00192     bool owndev;
    00193     bool noswap;
    00194     ByteOrder byteorder;
    00195     int ver;
    00196     Status q_status;
    00197 };
    00198
    00199
    00200 /*****************************************************************************
    00201   QDataStream inline functions
    00202  *****************************************************************************/
    00203
    00204 inline QIODevice *QDataStream::device() const
    00205 { return dev; }
    00206
    00207 inline QDataStream::ByteOrder QDataStream::byteOrder() const
    00208 { return byteorder; }
    00209
    00210 inline int QDataStream::version() const
    00211 { return ver; }
    00212
    00213 inline void QDataStream::setVersion(int v)
    00214 { ver = v; }
    00215
    00216 inline QDataStream &QDataStream::operator>>(quint8 &i)
    00217 { return *this >> reinterpret_cast<qint8&>(i); }
    00218
    00219 inline QDataStream &QDataStream::operator>>(quint16 &i)
    00220 { return *this >> reinterpret_cast<qint16&>(i); }
    00221
    00222 inline QDataStream &QDataStream::operator>>(quint32 &i)
    00223 { return *this >> reinterpret_cast<qint32&>(i); }
    00224
    00225 inline QDataStream &QDataStream::operator>>(quint64 &i)
    00226 { return *this >> reinterpret_cast<qint64&>(i); }
    00227
    00228 inline QDataStream &QDataStream::operator<<(quint8 i)
    00229 { return *this << qint8(i); }
    00230
    00231 inline QDataStream &QDataStream::operator<<(quint16 i)
    00232 { return *this << qint16(i); }
    00233
    00234 inline QDataStream &QDataStream::operator<<(quint32 i)
    00235 { return *this << qint32(i); }
    00236
    00237 inline QDataStream &QDataStream::operator<<(quint64 i)
    00238 { return *this << qint64(i); }
    00239
    00240 template <typename T>
    00241 QDataStream& operator>>(QDataStream& s, QList<T>& l)
    00242 {
    00243     l.clear();
    00244     quint32 c;
    00245     s >> c;
    00246     l.reserve(c);
    00247     for(quint32 i = 0; i < c; ++i)
    00248     {
    00249         T t;
    00250         s >> t;
    00251         l.append(t);
    00252         if (s.atEnd())
    00253             break;
    00254     }
    00255     return s;
    00256 }
    00257
    00258 template <typename T>
    00259 QDataStream& operator<<(QDataStream& s, const QList<T>& l)
    00260 {
    00261     s << quint32(l.size());
    00262     for (int i = 0; i < l.size(); ++i)
    00263         s << l.at(i);
    00264     return s;
    00265 }
    00266
    00267 template <typename T>
    00268 QDataStream& operator>>(QDataStream& s, QLinkedList<T>& l)
    00269 {
    00270     l.clear();
    00271     quint32 c;
    00272     s >> c;
    00273     for(quint32 i = 0; i < c; ++i)
    00274     {
    00275         T t;
    00276         s >> t;
    00277         l.append(t);
    00278         if (s.atEnd())
    00279             break;
    00280     }
    00281     return s;
    00282 }
    00283
    00284 template <typename T>
    00285 QDataStream& operator<<(QDataStream& s, const QLinkedList<T>& l)
    00286 {
    00287     s << quint32(l.size());
    00288     typename QLinkedList<T>::ConstIterator it = l.constBegin();
    00289     for(; it != l.constEnd(); ++it)
    00290         s << *it;
    00291     return s;
    00292 }
    00293
    00294 template<typename T>
    00295 QDataStream& operator>>(QDataStream& s, QVector<T>& v)
    00296 {
    00297     v.clear();
    00298     quint32 c;
    00299     s >> c;
    00300     v.resize(c);
    00301     for(quint32 i = 0; i < c; ++i) {
    00302         T t;
    00303         s >> t;
    00304         v[i] = t;
    00305     }
    00306     return s;
    00307 }
    00308
    00309 template<typename T>
    00310 QDataStream& operator<<(QDataStream& s, const QVector<T>& v)
    00311 {
    00312     s << quint32(v.size());
    00313     for (typename QVector<T>::const_iterator it = v.begin(); it != v.end(); ++it)
    00314         s << *it;
    00315     return s;
    00316 }
    00317
    00318 template <typename T>
    00319 QDataStream &operator>>(QDataStream &in, QSet<T> &set)
    00320 {
    00321     set.clear();
    00322     quint32 c;
    00323     in >> c;
    00324     for (quint32 i = 0; i < c; ++i) {
    00325         T t;
    00326         in >> t;
    00327         set << t;
    00328         if (in.atEnd())
    00329             break;
    00330     }
    00331     return in;
    00332 }
    00333
    00334 template <typename T>
    00335 QDataStream& operator<<(QDataStream &out, const QSet<T> &set)
    00336 {
    00337     out << quint32(set.size());
    00338     typename QSet<T>::const_iterator i = set.constBegin();
    00339     while (i != set.constEnd()) {
    00340         out << *i;
    00341         ++i;
    00342     }
    00343     return out;
    00344 }
    00345
    00346 template <class Key, class T>
    00347 Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash)
    00348 {
    00349     QDataStream::Status oldStatus = in.status();
    00350     in.resetStatus();
    00351     hash.clear();
    00352
    00353     quint32 n;
    00354     in >> n;
    00355
    00356     for (quint32 i = 0; i < n; ++i) {
    00357         if (in.status() != QDataStream::Ok)
    00358             break;
    00359
    00360         Key k;
    00361         T t;
    00362         in >> k >> t;
    00363         hash.insertMulti(k, t);
    00364     }
    00365
    00366     if (in.status() != QDataStream::Ok)
    00367         hash.clear();
    00368     if (oldStatus != QDataStream::Ok)
    00369         in.setStatus(oldStatus);
    00370     return in;
    00371 }
    00372
    00373 template <class Key, class T>
    00374 Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QHash<Key, T>& hash)
    00375 {
    00376     out << quint32(hash.size());
    00377     typename QHash<Key, T>::ConstIterator it = hash.end();
    00378     typename QHash<Key, T>::ConstIterator begin = hash.begin();
    00379     while (it != begin) {
    00380         --it;
    00381         out << it.key() << it.value();
    00382     }
    00383     return out;
    00384 }
    00385 #ifdef qdoc
    00386 template <class Key, class T>
    00387 Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<Key, T> &map)
    00388 #else
    00389 template <class aKey, class aT>
    00390 Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<aKey, aT> &map)
    00391 #endif
    00392 {
    00393     QDataStream::Status oldStatus = in.status();
    00394     in.resetStatus();
    00395     map.clear();
    00396
    00397     quint32 n;
    00398     in >> n;
    00399
    00400     map.detach();
    00401     map.setInsertInOrder(true);
    00402     for (quint32 i = 0; i < n; ++i) {
    00403         if (in.status() != QDataStream::Ok)
    00404             break;
    00405
    00406         aKey key;
    00407         aT value;
    00408         in >> key >> value;
    00409         map.insertMulti(key, value);
    00410     }
    00411     map.setInsertInOrder(false);
    00412     if (in.status() != QDataStream::Ok)
    00413         map.clear();
    00414     if (oldStatus != QDataStream::Ok)
    00415         in.setStatus(oldStatus);
    00416     return in;
    00417 }
    00418
    00419 template <class Key, class T>
    00420 Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QMap<Key, T> &map)
    00421 {
    00422     out << quint32(map.size());
    00423     typename QMap<Key, T>::ConstIterator it = map.end();
    00424     typename QMap<Key, T>::ConstIterator begin = map.begin();
    00425     while (it != begin) {
    00426         --it;
    00427         out << it.key() << it.value();
    00428     }
    00429     return out;
    00430 }
    00431
    00432 #endif // QT_NO_DATASTREAM
    00433
    00434 QT_END_NAMESPACE
    00435
    00436 QT_END_HEADER
    00437
    00438 #endif // QDATASTREAM_H