qDebug: add support for std::optional and std::nullopt_t

[ChangeLog][QtCore][QDebug] Added support for std::optional
and std::nullopt_t

Change-Id: I1e6196adb408401cae8776cd0c60af294a39a83f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
David Faure 2023-11-20 20:46:23 +01:00
parent dc7eb46352
commit b7657ddccb
3 changed files with 51 additions and 0 deletions

View File

@ -1018,6 +1018,15 @@ QDebug &QDebug::resetFormat()
\c T2 need to support streaming into QDebug.
*/
/*!
\since 6.7
\fn template <class T> QDebug operator<<(QDebug debug, const std::optional<T> &opt)
\relates QDebug
Writes the contents of \a opt (or \c nullopt if not set) to \a debug.
\c T needs to support streaming into QDebug.
*/
/*!
\fn template <typename T> QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)
\relates QDebug
@ -1050,6 +1059,13 @@ QDebug &QDebug::resetFormat()
\internal
*/
/*!
\since 6.7
\fn QDebug &QDebug::operator<<(std::nullopt_t)
Writes nullopt to the stream.
*/
/*!
\class QDebugStateSaver
\inmodule QtCore

View File

@ -126,6 +126,7 @@ public:
inline QDebug &operator<<(QByteArrayView t) { putByteArray(t.constData(), t.size(), ContainsBinary); return maybeSpace(); }
inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
inline QDebug &operator<<(std::nullptr_t) { stream->ts << "(nullptr)"; return maybeSpace(); }
inline QDebug &operator<<(std::nullopt_t) { stream->ts << "nullopt"; return maybeSpace(); }
inline QDebug &operator<<(QTextStreamFunction f) {
stream->ts << f;
return *this;
@ -361,6 +362,17 @@ inline QDebugIfHasDebugStreamContainer<QMultiHash<Key, T>, Key, T> operator<<(QD
return QtPrivate::printAssociativeContainer(debug, "QMultiHash", hash);
}
template <class T>
inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const std::optional<T> &opt)
{
const QDebugStateSaver saver(debug);
if (!opt)
debug.nospace() << std::nullopt;
else
debug.nospace() << "std::optional(" << *opt << ')';
return debug;
}
template <class T1, class T2>
inline QDebugIfHasDebugStream<T1, T2> operator<<(QDebug debug, const std::pair<T1, T2> &pair)
{

View File

@ -89,6 +89,7 @@ private slots:
void qDebugQFlags() const;
void qDebugStdChrono_data() const;
void qDebugStdChrono() const;
void qDebugStdOptional() const;
void textStreamModifiers() const;
void resetFormat() const;
void defaultMessagehandler() const;
@ -1215,6 +1216,28 @@ void tst_QDebug::qDebugStdChrono() const
QCOMPARE(fn(), expected);
}
void tst_QDebug::qDebugStdOptional() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
{
std::optional<QByteArray> notSet = std::nullopt;
std::optional<QByteArray> set("foo");
auto no = std::nullopt;
QDebug d = qDebug();
d << notSet << set << no;
}
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 4; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("nullopt std::optional(\"foo\") nullopt"));
QCOMPARE(QString::fromLatin1(s_file), file);
QCOMPARE(s_line, line);
QCOMPARE(QString::fromLatin1(s_function), function);
}
void tst_QDebug::textStreamModifiers() const
{
QString file, function;