qfloat16: add QTextStream & QDebug streaming operators

Change-Id: Ieba79baf5ac34264a988fffd172655bdcaf12a59
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
This commit is contained in:
Thiago Macieira 2022-11-10 13:16:14 -08:00
parent c53bf1b45e
commit 5838074912
5 changed files with 40 additions and 0 deletions

View File

@ -398,6 +398,19 @@ QDataStream &operator>>(QDataStream &ds, qfloat16 &f)
} }
#endif #endif
QTextStream &operator>>(QTextStream &ts, qfloat16 &f16)
{
float f;
ts >> f;
f16 = qfloat16(f);
return ts;
}
QTextStream &operator<<(QTextStream &ts, qfloat16 f)
{
return ts << float(f);
}
QT_END_NAMESPACE QT_END_NAMESPACE
#include "qfloat16tables.cpp" #include "qfloat16tables.cpp"

View File

@ -40,6 +40,7 @@ QT_BEGIN_NAMESPACE
#ifndef QT_NO_DATASTREAM #ifndef QT_NO_DATASTREAM
class QDataStream; class QDataStream;
#endif #endif
class QTextStream;
class qfloat16 class qfloat16
{ {
@ -223,6 +224,8 @@ QT_WARNING_POP
friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, qfloat16 f); friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, qfloat16 f);
friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &ds, qfloat16 &f); friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &ds, qfloat16 &f);
#endif #endif
friend Q_CORE_EXPORT QTextStream &operator<<(QTextStream &ts, qfloat16 f);
friend Q_CORE_EXPORT QTextStream &operator>>(QTextStream &ts, qfloat16 &f);
}; };
Q_DECLARE_TYPEINFO(qfloat16, Q_PRIMITIVE_TYPE); Q_DECLARE_TYPEINFO(qfloat16, Q_PRIMITIVE_TYPE);

View File

@ -108,6 +108,7 @@ public:
inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); }
inline QDebug &operator<<(qint64 t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(qint64 t) { stream->ts << t; return maybeSpace(); }
inline QDebug &operator<<(quint64 t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(quint64 t) { stream->ts << t; return maybeSpace(); }
inline QDebug &operator<<(qfloat16 t) { stream->ts << t; return maybeSpace(); }
inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
inline QDebug &operator<<(const char* t) { stream->ts << QString::fromUtf8(t); return maybeSpace(); } inline QDebug &operator<<(const char* t) { stream->ts << QString::fromUtf8(t); return maybeSpace(); }

View File

@ -4,6 +4,7 @@
#ifndef QTEXTSTREAM_H #ifndef QTEXTSTREAM_H
#define QTEXTSTREAM_H #define QTEXTSTREAM_H
#include <QtCore/qfloat16.h>
#include <QtCore/qiodevicebase.h> #include <QtCore/qiodevicebase.h>
#include <QtCore/qchar.h> #include <QtCore/qchar.h>
#include <QtCore/qscopedpointer.h> #include <QtCore/qscopedpointer.h>

View File

@ -4,6 +4,7 @@
#include <QTest> #include <QTest>
#include <QFloat16> #include <QFloat16>
#include <QTextStream>
#include <math.h> #include <math.h>
@ -42,6 +43,7 @@ private slots:
void limits(); void limits();
void mantissaOverflow(); void mantissaOverflow();
void dataStream(); void dataStream();
void textStream();
}; };
void tst_qfloat16::fuzzyCompare_data() void tst_qfloat16::fuzzyCompare_data()
@ -656,5 +658,25 @@ void tst_qfloat16::dataStream()
QCOMPARE(zero, qfloat16(0)); QCOMPARE(zero, qfloat16(0));
} }
void tst_qfloat16::textStream()
{
QString buffer;
{
QTextStream ts(&buffer);
ts << qfloat16(0) << Qt::endl << qfloat16(1.5);
QCOMPARE(ts.status(), QTextStream::Ok);
}
QCOMPARE(buffer, "0\n1.5");
{
QTextStream ts(&buffer);
qfloat16 zero = qfloat16(-2.5), threehalves = 1234;
ts >> zero >> threehalves;
QCOMPARE(ts.status(), QTextStream::Ok);
QCOMPARE(zero, qfloat16(0));
QCOMPARE(threehalves, 1.5);
}
}
QTEST_APPLESS_MAIN(tst_qfloat16) QTEST_APPLESS_MAIN(tst_qfloat16)
#include "tst_qfloat16.moc" #include "tst_qfloat16.moc"