21#ifndef QOBJECTUNIQUEPTR_H
22#define QOBJECTUNIQUEPTR_H
46 Q_STATIC_ASSERT_X( !std::is_pointer<T>::value,
"QObjectUniquePtr's template type must not be a pointer type" );
48 template<
typename U>
struct TypeSelector
52 template<
typename U>
struct TypeSelector<const U>
54 typedef const QObject Type;
56 typedef typename TypeSelector<T>::Type QObjectType;
57 QPointer<QObjectType> mPtr;
105 mPtr.assign(
static_cast<QObjectType *
>( p ) );
112 inline T *
data()
const {
return static_cast<T *
>( mPtr.data() ); }
117 inline T *
get()
const {
return static_cast<T *
>( mPtr.data() ); }
132 inline operator T *()
const {
return data(); }
137 inline bool isNull()
const {
return mPtr.isNull(); }
144 explicit inline operator bool()
const {
return !mPtr.isNull(); }
149 inline void clear() { mPtr.clear(); }
157 T *p = qobject_cast<T *>( mPtr.data() );
177 return o == p.operator->();
182 return p.operator->() == o;
187 return o == p.operator->();
192 return p.operator->() == o;
197 return p1.operator->() == p2.operator->();
202 return o != p.operator->();
207 return p.operator->() != o;
212 return o != p.operator->();
217 return p.operator->() != o;
222 return p1.operator->() != p2.operator->();
227 return QObjectUniquePtr<T>( qobject_cast<T *>( QtSharedPointer::weakPointerFromVariant_internal( variant ).toStrongRef().data() ) );
244 Q_STATIC_ASSERT_X( !std::is_pointer<T>::value,
"QObjectParentUniquePtr's template object type must not be a pointer type" );
248 QPointer<QObject> mParent;
249 QMetaObject::Connection mParentDestroyedConnection;
277 QObject::disconnect( mParentDestroyedConnection );
295 QObject::disconnect( mParentDestroyedConnection );
297 mParentDestroyedConnection = QObject::connect( parent, &QObject::destroyed, parent, [
this]() {
312 if ( child && !mParent )
314 qWarning() <<
"Assigning pointer to QObjectParentUniquePtr with nullptr parent.";
324 inline T *
data()
const {
return static_cast<T *
>( mChild ); }
329 inline T *
get()
const {
return static_cast<T *
>( mChild ); }
346 inline operator T *()
const {
return data(); }
351 inline bool isNull()
const {
return !mChild; }
359 explicit inline operator bool()
const {
return static_cast< bool >( mChild ); }
364 inline void clear() { mChild =
nullptr; }
374 T *p = qobject_cast<T *>( mChild );
391 qWarning() <<
"Assigning pointer to QObjectParentUniquePtr with nullptr parent.";
400 return o == p.operator->();
405 return p.operator->() == o;
410 return o == p.operator->();
415 return p.operator->() == o;
420 return p1.operator->() == p2.operator->();
425 return o != p.operator->();
430 return p.operator->() != o;
435 return o != p.operator->();
440 return p.operator->() != o;
445 return p1.operator->() != p2.operator->();
Keeps a pointer to an object owned by a QObject parent, and deletes it whenever this parent object is...
T * data() const
Returns the raw pointer to the managed QObject.
T * operator->() const
Returns a raw pointer to the managed child.
T & operator*() const
Dereferences the managed child.
QObjectParentUniquePtr< T > & operator=(T *child)
Assigns a new child to the pointer.
void setParentOwner(QObject *parent)
Sets the parent object.
void reset(T *p=nullptr)
Will reset the managed pointer to p.
QObjectParentUniquePtr()
Creates a new empty QObjectParentUniquePtr.
T * get() const
Returns the raw pointer to the managed child.
void clear()
Clears the pointer.
bool isNull() const
Checks if the managed pointer is nullptr.
T * release()
Clears the pointer and returns it.
~QObjectParentUniquePtr()
Will delete the contained object if the parent still exists.
QObjectParentUniquePtr(T *child, QObject *parent)
Takes a new QObjectParentUniquePtr and assign a child to it.
Keeps a pointer to a QObject and deletes it whenever this object is deleted.
QObjectUniquePtr< T > & operator=(T *p)
void reset(T *p=nullptr)
Will reset the managed pointer to p.
bool isNull() const
Checks if the managed pointer is nullptr.
QObjectUniquePtr(const QObjectUniquePtr &other)=delete
T & operator*() const
Dereferences the managed QObject.
QObjectUniquePtr()
Creates a new empty QObjectUniquePtr.
~QObjectUniquePtr()
Will delete the contained QObject if it still exists.
QObjectUniquePtr(QObjectUniquePtr &&other)
QObjectUniquePtr(T *p)
Takes a new QObjectUniquePtr and assigned p to it.
QObjectUniquePtr & operator=(const QObjectUniquePtr &other)=delete
T * release()
Clears the pointer and returns it.
T * get() const
Returns the raw pointer to the managed QObject.
void swap(QObjectUniquePtr &other)
Swaps the pointer managed by this instance with the pointer managed by other.
QObjectUniquePtr & operator=(QObjectUniquePtr &&other) noexcept
T * data() const
Returns the raw pointer to the managed QObject.
T * operator->() const
Returns a raw pointer to the managed QObject.
void clear()
Clears the pointer.
bool operator==(const T *o, const QObjectUniquePtr< T > &p)
Q_DECLARE_TYPEINFO_BODY(QObjectUniquePtr< T >, Q_MOVABLE_TYPE)
bool operator!=(const T *o, const QObjectUniquePtr< T > &p)
QObjectUniquePtr< T > QObjectUniquePtrFromVariant(const QVariant &variant)