Output registered enums in qDebug operator for QFlags.

Change-Id: Ia424df234fbd333782c10f0a4422875bb1bfc1f5
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Friedemann Kleint 2015-02-17 11:25:36 +01:00
parent 9608629d7e
commit cd46d94906
3 changed files with 36 additions and 2 deletions

View File

@ -661,6 +661,20 @@ QDebug qt_QMetaEnum_debugOperator(QDebug &dbg, int value, const QMetaObject *met
dbg << ')';
return dbg;
}
#endif
QDebug qt_QMetaEnum_flagDebugOperator(QDebug &debug, quint64 value, const QMetaObject *meta, const char *name)
{
QDebugStateSaver saver(debug);
debug.resetFormat();
debug.noquote();
debug.nospace();
debug << "QFlags<";
const QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
if (const char *scope = me.scope())
debug << scope << "::";
debug << me.name() << ">(" << me.valueToKeys(value) << ')';
return debug;
}
#endif // !QT_NO_QOBJECT
QT_END_NAMESPACE

View File

@ -263,6 +263,7 @@ inline QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)
#ifndef QT_NO_QOBJECT
Q_CORE_EXPORT QDebug qt_QMetaEnum_debugOperator(QDebug&, int value, const QMetaObject *meta, const char *name);
Q_CORE_EXPORT QDebug qt_QMetaEnum_flagDebugOperator(QDebug &dbg, quint64 value, const QMetaObject *meta, const char *name);
template<typename T>
typename QtPrivate::QEnableIf<QtPrivate::IsQEnumHelper<T>::Value, QDebug>::Type
@ -272,10 +273,21 @@ operator<<(QDebug dbg, T value)
const char *name = qt_getEnumName(value);
return qt_QMetaEnum_debugOperator(dbg, typename QFlags<T>::Int(value), obj, name);
}
#endif
template <class T>
inline typename QtPrivate::QEnableIf<QtPrivate::IsQEnumHelper<T>::Value, QDebug>::Type operator<<(QDebug debug, const QFlags<T> &flags)
{
const QMetaObject *obj = qt_getEnumMetaObject(T());
const char *name = qt_getEnumName(T());
return qt_QMetaEnum_flagDebugOperator(debug, quint64(flags), obj, name);
}
template <class T>
inline typename QtPrivate::QEnableIf<!QtPrivate::IsQEnumHelper<T>::Value, QDebug>::Type operator<<(QDebug debug, const QFlags<T> &flags)
#else // !QT_NO_QOBJECT
template <class T>
inline QDebug operator<<(QDebug debug, const QFlags<T> &flags)
#endif
{
QDebugStateSaver saver(debug);
debug.resetFormat();

View File

@ -1420,6 +1420,14 @@ void tst_QMetaObject::enumDebugStream()
QTest::ignoreMessage(QtDebugMsg, "Qt::WindowType(WindowTitleHint) Qt::WindowType(Window) Qt::WindowType(Desktop) Qt::WindowType(WindowSystemMenuHint)");
qDebug() << Qt::WindowTitleHint << Qt::Window <<Qt::Desktop << Qt::WindowSystemMenuHint;
QTest::ignoreMessage(QtDebugMsg, "hello QFlags<MyNamespace::MyClass::MyFlags>(MyFlag1) world");
MyNamespace::MyClass::MyFlags f1 = MyNamespace::MyClass::MyFlag1;
qDebug() << "hello" << f1 << "world";
MyNamespace::MyClass::MyFlags f2 = MyNamespace::MyClass::MyFlag2 | MyNamespace::MyClass::MyFlag3;
QTest::ignoreMessage(QtDebugMsg, "QFlags<MyNamespace::MyClass::MyFlags>(MyFlag1) QFlags<MyNamespace::MyClass::MyFlags>(MyFlag2|MyFlag3)");
qDebug() << f1 << f2;
}
QTEST_MAIN(tst_QMetaObject)