diff options
author | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com> | 2009-09-30 10:41:58 (GMT) |
---|---|---|
committer | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com> | 2009-09-30 10:46:00 (GMT) |
commit | 5170432e7cb2d0d1adf7ac2ec1ece627c75470f3 (patch) | |
tree | e9cca0ad6ba6c59ce1c0477542808e0f063dd7ac /src/corelib/io/qdatastream.h | |
parent | 45d5832a504516219167f0205901c56035118944 (diff) | |
download | Qt-5170432e7cb2d0d1adf7ac2ec1ece627c75470f3.zip Qt-5170432e7cb2d0d1adf7ac2ec1ece627c75470f3.tar.gz Qt-5170432e7cb2d0d1adf7ac2ec1ece627c75470f3.tar.bz2 |
Fix floating point precision when using qreal with QDataStream
A frequent bug when using QDataStream across platforms where the size
of qreal is different (such as any desktop platform and an ARM device)
is that you end up using different overloads for streaming the value
in and out (e.g. operator>>(double) on desktop and operator<<(float) on
ARM.)
This can leads to crashes and data corruption. To avoid the problem,
we define a single floating point precision for the entire data stream
and allow this to be set by the user. The default is to use 64-bit
precision for all floating point numbers.
Reviewed-by: Samuel
Reviewed-by: Thiago
Diffstat (limited to 'src/corelib/io/qdatastream.h')
-rw-r--r-- | src/corelib/io/qdatastream.h | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/corelib/io/qdatastream.h b/src/corelib/io/qdatastream.h index b376de6..6e83204 100644 --- a/src/corelib/io/qdatastream.h +++ b/src/corelib/io/qdatastream.h @@ -42,6 +42,7 @@ #ifndef QDATASTREAM_H #define QDATASTREAM_H +#include <QtCore/qscopedpointer.h> #include <QtCore/qiodevice.h> #include <QtCore/qglobal.h> @@ -83,7 +84,7 @@ public: Qt_4_3 = 9, Qt_4_4 = 10, Qt_4_5 = 11, - Qt_4_6 = Qt_4_5 + Qt_4_6 = 12, #if QT_VERSION >= 0x040700 #error Add the datastream version for this Qt version Qt_4_7 = Qt_4_6 @@ -101,6 +102,11 @@ public: ReadCorruptData }; + enum FloatingPointPrecision { + SinglePrecision, + DoublePrecision + }; + QDataStream(); explicit QDataStream(QIODevice *); #ifdef QT3_SUPPORT @@ -123,6 +129,9 @@ public: void setStatus(Status status); void resetStatus(); + FloatingPointPrecision floatingPointPrecision() const; + void setFloatingPointPrecision(FloatingPointPrecision precision); + ByteOrder byteOrder() const; void setByteOrder(ByteOrder); @@ -176,7 +185,7 @@ public: private: Q_DISABLE_COPY(QDataStream) - QDataStreamPrivate *d; + QScopedPointer<QDataStreamPrivate> d; QIODevice *dev; bool owndev; |