QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
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 <QPointer>
27#include <qtypeinfo.h>
28#include <QtDebug>
29
30class QVariant;
31
42template <class T>
44{
45 Q_STATIC_ASSERT_X( !std::is_pointer<T>::value, "QObjectUniquePtr's template type must not be a pointer type" );
46
47 template<typename U>
48 struct TypeSelector
49 {
50 typedef QObject Type;
51 };
52 template<typename U>
53 struct TypeSelector<const U>
54 {
55 typedef const QObject Type;
56 };
57 typedef typename TypeSelector<T>::Type QObjectType;
58 QPointer<QObjectType> mPtr;
59 public:
60
65 { }
66
70 inline QObjectUniquePtr( T *p ) : mPtr( p )
71 { }
72 // compiler-generated copy/move ctor/assignment operators are fine!
73
78 {
79 // Will be a nullptr if the QObject has been deleted from somewhere else (e.g. through parent ownership)
80 delete mPtr.data();
81 }
82
86 inline void swap( QObjectUniquePtr &other )
87 {
88 mPtr.swap( other.mPtr );
89 }
90
92 {
93 mPtr.assign( static_cast<QObjectType *>( p ) );
94 return *this;
95 }
96
100 inline T *data() const
101 {
102 return static_cast<T *>( mPtr.data() );
103 }
104
108 inline T *get() const
109 {
110 return static_cast<T *>( mPtr.data() );
111 }
112
116 inline T *operator->() const
117 {
118 return data();
119 }
120
124 inline T &operator*() const
125 {
126 return *data();
127 }
128
132 inline operator T *() const
133 {
134 return data();
135 }
136
140 inline bool isNull() const
141 {
142 return mPtr.isNull();
143 }
144
150 inline operator bool() const
151 {
152 return !mPtr.isNull();
153 }
154
158 inline void clear()
159 {
160 mPtr.clear();
161 }
162
167 inline T *release()
168 {
169 T *p = qobject_cast<T *>( mPtr.data() );
170 mPtr.clear();
171 return p;
172 }
173
179 void reset( T *p = nullptr )
180 {
181 delete mPtr.data();
182 mPtr = p;
183 }
184};
185template <class T> Q_DECLARE_TYPEINFO_BODY( QObjectUniquePtr<T>, Q_MOVABLE_TYPE );
186
187template <class T>
188inline bool operator==( const T *o, const QObjectUniquePtr<T> &p )
189{
190 return o == p.operator->();
191}
192
193template<class T>
194inline bool operator==( const QObjectUniquePtr<T> &p, const T *o )
195{
196 return p.operator->() == o;
197}
198
199template <class T>
200inline bool operator==( T *o, const QObjectUniquePtr<T> &p )
201{
202 return o == p.operator->();
203}
204
205template<class T>
206inline bool operator==( const QObjectUniquePtr<T> &p, T *o )
207{
208 return p.operator->() == o;
209}
210
211template<class T>
212inline bool operator==( const QObjectUniquePtr<T> &p1, const QObjectUniquePtr<T> &p2 )
213{
214 return p1.operator->() == p2.operator->();
215}
216
217template <class T>
218inline bool operator!=( const T *o, const QObjectUniquePtr<T> &p )
219{
220 return o != p.operator->();
221}
222
223template<class T>
224inline bool operator!= ( const QObjectUniquePtr<T> &p, const T *o )
225{
226 return p.operator->() != o;
227}
228
229template <class T>
230inline bool operator!=( T *o, const QObjectUniquePtr<T> &p )
231{
232 return o != p.operator->();
233}
234
235template<class T>
236inline bool operator!= ( const QObjectUniquePtr<T> &p, T *o )
237{
238 return p.operator->() != o;
239}
240
241template<class T>
242inline bool operator!= ( const QObjectUniquePtr<T> &p1, const QObjectUniquePtr<T> &p2 )
243{
244 return p1.operator->() != p2.operator->() ;
245}
246
247template<typename T>
249QObjectUniquePtrFromVariant( const QVariant &variant )
250{
251 return QObjectUniquePtr<T>( qobject_cast<T *>( QtSharedPointer::weakPointerFromVariant_internal( variant ).toStrongRef().data() ) );
252}
253
254
255
256
268template <class T>
270{
271 Q_STATIC_ASSERT_X( !std::is_pointer<T>::value, "QObjectParentUniquePtr's template object type must not be a pointer type" );
272
273 T *mChild = nullptr;
274
275 QPointer<QObject> mParent;
276 QMetaObject::Connection mParentDestroyedConnection;
277
278 public:
279
284 { }
285
289 inline QObjectParentUniquePtr( T *child, QObject *parent )
290 : mChild( child )
291 {
292 if ( parent )
293 {
294 setParentOwner( parent );
295 }
296 }
297 // compiler-generated copy/move ctor/assignment operators are fine!
298
303 {
304 if ( mParent )
305 {
306 QObject::disconnect( mParentDestroyedConnection );
307 mParent = nullptr;
308 }
309 if ( mChild )
310 {
311 // child is being deleted BEFORE parent, so we are responsible for deleting it
312 delete mChild;
313 mChild = nullptr;
314 }
315 }
316
320 inline void setParentOwner( QObject *parent )
321 {
322 if ( mParent )
323 {
324 QObject::disconnect( mParentDestroyedConnection );
325 }
326 mParentDestroyedConnection = QObject::connect( parent, &QObject::destroyed, parent, [ = ]()
327 {
328 mParent = nullptr;
329 // parent is being deleted BEFORE child, so it is responsible for deleting the child -- we don't need to delete it here!
330 mChild = nullptr;
331 } );
332 mParent = parent;
333 }
334
341 {
342 if ( child && !mParent )
343 {
344 qWarning() << "Assigning pointer to QObjectParentUniquePtr with nullptr parent.";
345 }
346 delete mChild;
347 mChild = child;
348 return *this;
349 }
350
354 inline T *data() const
355 {
356 return static_cast<T *>( mChild );
357 }
358
362 inline T *get() const
363 {
364 return static_cast<T *>( mChild );
365 }
366
370 inline T *operator->() const
371 {
372 return data();
373 }
374
380 inline T &operator*() const
381 {
382 return *data();
383 }
384
388 inline operator T *() const
389 {
390 return data();
391 }
392
396 inline bool isNull() const
397 {
398 return !mChild;
399 }
400
407 inline operator bool() const
408 {
409 return static_cast< bool >( mChild );
410 }
411
415 inline void clear()
416 {
417 mChild = nullptr;
418 }
419
426 inline T *release()
427 {
428 T *p = qobject_cast<T *>( mChild );
429 mChild = nullptr;
430 return p;
431 }
432
441 void reset( T *p = nullptr )
442 {
443 if ( p && !mParent )
444 {
445 qWarning() << "Assigning pointer to QObjectParentUniquePtr with nullptr parent.";
446 }
447 delete mChild;
448 mChild = p;
449 }
450};
451
452template <class T>
453inline bool operator==( const T *o, const QObjectParentUniquePtr<T> &p )
454{
455 return o == p.operator->();
456}
457
458template<class T>
459inline bool operator==( const QObjectParentUniquePtr<T> &p, const T *o )
460{
461 return p.operator->() == o;
462}
463
464template <class T>
465inline bool operator==( T *o, const QObjectParentUniquePtr<T> &p )
466{
467 return o == p.operator->();
468}
469
470template<class T>
471inline bool operator==( const QObjectParentUniquePtr<T> &p, T *o )
472{
473 return p.operator->() == o;
474}
475
476template<class T>
478{
479 return p1.operator->() == p2.operator->();
480}
481
482template <class T>
483inline bool operator!=( const T *o, const QObjectParentUniquePtr<T> &p )
484{
485 return o != p.operator->();
486}
487
488template<class T>
489inline bool operator!= ( const QObjectParentUniquePtr<T> &p, const T *o )
490{
491 return p.operator->() != o;
492}
493
494template <class T>
495inline bool operator!=( T *o, const QObjectParentUniquePtr<T> &p )
496{
497 return o != p.operator->();
498}
499
500template<class T>
501inline bool operator!= ( const QObjectParentUniquePtr<T> &p, T *o )
502{
503 return p.operator->() != o;
504}
505
506template<class T>
508{
509 return p1.operator->() != p2.operator->() ;
510}
511
512
513#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)