QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 : [email protected]
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 
30 class QVariant;
31 
42 template <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 };
185 template <class T> Q_DECLARE_TYPEINFO_BODY( QObjectUniquePtr<T>, Q_MOVABLE_TYPE );
186 
187 template <class T>
188 inline bool operator==( const T *o, const QObjectUniquePtr<T> &p )
189 {
190  return o == p.operator->();
191 }
192 
193 template<class T>
194 inline bool operator==( const QObjectUniquePtr<T> &p, const T *o )
195 {
196  return p.operator->() == o;
197 }
198 
199 template <class T>
200 inline bool operator==( T *o, const QObjectUniquePtr<T> &p )
201 {
202  return o == p.operator->();
203 }
204 
205 template<class T>
206 inline bool operator==( const QObjectUniquePtr<T> &p, T *o )
207 {
208  return p.operator->() == o;
209 }
210 
211 template<class T>
212 inline bool operator==( const QObjectUniquePtr<T> &p1, const QObjectUniquePtr<T> &p2 )
213 {
214  return p1.operator->() == p2.operator->();
215 }
216 
217 template <class T>
218 inline bool operator!=( const T *o, const QObjectUniquePtr<T> &p )
219 {
220  return o != p.operator->();
221 }
222 
223 template<class T>
224 inline bool operator!= ( const QObjectUniquePtr<T> &p, const T *o )
225 {
226  return p.operator->() != o;
227 }
228 
229 template <class T>
230 inline bool operator!=( T *o, const QObjectUniquePtr<T> &p )
231 {
232  return o != p.operator->();
233 }
234 
235 template<class T>
236 inline bool operator!= ( const QObjectUniquePtr<T> &p, T *o )
237 {
238  return p.operator->() != o;
239 }
240 
241 template<class T>
242 inline bool operator!= ( const QObjectUniquePtr<T> &p1, const QObjectUniquePtr<T> &p2 )
243 {
244  return p1.operator->() != p2.operator->() ;
245 }
246 
247 template<typename T>
249 QObjectUniquePtrFromVariant( const QVariant &variant )
250 {
251  return QObjectUniquePtr<T>( qobject_cast<T *>( QtSharedPointer::weakPointerFromVariant_internal( variant ).toStrongRef().data() ) );
252 }
253 
254 
255 
256 
268 template <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 
452 template <class T>
453 inline bool operator==( const T *o, const QObjectParentUniquePtr<T> &p )
454 {
455  return o == p.operator->();
456 }
457 
458 template<class T>
459 inline bool operator==( const QObjectParentUniquePtr<T> &p, const T *o )
460 {
461  return p.operator->() == o;
462 }
463 
464 template <class T>
465 inline bool operator==( T *o, const QObjectParentUniquePtr<T> &p )
466 {
467  return o == p.operator->();
468 }
469 
470 template<class T>
471 inline bool operator==( const QObjectParentUniquePtr<T> &p, T *o )
472 {
473  return p.operator->() == o;
474 }
475 
476 template<class T>
478 {
479  return p1.operator->() == p2.operator->();
480 }
481 
482 template <class T>
483 inline bool operator!=( const T *o, const QObjectParentUniquePtr<T> &p )
484 {
485  return o != p.operator->();
486 }
487 
488 template<class T>
489 inline bool operator!= ( const QObjectParentUniquePtr<T> &p, const T *o )
490 {
491  return p.operator->() != o;
492 }
493 
494 template <class T>
495 inline bool operator!=( T *o, const QObjectParentUniquePtr<T> &p )
496 {
497  return o != p.operator->();
498 }
499 
500 template<class T>
501 inline bool operator!= ( const QObjectParentUniquePtr<T> &p, T *o )
502 {
503  return p.operator->() != o;
504 }
505 
506 template<class T>
508 {
509  return p1.operator->() != p2.operator->() ;
510 }
511 
512 
513 #endif // QOBJECTUNIQUEPTR_H
QObjectParentUniquePtr::operator*
T & operator*() const
Dereferences the managed child.
Definition: qobjectuniqueptr.h:380
QObjectParentUniquePtr::reset
void reset(T *p=nullptr)
Will reset the managed pointer to p.
Definition: qobjectuniqueptr.h:441
QObjectUniquePtr::swap
void swap(QObjectUniquePtr &other)
Swaps the pointer managed by this instance with the pointer managed by other.
Definition: qobjectuniqueptr.h:86
QObjectUniquePtr::reset
void reset(T *p=nullptr)
Will reset the managed pointer to p.
Definition: qobjectuniqueptr.h:179
QObjectParentUniquePtr::clear
void clear()
Clears the pointer.
Definition: qobjectuniqueptr.h:415
operator!=
bool operator!=(const T *o, const QObjectUniquePtr< T > &p)
Definition: qobjectuniqueptr.h:218
QObjectUniquePtr::clear
void clear()
Clears the pointer.
Definition: qobjectuniqueptr.h:158
operator==
bool operator==(const T *o, const QObjectUniquePtr< T > &p)
Definition: qobjectuniqueptr.h:188
QObjectUniquePtr::operator->
T * operator->() const
Returns a raw pointer to the managed QObject.
Definition: qobjectuniqueptr.h:116
QObjectUniquePtr::QObjectUniquePtr
QObjectUniquePtr(T *p)
Takes a new QObjectUniquePtr and assigned p to it.
Definition: qobjectuniqueptr.h:70
QObjectUniquePtr::release
T * release()
Clears the pointer and returns it.
Definition: qobjectuniqueptr.h:167
QObjectParentUniquePtr
Keeps a pointer to an object owned by a QObject parent, and deletes it whenever this parent object is...
Definition: qobjectuniqueptr.h:269
QObjectParentUniquePtr::setParentOwner
void setParentOwner(QObject *parent)
Sets the parent object.
Definition: qobjectuniqueptr.h:320
QObjectUniquePtr::operator=
QObjectUniquePtr< T > & operator=(T *p)
Definition: qobjectuniqueptr.h:91
QObjectParentUniquePtr::get
T * get() const
Returns the raw pointer to the managed child.
Definition: qobjectuniqueptr.h:362
QObjectUniquePtr
Keeps a pointer to a QObject and deletes it whenever this object is deleted.
Definition: qobjectuniqueptr.h:43
QObjectParentUniquePtr::~QObjectParentUniquePtr
~QObjectParentUniquePtr()
Will delete the contained object if the parent still exists.
Definition: qobjectuniqueptr.h:302
QObjectParentUniquePtr::operator->
T * operator->() const
Returns a raw pointer to the managed child.
Definition: qobjectuniqueptr.h:370
QObjectParentUniquePtr::data
T * data() const
Returns the raw pointer to the managed QObject.
Definition: qobjectuniqueptr.h:354
QObjectParentUniquePtr::release
T * release()
Clears the pointer and returns it.
Definition: qobjectuniqueptr.h:426
QObjectUniquePtr::~QObjectUniquePtr
~QObjectUniquePtr()
Will delete the contained QObject if it still exists.
Definition: qobjectuniqueptr.h:77
QObjectUniquePtr::data
T * data() const
Returns the raw pointer to the managed QObject.
Definition: qobjectuniqueptr.h:100
QObjectUniquePtrFromVariant
QObjectUniquePtr< T > QObjectUniquePtrFromVariant(const QVariant &variant)
Definition: qobjectuniqueptr.h:249
QObjectParentUniquePtr::QObjectParentUniquePtr
QObjectParentUniquePtr()
Creates a new empty QObjectParentUniquePtr.
Definition: qobjectuniqueptr.h:283
QObjectParentUniquePtr::isNull
bool isNull() const
Checks if the managed pointer is nullptr.
Definition: qobjectuniqueptr.h:396
QObjectParentUniquePtr::QObjectParentUniquePtr
QObjectParentUniquePtr(T *child, QObject *parent)
Takes a new QObjectParentUniquePtr and assign a child to it.
Definition: qobjectuniqueptr.h:289
Q_DECLARE_TYPEINFO_BODY
Q_DECLARE_TYPEINFO_BODY(QObjectUniquePtr< T >, Q_MOVABLE_TYPE)
QObjectUniquePtr::get
T * get() const
Returns the raw pointer to the managed QObject.
Definition: qobjectuniqueptr.h:108
QObjectUniquePtr::QObjectUniquePtr
QObjectUniquePtr()
Creates a new empty QObjectUniquePtr.
Definition: qobjectuniqueptr.h:64
QObjectUniquePtr::operator*
T & operator*() const
Dereferences the managed QObject.
Definition: qobjectuniqueptr.h:124
QObjectUniquePtr::isNull
bool isNull() const
Checks if the managed pointer is nullptr.
Definition: qobjectuniqueptr.h:140
QObjectParentUniquePtr::operator=
QObjectParentUniquePtr< T > & operator=(T *child)
Assigns a new child to the pointer.
Definition: qobjectuniqueptr.h:340