summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/io/io.pri1
-rw-r--r--src/corelib/io/qdatastream.cpp102
-rw-r--r--src/corelib/io/qdatastream.h13
-rw-r--r--src/corelib/io/qdatastream_p.h72
4 files changed, 179 insertions, 9 deletions
diff --git a/src/corelib/io/io.pri b/src/corelib/io/io.pri
index bca9baa..02a1586 100644
--- a/src/corelib/io/io.pri
+++ b/src/corelib/io/io.pri
@@ -5,6 +5,7 @@ HEADERS += \
io/qabstractfileengine_p.h \
io/qbuffer.h \
io/qdatastream.h \
+ io/qdatastream_p.h \
io/qdebug.h \
io/qdir.h \
io/qdiriterator.h \
diff --git a/src/corelib/io/qdatastream.cpp b/src/corelib/io/qdatastream.cpp
index 9339b8e..cc62201 100644
--- a/src/corelib/io/qdatastream.cpp
+++ b/src/corelib/io/qdatastream.cpp
@@ -40,6 +40,7 @@
****************************************************************************/
#include "qdatastream.h"
+#include "qdatastream_p.h"
#ifndef QT_NO_DATASTREAM
#include "qbuffer.h"
@@ -193,6 +194,21 @@ QT_BEGIN_NAMESPACE
*/
/*!
+ \enum QDataStream::FloatingPointPrecision
+
+ The precision of floating point numbers used for reading/writing the data. This will only have
+ an effect if the version of the data stream is Qt_4_6 or higher.
+
+ \warning The floating point precision must be set to the same value on the object that writes
+ and the object that reads the data stream.
+
+ \value SinglePrecision All floating point numbers in the data stream have 32-bit precision.
+ \value DoublePrecision All floating point numbers in the data stream have 64-bit precision.
+
+ \sa setFloatingPointPrecision(), floatingPointPrecision()
+*/
+
+/*!
\enum QDataStream::Status
This enum describes the current status of the data stream.
@@ -222,7 +238,7 @@ QT_BEGIN_NAMESPACE
#endif
enum {
- DefaultStreamVersion = QDataStream::Qt_4_5
+ DefaultStreamVersion = QDataStream::Qt_4_6
};
// ### 5.0: when streaming invalid QVariants, just the type should
@@ -414,6 +430,42 @@ bool QDataStream::atEnd() const
}
/*!
+ Returns the floating point precision of the data stream.
+
+ \since 4.6
+
+ \sa FloatingPointPrecision setFloatingPointPrecision()
+*/
+QDataStream::FloatingPointPrecision QDataStream::floatingPointPrecision() const
+{
+ return d == 0 ? QDataStream::DoublePrecision : d->floatingPointPrecision;
+}
+
+/*!
+ Sets the floating point precision of the data stream. If the floating point precision is
+ DoublePrecision and the version of the data stream is Qt_4_6 or higher, all floating point
+ numbers will be written and read with 64-bit precision. If the floating point precision is
+ SinglePrecision and the version is Qt_4_6 or higher, all floating point numbers will be written
+ and read with 32-bit precision.
+
+ For versions prior to Qt_4_6, the precision of floating point numbers in the data stream depends
+ on the stream operator called.
+
+ The default is DoublePrecision.
+
+ \warning This property must be set to the same value on the object that writes and the object
+ that reads the data stream.
+
+ \since 4.6
+*/
+void QDataStream::setFloatingPointPrecision(QDataStream::FloatingPointPrecision precision)
+{
+ if (d == 0)
+ d.reset(new QDataStreamPrivate());
+ d->floatingPointPrecision = precision;
+}
+
+/*!
Returns the status of the data stream.
\sa Status setStatus() resetStatus()
@@ -517,7 +569,7 @@ void QDataStream::setByteOrder(ByteOrder bo)
\value Qt_4_3 Version 9 (Qt 4.3)
\value Qt_4_4 Version 10 (Qt 4.4)
\value Qt_4_5 Version 11 (Qt 4.5)
- \omitvalue Qt_4_6
+ \value Qt_4_6 Version 12 (Qt 4.6)
\sa setVersion(), version()
*/
@@ -754,13 +806,23 @@ QDataStream &QDataStream::operator>>(bool &i)
/*!
\overload
- Reads a 32-bit floating point number from the stream into \a f,
+ Reads a floating point number from the stream into \a f,
using the standard IEEE 754 format. Returns a reference to the
stream.
+
+ \sa setFloatingPointPrecision()
*/
QDataStream &QDataStream::operator>>(float &f)
-{
+{
+ if (version() >= QDataStream::Qt_4_6
+ && floatingPointPrecision() == QDataStream::DoublePrecision) {
+ double d;
+ *this >> d;
+ f = d;
+ return *this;
+ }
+
f = 0.0f;
CHECK_STREAM_PRECOND(*this)
if (noswap) {
@@ -796,13 +858,23 @@ QDataStream &QDataStream::operator>>(float &f)
/*!
\overload
- Reads a 64-bit floating point number from the stream into \a f,
+ Reads a floating point number from the stream into \a f,
using the standard IEEE 754 format. Returns a reference to the
stream.
+
+ \sa setFloatingPointPrecision()
*/
QDataStream &QDataStream::operator>>(double &f)
{
+ if (version() >= QDataStream::Qt_4_6
+ && floatingPointPrecision() == QDataStream::SinglePrecision) {
+ float d;
+ *this >> d;
+ f = d;
+ return *this;
+ }
+
f = 0.0;
CHECK_STREAM_PRECOND(*this)
#ifndef Q_DOUBLE_FORMAT
@@ -1115,12 +1187,20 @@ QDataStream &QDataStream::operator<<(bool i)
/*!
\overload
- Writes a 32-bit floating point number, \a f, to the stream using
+ Writes a floating point number, \a f, to the stream using
the standard IEEE 754 format. Returns a reference to the stream.
+
+ \sa setFloatingPointPrecision()
*/
QDataStream &QDataStream::operator<<(float f)
{
+ if (version() >= QDataStream::Qt_4_6
+ && floatingPointPrecision() == QDataStream::DoublePrecision) {
+ *this << double(f);
+ return *this;
+ }
+
CHECK_STREAM_PRECOND(*this)
float g = f; // fixes float-on-stack problem
if (noswap) { // no conversion needed
@@ -1146,12 +1226,20 @@ QDataStream &QDataStream::operator<<(float f)
/*!
\overload
- Writes a 64-bit floating point number, \a f, to the stream using
+ Writes a floating point number, \a f, to the stream using
the standard IEEE 754 format. Returns a reference to the stream.
+
+ \sa setFloatingPointPrecision()
*/
QDataStream &QDataStream::operator<<(double f)
{
+ if (version() >= QDataStream::Qt_4_6
+ && floatingPointPrecision() == QDataStream::SinglePrecision) {
+ *this << float(f);
+ return *this;
+ }
+
CHECK_STREAM_PRECOND(*this)
#ifndef Q_DOUBLE_FORMAT
if (noswap) {
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;
diff --git a/src/corelib/io/qdatastream_p.h b/src/corelib/io/qdatastream_p.h
new file mode 100644
index 0000000..157fee9
--- /dev/null
+++ b/src/corelib/io/qdatastream_p.h
@@ -0,0 +1,72 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QDATASTREAM_P_H
+#define QDATASTREAM_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <qdatastream.h>
+
+QT_BEGIN_NAMESPACE
+
+#ifndef QT_NO_DATASTREAM
+class QDataStreamPrivate
+{
+public:
+ QDataStreamPrivate() : floatingPointPrecision(QDataStream::DoublePrecision) { }
+
+ QDataStream::FloatingPointPrecision floatingPointPrecision;
+};
+#endif
+
+QT_END_NAMESPACE
+
+#endif // QDATASTREAM_P_H