add QScopedPointerDeleteLater, a custom deleter for QObjects

This is a custom deleter for QObjects that are participating in an event
loop (e.g. waiting for signals to complete a task), which need to be
deleted using deleteLater() rather than just delete.

Change-Id: I3084ea28a6829a299c7400006c617fc23cf15160
Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Matt Broadstone 2013-08-01 14:26:22 -04:00 committed by The Qt Project
parent b3a56454c3
commit ade0d8361c
2 changed files with 14 additions and 0 deletions

View File

@ -95,6 +95,9 @@ QT_BEGIN_NAMESPACE
this handler for pointers that were allocated with \c{new []}.
\li QScopedPointerPodDeleter - deletes the pointer using \c{free()}. Use this
handler for pointers that were allocated with \c{malloc()}.
\li QScopedPointerDeleteLater - deletes a pointer by calling \c{deleteLater()}
on it. Use this handler for pointers to QObject's that are actively
participating in a QEventLoop.
\endlist
You can pass your own classes as handlers, provided that they have a public

View File

@ -83,6 +83,17 @@ struct QScopedPointerPodDeleter
static inline void cleanup(void *pointer) { if (pointer) free(pointer); }
};
#ifndef QT_NO_QOBJECT
template <typename T>
struct QScopedPointerObjectDeleteLater
{
static inline void cleanup(T *pointer) { if (pointer) pointer->deleteLater(); }
};
class QObject;
typedef QScopedPointerObjectDeleteLater<QObject> QScopedPointerDeleteLater;
#endif
template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
class QScopedPointer
{