source: trunk/src/corelib/io/qdatastream.h@ 25

Last change on this file since 25 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 10.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QDATASTREAM_H
43#define QDATASTREAM_H
44
45#include <QtCore/qiodevice.h>
46#include <QtCore/qglobal.h>
47
48#ifdef Status
49#error qdatastream.h must be included before any header file that defines Status
50#endif
51
52QT_BEGIN_HEADER
53
54QT_BEGIN_NAMESPACE
55
56QT_MODULE(Core)
57
58class QByteArray;
59class QIODevice;
60
61template <typename T> class QList;
62template <typename T> class QLinkedList;
63template <typename T> class QVector;
64template <typename T> class QSet;
65template <class Key, class T> class QHash;
66template <class Key, class T> class QMap;
67
68class QDataStreamPrivate;
69
70#ifndef QT_NO_DATASTREAM
71class Q_CORE_EXPORT QDataStream
72{
73public:
74 enum Version {
75 Qt_1_0 = 1,
76 Qt_2_0 = 2,
77 Qt_2_1 = 3,
78 Qt_3_0 = 4,
79 Qt_3_1 = 5,
80 Qt_3_3 = 6,
81 Qt_4_0 = 7,
82 Qt_4_1 = Qt_4_0,
83 Qt_4_2 = 8,
84 Qt_4_3 = 9,
85 Qt_4_4 = 10,
86 Qt_4_5 = 11
87#if QT_VERSION >= 0x040600
88#error Add the datastream version for this Qt version
89 , Qt_4_6 = Qt_4_5
90#endif
91 };
92
93 enum ByteOrder {
94 BigEndian = QSysInfo::BigEndian,
95 LittleEndian = QSysInfo::LittleEndian
96 };
97
98 enum Status {
99 Ok,
100 ReadPastEnd,
101 ReadCorruptData
102 };
103
104 QDataStream();
105 explicit QDataStream(QIODevice *);
106#ifdef QT3_SUPPORT
107 QDataStream(QByteArray *, int mode);
108#endif
109 QDataStream(QByteArray *, QIODevice::OpenMode flags);
110 QDataStream(const QByteArray &);
111 virtual ~QDataStream();
112
113 QIODevice *device() const;
114 void setDevice(QIODevice *);
115 void unsetDevice();
116
117 bool atEnd() const;
118#ifdef QT3_SUPPORT
119 inline QT3_SUPPORT bool eof() const { return atEnd(); }
120#endif
121
122 Status status() const;
123 void setStatus(Status status);
124 void resetStatus();
125
126 ByteOrder byteOrder() const;
127 void setByteOrder(ByteOrder);
128
129 int version() const;
130 void setVersion(int);
131
132 QDataStream &operator>>(qint8 &i);
133 QDataStream &operator>>(quint8 &i);
134 QDataStream &operator>>(qint16 &i);
135 QDataStream &operator>>(quint16 &i);
136 QDataStream &operator>>(qint32 &i);
137 QDataStream &operator>>(quint32 &i);
138 QDataStream &operator>>(qint64 &i);
139 QDataStream &operator>>(quint64 &i);
140
141 QDataStream &operator>>(bool &i);
142 QDataStream &operator>>(float &f);
143 QDataStream &operator>>(double &f);
144 QDataStream &operator>>(char *&str);
145
146 QDataStream &operator<<(qint8 i);
147 QDataStream &operator<<(quint8 i);
148 QDataStream &operator<<(qint16 i);
149 QDataStream &operator<<(quint16 i);
150 QDataStream &operator<<(qint32 i);
151 QDataStream &operator<<(quint32 i);
152 QDataStream &operator<<(qint64 i);
153 QDataStream &operator<<(quint64 i);
154 QDataStream &operator<<(bool i);
155 QDataStream &operator<<(float f);
156 QDataStream &operator<<(double f);
157 QDataStream &operator<<(const char *str);
158
159 QDataStream &readBytes(char *&, uint &len);
160 int readRawData(char *, int len);
161
162 QDataStream &writeBytes(const char *, uint len);
163 int writeRawData(const char *, int len);
164
165 int skipRawData(int len);
166
167#ifdef QT3_SUPPORT
168 inline QT3_SUPPORT QDataStream &readRawBytes(char *str, uint len)
169 { readRawData(str, static_cast<int>(len)); return *this; }
170 inline QT3_SUPPORT QDataStream &writeRawBytes(const char *str, uint len)
171 { writeRawData(str, static_cast<int>(len)); return *this; }
172 inline QT3_SUPPORT bool isPrintableData() const { return false; }
173 inline QT3_SUPPORT void setPrintableData(bool) {}
174#endif
175
176private:
177 Q_DISABLE_COPY(QDataStream)
178
179 QDataStreamPrivate *d;
180
181 QIODevice *dev;
182 bool owndev;
183 bool noswap;
184 ByteOrder byteorder;
185 int ver;
186 Status q_status;
187};
188
189
190/*****************************************************************************
191 QDataStream inline functions
192 *****************************************************************************/
193
194inline QIODevice *QDataStream::device() const
195{ return dev; }
196
197inline QDataStream::ByteOrder QDataStream::byteOrder() const
198{ return byteorder; }
199
200inline int QDataStream::version() const
201{ return ver; }
202
203inline void QDataStream::setVersion(int v)
204{ ver = v; }
205
206inline QDataStream &QDataStream::operator>>(quint8 &i)
207{ return *this >> reinterpret_cast<qint8&>(i); }
208
209inline QDataStream &QDataStream::operator>>(quint16 &i)
210{ return *this >> reinterpret_cast<qint16&>(i); }
211
212inline QDataStream &QDataStream::operator>>(quint32 &i)
213{ return *this >> reinterpret_cast<qint32&>(i); }
214
215inline QDataStream &QDataStream::operator>>(quint64 &i)
216{ return *this >> reinterpret_cast<qint64&>(i); }
217
218inline QDataStream &QDataStream::operator<<(quint8 i)
219{ return *this << qint8(i); }
220
221inline QDataStream &QDataStream::operator<<(quint16 i)
222{ return *this << qint16(i); }
223
224inline QDataStream &QDataStream::operator<<(quint32 i)
225{ return *this << qint32(i); }
226
227inline QDataStream &QDataStream::operator<<(quint64 i)
228{ return *this << qint64(i); }
229
230template <typename T>
231QDataStream& operator>>(QDataStream& s, QList<T>& l)
232{
233 l.clear();
234 quint32 c;
235 s >> c;
236 for(quint32 i = 0; i < c; ++i)
237 {
238 T t;
239 s >> t;
240 l.append(t);
241 if (s.atEnd())
242 break;
243 }
244 return s;
245}
246
247template <typename T>
248QDataStream& operator<<(QDataStream& s, const QList<T>& l)
249{
250 s << quint32(l.size());
251 for (int i = 0; i < l.size(); ++i)
252 s << l.at(i);
253 return s;
254}
255
256template <typename T>
257QDataStream& operator>>(QDataStream& s, QLinkedList<T>& l)
258{
259 l.clear();
260 quint32 c;
261 s >> c;
262 for(quint32 i = 0; i < c; ++i)
263 {
264 T t;
265 s >> t;
266 l.append(t);
267 if (s.atEnd())
268 break;
269 }
270 return s;
271}
272
273template <typename T>
274QDataStream& operator<<(QDataStream& s, const QLinkedList<T>& l)
275{
276 s << quint32(l.size());
277 typename QLinkedList<T>::ConstIterator it = l.constBegin();
278 for(; it != l.constEnd(); ++it)
279 s << *it;
280 return s;
281}
282
283template<typename T>
284QDataStream& operator>>(QDataStream& s, QVector<T>& v)
285{
286 v.clear();
287 quint32 c;
288 s >> c;
289 v.resize(c);
290 for(quint32 i = 0; i < c; ++i) {
291 T t;
292 s >> t;
293 v[i] = t;
294 }
295 return s;
296}
297
298template<typename T>
299QDataStream& operator<<(QDataStream& s, const QVector<T>& v)
300{
301 s << quint32(v.size());
302 for (typename QVector<T>::const_iterator it = v.begin(); it != v.end(); ++it)
303 s << *it;
304 return s;
305}
306
307template <typename T>
308QDataStream &operator>>(QDataStream &in, QSet<T> &set)
309{
310 set.clear();
311 quint32 c;
312 in >> c;
313 for (quint32 i = 0; i < c; ++i) {
314 T t;
315 in >> t;
316 set << t;
317 if (in.atEnd())
318 break;
319 }
320 return in;
321}
322
323template <typename T>
324QDataStream& operator<<(QDataStream &out, const QSet<T> &set)
325{
326 out << quint32(set.size());
327 typename QSet<T>::const_iterator i = set.constBegin();
328 while (i != set.constEnd()) {
329 out << *i;
330 ++i;
331 }
332 return out;
333}
334
335template <class Key, class T>
336Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash)
337{
338 QDataStream::Status oldStatus = in.status();
339 in.resetStatus();
340 hash.clear();
341
342 quint32 n;
343 in >> n;
344
345 for (quint32 i = 0; i < n; ++i) {
346 if (in.status() != QDataStream::Ok)
347 break;
348
349 Key k;
350 T t;
351 in >> k >> t;
352 hash.insertMulti(k, t);
353 }
354
355 if (in.status() != QDataStream::Ok)
356 hash.clear();
357 if (oldStatus != QDataStream::Ok)
358 in.setStatus(oldStatus);
359 return in;
360}
361
362template <class Key, class T>
363Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QHash<Key, T>& hash)
364{
365 out << quint32(hash.size());
366 typename QHash<Key, T>::ConstIterator it = hash.end();
367 typename QHash<Key, T>::ConstIterator begin = hash.begin();
368 while (it != begin) {
369 --it;
370 out << it.key() << it.value();
371 }
372 return out;
373}
374#ifdef qdoc
375template <class Key, class T>
376Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<Key, T> &map)
377#else
378template <class aKey, class aT>
379Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<aKey, aT> &map)
380#endif
381{
382 QDataStream::Status oldStatus = in.status();
383 in.resetStatus();
384 map.clear();
385
386 quint32 n;
387 in >> n;
388
389 map.detach();
390 map.setInsertInOrder(true);
391 for (quint32 i = 0; i < n; ++i) {
392 if (in.status() != QDataStream::Ok)
393 break;
394
395 aKey key;
396 aT value;
397 in >> key >> value;
398 map.insertMulti(key, value);
399 }
400 map.setInsertInOrder(false);
401 if (in.status() != QDataStream::Ok)
402 map.clear();
403 if (oldStatus != QDataStream::Ok)
404 in.setStatus(oldStatus);
405 return in;
406}
407
408template <class Key, class T>
409Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QMap<Key, T> &map)
410{
411 out << quint32(map.size());
412 typename QMap<Key, T>::ConstIterator it = map.end();
413 typename QMap<Key, T>::ConstIterator begin = map.begin();
414 while (it != begin) {
415 --it;
416 out << it.key() << it.value();
417 }
418 return out;
419}
420
421#endif // QT_NO_DATASTREAM
422
423QT_END_NAMESPACE
424
425QT_END_HEADER
426
427#endif // QDATASTREAM_H
Note: See TracBrowser for help on using the repository browser.