Clean up QTextStream functions

They are the only Qt symbols neither prefixed with q or with the Qt
namespace. This changes preserves source and binary compatibility
while making it possible to define conflicting symbols.

Change-Id: I6d4181206e63faa922fa0c8b644e0a4b88826a97
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Allan Sandfeld Jensen 2018-10-24 16:49:45 +02:00
parent 1e70781e7f
commit 461e89ee1a
3 changed files with 143 additions and 69 deletions

View File

@ -135,30 +135,30 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384;
\table
\header \li Manipulator \li Description
\row \li \c bin \li Same as setIntegerBase(2).
\row \li \c oct \li Same as setIntegerBase(8).
\row \li \c dec \li Same as setIntegerBase(10).
\row \li \c hex \li Same as setIntegerBase(16).
\row \li \c showbase \li Same as setNumberFlags(numberFlags() | ShowBase).
\row \li \c forcesign \li Same as setNumberFlags(numberFlags() | ForceSign).
\row \li \c forcepoint \li Same as setNumberFlags(numberFlags() | ForcePoint).
\row \li \c noshowbase \li Same as setNumberFlags(numberFlags() & ~ShowBase).
\row \li \c noforcesign \li Same as setNumberFlags(numberFlags() & ~ForceSign).
\row \li \c noforcepoint \li Same as setNumberFlags(numberFlags() & ~ForcePoint).
\row \li \c uppercasebase \li Same as setNumberFlags(numberFlags() | UppercaseBase).
\row \li \c uppercasedigits \li Same as setNumberFlags(numberFlags() | UppercaseDigits).
\row \li \c lowercasebase \li Same as setNumberFlags(numberFlags() & ~UppercaseBase).
\row \li \c lowercasedigits \li Same as setNumberFlags(numberFlags() & ~UppercaseDigits).
\row \li \c fixed \li Same as setRealNumberNotation(FixedNotation).
\row \li \c scientific \li Same as setRealNumberNotation(ScientificNotation).
\row \li \c left \li Same as setFieldAlignment(AlignLeft).
\row \li \c right \li Same as setFieldAlignment(AlignRight).
\row \li \c center \li Same as setFieldAlignment(AlignCenter).
\row \li \c endl \li Same as operator<<('\\n') and flush().
\row \li \c flush \li Same as flush().
\row \li \c reset \li Same as reset().
\row \li \c ws \li Same as skipWhiteSpace().
\row \li \c bom \li Same as setGenerateByteOrderMark(true).
\row \li \c Qt::bin \li Same as setIntegerBase(2).
\row \li \c Qt::oct \li Same as setIntegerBase(8).
\row \li \c Qt::dec \li Same as setIntegerBase(10).
\row \li \c Qt::hex \li Same as setIntegerBase(16).
\row \li \c Qt::showbase \li Same as setNumberFlags(numberFlags() | ShowBase).
\row \li \c Qt::forcesign \li Same as setNumberFlags(numberFlags() | ForceSign).
\row \li \c Qt::forcepoint \li Same as setNumberFlags(numberFlags() | ForcePoint).
\row \li \c Qt::noshowbase \li Same as setNumberFlags(numberFlags() & ~ShowBase).
\row \li \c Qt::noforcesign \li Same as setNumberFlags(numberFlags() & ~ForceSign).
\row \li \c Qt::noforcepoint \li Same as setNumberFlags(numberFlags() & ~ForcePoint).
\row \li \c Qt::uppercasebase \li Same as setNumberFlags(numberFlags() | UppercaseBase).
\row \li \c Qt::uppercasedigits \li Same as setNumberFlags(numberFlags() | UppercaseDigits).
\row \li \c Qt::lowercasebase \li Same as setNumberFlags(numberFlags() & ~UppercaseBase).
\row \li \c Qt::lowercasedigits \li Same as setNumberFlags(numberFlags() & ~UppercaseDigits).
\row \li \c Qt::fixed \li Same as setRealNumberNotation(FixedNotation).
\row \li \c Qt::scientific \li Same as setRealNumberNotation(ScientificNotation).
\row \li \c Qt::left \li Same as setFieldAlignment(AlignLeft).
\row \li \c Qt::right \li Same as setFieldAlignment(AlignRight).
\row \li \c Qt::center \li Same as setFieldAlignment(AlignCenter).
\row \li \c Qt::endl \li Same as operator<<('\\n') and flush().
\row \li \c Qt::flush \li Same as flush().
\row \li \c Qt::reset \li Same as reset().
\row \li \c Qt::ws \li Same as skipWhiteSpace().
\row \li \c Qt::bom \li Same as setGenerateByteOrderMark(true).
\endtable
In addition, Qt provides three global manipulators that take a
@ -2689,6 +2689,11 @@ QTextStream &QTextStream::operator<<(const void *ptr)
d->params.numberFlags = oldFlags;
return *this;
}
#if defined(Q_QDOC) || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
namespace Qt {
#else
namespace QTextStreamFunctions {
#endif
/*!
\relates QTextStream
@ -3015,6 +3020,8 @@ QTextStream &ws(QTextStream &stream)
return stream;
}
} // namespace QTextStreamFunctions
/*!
\fn QTextStreamManipulator qSetFieldWidth(int width)
\relates QTextStream
@ -3037,6 +3044,12 @@ QTextStream &ws(QTextStream &stream)
*/
#if QT_CONFIG(textcodec)
#if defined(Q_QDOC) || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
namespace Qt {
#else
namespace QTextStreamFunctions {
#endif
/*!
\relates QTextStream
@ -3051,6 +3064,8 @@ QTextStream &bom(QTextStream &stream)
return stream;
}
} // namespace QTextStreamFunctions
/*!
Sets the codec for this stream to \a codec. The codec is used for
decoding any data that is read from the assigned device, and for
@ -3200,6 +3215,43 @@ QLocale QTextStream::locale() const
return d->locale;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC)
// Binary compatible definitions for Qt<5.14
Q_CORE_EXPORT QTextStream &bin(QTextStream &s) { return Qt::bin(s); }
Q_CORE_EXPORT QTextStream &oct(QTextStream &s) { return Qt::oct(s); }
Q_CORE_EXPORT QTextStream &dec(QTextStream &s) { return Qt::dec(s); }
Q_CORE_EXPORT QTextStream &hex(QTextStream &s) { return Qt::hex(s); }
Q_CORE_EXPORT QTextStream &showbase(QTextStream &s) { return Qt::showbase(s); }
Q_CORE_EXPORT QTextStream &forcesign(QTextStream &s) { return Qt::forcesign(s); }
Q_CORE_EXPORT QTextStream &forcepoint(QTextStream &s) { return Qt::forcepoint(s); }
Q_CORE_EXPORT QTextStream &noshowbase(QTextStream &s) { return Qt::noshowbase(s); }
Q_CORE_EXPORT QTextStream &noforcesign(QTextStream &s) { return Qt::noforcesign(s); }
Q_CORE_EXPORT QTextStream &noforcepoint(QTextStream &s) { return Qt::noforcepoint(s); }
Q_CORE_EXPORT QTextStream &uppercasebase(QTextStream &s) { return Qt::uppercasebase(s); }
Q_CORE_EXPORT QTextStream &uppercasedigits(QTextStream &s) { return Qt::uppercasedigits(s); }
Q_CORE_EXPORT QTextStream &lowercasebase(QTextStream &s) { return Qt::lowercasebase(s); }
Q_CORE_EXPORT QTextStream &lowercasedigits(QTextStream &s) { return Qt::lowercasedigits(s); }
Q_CORE_EXPORT QTextStream &fixed(QTextStream &s) { return Qt::fixed(s); }
Q_CORE_EXPORT QTextStream &scientific(QTextStream &s) { return Qt::scientific(s); }
Q_CORE_EXPORT QTextStream &left(QTextStream &s) { return Qt::left(s); }
Q_CORE_EXPORT QTextStream &right(QTextStream &s) { return Qt::right(s); }
Q_CORE_EXPORT QTextStream &center(QTextStream &s) { return Qt::center(s); }
Q_CORE_EXPORT QTextStream &endl(QTextStream &s) { return Qt::endl(s); }
Q_CORE_EXPORT QTextStream &flush(QTextStream &s) { return Qt::flush(s); }
Q_CORE_EXPORT QTextStream &reset(QTextStream &s) { return Qt::reset(s); }
Q_CORE_EXPORT QTextStream &ws(QTextStream &s) { return Qt::ws(s); }
#if QT_CONFIG(textcodec)
Q_CORE_EXPORT QTextStream &bom(QTextStream &s) { return Qt::bom(s); }
#endif
#endif
QT_END_NAMESPACE
#ifndef QT_NO_QOBJECT

View File

@ -233,6 +233,13 @@ inline QTextStream &operator<<(QTextStream &s, QTextStreamFunction f)
inline QTextStream &operator<<(QTextStream &s, QTextStreamManipulator m)
{ m.exec(s); return s; }
#if defined(Q_QDOC) || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
namespace Qt {
#else
// This namespace only exists for 'using namespace' declarations.
namespace QTextStreamFunctions {
#endif
Q_CORE_EXPORT QTextStream &bin(QTextStream &s);
Q_CORE_EXPORT QTextStream &oct(QTextStream &s);
Q_CORE_EXPORT QTextStream &dec(QTextStream &s);
@ -265,6 +272,18 @@ Q_CORE_EXPORT QTextStream &bom(QTextStream &s);
Q_CORE_EXPORT QTextStream &ws(QTextStream &s);
} // namespace QTextStreamFunctions
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC)
namespace Qt {
using namespace QTextStreamFunctions;
}
// We use 'using namespace' as that doesn't cause
// conflicting definitions compiler errors.
using namespace QTextStreamFunctions;
#endif // QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC)
inline QTextStreamManipulator qSetFieldWidth(int width)
{
QTSMFI func = &QTextStream::setFieldWidth;

View File

@ -1102,7 +1102,7 @@ void tst_QTextStream::hexTest()
QByteArray array;
QTextStream stream(&array);
stream << showbase << hex << number;
stream << Qt::showbase << Qt::hex << number;
stream.flush();
QCOMPARE(array, data);
}
@ -1132,7 +1132,7 @@ void tst_QTextStream::binTest()
QByteArray array;
QTextStream stream(&array);
stream << showbase << bin << number;
stream << Qt::showbase << Qt::bin << number;
stream.flush();
QCOMPARE(array.constData(), data.constData());
}
@ -1155,7 +1155,7 @@ void tst_QTextStream::octTest()
QByteArray array;
QTextStream stream(&array);
stream << showbase << oct << number;
stream << Qt::showbase << Qt::oct << number;
stream.flush();
QCOMPARE(array, data);
}
@ -1196,7 +1196,7 @@ void tst_QTextStream::ws_manipulator()
QTextStream stream(&string);
char a, b, c, d;
stream >> a >> ws >> b >> ws >> c >> ws >> d;
stream >> a >> Qt::ws >> b >> Qt::ws >> c >> Qt::ws >> d;
QCOMPARE(a, 'a');
QCOMPARE(b, 'b');
QCOMPARE(c, 'c');
@ -1623,18 +1623,18 @@ void tst_QTextStream::forcePoint()
{
QString str;
QTextStream stream(&str);
stream << fixed << forcepoint << 1.0 << ' ' << 1 << ' ' << 0 << ' ' << -1.0 << ' ' << -1;
stream << Qt::fixed << Qt::forcepoint << 1.0 << ' ' << 1 << ' ' << 0 << ' ' << -1.0 << ' ' << -1;
QCOMPARE(str, QString("1.000000 1 0 -1.000000 -1"));
str.clear();
stream.seek(0);
stream << scientific << forcepoint << 1.0 << ' ' << 1 << ' ' << 0 << ' ' << -1.0 << ' ' << -1;
stream << Qt::scientific << Qt::forcepoint << 1.0 << ' ' << 1 << ' ' << 0 << ' ' << -1.0 << ' ' << -1;
QCOMPARE(str, QString("1.000000e+00 1 0 -1.000000e+00 -1"));
str.clear();
stream.seek(0);
stream.setRealNumberNotation(QTextStream::SmartNotation);
stream << forcepoint << 1.0 << ' ' << 1 << ' ' << 0 << ' ' << -1.0 << ' ' << -1;
stream << Qt::forcepoint << 1.0 << ' ' << 1 << ' ' << 0 << ' ' << -1.0 << ' ' << -1;
QCOMPARE(str, QString("1.00000 1 0 -1.00000 -1"));
}
@ -1644,7 +1644,7 @@ void tst_QTextStream::forceSign()
{
QString str;
QTextStream stream(&str);
stream << forcesign << 1.2 << ' ' << -1.2 << ' ' << 0;
stream << Qt::forcesign << 1.2 << ' ' << -1.2 << ' ' << 0;
QCOMPARE(str, QString("+1.2 -1.2 +0"));
}
@ -1663,19 +1663,22 @@ void tst_QTextStream::read0d0d0a()
Q_DECLARE_METATYPE(QTextStreamFunction);
// Also tests that we can have namespaces that conflict with our QTextStream constants.
namespace ws {
QTextStream &noop(QTextStream &s) { return s; }
}
void tst_QTextStream::numeralCase_data()
{
QTextStreamFunction noop_ = noop;
QTextStreamFunction bin_ = bin;
QTextStreamFunction oct_ = oct;
QTextStreamFunction hex_ = hex;
QTextStreamFunction base = showbase;
QTextStreamFunction ucb = uppercasebase;
QTextStreamFunction lcb = lowercasebase;
QTextStreamFunction ucd = uppercasedigits;
QTextStreamFunction lcd = lowercasedigits;
QTextStreamFunction noop_ = ws::noop;
QTextStreamFunction bin = Qt::bin;
QTextStreamFunction oct = Qt::oct;
QTextStreamFunction hex = Qt::hex;
QTextStreamFunction base = Qt::showbase;
QTextStreamFunction ucb = Qt::uppercasebase;
QTextStreamFunction lcb = Qt::lowercasebase;
QTextStreamFunction ucd = Qt::uppercasedigits;
QTextStreamFunction lcd = Qt::lowercasedigits;
QTest::addColumn<QTextStreamFunction>("func1");
QTest::addColumn<QTextStreamFunction>("func2");
@ -1686,30 +1689,30 @@ void tst_QTextStream::numeralCase_data()
QTest::newRow("dec 1") << noop_ << noop_ << noop_ << noop_ << 31 << "31";
QTest::newRow("dec 2") << noop_ << base << noop_ << noop_ << 31 << "31";
QTest::newRow("hex 1") << hex_ << noop_ << noop_ << noop_ << 31 << "1f";
QTest::newRow("hex 2") << hex_ << noop_ << noop_ << lcd << 31 << "1f";
QTest::newRow("hex 3") << hex_ << noop_ << ucb << noop_ << 31 << "1f";
QTest::newRow("hex 4") << hex_ << noop_ << noop_ << ucd << 31 << "1F";
QTest::newRow("hex 5") << hex_ << noop_ << lcb << ucd << 31 << "1F";
QTest::newRow("hex 6") << hex_ << noop_ << ucb << ucd << 31 << "1F";
QTest::newRow("hex 7") << hex_ << base << noop_ << noop_ << 31 << "0x1f";
QTest::newRow("hex 8") << hex_ << base << lcb << lcd << 31 << "0x1f";
QTest::newRow("hex 9") << hex_ << base << ucb << noop_ << 31 << "0X1f";
QTest::newRow("hex 10") << hex_ << base << ucb << lcd << 31 << "0X1f";
QTest::newRow("hex 11") << hex_ << base << noop_ << ucd << 31 << "0x1F";
QTest::newRow("hex 12") << hex_ << base << lcb << ucd << 31 << "0x1F";
QTest::newRow("hex 13") << hex_ << base << ucb << ucd << 31 << "0X1F";
QTest::newRow("hex 1") << hex << noop_ << noop_ << noop_ << 31 << "1f";
QTest::newRow("hex 2") << hex << noop_ << noop_ << lcd << 31 << "1f";
QTest::newRow("hex 3") << hex << noop_ << ucb << noop_ << 31 << "1f";
QTest::newRow("hex 4") << hex << noop_ << noop_ << ucd << 31 << "1F";
QTest::newRow("hex 5") << hex << noop_ << lcb << ucd << 31 << "1F";
QTest::newRow("hex 6") << hex << noop_ << ucb << ucd << 31 << "1F";
QTest::newRow("hex 7") << hex << base << noop_ << noop_ << 31 << "0x1f";
QTest::newRow("hex 8") << hex << base << lcb << lcd << 31 << "0x1f";
QTest::newRow("hex 9") << hex << base << ucb << noop_ << 31 << "0X1f";
QTest::newRow("hex 10") << hex << base << ucb << lcd << 31 << "0X1f";
QTest::newRow("hex 11") << hex << base << noop_ << ucd << 31 << "0x1F";
QTest::newRow("hex 12") << hex << base << lcb << ucd << 31 << "0x1F";
QTest::newRow("hex 13") << hex << base << ucb << ucd << 31 << "0X1F";
QTest::newRow("bin 1") << bin_ << noop_ << noop_ << noop_ << 31 << "11111";
QTest::newRow("bin 2") << bin_ << base << noop_ << noop_ << 31 << "0b11111";
QTest::newRow("bin 3") << bin_ << base << lcb << noop_ << 31 << "0b11111";
QTest::newRow("bin 4") << bin_ << base << ucb << noop_ << 31 << "0B11111";
QTest::newRow("bin 5") << bin_ << base << noop_ << ucd << 31 << "0b11111";
QTest::newRow("bin 6") << bin_ << base << lcb << ucd << 31 << "0b11111";
QTest::newRow("bin 7") << bin_ << base << ucb << ucd << 31 << "0B11111";
QTest::newRow("bin 1") << bin << noop_ << noop_ << noop_ << 31 << "11111";
QTest::newRow("bin 2") << bin << base << noop_ << noop_ << 31 << "0b11111";
QTest::newRow("bin 3") << bin << base << lcb << noop_ << 31 << "0b11111";
QTest::newRow("bin 4") << bin << base << ucb << noop_ << 31 << "0B11111";
QTest::newRow("bin 5") << bin << base << noop_ << ucd << 31 << "0b11111";
QTest::newRow("bin 6") << bin << base << lcb << ucd << 31 << "0b11111";
QTest::newRow("bin 7") << bin << base << ucb << ucd << 31 << "0B11111";
QTest::newRow("oct 1") << oct_ << noop_ << noop_ << noop_ << 31 << "37";
QTest::newRow("oct 2") << oct_ << base << noop_ << noop_ << 31 << "037";
QTest::newRow("oct 1") << oct << noop_ << noop_ << noop_ << 31 << "37";
QTest::newRow("oct 2") << oct << base << noop_ << noop_ << 31 << "037";
}
void tst_QTextStream::numeralCase()
@ -1782,9 +1785,9 @@ void tst_QTextStream::nanInf()
QString s;
QTextStream out(&s);
out << qInf() << ' ' << -qInf() << ' ' << qQNaN()
<< uppercasedigits << ' '
<< Qt::uppercasedigits << ' '
<< qInf() << ' ' << -qInf() << ' ' << qQNaN()
<< flush;
<< Qt::flush;
QCOMPARE(s, QString("inf -inf nan INF -INF NAN"));
}
@ -1859,9 +1862,9 @@ void tst_QTextStream::writeSeekWriteNoBOM()
int number = 0;
QString sizeStr = QLatin1String("Size=")
+ QString::number(number).rightJustified(10, QLatin1Char('0'));
stream << sizeStr << endl;
stream << "Version=" << QString::number(14) << endl;
stream << "blah blah blah" << endl;
stream << sizeStr << Qt::endl;
stream << "Version=" << QString::number(14) << Qt::endl;
stream << "blah blah blah" << Qt::endl;
stream.flush();
QCOMPARE(out.buffer().constData(), "Size=0000000000\nVersion=14\nblah blah blah\n");
@ -1871,7 +1874,7 @@ void tst_QTextStream::writeSeekWriteNoBOM()
stream.seek(0);
sizeStr = QLatin1String("Size=")
+ QString::number(number).rightJustified(10, QLatin1Char('0'));
stream << sizeStr << endl;
stream << sizeStr << Qt::endl;
stream.flush();
// Check buffer is still OK