*/
/*!
- \enum QDataStream::FloatingPointPrecision
-
- The precision of floating point numbers used for reading/writing the data.
-
- \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::DataStatus
This enum describes the current status of the data stream.
: dev(nullptr),
owndev(false),
byteorder(QDataStream::HostEndian),
- q_status(QDataStream::Ok),
- floatingPrecision(QDataStream::DoublePrecision)
+ q_status(QDataStream::Ok)
{
}
: dev(device),
owndev(false),
byteorder(QDataStream::HostEndian),
- q_status(QDataStream::Ok),
- floatingPrecision(QDataStream::DoublePrecision)
+ q_status(QDataStream::Ok)
{
}
: dev(nullptr),
owndev(true),
byteorder(QDataStream::HostEndian),
- q_status(QDataStream::Ok),
- floatingPrecision(QDataStream::DoublePrecision)
+ q_status(QDataStream::Ok)
{
QBuffer *buf = new QBuffer(a);
#ifndef QT_NO_QOBJECT
: dev(nullptr),
owndev(true),
byteorder(QDataStream::HostEndian),
- q_status(QDataStream::Ok),
- floatingPrecision(QDataStream::DoublePrecision)
+ q_status(QDataStream::Ok)
{
QBuffer *buf = new QBuffer;
#ifndef QT_NO_QOBJECT
}
/*!
- Returns the floating point precision of the data stream.
-
- \since 4.6
-
- \sa FloatingPointPrecision setFloatingPointPrecision()
-*/
-QDataStream::FloatingPointPrecision QDataStream::floatingPointPrecision() const
-{
- return floatingPrecision;
-}
-
-/*!
- Sets the floating point precision of the data stream to \a precision. If the floating point
- precision is DoublePrecision, all floating point numbers will be written and read with 64-bit
- precision. If the floating point precision is SinglePrecision, all floating point numbers will
- be written and read with 32-bit precision.
-
- 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)
-{
- floatingPrecision = precision;
-}
-
-/*!
Returns the status of the data stream.
\sa Status setStatus() resetStatus()
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 (floatingPointPrecision() == QDataStream::DoublePrecision) {
- double dbl;
- *this >> dbl;
- f = dbl;
- return *this;
- }
-
f = 0.0f;
CHECK_STREAM_PRECOND(*this)
if (dev->read((char *)&f, sizeof(float)) != sizeof(float)) {
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 (floatingPointPrecision() == QDataStream::SinglePrecision) {
- float flt;
- *this >> flt;
- f = flt;
- return *this;
- }
-
f = 0.0;
CHECK_STREAM_PRECOND(*this)
if (dev->read((char *)&f, sizeof(double)) != sizeof(double)) {
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 (floatingPointPrecision() == QDataStream::DoublePrecision) {
- *this << double(f);
- return *this;
- }
-
CHECK_STREAM_WRITE_PRECOND(*this)
float g = f; // fixes float-on-stack problem
if (byteorder != QDataStream::HostEndian) {
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 (floatingPointPrecision() == QDataStream::SinglePrecision) {
- *this << float(f);
- return *this;
- }
-
CHECK_STREAM_WRITE_PRECOND(*this)
if (byteorder == QDataStream::HostEndian) {
if (dev->write((char *)&f, sizeof(double)) != sizeof(double))
WriteFailed
};
- enum FloatingPointPrecision {
- SinglePrecision,
- DoublePrecision
- };
-
QDataStream();
explicit QDataStream(QIODevice *);
QDataStream(QByteArray *, QIODevice::OpenMode flags);
void setStatus(DataStatus status);
void resetStatus();
- FloatingPointPrecision floatingPointPrecision() const;
- void setFloatingPointPrecision(FloatingPointPrecision precision);
-
ByteOrder byteOrder() const;
void setByteOrder(ByteOrder);
bool owndev;
ByteOrder byteorder;
DataStatus q_status;
- FloatingPointPrecision floatingPrecision;
};
*/
QTextStream &QTextStream::operator<<(float f)
{
+ // TODO: implement and use QLocale::floatToString()
return *this << double(f);
}
QDataStream &operator<<(QDataStream &s, const QPointF &p)
{
- s << double(p.x()) << double(p.y());
+ s << (qreal)p.x() << (qreal)p.y();
return s;
}
QDataStream &operator>>(QDataStream &s, QPointF &p)
{
- double x, y;
+ qreal x = 0.0;
+ qreal y = 0.0;
s >> x;
s >> y;
- p.setX(qreal(x));
- p.setY(qreal(y));
+ p.setX(x);
+ p.setY(y);
return s;
}
#endif // QT_NO_DATASTREAM
QDataStream &operator<<(QDataStream &s, const QRectF &r)
{
- s << double(r.x()) << double(r.y()) << double(r.width()) << double(r.height());
+ s << (qreal)r.x() << (qreal)r.y() << (qreal)r.width() << (qreal)r.height();
return s;
}
QDataStream &operator>>(QDataStream &s, QRectF &r)
{
- double x, y, w, h;
+ qreal x = 0.0;
+ qreal y = 0.0;
+ qreal w = 0.0;
+ qreal h = 0.0;
s >> x;
s >> y;
s >> w;
s >> h;
- r.setRect(qreal(x), qreal(y), qreal(w), qreal(h));
+ r.setRect(x, y, w, h);
return s;
}
#endif // QT_NO_DATASTREAM
-
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QRectF &r) {
dbg.nospace() << "QRectF(" << r.x() << ',' << r.y() << ' '
QDataStream &operator<<(QDataStream &s, const QSizeF &sz)
{
- s << double(sz.width()) << double(sz.height());
+ s << (qreal)sz.width() << (qreal)sz.height();
return s;
}
QDataStream &operator>>(QDataStream &s, QSizeF &sz)
{
- double w, h;
+ qreal w = 0.0;
+ qreal h = 0.0;
s >> w;
s >> h;
- sz.setWidth(qreal(w));
- sz.setHeight(qreal(h));
+ sz.setWidth(w);
+ sz.setHeight(h);
return s;
}
#endif // QT_NO_DATASTREAM
const QDBusArgument &operator>>(const QDBusArgument &a, QPointF &pt)
{
+ // NOTE: D-Bus does not support float numbers
double x = 0.0;
double y = 0.0;
a.beginStructure();
{
for (int row = 0; row < M; ++row)
for (int col = 0; col < N; ++col)
- stream << double(matrix(row, col));
+ stream << qreal(matrix(row, col));
return stream;
}
template <int N, int M>
QDataStream &operator>>(QDataStream &stream, QGenericMatrix<N, M> &matrix)
{
- double x;
+ qreal x = 0.0;
for (int row = 0; row < M; ++row) {
for (int col = 0; col < N; ++col) {
stream >> x;
- matrix(row, col) = qreal(x);
+ matrix(row, col) = x;
}
}
return stream;
{
for (int row = 0; row < 4; ++row)
for (int col = 0; col < 4; ++col)
- stream << double(matrix(row, col));
+ stream << qreal(matrix(row, col));
return stream;
}
QDataStream &operator>>(QDataStream &stream, QMatrix4x4 &matrix)
{
- double x;
+ qreal x = 0.0;
for (int row = 0; row < 4; ++row) {
for (int col = 0; col < 4; ++col) {
stream >> x;
- matrix(row, col) = qreal(x);
+ matrix(row, col) = x;
}
}
matrix.optimize();
QDataStream &operator<<(QDataStream &stream, const QVector2D &vector)
{
- stream << double(vector.x()) << double(vector.y());
+ stream << (qreal)vector.x() << (qreal)vector.y();
return stream;
}
QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
{
- double x, y;
+ qreal x = 0.0;
+ qreal y = 0.0;
stream >> x;
stream >> y;
- vector.setX(qreal(x));
- vector.setY(qreal(y));
+ vector.setX(x);
+ vector.setY(y);
return stream;
}
QDataStream &operator<<(QDataStream &stream, const QVector3D &vector)
{
- stream << double(vector.x()) << double(vector.y())
- << double(vector.z());
+ stream << (qreal)vector.x() << (qreal)vector.y()
+ << (qreal)vector.z();
return stream;
}
QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
{
- double x, y, z;
+ qreal x = 0.0;
+ qreal y = 0.0;
+ qreal z = 0.0;
stream >> x;
stream >> y;
stream >> z;
- vector.setX(qreal(x));
- vector.setY(qreal(y));
- vector.setZ(qreal(z));
+ vector.setX(x);
+ vector.setY(y);
+ vector.setZ(z);
return stream;
}
QDataStream &operator<<(QDataStream &stream, const QVector4D &vector)
{
- stream << double(vector.x()) << double(vector.y())
- << double(vector.z()) << double(vector.w());
+ stream << (qreal)vector.x() << (qreal)vector.y()
+ << (qreal)vector.z() << (qreal)vector.w();
return stream;
}
QDataStream &operator>>(QDataStream &stream, QVector4D &vector)
{
- double x, y, z, w;
+ qreal x = 0.0;
+ qreal y = 0.0;
+ qreal z = 0.0;
+ qreal w = 0.0;
stream >> x;
stream >> y;
stream >> z;
stream >> w;
- vector.setX(qreal(x));
- vector.setY(qreal(y));
- vector.setZ(qreal(z));
- vector.setW(qreal(w));
+ vector.setX(x);
+ vector.setY(y);
+ vector.setZ(z);
+ vector.setW(w);
return stream;
}
#endif // QT_NO_VECTOR4D
QT_END_NAMESPACE
-
-
-
-
s << qint8(gradient->type());
s << qint8(gradient->spread());
s << qint8(gradient->coordinateMode());
-
s << qint8(gradient->interpolationMode());
-
-#ifdef QT_NO_FPU
- // ensure that we write doubles here instead of streaming the stops
- // directly; otherwise, platforms that redefine qreal might generate
- // data that cannot be read on other platforms.
- QVector<QGradientStop> stops = gradient->stops();
- s << quint32(stops.size());
- for (int i = 0; i < stops.size(); ++i) {
- const QGradientStop &stop = stops.at(i);
- s << QPair<double, QColor>(double(stop.first), stop.second);
- }
-#else
s << gradient->stops();
-#endif
switch (gradient->type()) {
case QGradient::LinearGradient: {
case QGradient::RadialGradient: {
s << static_cast<const QRadialGradient *>(gradient)->center();
s << static_cast<const QRadialGradient *>(gradient)->focalPoint();
- s << (double) static_cast<const QRadialGradient *>(gradient)->radius();
+ s << (qreal) static_cast<const QRadialGradient *>(gradient)->radius();
break;
}
}
QPixmap pm;
s >> pm;
b = QBrush(color, pm);
- } else if (style == Qt::LinearGradientPattern
- || style == Qt::RadialGradientPattern) {
-
+ } else if (style == Qt::LinearGradientPattern || style == Qt::RadialGradientPattern) {
qint8 type_as_int;
QGradient::Type type;
QGradientStops stops;
QGradient::CoordinateMode cmode = QGradient::LogicalMode;
QGradient::Spread spread = QGradient::PadSpread;
QGradient::InterpolationMode imode = QGradient::ColorInterpolation;
-
s >> type_as_int;
type = QGradient::Type(type_as_int);
s >> type_as_int;
spread = QGradient::Spread(type_as_int);
s >> type_as_int;
cmode = QGradient::CoordinateMode(type_as_int);
-
s >> type_as_int;
imode = QGradient::InterpolationMode(type_as_int);
-
-#ifdef QT_NO_FPU
- quint32 numStops;
- double n;
- QColor c;
-
- s >> numStops;
- for (quint32 i = 0; i < numStops; ++i) {
- s >> n >> c;
- stops << QPair<qreal, QColor>(n, c);
- }
-#else
s >> stops;
-#endif
if (type == QGradient::LinearGradient) {
QPointF p1, p2;
b = QBrush(lg);
} else if (type == QGradient::RadialGradient) {
QPointF center, focal;
- double radius;
+ qreal radius;
s >> center;
s >> focal;
s >> radius;
QDataStream &operator<<(QDataStream &s, const QMatrix &m)
{
- s << double(m.m11())
- << double(m.m12())
- << double(m.m21())
- << double(m.m22())
- << double(m.dx())
- << double(m.dy());
+ s << (qreal)m.m11()
+ << (qreal)m.m12()
+ << (qreal)m.m21()
+ << (qreal)m.m22()
+ << (qreal)m.dx()
+ << (qreal)m.dy();
return s;
}
QDataStream &operator>>(QDataStream &s, QMatrix &m)
{
- double m11, m12, m21, m22, dx, dy;
+ qreal m11 = 0.0;
+ qreal m12 = 0.0;
+ qreal m21 = 0.0;
+ qreal m22 = 0.0;
+ qreal dx = 0.0;
+ qreal dy = 0.0;
s >> m11;
s >> m12;
s >> m21;
}
s << p.elementCount();
- for (int i=0; i < p.d_func()->elements.size(); ++i) {
+ for (int i = 0; i < p.d_func()->elements.size(); ++i) {
const QPainterPath::Element &e = p.d_func()->elements.at(i);
s << int(e.type);
- s << double(e.x) << double(e.y);
+ s << qreal(e.x) << qreal(e.y);
}
s << p.d_func()->cStart;
s << int(p.d_func()->fillRule);
p.d_func()->elements.clear();
}
p.d_func()->elements.reserve(p.d_func()->elements.size() + size);
- for (int i=0; i<size; ++i) {
+ for (int i = 0; i < size; ++i) {
int type;
- double x, y;
+ qreal x, y;
s >> type;
s >> x;
s >> y;
s << (quint8)p.joinStyle();
s << (bool)(p.d->cosmetic);
- s << double(p.widthF());
+ s << (qreal)p.widthF();
s << p.brush();
- s << double(p.miterLimit());
-#ifdef QT_NO_FPU
- // ensure that we write doubles here instead of streaming the pattern
- // directly; otherwise, platforms that redefine qreal might generate
- // data that cannot be read on other platforms.
- QVector<qreal> pattern = p.dashPattern();
- s << quint32(pattern.size());
- for (int i = 0; i < pattern.size(); ++i) {
- s << double(pattern.at(i));
- }
-#else
+ s << (qreal)p.miterLimit();
s << p.dashPattern();
-#endif
- s << double(p.dashOffset());
+ s << (qreal)p.dashOffset();
return s;
}
QDataStream &operator>>(QDataStream &s, QPen &p)
{
quint8 style, capStyle, joinStyle;
- double width = 0;
+ qreal width = 0.0;
QColor color;
QBrush brush;
- double miterLimit = 2;
+ qreal miterLimit = 2.0;
QVector<qreal> dashPattern;
- double dashOffset = 0;
+ qreal dashOffset = 0.0;
bool cosmetic = false;
s >> style;
s >> capStyle;
s >> width;
s >> brush;
s >> miterLimit;
-#ifdef QT_NO_FPU
- quint32 numDashes;
- s >> numDashes;
- double dash;
- for (quint32 i = 0; i < numDashes; ++i) {
- s >> dash;
- dashPattern << dash;
- }
-#else
s >> dashPattern;
-#endif
s >> dashOffset;
qAtomicDetach(p.d);
*/
QDataStream & operator<<(QDataStream &s, const QTransform &m)
{
- s << double(m.m11())
- << double(m.m12())
- << double(m.m13())
- << double(m.m21())
- << double(m.m22())
- << double(m.m23())
- << double(m.m31())
- << double(m.m32())
- << double(m.m33());
+ s << (qreal)m.m11()
+ << (qreal)m.m12()
+ << (qreal)m.m13()
+ << (qreal)m.m21()
+ << (qreal)m.m22()
+ << (qreal)m.m23()
+ << (qreal)m.m31()
+ << (qreal)m.m32()
+ << (qreal)m.m33();
return s;
}
*/
QDataStream & operator>>(QDataStream &s, QTransform &t)
{
- double m11, m12, m13,
+ qreal m11, m12, m13,
m21, m22, m23,
m31, m32, m33;
void streamRealDataTypes();
- void floatingPointPrecision();
-
- void floatingPointNaN();
-
private:
void writebool(QDataStream *s);
void writeQBitArray(QDataStream *s);
QFETCH(int, expectedStatus); \
QFETCH(double, expectedValue); \
\
- QDataStream::FloatingPointPrecision prec = sizeof(T) == sizeof(double) ? QDataStream::DoublePrecision : QDataStream::SinglePrecision; \
\
{ \
QDataStream stream(&bigEndianData, QIODevice::ReadOnly); \
stream.setByteOrder(QDataStream::BigEndian); \
- stream.setFloatingPointPrecision(prec); \
T i; \
stream >> i; \
QCOMPARE((int) stream.status(), expectedStatus); \
{ \
QDataStream stream(&littleEndianData, QIODevice::ReadOnly); \
stream.setByteOrder(QDataStream::LittleEndian); \
- stream.setFloatingPointPrecision(prec); \
T i; \
stream >> i; \
QCOMPARE((int) stream.status(), expectedStatus); \
QCOMPARE(stream.status(), QDataStream::Ok);
}
-void tst_QDataStream::floatingPointNaN()
-{
-#if Q_BYTE_ORDER == Q_BIG_ENDIAN
- QDataStream::ByteOrder bo = QDataStream::LittleEndian;
-#else
- QDataStream::ByteOrder bo = QDataStream::BigEndian;
-#endif
-
- // Test and verify that values that become (s)nan's after swapping endianness
- // don't change in the process.
- QByteArray ba;
-
- union {
- float f;
- quint32 i;
- } xs[2];
-
- xs[0].i = qbswap<quint32>(0xff800001);
- xs[1].i = qbswap<quint32>(0x7f800001);
-
- {
- QDataStream stream(&ba, QIODevice::WriteOnly);
- stream.setByteOrder(bo);
- stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
- stream << xs[0].f;
- stream << xs[1].f;
- }
-
- {
- QDataStream stream(ba);
- stream.setByteOrder(bo);
- stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
- float fr = 0.0f;
- stream >> fr;
- QCOMPARE(fr, xs[0].f);
- stream >> fr;
- QCOMPARE(fr, xs[1].f);
- }
-}
-
-void tst_QDataStream::floatingPointPrecision()
-{
- QByteArray ba;
- {
- QDataStream stream(&ba, QIODevice::WriteOnly);
- QCOMPARE(QDataStream::DoublePrecision, stream.floatingPointPrecision());
-
- float f = 123.0f;
- stream << f;
- QCOMPARE(ba.size(), int(sizeof(double)));
-
- double d = 234.0;
- stream << d;
- QCOMPARE(ba.size(), int(sizeof(double)*2));
-
- stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
-
- f = 123.0f;
- stream << f;
- QCOMPARE(ba.size(), int(sizeof(double)*2 + sizeof(float)));
-
- d = 234.0;
- stream << d;
- QCOMPARE(ba.size(), int(sizeof(double)*2 + sizeof(float)*2));
- }
-
- {
- QDataStream stream(ba);
-
- float f = 0.0f;
- stream >> f;
- QCOMPARE(123.0f, f);
-
- double d = 0.0;
- stream >> d;
- QCOMPARE(234.0, d);
-
- f = 0.0f;
- stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
- stream >> f;
- QCOMPARE(123.0f, f);
-
- d = 0.0;
- stream >> d;
- QCOMPARE(234.0, d);
- }
-
-}
-
QTEST_MAIN(tst_QDataStream)
#include "moc_tst_qdatastream.cpp"