|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtCore module of the Qt Toolkit. |
|
8 ** |
|
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 |
|
14 ** this package. |
|
15 ** |
|
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. |
|
23 ** |
|
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. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #ifndef QDATASTREAM_H |
|
43 #define QDATASTREAM_H |
|
44 |
|
45 #include <QtCore/qscopedpointer.h> |
|
46 #include <QtCore/qiodevice.h> |
|
47 #include <QtCore/qglobal.h> |
|
48 |
|
49 #ifdef Status |
|
50 #error qdatastream.h must be included before any header file that defines Status |
|
51 #endif |
|
52 |
|
53 QT_BEGIN_HEADER |
|
54 |
|
55 QT_BEGIN_NAMESPACE |
|
56 |
|
57 QT_MODULE(Core) |
|
58 |
|
59 class QByteArray; |
|
60 class QIODevice; |
|
61 |
|
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; |
|
68 |
|
69 #ifndef QT_NO_DATASTREAM |
|
70 class QDataStreamPrivate; |
|
71 class Q_CORE_EXPORT QDataStream |
|
72 { |
|
73 public: |
|
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 Qt_4_6 = 12 |
|
88 #if QT_VERSION >= 0x040700 |
|
89 #error Add the datastream version for this Qt version |
|
90 Qt_4_7 = Qt_4_6 |
|
91 #endif |
|
92 }; |
|
93 |
|
94 enum ByteOrder { |
|
95 BigEndian = QSysInfo::BigEndian, |
|
96 LittleEndian = QSysInfo::LittleEndian |
|
97 }; |
|
98 |
|
99 enum Status { |
|
100 Ok, |
|
101 ReadPastEnd, |
|
102 ReadCorruptData |
|
103 }; |
|
104 |
|
105 enum FloatingPointPrecision { |
|
106 SinglePrecision, |
|
107 DoublePrecision |
|
108 }; |
|
109 |
|
110 QDataStream(); |
|
111 explicit QDataStream(QIODevice *); |
|
112 #ifdef QT3_SUPPORT |
|
113 QDataStream(QByteArray *, int mode); |
|
114 #endif |
|
115 QDataStream(QByteArray *, QIODevice::OpenMode flags); |
|
116 QDataStream(const QByteArray &); |
|
117 virtual ~QDataStream(); |
|
118 |
|
119 QIODevice *device() const; |
|
120 void setDevice(QIODevice *); |
|
121 void unsetDevice(); |
|
122 |
|
123 bool atEnd() const; |
|
124 #ifdef QT3_SUPPORT |
|
125 inline QT3_SUPPORT bool eof() const { return atEnd(); } |
|
126 #endif |
|
127 |
|
128 Status status() const; |
|
129 void setStatus(Status status); |
|
130 void resetStatus(); |
|
131 |
|
132 FloatingPointPrecision floatingPointPrecision() const; |
|
133 void setFloatingPointPrecision(FloatingPointPrecision precision); |
|
134 |
|
135 ByteOrder byteOrder() const; |
|
136 void setByteOrder(ByteOrder); |
|
137 |
|
138 int version() const; |
|
139 void setVersion(int); |
|
140 |
|
141 QDataStream &operator>>(qint8 &i); |
|
142 QDataStream &operator>>(quint8 &i); |
|
143 QDataStream &operator>>(qint16 &i); |
|
144 QDataStream &operator>>(quint16 &i); |
|
145 QDataStream &operator>>(qint32 &i); |
|
146 QDataStream &operator>>(quint32 &i); |
|
147 QDataStream &operator>>(qint64 &i); |
|
148 QDataStream &operator>>(quint64 &i); |
|
149 |
|
150 QDataStream &operator>>(bool &i); |
|
151 QDataStream &operator>>(float &f); |
|
152 QDataStream &operator>>(double &f); |
|
153 QDataStream &operator>>(char *&str); |
|
154 |
|
155 QDataStream &operator<<(qint8 i); |
|
156 QDataStream &operator<<(quint8 i); |
|
157 QDataStream &operator<<(qint16 i); |
|
158 QDataStream &operator<<(quint16 i); |
|
159 QDataStream &operator<<(qint32 i); |
|
160 QDataStream &operator<<(quint32 i); |
|
161 QDataStream &operator<<(qint64 i); |
|
162 QDataStream &operator<<(quint64 i); |
|
163 QDataStream &operator<<(bool i); |
|
164 QDataStream &operator<<(float f); |
|
165 QDataStream &operator<<(double f); |
|
166 QDataStream &operator<<(const char *str); |
|
167 |
|
168 QDataStream &readBytes(char *&, uint &len); |
|
169 int readRawData(char *, int len); |
|
170 |
|
171 QDataStream &writeBytes(const char *, uint len); |
|
172 int writeRawData(const char *, int len); |
|
173 |
|
174 int skipRawData(int len); |
|
175 |
|
176 #ifdef QT3_SUPPORT |
|
177 inline QT3_SUPPORT QDataStream &readRawBytes(char *str, uint len) |
|
178 { readRawData(str, static_cast<int>(len)); return *this; } |
|
179 inline QT3_SUPPORT QDataStream &writeRawBytes(const char *str, uint len) |
|
180 { writeRawData(str, static_cast<int>(len)); return *this; } |
|
181 inline QT3_SUPPORT bool isPrintableData() const { return false; } |
|
182 inline QT3_SUPPORT void setPrintableData(bool) {} |
|
183 #endif |
|
184 |
|
185 private: |
|
186 Q_DISABLE_COPY(QDataStream) |
|
187 |
|
188 QScopedPointer<QDataStreamPrivate> d; |
|
189 |
|
190 QIODevice *dev; |
|
191 bool owndev; |
|
192 bool noswap; |
|
193 ByteOrder byteorder; |
|
194 int ver; |
|
195 Status q_status; |
|
196 }; |
|
197 |
|
198 |
|
199 /***************************************************************************** |
|
200 QDataStream inline functions |
|
201 *****************************************************************************/ |
|
202 |
|
203 inline QIODevice *QDataStream::device() const |
|
204 { return dev; } |
|
205 |
|
206 inline QDataStream::ByteOrder QDataStream::byteOrder() const |
|
207 { return byteorder; } |
|
208 |
|
209 inline int QDataStream::version() const |
|
210 { return ver; } |
|
211 |
|
212 inline void QDataStream::setVersion(int v) |
|
213 { ver = v; } |
|
214 |
|
215 inline QDataStream &QDataStream::operator>>(quint8 &i) |
|
216 { return *this >> reinterpret_cast<qint8&>(i); } |
|
217 |
|
218 inline QDataStream &QDataStream::operator>>(quint16 &i) |
|
219 { return *this >> reinterpret_cast<qint16&>(i); } |
|
220 |
|
221 inline QDataStream &QDataStream::operator>>(quint32 &i) |
|
222 { return *this >> reinterpret_cast<qint32&>(i); } |
|
223 |
|
224 inline QDataStream &QDataStream::operator>>(quint64 &i) |
|
225 { return *this >> reinterpret_cast<qint64&>(i); } |
|
226 |
|
227 inline QDataStream &QDataStream::operator<<(quint8 i) |
|
228 { return *this << qint8(i); } |
|
229 |
|
230 inline QDataStream &QDataStream::operator<<(quint16 i) |
|
231 { return *this << qint16(i); } |
|
232 |
|
233 inline QDataStream &QDataStream::operator<<(quint32 i) |
|
234 { return *this << qint32(i); } |
|
235 |
|
236 inline QDataStream &QDataStream::operator<<(quint64 i) |
|
237 { return *this << qint64(i); } |
|
238 |
|
239 template <typename T> |
|
240 QDataStream& operator>>(QDataStream& s, QList<T>& l) |
|
241 { |
|
242 l.clear(); |
|
243 quint32 c; |
|
244 s >> c; |
|
245 for(quint32 i = 0; i < c; ++i) |
|
246 { |
|
247 T t; |
|
248 s >> t; |
|
249 l.append(t); |
|
250 if (s.atEnd()) |
|
251 break; |
|
252 } |
|
253 return s; |
|
254 } |
|
255 |
|
256 template <typename T> |
|
257 QDataStream& operator<<(QDataStream& s, const QList<T>& l) |
|
258 { |
|
259 s << quint32(l.size()); |
|
260 for (int i = 0; i < l.size(); ++i) |
|
261 s << l.at(i); |
|
262 return s; |
|
263 } |
|
264 |
|
265 template <typename T> |
|
266 QDataStream& operator>>(QDataStream& s, QLinkedList<T>& l) |
|
267 { |
|
268 l.clear(); |
|
269 quint32 c; |
|
270 s >> c; |
|
271 for(quint32 i = 0; i < c; ++i) |
|
272 { |
|
273 T t; |
|
274 s >> t; |
|
275 l.append(t); |
|
276 if (s.atEnd()) |
|
277 break; |
|
278 } |
|
279 return s; |
|
280 } |
|
281 |
|
282 template <typename T> |
|
283 QDataStream& operator<<(QDataStream& s, const QLinkedList<T>& l) |
|
284 { |
|
285 s << quint32(l.size()); |
|
286 typename QLinkedList<T>::ConstIterator it = l.constBegin(); |
|
287 for(; it != l.constEnd(); ++it) |
|
288 s << *it; |
|
289 return s; |
|
290 } |
|
291 |
|
292 template<typename T> |
|
293 QDataStream& operator>>(QDataStream& s, QVector<T>& v) |
|
294 { |
|
295 v.clear(); |
|
296 quint32 c; |
|
297 s >> c; |
|
298 v.resize(c); |
|
299 for(quint32 i = 0; i < c; ++i) { |
|
300 T t; |
|
301 s >> t; |
|
302 v[i] = t; |
|
303 } |
|
304 return s; |
|
305 } |
|
306 |
|
307 template<typename T> |
|
308 QDataStream& operator<<(QDataStream& s, const QVector<T>& v) |
|
309 { |
|
310 s << quint32(v.size()); |
|
311 for (typename QVector<T>::const_iterator it = v.begin(); it != v.end(); ++it) |
|
312 s << *it; |
|
313 return s; |
|
314 } |
|
315 |
|
316 template <typename T> |
|
317 QDataStream &operator>>(QDataStream &in, QSet<T> &set) |
|
318 { |
|
319 set.clear(); |
|
320 quint32 c; |
|
321 in >> c; |
|
322 for (quint32 i = 0; i < c; ++i) { |
|
323 T t; |
|
324 in >> t; |
|
325 set << t; |
|
326 if (in.atEnd()) |
|
327 break; |
|
328 } |
|
329 return in; |
|
330 } |
|
331 |
|
332 template <typename T> |
|
333 QDataStream& operator<<(QDataStream &out, const QSet<T> &set) |
|
334 { |
|
335 out << quint32(set.size()); |
|
336 typename QSet<T>::const_iterator i = set.constBegin(); |
|
337 while (i != set.constEnd()) { |
|
338 out << *i; |
|
339 ++i; |
|
340 } |
|
341 return out; |
|
342 } |
|
343 |
|
344 template <class Key, class T> |
|
345 Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash) |
|
346 { |
|
347 QDataStream::Status oldStatus = in.status(); |
|
348 in.resetStatus(); |
|
349 hash.clear(); |
|
350 |
|
351 quint32 n; |
|
352 in >> n; |
|
353 |
|
354 for (quint32 i = 0; i < n; ++i) { |
|
355 if (in.status() != QDataStream::Ok) |
|
356 break; |
|
357 |
|
358 Key k; |
|
359 T t; |
|
360 in >> k >> t; |
|
361 hash.insertMulti(k, t); |
|
362 } |
|
363 |
|
364 if (in.status() != QDataStream::Ok) |
|
365 hash.clear(); |
|
366 if (oldStatus != QDataStream::Ok) |
|
367 in.setStatus(oldStatus); |
|
368 return in; |
|
369 } |
|
370 |
|
371 template <class Key, class T> |
|
372 Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QHash<Key, T>& hash) |
|
373 { |
|
374 out << quint32(hash.size()); |
|
375 typename QHash<Key, T>::ConstIterator it = hash.end(); |
|
376 typename QHash<Key, T>::ConstIterator begin = hash.begin(); |
|
377 while (it != begin) { |
|
378 --it; |
|
379 out << it.key() << it.value(); |
|
380 } |
|
381 return out; |
|
382 } |
|
383 #ifdef qdoc |
|
384 template <class Key, class T> |
|
385 Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<Key, T> &map) |
|
386 #else |
|
387 template <class aKey, class aT> |
|
388 Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<aKey, aT> &map) |
|
389 #endif |
|
390 { |
|
391 QDataStream::Status oldStatus = in.status(); |
|
392 in.resetStatus(); |
|
393 map.clear(); |
|
394 |
|
395 quint32 n; |
|
396 in >> n; |
|
397 |
|
398 map.detach(); |
|
399 map.setInsertInOrder(true); |
|
400 for (quint32 i = 0; i < n; ++i) { |
|
401 if (in.status() != QDataStream::Ok) |
|
402 break; |
|
403 |
|
404 aKey key; |
|
405 aT value; |
|
406 in >> key >> value; |
|
407 map.insertMulti(key, value); |
|
408 } |
|
409 map.setInsertInOrder(false); |
|
410 if (in.status() != QDataStream::Ok) |
|
411 map.clear(); |
|
412 if (oldStatus != QDataStream::Ok) |
|
413 in.setStatus(oldStatus); |
|
414 return in; |
|
415 } |
|
416 |
|
417 template <class Key, class T> |
|
418 Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QMap<Key, T> &map) |
|
419 { |
|
420 out << quint32(map.size()); |
|
421 typename QMap<Key, T>::ConstIterator it = map.end(); |
|
422 typename QMap<Key, T>::ConstIterator begin = map.begin(); |
|
423 while (it != begin) { |
|
424 --it; |
|
425 out << it.key() << it.value(); |
|
426 } |
|
427 return out; |
|
428 } |
|
429 |
|
430 #endif // QT_NO_DATASTREAM |
|
431 |
|
432 QT_END_NAMESPACE |
|
433 |
|
434 QT_END_HEADER |
|
435 |
|
436 #endif // QDATASTREAM_H |