Implement QDebug support for QSharedPointer

... otherwise the type goes through operator bool() and prints
"true" or "false" (!).

[ChangeLog][QtCore][QSharedPointer] Added support for debug
printing via QDebug.

Change-Id: Ic3ef9b9feee8d6ca08f1dd69f20f421fea20ca00
Reviewed-by: David Faure <david.faure@kdab.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Giuseppe D'Angelo 2015-09-02 16:22:41 +02:00
parent 4116fe873e
commit 9129d2714a
3 changed files with 24 additions and 0 deletions

View File

@ -1292,6 +1292,17 @@
compile. Use qSharedPointerConstCast to cast away the constness.
*/
/*!
\fn QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr)
\relates QSharedPointer
\since 5.7
Writes the pointer tracked by \a ptr into the debug object \a debug for
debugging purposes.
\sa {Debugging Techniques}
*/
#include <qset.h>
#include <qmutex.h>

View File

@ -153,6 +153,8 @@ template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QWe
template <class X, class T> QWeakPointer<X> qWeakPointerCast(const QWeakPointer<T> &src);
template <class T> QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr);
QT_END_NAMESPACE
#endif // Q_QDOC

View File

@ -55,6 +55,7 @@ QT_END_NAMESPACE
#include <new>
#include <QtCore/qatomic.h>
#include <QtCore/qobject.h> // for qobject_cast
#include <QtCore/qdebug.h>
#if QT_DEPRECATED_SINCE(5, 5)
#include <QtCore/qhash.h>
#endif
@ -858,6 +859,16 @@ inline void qSwap(QSharedPointer<T> &p1, QSharedPointer<T> &p2)
p1.swap(p2);
}
#ifndef QT_NO_DEBUG_STREAM
template <class T>
Q_INLINE_TEMPLATE QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr)
{
QDebugStateSaver saver(debug);
debug.nospace() << "QSharedPointer(" << ptr.data() << ")";
return debug;
}
#endif
QT_END_NAMESPACE
namespace std {
template <class T>