1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \class QPointer
|
---|
44 | \brief The QPointer class is a template class that provides guarded pointers to QObjects.
|
---|
45 |
|
---|
46 | \ingroup objectmodel
|
---|
47 |
|
---|
48 |
|
---|
49 | A guarded pointer, QPointer<T>, behaves like a normal C++
|
---|
50 | pointer \c{T *}, except that it is automatically set to 0 when the
|
---|
51 | referenced object is destroyed (unlike normal C++ pointers, which
|
---|
52 | become "dangling pointers" in such cases). \c T must be a
|
---|
53 | subclass of QObject.
|
---|
54 |
|
---|
55 | Guarded pointers are useful whenever you need to store a pointer
|
---|
56 | to a QObject that is owned by someone else, and therefore might be
|
---|
57 | destroyed while you still hold a reference to it. You can safely
|
---|
58 | test the pointer for validity.
|
---|
59 |
|
---|
60 | Qt also provides QSharedPointer, an implementation of a reference-counted
|
---|
61 | shared pointer object, which can be used to maintain a collection of
|
---|
62 | references to an individual pointer.
|
---|
63 |
|
---|
64 | Example:
|
---|
65 |
|
---|
66 | \snippet doc/src/snippets/pointer/pointer.cpp 0
|
---|
67 | \dots
|
---|
68 | \snippet doc/src/snippets/pointer/pointer.cpp 1
|
---|
69 | \snippet doc/src/snippets/pointer/pointer.cpp 2
|
---|
70 |
|
---|
71 | If the QLabel is deleted in the meantime, the \c label variable
|
---|
72 | will hold 0 instead of an invalid address, and the last line will
|
---|
73 | never be executed.
|
---|
74 |
|
---|
75 | The functions and operators available with a QPointer are the
|
---|
76 | same as those available with a normal unguarded pointer, except
|
---|
77 | the pointer arithmetic operators (\c{+}, \c{-}, \c{++}, and
|
---|
78 | \c{--}), which are normally used only with arrays of objects.
|
---|
79 |
|
---|
80 | Use QPointers like normal pointers and you will not need to read
|
---|
81 | this class documentation.
|
---|
82 |
|
---|
83 | For creating guarded pointers, you can construct or assign to them
|
---|
84 | from a T* or from another guarded pointer of the same type. You
|
---|
85 | can compare them with each other using operator==() and
|
---|
86 | operator!=(), or test for 0 with isNull(). You can dereference
|
---|
87 | them using either the \c *x or the \c x->member notation.
|
---|
88 |
|
---|
89 | A guarded pointer will automatically cast to a \c T *, so you can
|
---|
90 | freely mix guarded and unguarded pointers. This means that if you
|
---|
91 | have a QPointer<QWidget>, you can pass it to a function that
|
---|
92 | requires a QWidget *. For this reason, it is of little value to
|
---|
93 | declare functions to take a QPointer as a parameter; just use
|
---|
94 | normal pointers. Use a QPointer when you are storing a pointer
|
---|
95 | over time.
|
---|
96 |
|
---|
97 | Note that class \c T must inherit QObject, or a compilation or
|
---|
98 | link error will result.
|
---|
99 |
|
---|
100 | \sa QSharedPointer, QObject, QObjectCleanupHandler
|
---|
101 | */
|
---|
102 |
|
---|
103 | /*!
|
---|
104 | \fn QPointer::QPointer()
|
---|
105 |
|
---|
106 | Constructs a 0 guarded pointer.
|
---|
107 |
|
---|
108 | \sa isNull()
|
---|
109 | */
|
---|
110 |
|
---|
111 | /*!
|
---|
112 | \fn QPointer::QPointer(T* p)
|
---|
113 |
|
---|
114 | Constructs a guarded pointer that points to same object that \a p
|
---|
115 | points to.
|
---|
116 | */
|
---|
117 |
|
---|
118 | /*!
|
---|
119 | \fn QPointer::QPointer(const QPointer<T> &p)
|
---|
120 |
|
---|
121 | Copies one guarded pointer from another. The constructed guarded
|
---|
122 | pointer points to the same object that \a p points to (which may
|
---|
123 | be 0).
|
---|
124 | */
|
---|
125 |
|
---|
126 | /*!
|
---|
127 | \fn QPointer::~QPointer()
|
---|
128 |
|
---|
129 | Destroys the guarded pointer. Just like a normal pointer,
|
---|
130 | destroying a guarded pointer does \e not destroy the object being
|
---|
131 | pointed to.
|
---|
132 | */
|
---|
133 |
|
---|
134 | /*!
|
---|
135 | \fn QPointer<T>& QPointer::operator=(const QPointer<T> &p)
|
---|
136 |
|
---|
137 | Assignment operator. This guarded pointer will now point to the
|
---|
138 | same object that \a p points to.
|
---|
139 | */
|
---|
140 |
|
---|
141 | /*!
|
---|
142 | \fn QPointer<T> & QPointer::operator=(T* p)
|
---|
143 |
|
---|
144 | Assignment operator. This guarded pointer will now point to the
|
---|
145 | same object that \a p points to.
|
---|
146 | */
|
---|
147 |
|
---|
148 | /*!
|
---|
149 | \fn T* QPointer::data() const
|
---|
150 | \since 4.4
|
---|
151 |
|
---|
152 | Returns the pointer to the object being guarded.
|
---|
153 | */
|
---|
154 |
|
---|
155 | /*!
|
---|
156 | \fn bool QPointer::isNull() const
|
---|
157 |
|
---|
158 | Returns \c true if the referenced object has been destroyed or if
|
---|
159 | there is no referenced object; otherwise returns false.
|
---|
160 | */
|
---|
161 |
|
---|
162 | /*!
|
---|
163 | \fn T* QPointer::operator->() const
|
---|
164 |
|
---|
165 | Overloaded arrow operator; implements pointer semantics. Just use
|
---|
166 | this operator as you would with a normal C++ pointer.
|
---|
167 | */
|
---|
168 |
|
---|
169 | /*!
|
---|
170 | \fn T& QPointer::operator*() const
|
---|
171 |
|
---|
172 | Dereference operator; implements pointer semantics. Just use this
|
---|
173 | operator as you would with a normal C++ pointer.
|
---|
174 | */
|
---|
175 |
|
---|
176 | /*!
|
---|
177 | \fn QPointer::operator T*() const
|
---|
178 |
|
---|
179 | Cast operator; implements pointer semantics. Because of this
|
---|
180 | function you can pass a QPointer\<T\> to a function where a T*
|
---|
181 | is required.
|
---|
182 | */
|
---|
183 |
|
---|
184 | /*!
|
---|
185 | \fn bool operator==(const T *o, const QPointer<T> &p)
|
---|
186 |
|
---|
187 | Equality operator. Returns true if \a o and the guarded
|
---|
188 | pointer \a p are pointing to the same object, otherwise
|
---|
189 | returns false.
|
---|
190 |
|
---|
191 | */
|
---|
192 | /*!
|
---|
193 | \fn bool operator==(const QPointer<T> &p, const T *o)
|
---|
194 |
|
---|
195 | Equality operator. Returns true if \a o and the guarded
|
---|
196 | pointer \a p are pointing to the same object, otherwise
|
---|
197 | returns false.
|
---|
198 |
|
---|
199 | */
|
---|
200 | /*!
|
---|
201 | \fn bool operator==(T *o, const QPointer<T> &p)
|
---|
202 |
|
---|
203 | Equality operator. Returns true if \a o and the guarded
|
---|
204 | pointer \a p are pointing to the same object, otherwise
|
---|
205 | returns false.
|
---|
206 |
|
---|
207 | */
|
---|
208 | /*!
|
---|
209 | \fn bool operator==(const QPointer<T> &p, T *o)
|
---|
210 |
|
---|
211 | Equality operator. Returns true if \a o and the guarded
|
---|
212 | pointer \a p are pointing to the same object, otherwise
|
---|
213 | returns false.
|
---|
214 |
|
---|
215 | */
|
---|
216 | /*!
|
---|
217 | \fn bool operator==(const QPointer<T> &p1, const QPointer<T> &p2)
|
---|
218 |
|
---|
219 | Equality operator. Returns true if the guarded pointers \a p1 and \a p2
|
---|
220 | are pointing to the same object, otherwise
|
---|
221 | returns false.
|
---|
222 |
|
---|
223 | */
|
---|
224 |
|
---|
225 |
|
---|
226 | /*!
|
---|
227 | \fn bool operator!=(const T *o, const QPointer<T> &p)
|
---|
228 |
|
---|
229 | Inequality operator. Returns true if \a o and the guarded
|
---|
230 | pointer \a p are not pointing to the same object, otherwise
|
---|
231 | returns false.
|
---|
232 | */
|
---|
233 | /*!
|
---|
234 | \fn bool operator!=(const QPointer<T> &p, const T *o)
|
---|
235 |
|
---|
236 | Inequality operator. Returns true if \a o and the guarded
|
---|
237 | pointer \a p are not pointing to the same object, otherwise
|
---|
238 | returns false.
|
---|
239 | */
|
---|
240 | /*!
|
---|
241 | \fn bool operator!=(T *o, const QPointer<T> &p)
|
---|
242 |
|
---|
243 | Inequality operator. Returns true if \a o and the guarded
|
---|
244 | pointer \a p are not pointing to the same object, otherwise
|
---|
245 | returns false.
|
---|
246 | */
|
---|
247 | /*!
|
---|
248 | \fn bool operator!=(const QPointer<T> &p, T *o)
|
---|
249 |
|
---|
250 | Inequality operator. Returns true if \a o and the guarded
|
---|
251 | pointer \a p are not pointing to the same object, otherwise
|
---|
252 | returns false.
|
---|
253 | */
|
---|
254 | /*!
|
---|
255 | \fn bool operator!=(const QPointer<T> &p1, const QPointer<T> &p2)
|
---|
256 |
|
---|
257 | Inequality operator. Returns true if the guarded pointers \a p1 and
|
---|
258 | \a p2 are not pointing to the same object, otherwise
|
---|
259 | returns false.
|
---|
260 | */
|
---|