QGIS API Documentation 3.43.0-Master (3ee7834ace6)
qobjectuniqueptr.h
Go to the documentation of this file.
1/***************************************************************************
2 qobjectuniqueptr.h
3
4 A unique pointer to a QObject.
5
6 -------------------
7 begin : June 2019
8 copyright : (C) 2009 by Matthias Kuhn
9 email : matthias@opengis.ch
10 ***************************************************************************/
11
12/***************************************************************************
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 ***************************************************************************/
20
21#ifndef QOBJECTUNIQUEPTR_H
22#define QOBJECTUNIQUEPTR_H
23
24#define SIP_NO_FILE
25
26#include <QObject>
27#include <QPointer>
28#include <qtypeinfo.h>
29#include <QtDebug>
30
31class QVariant;
32
43template <class T>
45{
46 Q_STATIC_ASSERT_X( !std::is_pointer<T>::value, "QObjectUniquePtr's template type must not be a pointer type" );
47
48 template<typename U>
49 struct TypeSelector
50 {
51 typedef QObject Type;
52 };
53 template<typename U>
54 struct TypeSelector<const U>
55 {
56 typedef const QObject Type;
57 };
58 typedef typename TypeSelector<T>::Type QObjectType;
59 QPointer<QObjectType> mPtr;
60 public:
61
66 { }
67
71 inline QObjectUniquePtr( T *p ) : mPtr( p )
72 { }
73 // compiler-generated copy/move ctor/assignment operators are fine!
74
79 {
80 // Will be a nullptr if the QObject has been deleted from somewhere else (e.g. through parent ownership)
81 delete mPtr.data();
82 }
83
87 inline void swap( QObjectUniquePtr &other )
88 {
89 mPtr.swap( other.mPtr );
90 }
91
93 {
94 mPtr.assign( static_cast<QObjectType *>( p ) );
95 return *this;
96 }
97
101 inline T *data() const
102 {
103 return static_cast<T *>( mPtr.data() );
104 }
105
109 inline T *get() const
110 {
111 return static_cast<T *>( mPtr.data() );
112 }
113
117 inline T *operator->() const
118 {
119 return data();
120 }
121
125 inline T &operator*() const
126 {
127 return *data();
128 }
129
133 inline operator T *() const
134 {
135 return data();
136 }
137
141 inline bool isNull() const
142 {
143 return mPtr.isNull();
144 }
145
151 explicit inline operator bool() const
152 {
153 return !mPtr.isNull();
154 }
155
159 inline void clear()
160 {
161 mPtr.clear();
162 }
163
168 inline T *release()
169 {
170 T *p = qobject_cast<T *>( mPtr.data() );
171 mPtr.clear();
172 return p;
173 }
174
180 void reset( T *p = nullptr )
181 {
182 delete mPtr.data();
183 mPtr = p;
184 }
185};
186template <class T> Q_DECLARE_TYPEINFO_BODY( QObjectUniquePtr<T>, Q_MOVABLE_TYPE );
187
188template <class T>
189inline bool operator==( const T *o, const QObjectUniquePtr<T> &p )
190{
191 return o == p.operator->();
192}
193
194template<class T>
195inline bool operator==( const QObjectUniquePtr<T> &p, const T *o )
196{
197 return p.operator->() == o;
198}
199
200template <class T>
201inline bool operator==( T *o, const QObjectUniquePtr<T> &p )
202{
203 return o == p.operator->();
204}
205
206template<class T>
207inline bool operator==( const QObjectUniquePtr<T> &p, T *o )
208{
209 return p.operator->() == o;
210}
211
212template<class T>
213inline bool operator==( const QObjectUniquePtr<T> &p1, const QObjectUniquePtr<T> &p2 )
214{
215 return p1.operator->() == p2.operator->();
216}
217
218template <class T>
219inline bool operator!=( const T *o, const QObjectUniquePtr<T> &p )
220{
221 return o != p.operator->();
222}
223
224template<class T>
225inline bool operator!= ( const QObjectUniquePtr<T> &p, const T *o )
226{
227 return p.operator->() != o;
228}
229
230template <class T>
231inline bool operator!=( T *o, const QObjectUniquePtr<T> &p )
232{
233 return o != p.operator->();
234}
235
236template<class T>
237inline bool operator!= ( const QObjectUniquePtr<T> &p, T *o )
238{
239 return p.operator->() != o;
240}
241
242template<class T>
243inline bool operator!= ( const QObjectUniquePtr<T> &p1, const QObjectUniquePtr<T> &p2 )
244{
245 return p1.operator->() != p2.operator->() ;
246}
247
248template<typename T>
250QObjectUniquePtrFromVariant( const QVariant &variant )
251{
252 return QObjectUniquePtr<T>( qobject_cast<T *>( QtSharedPointer::weakPointerFromVariant_internal( variant ).toStrongRef().data() ) );
253}
254
255
256
257
269template <class T>
271{
272 Q_STATIC_ASSERT_X( !std::is_pointer<T>::value, "QObjectParentUniquePtr's template object type must not be a pointer type" );
273
274 T *mChild = nullptr;
275
276 QPointer<QObject> mParent;
277 QMetaObject::Connection mParentDestroyedConnection;
278
279 public:
280
285 { }
286
290 inline QObjectParentUniquePtr( T *child, QObject *parent )
291 : mChild( child )
292 {
293 if ( parent )
294 {
295 setParentOwner( parent );
296 }
297 }
298 // compiler-generated copy/move ctor/assignment operators are fine!
299
304 {
305 if ( mParent )
306 {
307 QObject::disconnect( mParentDestroyedConnection );
308 mParent = nullptr;
309 }
310 if ( mChild )
311 {
312 // child is being deleted BEFORE parent, so we are responsible for deleting it
313 delete mChild;
314 mChild = nullptr;
315 }
316 }
317
321 inline void setParentOwner( QObject *parent )
322 {
323 if ( mParent )
324 {
325 QObject::disconnect( mParentDestroyedConnection );
326 }
327 mParentDestroyedConnection = QObject::connect( parent, &QObject::destroyed, parent, [ = ]()
328 {
329 mParent = nullptr;
330 // parent is being deleted BEFORE child, so it is responsible for deleting the child -- we don't need to delete it here!
331 mChild = nullptr;
332 } );
333 mParent = parent;
334 }
335
342 {
343 if ( child && !mParent )
344 {
345 qWarning() << "Assigning pointer to QObjectParentUniquePtr with nullptr parent.";
346 }
347 delete mChild;
348 mChild = child;
349 return *this;
350 }
351
355 inline T *data() const
356 {
357 return static_cast<T *>( mChild );
358 }
359
363 inline T *get() const
364 {
365 return static_cast<T *>( mChild );
366 }
367
371 inline T *operator->() const
372 {
373 return data();
374 }
375
381 inline T &operator*() const
382 {
383 return *data();
384 }
385
389 inline operator T *() const
390 {
391 return data();
392 }
393
397 inline bool isNull() const
398 {
399 return !mChild;
400 }
401
408 explicit inline operator bool() const
409 {
410 return static_cast< bool >( mChild );
411 }
412
416 inline void clear()
417 {
418 mChild = nullptr;
419 }
420
427 inline T *release()
428 {
429 T *p = qobject_cast<T *>( mChild );
430 mChild = nullptr;
431 return p;
432 }
433
442 void reset( T *p = nullptr )
443 {
444 if ( p && !mParent )
445 {
446 qWarning() << "Assigning pointer to QObjectParentUniquePtr with nullptr parent.";
447 }
448 delete mChild;
449 mChild = p;
450 }
451};
452
453template <class T>
454inline bool operator==( const T *o, const QObjectParentUniquePtr<T> &p )
455{
456 return o == p.operator->();
457}
458
459template<class T>
460inline bool operator==( const QObjectParentUniquePtr<T> &p, const T *o )
461{
462 return p.operator->() == o;
463}
464
465template <class T>
466inline bool operator==( T *o, const QObjectParentUniquePtr<T> &p )
467{
468 return o == p.operator->();
469}
470
471template<class T>
472inline bool operator==( const QObjectParentUniquePtr<T> &p, T *o )
473{
474 return p.operator->() == o;
475}
476
477template<class T>
479{
480 return p1.operator->() == p2.operator->();
481}
482
483template <class T>
484inline bool operator!=( const T *o, const QObjectParentUniquePtr<T> &p )
485{
486 return o != p.operator->();
487}
488
489template<class T>
490inline bool operator!= ( const QObjectParentUniquePtr<T> &p, const T *o )
491{
492 return p.operator->() != o;
493}
494
495template <class T>
496inline bool operator!=( T *o, const QObjectParentUniquePtr<T> &p )
497{
498 return o != p.operator->();
499}
500
501template<class T>
502inline bool operator!= ( const QObjectParentUniquePtr<T> &p, T *o )
503{
504 return p.operator->() != o;
505}
506
507template<class T>
509{
510 return p1.operator->() != p2.operator->() ;
511}
512
513
514#endif // QOBJECTUNIQUEPTR_H
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.
T & operator*() const
Dereferences the managed QObject.
QObjectUniquePtr()
Creates a new empty QObjectUniquePtr.
~QObjectUniquePtr()
Will delete the contained QObject if it still exists.
QObjectUniquePtr(T *p)
Takes a new QObjectUniquePtr and assigned p to it.
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.
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)