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" );
54 struct TypeSelector<const U>
56 typedef const QObject Type;
58 typedef typename TypeSelector<T>::Type QObjectType;
59 QPointer<QObjectType> mPtr;
89 *
this = std::move( other );
108 mPtr.swap( other.mPtr );
113 mPtr.assign(
static_cast<QObjectType *
>( p ) );
122 return static_cast<T *
>( mPtr.data() );
130 return static_cast<T *
>( mPtr.data() );
152 inline operator T *()
const
162 return mPtr.isNull();
170 explicit inline operator bool()
const
172 return !mPtr.isNull();
189 T *p = qobject_cast<T *>( mPtr.data() );
210 return o == p.operator->();
216 return p.operator->() == o;
222 return o == p.operator->();
228 return p.operator->() == o;
234 return p1.operator->() == p2.operator->();
240 return o != p.operator->();
246 return p.operator->() != o;
252 return o != p.operator->();
258 return p.operator->() != o;
264 return p1.operator->() != p2.operator->() ;
271 return QObjectUniquePtr<T>( qobject_cast<T *>( QtSharedPointer::weakPointerFromVariant_internal( variant ).toStrongRef().data() ) );
291 Q_STATIC_ASSERT_X( !std::is_pointer<T>::value,
"QObjectParentUniquePtr's template object type must not be a pointer type" );
295 QPointer<QObject> mParent;
296 QMetaObject::Connection mParentDestroyedConnection;
326 QObject::disconnect( mParentDestroyedConnection );
344 QObject::disconnect( mParentDestroyedConnection );
346 mParentDestroyedConnection = QObject::connect( parent, &QObject::destroyed, parent, [
this]()
362 if ( child && !mParent )
364 qWarning() <<
"Assigning pointer to QObjectParentUniquePtr with nullptr parent.";
376 return static_cast<T *
>( mChild );
384 return static_cast<T *
>( mChild );
408 inline operator T *()
const
427 explicit inline operator bool()
const
429 return static_cast< bool >( mChild );
448 T *p = qobject_cast<T *>( mChild );
465 qWarning() <<
"Assigning pointer to QObjectParentUniquePtr with nullptr parent.";
475 return o == p.operator->();
481 return p.operator->() == o;
487 return o == p.operator->();
493 return p.operator->() == o;
499 return p1.operator->() == p2.operator->();
505 return o != p.operator->();
511 return p.operator->() != o;
517 return o != p.operator->();
523 return p.operator->() != o;
529 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)