summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/io/qdebug.cpp22
-rw-r--r--src/corelib/io/qdebug.h11
2 files changed, 33 insertions, 0 deletions
diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp
index 600b8b13b80..1c6aecaaabb 100644
--- a/src/corelib/io/qdebug.cpp
+++ b/src/corelib/io/qdebug.cpp
@@ -1000,6 +1000,8 @@ QDebug &QDebug::resetFormat()
\since 6.0
\include qdebug-toString.qdocinc
+
+ \sa toBytes()
*/
/*! \internal */
@@ -1014,6 +1016,26 @@ QString QDebug::toStringImpl(StreamTypeErased s, const void *obj)
}
/*!
+ \fn template <class T> QByteArray QDebug::toBytes(const T &object)
+ \since 6.9
+
+ This is equivalent to \c{QDebug::toString(object).toUtf8()}, but more efficient.
+
+ \sa toString()
+*/
+
+/*! \internal */
+QByteArray QDebug::toBytesImpl(StreamTypeErased s, const void *obj)
+{
+ QByteArray result;
+ {
+ QDebug d(&result);
+ s(d.nospace(), obj);
+ }
+ return result;
+}
+
+/*!
\fn template <class T> QDebug operator<<(QDebug debug, const QList<T> &list)
\relates QDebug
diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h
index fe3c66180c3..ab1f53afe77 100644
--- a/src/corelib/io/qdebug.h
+++ b/src/corelib/io/qdebug.h
@@ -51,6 +51,9 @@ class QT6_ONLY(Q_CORE_EXPORT) QDebug : public QIODeviceBase
explicit Stream(QString *string)
: ts(string, WriteOnly)
{}
+ explicit Stream(QByteArray *ba)
+ : ts(ba, WriteOnly)
+ {}
explicit Stream(QtMsgType t)
: ts(&buffer, WriteOnly),
type(t),
@@ -78,6 +81,7 @@ class QT6_ONLY(Q_CORE_EXPORT) QDebug : public QIODeviceBase
public:
explicit QDebug(QIODevice *device) : stream(new Stream(device)) {}
explicit QDebug(QString *string) : stream(new Stream(string)) {}
+ explicit QDebug(QByteArray *bytes) : stream(new Stream(bytes)) {}
explicit QDebug(QtMsgType t) : stream(new Stream(t)) {}
QDebug(const QDebug &o) : stream(o.stream) { ++stream->ref; }
QDebug(QDebug &&other) noexcept : stream{std::exchange(other.stream, nullptr)} {}
@@ -232,12 +236,19 @@ private:
}
using StreamTypeErased = void(*)(QDebug&, const void*);
QT7_ONLY(Q_CORE_EXPORT) static QString toStringImpl(StreamTypeErased s, const void *obj);
+ QT7_ONLY(Q_CORE_EXPORT) static QByteArray toBytesImpl(StreamTypeErased s, const void *obj);
public:
template <typename T>
static QString toString(const T &object)
{
return toStringImpl(&streamTypeErased<T>, &object);
}
+
+ template <typename T>
+ static QByteArray toBytes(const T &object)
+ {
+ return toBytesImpl(&streamTypeErased<T>, &object);
+ }
};
Q_DECLARE_SHARED(QDebug)