QDebug: make toString() SCARY

Extract the non-template-dependent code into an out-of-line function
and pass the actual streaming operation as a callback.

Saves 4% (174811→167936) in executable size for tst_qdebug and 2.7%
(93639→91122) for tst_tostring on Linux GCC 9 AMD64 release builds.

Change-Id: If232e5b26c66981ffcb614f1bdb7007c71e879bf
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 5cdd1f594d26e1d4f84b00741be1ab7231458512)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-07-18 21:35:54 +02:00 committed by Qt Cherry-pick Bot
parent fbef250c6c
commit 853f7b661f
2 changed files with 21 additions and 4 deletions

View File

@ -1002,6 +1002,17 @@ QDebug &QDebug::resetFormat()
\include qdebug-toString.qdocinc \include qdebug-toString.qdocinc
*/ */
/*! \internal */
QString QDebug::toStringImpl(StreamTypeErased s, const void *obj)
{
QString result;
{
QDebug d(&result);
s(d.nospace(), obj);
}
return result;
}
/*! /*!
\fn template <class T> QDebug operator<<(QDebug debug, const QList<T> &list) \fn template <class T> QDebug operator<<(QDebug debug, const QList<T> &list)
\relates QDebug \relates QDebug

View File

@ -223,13 +223,19 @@ public:
QDebug &operator<<(T u128) { putUInt128(&u128); return maybeSpace(); } QDebug &operator<<(T u128) { putUInt128(&u128); return maybeSpace(); }
#endif // QT_SUPPORTS_INT128 #endif // QT_SUPPORTS_INT128
private:
template <typename T>
static void streamTypeErased(QDebug &d, const void *obj)
{
d << *static_cast<const T*>(obj);
}
using StreamTypeErased = void(*)(QDebug&, const void*);
QT7_ONLY(Q_CORE_EXPORT) static QString toStringImpl(StreamTypeErased s, const void *obj);
public:
template <typename T> template <typename T>
static QString toString(const T &object) static QString toString(const T &object)
{ {
QString buffer; return toStringImpl(&streamTypeErased<T>, &object);
QDebug stream(&buffer);
stream.nospace() << object;
return buffer;
} }
}; };