QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
29 class QVariant;
30 
39 template <class T>
41 {
42  Q_STATIC_ASSERT_X( !std::is_pointer<T>::value, "QObjectUniquePtr's template type must not be a pointer type" );
43 
44  template<typename U>
45  struct TypeSelector
46  {
47  typedef QObject Type;
48  };
49  template<typename U>
50  struct TypeSelector<const U>
51  {
52  typedef const QObject Type;
53  };
54  typedef typename TypeSelector<T>::Type QObjectType;
55  QPointer<QObjectType> mPtr;
56  public:
57 
62  { }
63 
67  inline QObjectUniquePtr( T *p ) : mPtr( p )
68  { }
69  // compiler-generated copy/move ctor/assignment operators are fine!
70 
75  {
76  // Will be a nullptr if the QObject has been deleted from somewhere else (e.g. through parent ownership)
77  delete mPtr.data();
78  }
79 
83  inline void swap( QObjectUniquePtr &other )
84  {
85  mPtr.swap( other.mPtr );
86  }
87 
89  {
90  mPtr.assign( static_cast<QObjectType *>( p ) );
91  return *this;
92  }
93 
97  inline T *data() const
98  {
99  return static_cast<T *>( mPtr.data() );
100  }
101 
105  inline T *get() const
106  {
107  return static_cast<T *>( mPtr.data() );
108  }
109 
113  inline T *operator->() const
114  {
115  return data();
116  }
117 
121  inline T &operator*() const
122  {
123  return *data();
124  }
125 
129  inline operator T *() const
130  {
131  return data();
132  }
133 
137  inline bool isNull() const
138  {
139  return mPtr.isNull();
140  }
141 
147  inline operator bool() const
148  {
149  return !mPtr.isNull();
150  }
151 
155  inline void clear()
156  {
157  mPtr.clear();
158  }
159 
164  inline T *release()
165  {
166  T *p = qobject_cast<T *>( mPtr.data() );
167  mPtr.clear();
168  return p;
169  }
170 
176  void reset( T *p = nullptr )
177  {
178  delete mPtr.data();
179  mPtr = p;
180  }
181 };
182 template <class T> Q_DECLARE_TYPEINFO_BODY( QObjectUniquePtr<T>, Q_MOVABLE_TYPE );
183 
184 template <class T>
185 inline bool operator==( const T *o, const QObjectUniquePtr<T> &p )
186 {
187  return o == p.operator->();
188 }
189 
190 template<class T>
191 inline bool operator==( const QObjectUniquePtr<T> &p, const T *o )
192 {
193  return p.operator->() == o;
194 }
195 
196 template <class T>
197 inline bool operator==( T *o, const QObjectUniquePtr<T> &p )
198 {
199  return o == p.operator->();
200 }
201 
202 template<class T>
203 inline bool operator==( const QObjectUniquePtr<T> &p, T *o )
204 {
205  return p.operator->() == o;
206 }
207 
208 template<class T>
209 inline bool operator==( const QObjectUniquePtr<T> &p1, const QObjectUniquePtr<T> &p2 )
210 {
211  return p1.operator->() == p2.operator->();
212 }
213 
214 template <class T>
215 inline bool operator!=( const T *o, const QObjectUniquePtr<T> &p )
216 {
217  return o != p.operator->();
218 }
219 
220 template<class T>
221 inline bool operator!= ( const QObjectUniquePtr<T> &p, const T *o )
222 {
223  return p.operator->() != o;
224 }
225 
226 template <class T>
227 inline bool operator!=( T *o, const QObjectUniquePtr<T> &p )
228 {
229  return o != p.operator->();
230 }
231 
232 template<class T>
233 inline bool operator!= ( const QObjectUniquePtr<T> &p, T *o )
234 {
235  return p.operator->() != o;
236 }
237 
238 template<class T>
239 inline bool operator!= ( const QObjectUniquePtr<T> &p1, const QObjectUniquePtr<T> &p2 )
240 {
241  return p1.operator->() != p2.operator->() ;
242 }
243 
244 template<typename T>
246 QObjectUniquePtrFromVariant( const QVariant &variant )
247 {
248  return QObjectUniquePtr<T>( qobject_cast<T *>( QtSharedPointer::weakPointerFromVariant_internal( variant ).data() ) );
249 }
250 
251 #endif // QOBJECTUNIQUEPTR_H
QObjectUniquePtr::swap
void swap(QObjectUniquePtr &other)
Swaps the pointer managed by this instance with the pointer managed by other.
Definition: qobjectuniqueptr.h:83
QObjectUniquePtr::reset
void reset(T *p=nullptr)
Will reset the managed pointer to p.
Definition: qobjectuniqueptr.h:176
operator!=
bool operator!=(const T *o, const QObjectUniquePtr< T > &p)
Definition: qobjectuniqueptr.h:215
QObjectUniquePtr::clear
void clear()
Clears the pointer.
Definition: qobjectuniqueptr.h:155
operator==
bool operator==(const T *o, const QObjectUniquePtr< T > &p)
Definition: qobjectuniqueptr.h:185
QObjectUniquePtr::operator->
T * operator->() const
Returns a raw pointer to the managed QObject.
Definition: qobjectuniqueptr.h:113
QObjectUniquePtr::QObjectUniquePtr
QObjectUniquePtr(T *p)
Takes a new QObjectUniquePtr and assigned p to it.
Definition: qobjectuniqueptr.h:67
QObjectUniquePtr::release
T * release()
Clears the pointer and returns it.
Definition: qobjectuniqueptr.h:164
QObjectUniquePtr::operator=
QObjectUniquePtr< T > & operator=(T *p)
Definition: qobjectuniqueptr.h:88
QObjectUniquePtr
Keeps a pointer to a QObject and deletes it whenever this object is deleted.
Definition: qobjectuniqueptr.h:40
QObjectUniquePtr::~QObjectUniquePtr
~QObjectUniquePtr()
Will delete the contained QObject if it still exists.
Definition: qobjectuniqueptr.h:74
QObjectUniquePtr::data
T * data() const
Returns the raw pointer to the managed QObject.
Definition: qobjectuniqueptr.h:97
QObjectUniquePtrFromVariant
QObjectUniquePtr< T > QObjectUniquePtrFromVariant(const QVariant &variant)
Definition: qobjectuniqueptr.h:246
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:105
QObjectUniquePtr::QObjectUniquePtr
QObjectUniquePtr()
Creates a new empty QObjectUniquePtr.
Definition: qobjectuniqueptr.h:61
QObjectUniquePtr::operator*
T & operator*() const
Dereferences the managed QObject.
Definition: qobjectuniqueptr.h:121
QObjectUniquePtr::isNull
bool isNull() const
Checks if the managed pointer is nullptr.
Definition: qobjectuniqueptr.h:137