From d56d7f107f9d18810d742ac4d3a2e36077722cb8 Mon Sep 17 00:00:00 2001 From: Liang Qi Date: Fri, 20 May 2011 09:48:30 +0200 Subject: QDataStream: speedup steaming of QUuid. By reading and writing as a whole block, because the size of QUuid is fixed. Reviewed-by: joao --- src/corelib/plugin/quuid.cpp | 64 ++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index 4b1856d..ddc388c 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -591,11 +591,30 @@ QByteArray QUuid::toRfc4122() const */ QDataStream &operator<<(QDataStream &s, const QUuid &id) { - s << (quint32)id.data1; - s << (quint16)id.data2; - s << (quint16)id.data3; - for (int i = 0; i < 8; i++) - s << (quint8)id.data4[i]; + QByteArray bytes; + if (s.byteOrder() == QDataStream::BigEndian) { + bytes = id.toRfc4122(); + } else { + // we know how many bytes a UUID has, I hope :) + bytes = QByteArray(16, Qt::Uninitialized); + uchar *data = reinterpret_cast(bytes.data()); + + qToLittleEndian(id.data1, data); + data += sizeof(quint32); + qToLittleEndian(id.data2, data); + data += sizeof(quint16); + qToLittleEndian(id.data3, data); + data += sizeof(quint16); + + for (int i = 0; i < 8; ++i) { + *(data) = id.data4[i]; + data++; + } + } + + if (s.writeRawData(bytes.data(), 16) != 16) { + s.setStatus(QDataStream::WriteFailed); + } return s; } @@ -605,19 +624,30 @@ QDataStream &operator<<(QDataStream &s, const QUuid &id) */ QDataStream &operator>>(QDataStream &s, QUuid &id) { - quint32 u32; - quint16 u16; - quint8 u8; - s >> u32; - id.data1 = u32; - s >> u16; - id.data2 = u16; - s >> u16; - id.data3 = u16; - for (int i = 0; i < 8; i++) { - s >> u8; - id.data4[i] = u8; + QByteArray bytes(16, Qt::Uninitialized); + if (s.readRawData(bytes.data(), 16) != 16) { + s.setStatus(QDataStream::ReadPastEnd); + return s; } + + if (s.byteOrder() == QDataStream::BigEndian) { + id = QUuid::fromRfc4122(bytes); + } else { + const uchar *data = reinterpret_cast(bytes.constData()); + + id.data1 = qFromLittleEndian(data); + data += sizeof(quint32); + id.data2 = qFromLittleEndian(data); + data += sizeof(quint16); + id.data3 = qFromLittleEndian(data); + data += sizeof(quint16); + + for (int i = 0; i < 8; ++i) { + id.data4[i] = *(data); + data++; + } + } + return s; } #endif // QT_NO_DATASTREAM -- cgit v0.12