From 50e2bc2aa0f46562936a3137c15a2c9b4f67afde Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 16 Feb 2015 15:25:46 +0100 Subject: Fix QDebug cumulating operator<< with many Q_ENUM The problem is that the operator<< was taking a non-const reference to the QDebug object. This causes a problem as all other operator<< return a temporary. Since every other roperator<< takes the QDebug by value, we should also take it by value in this case. Move the operator<< in qdebug.h because i don't want to #include qdebug.h from qobject.h And move the qt_QMetaEnum_debugOperator to be in the corresponding .cpp Task-number: QTBUG-44462 Change-Id: Ia01629224c58930c2997e767efc43de90d6309e2 Reviewed-by: Friedemann Kleint --- src/corelib/io/qdebug.cpp | 20 ++++++++++++++++++++ src/corelib/io/qdebug.h | 13 +++++++++++++ 2 files changed, 33 insertions(+) (limited to 'src/corelib/io') diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index b0ee715c75..7b49505228 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -40,6 +40,7 @@ #endif #include "qdebug.h" +#include "qmetaobject.h" #include #include @@ -643,4 +644,23 @@ QDebugStateSaver::~QDebugStateSaver() d->restoreState(); } +#ifndef QT_NO_QOBJECT +/*! + \internal + */ +QDebug qt_QMetaEnum_debugOperator(QDebug &dbg, int value, const QMetaObject *meta, const char *name) +{ + QDebugStateSaver saver(dbg); + QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name)); + const char *key = me.valueToKey(value); + dbg.nospace() << meta->className() << "::" << name << '('; + if (key) + dbg << key; + else + dbg << value; + dbg << ')'; + return dbg; +} +#endif + QT_END_NAMESPACE diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 56a5e1fa74..435e7450c7 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -261,6 +261,19 @@ inline QDebug operator<<(QDebug debug, const QContiguousCache &cache) return debug.maybeSpace(); } +#ifndef QT_NO_QOBJECT +Q_CORE_EXPORT QDebug qt_QMetaEnum_debugOperator(QDebug&, int value, const QMetaObject *meta, const char *name); + +template +typename QtPrivate::QEnableIf::Value, QDebug>::Type +operator<<(QDebug dbg, T value) +{ + const QMetaObject *obj = qt_getEnumMetaObject(value); + const char *name = qt_getEnumName(value); + return qt_QMetaEnum_debugOperator(dbg, typename QFlags::Int(value), obj, name); +} +#endif + template inline QDebug operator<<(QDebug debug, const QFlags &flags) { -- cgit v1.2.3