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:
parent
fbef250c6c
commit
853f7b661f
@ -1002,6 +1002,17 @@ QDebug &QDebug::resetFormat()
|
||||
\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)
|
||||
\relates QDebug
|
||||
|
@ -223,13 +223,19 @@ public:
|
||||
QDebug &operator<<(T u128) { putUInt128(&u128); return maybeSpace(); }
|
||||
#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>
|
||||
static QString toString(const T &object)
|
||||
{
|
||||
QString buffer;
|
||||
QDebug stream(&buffer);
|
||||
stream.nospace() << object;
|
||||
return buffer;
|
||||
return toStringImpl(&streamTypeErased<T>, &object);
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user