21#ifndef QOBJECTUNIQUEPTR_H
22#define QOBJECTUNIQUEPTR_H
47 Q_STATIC_ASSERT_X( !std::is_pointer<T>::value,
"QObjectUniquePtr's template type must not be a pointer type" );
55 struct TypeSelector<const U>
57 typedef const QObject Type;
59 typedef typename TypeSelector<T>::Type QObjectType;
60 QPointer<QObjectType> mPtr;
90 *
this = std::move( other );
109 mPtr.swap( other.mPtr );
114 mPtr.assign(
static_cast<QObjectType *
>( p ) );
123 return static_cast<T *
>( mPtr.data() );
131 return static_cast<T *
>( mPtr.data() );
153 inline operator T *()
const
163 return mPtr.isNull();
171 explicit inline operator bool()
const
173 return !mPtr.isNull();
190 T *p = qobject_cast<T *>( mPtr.data() );
211 return o == p.operator->();
217 return p.operator->() == o;
223 return o == p.operator->();
229 return p.operator->() == o;
235 return p1.operator->() == p2.operator->();
241 return o != p.operator->();
247 return p.operator->() != o;
253 return o != p.operator->();
259 return p.operator->() != o;
265 return p1.operator->() != p2.operator->() ;
272 return QObjectUniquePtr<T>( qobject_cast<T *>( QtSharedPointer::weakPointerFromVariant_internal( variant ).toStrongRef().data() ) );
292 Q_STATIC_ASSERT_X( !std::is_pointer<T>::value,
"QObjectParentUniquePtr's template object type must not be a pointer type" );
296 QPointer<QObject> mParent;
297 QMetaObject::Connection mParentDestroyedConnection;
327 QObject::disconnect( mParentDestroyedConnection );
345 QObject::disconnect( mParentDestroyedConnection );
347 mParentDestroyedConnection = QObject::connect( parent, &QObject::destroyed, parent, [
this]()
363 if ( child && !mParent )
365 qWarning() <<
"Assigning pointer to QObjectParentUniquePtr with nullptr parent.";
377 return static_cast<T *
>( mChild );
385 return static_cast<T *
>( mChild );
409 inline operator T *()
const
428 explicit inline operator bool()
const
430 return static_cast< bool >( mChild );
449 T *p = qobject_cast<T *>( mChild );
466 qWarning() <<
"Assigning pointer to QObjectParentUniquePtr with nullptr parent.";
476 return o == p.operator->();
482 return p.operator->() == o;
488 return o == p.operator->();
494 return p.operator->() == o;
500 return p1.operator->() == p2.operator->();
506 return o != p.operator->();
512 return p.operator->() != o;
518 return o != p.operator->();
524 return p.operator->() != o;
530 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)