qsharedpointer.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qsharedpointer.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2015 The Qt Company Ltd.-
4** Contact: http://www.qt.io/licensing/-
5**-
6** This file is part of the QtCore module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL21$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see http://www.qt.io/terms-conditions. For further-
15** information use the contact form at http://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 2.1 or version 3 as published by the Free-
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and-
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the-
22** following information to ensure the GNU Lesser General Public License-
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and-
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.-
25**-
26** As a special exception, The Qt Company gives you certain additional-
27** rights. These rights are described in The Qt Company LGPL Exception-
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.-
29**-
30** $QT_END_LICENSE$-
31**-
32****************************************************************************/-
33-
34#include "qsharedpointer.h"-
35-
36// to be sure we aren't causing a namespace clash:-
37#include "qshareddata.h"-
38-
39/*!-
40 \class QSharedPointer-
41 \inmodule QtCore-
42 \brief The QSharedPointer class holds a strong reference to a shared pointer-
43 \since 4.5-
44-
45 \reentrant-
46-
47 The QSharedPointer is an automatic, shared pointer in C++. It-
48 behaves exactly like a normal pointer for normal purposes,-
49 including respect for constness.-
50-
51 QSharedPointer will delete the pointer it is holding when it goes-
52 out of scope, provided no other QSharedPointer objects are-
53 referencing it.-
54-
55 A QSharedPointer object can be created from a normal pointer,-
56 another QSharedPointer object or by promoting a-
57 QWeakPointer object to a strong reference.-
58-
59 \section1 Thread-Safety-
60-
61 QSharedPointer and QWeakPointer are thread-safe and operate-
62 atomically on the pointer value. Different threads can also access-
63 the QSharedPointer or QWeakPointer pointing to the same object at-
64 the same time without need for locking mechanisms.-
65-
66 It should be noted that, while the pointer value can be accessed-
67 in this manner, QSharedPointer and QWeakPointer provide no-
68 guarantee about the object being pointed to. Thread-safety and-
69 reentrancy rules for that object still apply.-
70-
71 \section1 Other Pointer Classes-
72-
73 Qt also provides two other pointer wrapper classes: QPointer and-
74 QSharedDataPointer. They are incompatible with one another, since-
75 each has its very different use case.-
76-
77 QSharedPointer holds a shared pointer by means of an external-
78 reference count (i.e., a reference counter placed outside the-
79 object). Like its name indicates, the pointer value is shared-
80 among all instances of QSharedPointer and QWeakPointer. The-
81 contents of the object pointed to by the pointer should not be-
82 considered shared, however: there is only one object. For that-
83 reason, QSharedPointer does not provide a way to detach or make-
84 copies of the pointed object.-
85-
86 QSharedDataPointer, on the other hand, holds a pointer to shared-
87 data (i.e., a class derived from QSharedData). It does so by means-
88 of an internal reference count, placed in the QSharedData base-
89 class. This class can, therefore, detach based on the type of-
90 access made to the data being guarded: if it's a non-const access,-
91 it creates a copy atomically for the operation to complete.-
92-
93 QExplicitlySharedDataPointer is a variant of QSharedDataPointer, except-
94 that it only detaches if QExplicitlySharedDataPointer::detach() is-
95 explicitly called (hence the name).-
96-
97 QScopedPointer simply holds a pointer to a heap allocated object and-
98 deletes it in its destructor. This class is useful when an object needs to-
99 be heap allocated and deleted, but no more. QScopedPointer is lightweight,-
100 it makes no use of additional structure or reference counting.-
101-
102 Finally, QPointer holds a pointer to a QObject-derived object, but it-
103 does so weakly. QWeakPointer has the same functionality, but its use for-
104 that function is deprecated.-
105-
106 \section1 Optional Pointer Tracking-
107-
108 A feature of QSharedPointer that can be enabled at compile-time for-
109 debugging purposes is a pointer tracking mechanism. When enabled,-
110 QSharedPointer registers in a global set all the pointers that it tracks.-
111 This allows one to catch mistakes like assigning the same pointer to two-
112 QSharedPointer objects.-
113-
114 This function is enabled by defining the \tt{QT_SHAREDPOINTER_TRACK_POINTERS}-
115 macro before including the QSharedPointer header.-
116-
117 It is safe to use this feature even with code compiled without the-
118 feature. QSharedPointer will ensure that the pointer is removed from the-
119 tracker even from code compiled without pointer tracking.-
120-
121 Note, however, that the pointer tracking feature has limitations on-
122 multiple- or virtual-inheritance (that is, in cases where two different-
123 pointer addresses can refer to the same object). In that case, if a-
124 pointer is cast to a different type and its value changes,-
125 QSharedPointer's pointer tracking mechanism may fail to detect that the-
126 object being tracked is the same.-
127-
128 \omit-
129 \section1 QSharedPointer internals-
130-
131 QSharedPointer has two "private" members: the pointer itself being tracked-
132 and a d-pointer. Those members are private to the class, but QSharedPointer-
133 is friends with QWeakPointer and other QSharedPointer with different-
134 template arguments. (On some compilers, template friends are not supported,-
135 so the members are technically public)-
136-
137 The reason for keeping the pointer value itself outside the d-pointer is-
138 because of multiple inheritance needs. If you have two QSharedPointer-
139 objects of different pointer types, but pointing to the same object in-
140 memory, it could happen that the pointer values are different. The \tt-
141 differentPointers autotest exemplifies this problem. The same thing could-
142 happen in the case of virtual inheritance: a pointer of class matching-
143 the virtual base has different address compared to the pointer of the-
144 complete object. See the \tt virtualBaseDifferentPointers autotest for-
145 this problem.-
146-
147 The d pointer is a pointer to QtSharedPointer::ExternalRefCountData, but it-
148 always points to one of the two classes derived from ExternalRefCountData.-
149-
150 \section2 d-pointer-
151 \section3 QtSharedPointer::ExternalRefCountData-
152-
153 It is basically a reference-counted reference-counter plus a pointer to the-
154 function to be used to delete the pointer. It has three members: \tt-
155 strongref, \tt weakref, and \tt destroyer. The strong reference counter is-
156 controlling the lifetime of the object tracked by QSharedPointer. A-
157 positive value indicates that the object is alive. It's also the number of-
158 QSharedObject instances that are attached to this Data.-
159-
160 When the strong reference count decreases to zero, the object is deleted-
161 (see below for information on custom deleters). The strong reference count-
162 can also exceptionally be -1, indicating that there are no QSharedPointers-
163 attached to an object, which is tracked too. The only case where this is-
164 possible is that of QWeakPointers and QPointers tracking a QObject. Note-
165 that QWeakPointers tracking a QObject is a deprecated feature as of Qt 5.0,-
166 kept only for compatibility with Qt 4.x.-
167-
168 The weak reference count controls the lifetime of the d-pointer itself.-
169 It can be thought of as an internal/intrusive reference count for-
170 ExternalRefCountData itself. This count is equal to the number of-
171 QSharedPointers and QWeakPointers that are tracking this object. In case-
172 the object is a QObject being tracked by QPointer, this number is increased-
173 by 1, since QObjectPrivate tracks it too.-
174-
175 The third member is a pointer to the function that is used to delete the-
176 pointer being tracked. That happens when the destroy() function is called.-
177-
178 The size of this class is the size of the two atomic ints plus the size of-
179 a pointer. On 32-bit architectures, that's 12 bytes, whereas on 64-bit ones-
180 it's 16 bytes. There is no padding.-
181-
182 \section3 QtSharedPointer::ExternalRefCountWithCustomDeleter-
183-
184 This class derives from ExternalRefCountData and is a template class. As-
185 template parameters, it has the type of the pointer being tracked (\tt T)-
186 and a \tt Deleter, which is anything. It adds two fields to its parent-
187 class, matching those template parameters: a member of type \tt Deleter and-
188 a member of type \tt T*. Those members are actually inside a template-
189 struct of type CustomDeleter, which is partially-specialized for normal-
190 deletion. See below for more details on that.-
191-
192 The purpose of this class is to store the pointer to be deleted and the-
193 deleter code along with the d-pointer. This allows the last strong-
194 reference to call any arbitrary function that disposes of the object. For-
195 example, this allows calling QObject::deleteLater() on a given object.-
196 The pointer to the object is kept here because it needs to match the actual-
197 deleter function's parameters, regardless of what template argument the-
198 last QSharedPointer instance had.-
199-
200 This class is never instantiated directly: the constructors and-
201 destructor are private and, in C++11, deleted. Only the create() function-
202 may be called to return an object of this type. See below for construction-
203 details.-
204-
205 The size of this class depends on the size of \tt Deleter. If it's an empty-
206 functor (i.e., no members), ABIs generally assign it the size of 1. But-
207 given that it's followed by a pointer, padding bytes may be inserted so-
208 that the alignment of the class and of the pointer are correct. In that-
209 case, the size of this class is 12+4+4 = 20 bytes on 32-bit architectures,-
210 or 16+8+8 = 40 bytes on 64-bit architectures. If \tt Deleter is a function-
211 pointer, the size should be the same as the empty structure case. If \tt-
212 Deleter is a pointer to a member function (PMF), the size will be bigger-
213 and will depend on the ABI. For architectures using the Itanium C++ ABI, a-
214 PMF is twice the size of a normal pointer. In that case, the size of this-
215 structure will be 12+8+4 = 24 bytes on 32-bit architectures, 16+16+8 = 40-
216 bytes on 64-bit ones.-
217-
218 If the deleter was not specified when creating the QSharedPointer object-
219 (i.e., if a standard \tt delete call is expected), then there's an-
220 optimization that avoids the need to store another function pointer in-
221 ExternalRefCountWithCustomDeleter. Instead, a template specialization makes-
222 a direct delete call. The size of the structure, in this case, is 12+4 = 16-
223 bytes on 32-bit architectures, 16+8 = 24 bytes on 64-bit ones.-
224-
225 \section3 QtSharedPointer::ExternalRefCountWithContiguousData-
226-
227 This class also derives from ExternalRefCountData and it is-
228 also a template class. The template parameter is the type \tt T of the-
229 class which QSharedPointer tracks. It adds only one member to its parent,-
230 which is of type \tt T (the actual type, not a pointer to it).-
231-
232 The purpose of this class is to lay the \tt T object out next to the-
233 reference counts, saving one memory allocation per shared pointer. This-
234 is particularly interesting for small \tt T or for the cases when there-
235 are few if any QWeakPointer tracking the object. This class exists to-
236 implement the QSharedPointer::create() call.-
237-
238 Like ExternalRefCountWithCustomDeleter, this class is never instantiated-
239 directly. This class also provides a create() member that returns the-
240 pointer, and hides its constructors and destructor. With C++11, they're-
241 deleted.-
242-
243 The size of this class depends on the size of \tt T.-
244-
245 \section3 Instantiating ExternalRefCountWithCustomDeleter and ExternalRefCountWithContiguousData-
246-
247 Like explained above, these classes have private constructors. Moreover,-
248 they are not defined anywhere, so trying to call \tt{new ClassType} would-
249 result in a compilation or linker error. Instead, these classes must be-
250 constructed via their create() methods.-
251-
252 Instead of instantiating the class by the normal way, the create() method-
253 calls \tt{operator new} directly with the size of the class, then calls-
254 the parent class's constructor only (that is, ExternalRefCountData's constructor).-
255 This ensures that the inherited members are initialised properly.-
256-
257 After initialising the base class, the-
258 ExternalRefCountWithCustomDeleter::create() function initialises the new-
259 members directly, by using the placement \tt{operator new}. In the case-
260 of the ExternalRefCountWithContiguousData::create() function, the address-
261 to the still-uninitialised \tt T member is saved for the callee to use.-
262 The member is only initialised in QSharedPointer::create(), so that we-
263 avoid having many variants of the internal functions according to the-
264 arguments in use for calling the constructor.-
265-
266 When initialising the parent class, the create() functions pass the-
267 address of the static deleter() member function. That is, when the-
268 destroy() function is called by QSharedPointer, the deleter() functions-
269 are called instead. These functions static_cast the ExternalRefCountData*-
270 parameter to their own type and execute their deletion: for the-
271 ExternalRefCountWithCustomDeleter::deleter() case, it runs the user's-
272 custom deleter, then destroys the deleter; for-
273 ExternalRefCountWithContiguousData::deleter, it simply calls the \tt T-
274 destructor directly.-
275-
276 Only one non-inline function is required per template, which is-
277 the deleter() static member. All the other functions can be inlined.-
278 What's more, the address of deleter() is calculated only in code, which-
279 can be resolved at link-time if the linker can determine that the-
280 function lies in the current application or library module (since these-
281 classes are not exported, that is the case for Windows or for builds with-
282 \tt{-fvisibility=hidden}).-
283-
284 \section3 Modifications due to pointer-tracking-
285-
286 To ensure that pointers created with pointer-tracking enabled get-
287 un-tracked when destroyed, even if destroyed by code compiled without the-
288 feature, QSharedPointer modifies slightly the instructions of the-
289 previous sections.-
290-
291 When ExternalRefCountWithCustomDeleter or-
292 ExternalRefCountWithContiguousData are used, their create() functions-
293 will set the ExternalRefCountData::destroyer function-
294 pointer to safetyCheckDeleter() instead. These static member functions-
295 simply call internalSafetyCheckRemove() before passing control to the-
296 normal deleter() function.-
297-
298 If neither custom deleter nor QSharedPointer::create() are used, then-
299 QSharedPointer uses a custom deleter of its own: the normalDeleter()-
300 function, which simply calls \tt delete. By using a custom deleter, the-
301 safetyCheckDeleter() procedure described above kicks in.-
302-
303 \endomit-
304-
305 \sa QSharedDataPointer, QWeakPointer, QScopedPointer-
306*/-
307-
308/*!-
309 \class QWeakPointer-
310 \inmodule QtCore-
311 \brief The QWeakPointer class holds a weak reference to a shared pointer-
312 \since 4.5-
313 \reentrant-
314-
315 The QWeakPointer is an automatic weak reference to a-
316 pointer in C++. It cannot be used to dereference the pointer-
317 directly, but it can be used to verify if the pointer has been-
318 deleted or not in another context.-
319-
320 QWeakPointer objects can only be created by assignment from a-
321 QSharedPointer.-
322-
323 It's important to note that QWeakPointer provides no automatic casting-
324 operators to prevent mistakes from happening. Even though QWeakPointer-
325 tracks a pointer, it should not be considered a pointer itself, since it-
326 doesn't guarantee that the pointed object remains valid.-
327-
328 Therefore, to access the pointer that QWeakPointer is tracking, you must-
329 first promote it to QSharedPointer and verify if the resulting object is-
330 null or not. QSharedPointer guarantees that the object isn't deleted, so-
331 if you obtain a non-null object, you may use the pointer. See-
332 QWeakPointer::toStrongRef() for an example.-
333-
334 QWeakPointer also provides the QWeakPointer::data() method that returns-
335 the tracked pointer without ensuring that it remains valid. This function-
336 is provided if you can guarantee by external means that the object will-
337 not get deleted (or if you only need the pointer value) and the cost of-
338 creating a QSharedPointer using toStrongRef() is too high.-
339-
340 \omit-
341 \section1 QWeakPointer internals-
342-
343 QWeakPointer shares most of its internal functionality with-
344 \l{QSharedPointer#qsharedpointer-internals}{QSharedPointer}, so see that-
345 class's internal documentation for more information.-
346-
347 QWeakPointer requires an external reference counter in order to operate.-
348 Therefore, it is incompatible by design with \l QSharedData-derived-
349 classes.-
350-
351 It has a special QObject constructor, which works by calling-
352 QtSharedPointer::ExternalRefCountData::getAndRef, which retrieves the-
353 d-pointer from QObjectPrivate. If one isn't set yet, that function-
354 creates the d-pointer and atomically sets it.-
355-
356 If getAndRef needs to create a d-pointer, it sets the strongref to -1,-
357 indicating that the QObject is not shared: QWeakPointer is used only to-
358 determine whether the QObject has been deleted. In that case, it cannot-
359 be upgraded to QSharedPointer (see the previous section).-
360-
361 \endomit-
362-
363 \sa QSharedPointer, QScopedPointer-
364*/-
365-
366/*!-
367 \class QEnableSharedFromThis-
368 \inmodule QtCore-
369 \brief A base class that allows obtaining a QSharedPointer for an object already managed by a shared pointer-
370 \since 5.4-
371-
372 You can inherit this class when you need to create a QSharedPointer-
373 from any instance of a class; for instance, from within the-
374 object itself. The key point is that the technique of-
375 just returning QSharedPointer<T>(this) can not be used, because-
376 this winds up creating multiple distinct QSharedPointer objects-
377 with separate reference counts. For this reason you must never-
378 create more than one QSharedPointer from the same raw pointer.-
379-
380 QEnableSharedFromThis defines two member functions called-
381 sharedFromThis() that return a QSharedPointer<T> and-
382 QSharedPointer<const T>, depending on constness, to \c this:-
383-
384 \code-
385 class Y: public QEnableSharedFromThis<Y>-
386 {-
387 public:-
388 QSharedPointer<Y> f()-
389 {-
390 return sharedFromThis();-
391 }-
392 };-
393-
394 int main()-
395 {-
396 QSharedPointer<Y> p(new Y());-
397 QSharedPointer<Y> y = p->f();-
398 Q_ASSERT(p == y); // p and q must share ownership-
399 }-
400 \endcode-
401-
402 It is also possible to get a shared pointer from an object outside of-
403 the class itself. This is especially useful in code that provides an-
404 interface to scripts, where it is currently not possible to use shared-
405 pointers. For example:-
406-
407 \code-
408 class ScriptInterface : public QObject-
409 {-
410 Q_OBJECT-
411-
412 // ...-
413-
414 public slots:-
415 void slotCalledByScript(Y *managedBySharedPointer)-
416 {-
417 QSharedPointer<Y> yPtr = managedBySharedPointer->sharedFromThis();-
418 // Some other code unrelated to scripts that expects a QSharedPointer<Y> ...-
419 }-
420 };-
421 \endcode-
422*/-
423-
424/*!-
425 \fn QSharedPointer::QSharedPointer()-
426-
427 Creates a QSharedPointer that points to null (0).-
428*/-
429-
430/*!-
431 \fn QSharedPointer::~QSharedPointer()-
432-
433 Destroys this QSharedPointer object. If it is the last reference to-
434 the pointer stored, this will delete the pointer as well.-
435*/-
436-
437/*!-
438 \fn QSharedPointer::QSharedPointer(T *ptr)-
439-
440 Creates a QSharedPointer that points to \a ptr. The pointer \a ptr-
441 becomes managed by this QSharedPointer and must not be passed to-
442 another QSharedPointer object or deleted outside this object.-
443*/-
444-
445/*!-
446 \fn QSharedPointer::QSharedPointer(T *ptr, Deleter deleter)-
447-
448 Creates a QSharedPointer that points to \a ptr. The pointer \a ptr-
449 becomes managed by this QSharedPointer and must not be passed to-
450 another QSharedPointer object or deleted outside this object.-
451-
452 The \a deleter parameter specifies the custom deleter for this-
453 object. The custom deleter is called, instead of the operator delete(),-
454 when the strong reference count drops to 0. This is useful,-
455 for instance, for calling \l {QObject::}{deleteLater()} on a QObject instead:-
456-
457 \code-
458 static void doDeleteLater(MyObject *obj)-
459 {-
460 obj->deleteLater();-
461 }-
462-
463 void otherFunction()-
464 {-
465 QSharedPointer<MyObject> obj =-
466 QSharedPointer<MyObject>(new MyObject, doDeleteLater);-
467-
468 // continue using obj-
469 obj.clear(); // calls obj->deleteLater();-
470 }-
471 \endcode-
472-
473 It is also possible to specify a member function directly, as in:-
474 \code-
475 QSharedPointer<MyObject> obj =-
476 QSharedPointer<MyObject>(new MyObject, &QObject::deleteLater);-
477 \endcode-
478-
479 \sa clear()-
480*/-
481-
482/*!-
483 \fn QSharedPointer::QSharedPointer(const QSharedPointer<T> &other)-
484-
485 Creates a QSharedPointer object that shares \a other's pointer.-
486-
487 If \tt T is a derived type of the template parameter of this class,-
488 QSharedPointer will perform an automatic cast. Otherwise, you will-
489 get a compiler error.-
490*/-
491-
492/*!-
493 \fn QSharedPointer::QSharedPointer(const QWeakPointer<T> &other)-
494-
495 Creates a QSharedPointer by promoting the weak reference \a other-
496 to strong reference and sharing its pointer.-
497-
498 If \tt T is a derived type of the template parameter of this-
499 class, QSharedPointer will perform an automatic cast. Otherwise,-
500 you will get a compiler error.-
501-
502 \sa QWeakPointer::toStrongRef()-
503*/-
504-
505/*!-
506 \fn QSharedPointer &QSharedPointer::operator=(const QSharedPointer<T> &other)-
507-
508 Makes this object share \a other's pointer. The current pointer-
509 reference is discarded and, if it was the last, the pointer will-
510 be deleted.-
511-
512 If \tt T is a derived type of the template parameter of this-
513 class, QSharedPointer will perform an automatic cast. Otherwise,-
514 you will get a compiler error.-
515*/-
516-
517/*!-
518 \fn QSharedPointer &QSharedPointer::operator=(const QWeakPointer<T> &other)-
519-
520 Promotes \a other to a strong reference and makes this object-
521 share a reference to the pointer referenced by it. The current pointer-
522 reference is discarded and, if it was the last, the pointer will-
523 be deleted.-
524-
525 If \tt T is a derived type of the template parameter of this-
526 class, QSharedPointer will perform an automatic cast. Otherwise,-
527 you will get a compiler error.-
528*/-
529-
530/*!-
531 \fn void QSharedPointer::swap(QSharedPointer<T> &other);-
532 \since 5.3-
533-
534 Swaps this shared pointer instance with \a other. This function is-
535 very fast and never fails.-
536*/-
537-
538/*!-
539 \fn T *QSharedPointer::data() const-
540-
541 Returns the value of the pointer referenced by this object.-
542-
543 Note: do not delete the pointer returned by this function or pass-
544 it to another function that could delete it, including creating-
545 QSharedPointer or QWeakPointer objects.-
546*/-
547-
548/*!-
549 \fn T &QSharedPointer::operator *() const-
550-
551 Provides access to the shared pointer's members.-
552-
553 \sa isNull()-
554*/-
555-
556/*!-
557 \fn T *QSharedPointer::operator ->() const-
558-
559 Provides access to the shared pointer's members.-
560-
561 \sa isNull()-
562*/-
563-
564/*!-
565 \fn bool QSharedPointer::isNull() const-
566-
567 Returns \c true if this object is holding a reference to a null-
568 pointer.-
569*/-
570-
571/*!-
572 \fn QSharedPointer::operator bool() const-
573-
574 Returns \c true if this object is not null. This function is suitable-
575 for use in \tt if-constructs, like:-
576-
577 \code-
578 if (sharedptr) { ... }-
579 \endcode-
580-
581 \sa isNull()-
582*/-
583-
584/*!-
585 \fn bool QSharedPointer::operator !() const-
586-
587 Returns \c true if this object is null. This function is suitable-
588 for use in \tt if-constructs, like:-
589-
590 \code-
591 if (!sharedptr) { ... }-
592 \endcode-
593-
594 \sa isNull()-
595*/-
596-
597/*!-
598 \fn QSharedPointer<X> QSharedPointer::staticCast() const-
599-
600 Performs a static cast from this pointer's type to \tt X and returns-
601 a QSharedPointer that shares the reference. This function can be-
602 used for up- and for down-casting, but is more useful for-
603 up-casting.-
604-
605 Note: the template type \c X must have the same const and volatile-
606 qualifiers as the template of this object, or the cast will-
607 fail. Use constCast() if you need to drop those qualifiers.-
608-
609 \sa dynamicCast(), constCast(), qSharedPointerCast()-
610*/-
611-
612/*!-
613 \fn QSharedPointer<X> QSharedPointer::dynamicCast() const-
614-
615 Performs a dynamic cast from this pointer's type to \tt X and-
616 returns a QSharedPointer that shares the reference. If this-
617 function is used to up-cast, then QSharedPointer will perform a \tt-
618 dynamic_cast, which means that if the object being pointed by this-
619 QSharedPointer is not of type \tt X, the returned object will be-
620 null.-
621-
622 Note: the template type \c X must have the same const and volatile-
623 qualifiers as the template of this object, or the cast will-
624 fail. Use constCast() if you need to drop those qualifiers.-
625-
626 \sa qSharedPointerDynamicCast()-
627*/-
628-
629/*!-
630 \fn QSharedPointer<X> QSharedPointer::constCast() const-
631-
632 Performs a \tt const_cast from this pointer's type to \tt X and returns-
633 a QSharedPointer that shares the reference. This function can be-
634 used for up- and for down-casting, but is more useful for-
635 up-casting.-
636-
637 \sa isNull(), qSharedPointerConstCast()-
638*/-
639-
640/*!-
641 \fn QSharedPointer<X> QSharedPointer::objectCast() const-
642 \since 4.6-
643-
644 Performs a \l qobject_cast() from this pointer's type to \tt X and-
645 returns a QSharedPointer that shares the reference. If this-
646 function is used to up-cast, then QSharedPointer will perform a \tt-
647 qobject_cast, which means that if the object being pointed by this-
648 QSharedPointer is not of type \tt X, the returned object will be-
649 null.-
650-
651 Note: the template type \c X must have the same const and volatile-
652 qualifiers as the template of this object, or the cast will-
653 fail. Use constCast() if you need to drop those qualifiers.-
654-
655 \sa qSharedPointerObjectCast()-
656*/-
657-
658/*!-
659 \fn QSharedPointer<T> QSharedPointer::create()-
660 \since 5.1-
661-
662 Creates a QSharedPointer object and allocates a new item of type \tt T. The-
663 QSharedPointer internals and the object are allocated in one single memory-
664 allocation, which could help reduce memory fragmentation in a long-running-
665 application.-
666-
667 This function calls the default constructor for type \tt T.-
668*/-
669-
670/*!-
671 \fn QSharedPointer<T> QSharedPointer::create(...)-
672 \overload-
673 \since 5.1-
674-
675 Creates a QSharedPointer object and allocates a new item of type \tt T. The-
676 QSharedPointer internals and the object are allocated in one single memory-
677 allocation, which could help reduce memory fragmentation in a long-running-
678 application.-
679-
680 This function will attempt to call a constructor for type \tt T that can-
681 accept all the arguments passed. Arguments will be perfectly-forwarded.-
682-
683 \note This function is only fully available with a C++11 compiler that-
684 supports perfect forwarding of an arbitrary number of arguments.-
685-
686 If the compiler does not support the necessary C++11 features,-
687 then a restricted version is available since Qt 5.4: you may pass-
688 one (but just one) argument, and it will always be passed by const-
689 reference.-
690-
691 If you target Qt before version 5.4, you must use the overload-
692 that calls the default constructor.-
693*/-
694-
695/*!-
696 \fn QWeakPointer<T> QSharedPointer::toWeakRef() const-
697-
698 Returns a weak reference object that shares the pointer referenced-
699 by this object.-
700-
701 \sa QWeakPointer::QWeakPointer()-
702*/-
703-
704/*!-
705 \fn void QSharedPointer::clear()-
706-
707 Clears this QSharedPointer object, dropping the reference that it-
708 may have had to the pointer. If this was the last reference, then-
709 the pointer itself will be deleted.-
710*/-
711-
712/*!-
713 \fn void QSharedPointer::reset()-
714 \since 5.0-
715-
716 Same as clear(). For std::shared_ptr compatibility.-
717*/-
718-
719/*!-
720 \fn void QSharedPointer::reset(T *t)-
721 \since 5.0-
722-
723 Resets this QSharedPointer object to point to \a t-
724 instead. Equivalent to:-
725 \code-
726 QSharedPointer<T> other(t); this->swap(other);-
727 \endcode-
728*/-
729-
730/*!-
731 \fn void QSharedPointer::reset(T *t, Deleter deleter)-
732 \since 5.0-
733-
734 Resets this QSharedPointer object to point to \a t-
735 instead, with deleter \a deleter. Equivalent to:-
736 \code-
737 QSharedPointer<T> other(t, deleter); this->swap(other);-
738 \endcode-
739*/-
740-
741/*!-
742 \fn QWeakPointer::QWeakPointer()-
743-
744 Creates a QWeakPointer that points to nothing.-
745*/-
746-
747/*!-
748 \fn QWeakPointer::~QWeakPointer()-
749-
750 Destroys this QWeakPointer object. The pointer referenced-
751 by this object will not be deleted.-
752*/-
753-
754/*!-
755 \fn QWeakPointer::QWeakPointer(const QWeakPointer<T> &other)-
756-
757 Creates a QWeakPointer that holds a weak reference to the-
758 pointer referenced by \a other.-
759-
760 If \tt T is a derived type of the template parameter of this-
761 class, QWeakPointer will perform an automatic cast. Otherwise,-
762 you will get a compiler error.-
763*/-
764-
765/*!-
766 \fn QWeakPointer::QWeakPointer(const QSharedPointer<T> &other)-
767-
768 Creates a QWeakPointer that holds a weak reference to the-
769 pointer referenced by \a other.-
770-
771 If \tt T is a derived type of the template parameter of this-
772 class, QWeakPointer will perform an automatic cast. Otherwise,-
773 you will get a compiler error.-
774*/-
775-
776/*!-
777 \fn QWeakPointer::QWeakPointer(const QObject *obj)-
778 \since 4.6-
779 \deprecated-
780-
781 Creates a QWeakPointer that holds a weak reference directly to the-
782 QObject \a obj. This constructor is only available if the template type-
783 \tt T is QObject or derives from it (otherwise a compilation error will-
784 result).-
785-
786 You can use this constructor with any QObject, even if they were not-
787 created with \l QSharedPointer.-
788-
789 Note that QWeakPointers created this way on arbitrary QObjects usually-
790 cannot be promoted to QSharedPointer.-
791-
792 \sa QSharedPointer, QPointer-
793*/-
794-
795/*!-
796 \fn QWeakPointer &QWeakPointer::operator=(const QObject *obj)-
797 \since 4.6-
798 \deprecated-
799-
800 Makes this QWeakPointer hold a weak reference directly to the QObject-
801 \a obj. This function is only available if the template type \tt T is-
802 QObject or derives from it.-
803-
804 \sa QPointer-
805*/-
806-
807/*!-
808 \fn QWeakPointer &QWeakPointer::operator=(const QWeakPointer<T> &other)-
809-
810 Makes this object share \a other's pointer. The current pointer-
811 reference is discarded but is not deleted.-
812-
813 If \tt T is a derived type of the template parameter of this-
814 class, QWeakPointer will perform an automatic cast. Otherwise,-
815 you will get a compiler error.-
816*/-
817-
818/*!-
819 \fn QWeakPointer &QWeakPointer::operator=(const QSharedPointer<T> &other)-
820-
821 Makes this object share \a other's pointer. The current pointer-
822 reference is discarded but is not deleted.-
823-
824 If \tt T is a derived type of the template parameter of this-
825 class, QWeakPointer will perform an automatic cast. Otherwise,-
826 you will get a compiler error.-
827*/-
828-
829/*!-
830 \fn void QWeakPointer::swap(QWeakPointer<T> &other)-
831 \since 5.4-
832-
833 Swaps this weak pointer instance with \a other. This function is-
834 very fast and never fails.-
835*/-
836-
837/*!-
838 \fn bool QWeakPointer::isNull() const-
839-
840 Returns \c true if this object is holding a reference to a null-
841 pointer.-
842-
843 Note that, due to the nature of weak references, the pointer that-
844 QWeakPointer references can become null at any moment, so-
845 the value returned from this function can change from false to-
846 true from one call to the next.-
847*/-
848-
849/*!-
850 \fn QWeakPointer::operator bool() const-
851-
852 Returns \c true if this object is not null. This function is suitable-
853 for use in \tt if-constructs, like:-
854-
855 \code-
856 if (weakref) { ... }-
857 \endcode-
858-
859 Note that, due to the nature of weak references, the pointer that-
860 QWeakPointer references can become null at any moment, so-
861 the value returned from this function can change from true to-
862 false from one call to the next.-
863-
864 \sa isNull()-
865*/-
866-
867/*!-
868 \fn bool QWeakPointer::operator !() const-
869-
870 Returns \c true if this object is null. This function is suitable-
871 for use in \tt if-constructs, like:-
872-
873 \code-
874 if (!weakref) { ... }-
875 \endcode-
876-
877 Note that, due to the nature of weak references, the pointer that-
878 QWeakPointer references can become null at any moment, so-
879 the value returned from this function can change from false to-
880 true from one call to the next.-
881-
882 \sa isNull()-
883*/-
884-
885/*!-
886 \fn T *QWeakPointer::data() const-
887 \since 4.6-
888-
889 Returns the value of the pointer being tracked by this QWeakPointer,-
890 \b without ensuring that it cannot get deleted. To have that guarantee,-
891 use toStrongRef(), which returns a QSharedPointer object. If this-
892 function can determine that the pointer has already been deleted, it-
893 returns 0.-
894-
895 It is ok to obtain the value of the pointer and using that value itself,-
896 like for example in debugging statements:-
897-
898 \code-
899 qDebug("Tracking %p", weakref.data());-
900 \endcode-
901-
902 However, dereferencing the pointer is only allowed if you can guarantee-
903 by external means that the pointer does not get deleted. For example,-
904 if you can be certain that no other thread can delete it, nor the-
905 functions that you may call.-
906-
907 If that is the case, then the following code is valid:-
908-
909 \code-
910 // this pointer cannot be used in another thread-
911 // so other threads cannot delete it-
912 QWeakPointer<int> weakref = obtainReference();-
913-
914 Object *obj = weakref.data();-
915 if (obj) {-
916 // if the pointer wasn't deleted yet, we know it can't get-
917 // deleted by our own code here nor the functions we call-
918 otherFunction(obj);-
919 }-
920 \endcode-
921-
922 Use this function with care.-
923-
924 \sa isNull(), toStrongRef()-
925*/-
926-
927/*!-
928 \fn QSharedPointer<T> QWeakPointer::toStrongRef() const-
929-
930 Promotes this weak reference to a strong one and returns a-
931 QSharedPointer object holding that reference. When promoting to-
932 QSharedPointer, this function verifies if the object has been deleted-
933 already or not. If it hasn't, this function increases the reference-
934 count to the shared object, thus ensuring that it will not get-
935 deleted.-
936-
937 Since this function can fail to obtain a valid strong reference to the-
938 shared object, you should always verify if the conversion succeeded,-
939 by calling QSharedPointer::isNull() on the returned object.-
940-
941 For example, the following code promotes a QWeakPointer that was held-
942 to a strong reference and, if it succeeded, it prints the value of the-
943 integer that was held:-
944-
945 \code-
946 QWeakPointer<int> weakref;-
947-
948 // ...-
949-
950 QSharedPointer<int> strong = weakref.toStrongRef();-
951 if (strong)-
952 qDebug() << "The value is:" << *strong;-
953 else-
954 qDebug() << "The value has already been deleted";-
955 \endcode-
956-
957 \sa QSharedPointer::QSharedPointer()-
958*/-
959-
960/*!-
961 \fn QSharedPointer<T> QWeakPointer::lock() const-
962 \since 5.4-
963-
964 Same as toStrongRef().-
965-
966 This function is provided for API compatibility with std::weak_ptr.-
967*/-
968-
969/*!-
970 \fn void QWeakPointer::clear()-
971-
972 Clears this QWeakPointer object, dropping the reference that it-
973 may have had to the pointer.-
974*/-
975-
976/*!-
977 \fn QSharedPointer<T> QEnableSharedFromThis::sharedFromThis()-
978 \since 5.4-
979-
980 If \c this (that is, the subclass instance invoking this method) is being-
981 managed by a QSharedPointer, returns a shared pointer instance pointing to-
982 \c this; otherwise returns a QSharedPointer holding a null pointer.-
983*/-
984-
985/*!-
986 \fn QSharedPointer<const T> QEnableSharedFromThis::sharedFromThis() const-
987 \overload-
988 \since 5.4-
989-
990 Const overload of sharedFromThis().-
991*/-
992-
993/*!-
994 \fn bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2)-
995 \relates QSharedPointer-
996-
997 Returns \c true if the pointer referenced by \a ptr1 is the-
998 same pointer as that referenced by \a ptr2.-
999-
1000 If \a ptr2's template parameter is different from \a ptr1's,-
1001 QSharedPointer will attempt to perform an automatic \tt static_cast-
1002 to ensure that the pointers being compared are equal. If \a ptr2's-
1003 template parameter is not a base or a derived type from-
1004 \a ptr1's, you will get a compiler error.-
1005*/-
1006-
1007/*!-
1008 \fn bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2)-
1009 \relates QSharedPointer-
1010-
1011 Returns \c true if the pointer referenced by \a ptr1 is not the-
1012 same pointer as that referenced by \a ptr2.-
1013-
1014 If \a ptr2's template parameter is different from \a ptr1's,-
1015 QSharedPointer will attempt to perform an automatic \tt static_cast-
1016 to ensure that the pointers being compared are equal. If \a ptr2's-
1017 template parameter is not a base or a derived type from-
1018 \a ptr1's, you will get a compiler error.-
1019*/-
1020-
1021/*!-
1022 \fn bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2)-
1023 \relates QSharedPointer-
1024-
1025 Returns \c true if the pointer referenced by \a ptr1 is the-
1026 same pointer as \a ptr2.-
1027-
1028 If \a ptr2's type is different from \a ptr1's,-
1029 QSharedPointer will attempt to perform an automatic \tt static_cast-
1030 to ensure that the pointers being compared are equal. If \a ptr2's-
1031 type is not a base or a derived type from this-
1032 \a ptr1's, you will get a compiler error.-
1033*/-
1034-
1035/*!-
1036 \fn bool operator!=(const QSharedPointer<T> &ptr1, const X *ptr2)-
1037 \relates QSharedPointer-
1038-
1039 Returns \c true if the pointer referenced by \a ptr1 is not the-
1040 same pointer as \a ptr2.-
1041-
1042 If \a ptr2's type is different from \a ptr1's,-
1043 QSharedPointer will attempt to perform an automatic \tt static_cast-
1044 to ensure that the pointers being compared are equal. If \a ptr2's-
1045 type is not a base or a derived type from this-
1046 \a ptr1's, you will get a compiler error.-
1047*/-
1048-
1049/*!-
1050 \fn bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2)-
1051 \relates QSharedPointer-
1052-
1053 Returns \c true if the pointer \a ptr1 is the-
1054 same pointer as that referenced by \a ptr2.-
1055-
1056 If \a ptr2's template parameter is different from \a ptr1's type,-
1057 QSharedPointer will attempt to perform an automatic \tt static_cast-
1058 to ensure that the pointers being compared are equal. If \a ptr2's-
1059 template parameter is not a base or a derived type from-
1060 \a ptr1's type, you will get a compiler error.-
1061*/-
1062-
1063/*!-
1064 \fn bool operator!=(const T *ptr1, const QSharedPointer<X> &ptr2)-
1065 \relates QSharedPointer-
1066-
1067 Returns \c true if the pointer \a ptr1 is not the-
1068 same pointer as that referenced by \a ptr2.-
1069-
1070 If \a ptr2's template parameter is different from \a ptr1's type,-
1071 QSharedPointer will attempt to perform an automatic \tt static_cast-
1072 to ensure that the pointers being compared are equal. If \a ptr2's-
1073 template parameter is not a base or a derived type from-
1074 \a ptr1's type, you will get a compiler error.-
1075*/-
1076-
1077/*!-
1078 \fn bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2)-
1079 \relates QWeakPointer-
1080-
1081 Returns \c true if the pointer referenced by \a ptr1 is the-
1082 same pointer as that referenced by \a ptr2.-
1083-
1084 If \a ptr2's template parameter is different from \a ptr1's,-
1085 QSharedPointer will attempt to perform an automatic \tt static_cast-
1086 to ensure that the pointers being compared are equal. If \a ptr2's-
1087 template parameter is not a base or a derived type from-
1088 \a ptr1's, you will get a compiler error.-
1089*/-
1090-
1091/*!-
1092 \fn bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2)-
1093 \relates QWeakPointer-
1094-
1095 Returns \c true if the pointer referenced by \a ptr1 is not the-
1096 same pointer as that referenced by \a ptr2.-
1097-
1098 If \a ptr2's template parameter is different from \a ptr1's,-
1099 QSharedPointer will attempt to perform an automatic \tt static_cast-
1100 to ensure that the pointers being compared are equal. If \a ptr2's-
1101 template parameter is not a base or a derived type from-
1102 \a ptr1's, you will get a compiler error.-
1103*/-
1104-
1105/*!-
1106 \fn bool operator==(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2)-
1107 \relates QWeakPointer-
1108-
1109 Returns \c true if the pointer referenced by \a ptr1 is the-
1110 same pointer as that referenced by \a ptr2.-
1111-
1112 If \a ptr2's template parameter is different from \a ptr1's,-
1113 QSharedPointer will attempt to perform an automatic \tt static_cast-
1114 to ensure that the pointers being compared are equal. If \a ptr2's-
1115 template parameter is not a base or a derived type from-
1116 \a ptr1's, you will get a compiler error.-
1117*/-
1118-
1119/*!-
1120 \fn bool operator!=(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2)-
1121 \relates QWeakPointer-
1122-
1123 Returns \c true if the pointer referenced by \a ptr1 is not the-
1124 same pointer as that referenced by \a ptr2.-
1125-
1126 If \a ptr2's template parameter is different from \a ptr1's,-
1127 QSharedPointer will attempt to perform an automatic \tt static_cast-
1128 to ensure that the pointers being compared are equal. If \a ptr2's-
1129 template parameter is not a base or a derived type from-
1130 \a ptr1's, you will get a compiler error.-
1131*/-
1132-
1133/*!-
1134 \fn QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &other)-
1135 \relates QSharedPointer-
1136-
1137 Returns a shared pointer to the pointer held by \a other, cast to-
1138 type \tt X. The types \tt T and \tt X must belong to one-
1139 hierarchy for the \tt static_cast to succeed.-
1140-
1141 Note that \tt X must have the same cv-qualifiers (\tt const and-
1142 \tt volatile) that \tt T has, or the code will fail to-
1143 compile. Use qSharedPointerConstCast to cast away the constness.-
1144-
1145 \sa QSharedPointer::staticCast(), qSharedPointerDynamicCast(), qSharedPointerConstCast()-
1146*/-
1147-
1148/*!-
1149 \fn QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &other)-
1150 \relates QSharedPointer-
1151 \relates QWeakPointer-
1152-
1153 Returns a shared pointer to the pointer held by \a other, cast to-
1154 type \tt X. The types \tt T and \tt X must belong to one-
1155 hierarchy for the \tt static_cast to succeed.-
1156-
1157 The \a other object is converted first to a strong reference. If-
1158 that conversion fails (because the object it's pointing to has-
1159 already been deleted), this function returns a null-
1160 QSharedPointer.-
1161-
1162 Note that \tt X must have the same cv-qualifiers (\tt const and-
1163 \tt volatile) that \tt T has, or the code will fail to-
1164 compile. Use qSharedPointerConstCast to cast away the constness.-
1165-
1166 \sa QWeakPointer::toStrongRef(), qSharedPointerDynamicCast(), qSharedPointerConstCast()-
1167*/-
1168-
1169/*!-
1170 \fn QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &other)-
1171 \relates QSharedPointer-
1172-
1173 Returns a shared pointer to the pointer held by \a other, using a-
1174 dynamic cast to type \tt X to obtain an internal pointer of the-
1175 appropriate type. If the \tt dynamic_cast fails, the object-
1176 returned will be null.-
1177-
1178 Note that \tt X must have the same cv-qualifiers (\tt const and-
1179 \tt volatile) that \tt T has, or the code will fail to-
1180 compile. Use qSharedPointerConstCast to cast away the constness.-
1181-
1182 \sa QSharedPointer::dynamicCast(), qSharedPointerCast(), qSharedPointerConstCast()-
1183*/-
1184-
1185/*!-
1186 \fn QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer<T> &other)-
1187 \relates QSharedPointer-
1188 \relates QWeakPointer-
1189-
1190 Returns a shared pointer to the pointer held by \a other, using a-
1191 dynamic cast to type \tt X to obtain an internal pointer of the-
1192 appropriate type. If the \tt dynamic_cast fails, the object-
1193 returned will be null.-
1194-
1195 The \a other object is converted first to a strong reference. If-
1196 that conversion fails (because the object it's pointing to has-
1197 already been deleted), this function also returns a null-
1198 QSharedPointer.-
1199-
1200 Note that \tt X must have the same cv-qualifiers (\tt const and-
1201 \tt volatile) that \tt T has, or the code will fail to-
1202 compile. Use qSharedPointerConstCast to cast away the constness.-
1203-
1204 \sa QWeakPointer::toStrongRef(), qSharedPointerCast(), qSharedPointerConstCast()-
1205*/-
1206-
1207/*!-
1208 \fn QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &other)-
1209 \relates QSharedPointer-
1210-
1211 Returns a shared pointer to the pointer held by \a other, cast to-
1212 type \tt X. The types \tt T and \tt X must belong to one-
1213 hierarchy for the \tt const_cast to succeed. The \tt const and \tt-
1214 volatile differences between \tt T and \tt X are ignored.-
1215-
1216 \sa QSharedPointer::constCast(), qSharedPointerCast(), qSharedPointerDynamicCast()-
1217*/-
1218-
1219/*!-
1220 \fn QSharedPointer<X> qSharedPointerConstCast(const QWeakPointer<T> &other)-
1221 \relates QSharedPointer-
1222 \relates QWeakPointer-
1223-
1224 Returns a shared pointer to the pointer held by \a other, cast to-
1225 type \tt X. The types \tt T and \tt X must belong to one-
1226 hierarchy for the \tt const_cast to succeed. The \tt const and-
1227 \tt volatile differences between \tt T and \tt X are ignored.-
1228-
1229 The \a other object is converted first to a strong reference. If-
1230 that conversion fails (because the object it's pointing to has-
1231 already been deleted), this function returns a null-
1232 QSharedPointer.-
1233-
1234 \sa QWeakPointer::toStrongRef(), qSharedPointerCast(), qSharedPointerDynamicCast()-
1235*/-
1236-
1237/*!-
1238 \fn QSharedPointer<X> qSharedPointerObjectCast(const QSharedPointer<T> &other)-
1239 \relates QSharedPointer-
1240 \since 4.6-
1241-
1242 \brief The qSharedPointerObjectCast function is for casting a shared pointer.-
1243-
1244 Returns a shared pointer to the pointer held by \a other, using a-
1245 \l qobject_cast() to type \tt X to obtain an internal pointer of the-
1246 appropriate type. If the \tt qobject_cast fails, the object-
1247 returned will be null.-
1248-
1249 Note that \tt X must have the same cv-qualifiers (\tt const and-
1250 \tt volatile) that \tt T has, or the code will fail to-
1251 compile. Use qSharedPointerConstCast to cast away the constness.-
1252-
1253 \sa QSharedPointer::objectCast(), qSharedPointerCast(), qSharedPointerConstCast()-
1254*/-
1255-
1256/*!-
1257 \fn QSharedPointer<X> qSharedPointerObjectCast(const QWeakPointer<T> &other)-
1258 \relates QSharedPointer-
1259 \relates QWeakPointer-
1260 \since 4.6-
1261-
1262 \brief The qSharedPointerObjectCast function is for casting a shared pointer.-
1263-
1264 Returns a shared pointer to the pointer held by \a other, using a-
1265 \l qobject_cast() to type \tt X to obtain an internal pointer of the-
1266 appropriate type. If the \tt qobject_cast fails, the object-
1267 returned will be null.-
1268-
1269 The \a other object is converted first to a strong reference. If-
1270 that conversion fails (because the object it's pointing to has-
1271 already been deleted), this function also returns a null-
1272 QSharedPointer.-
1273-
1274 Note that \tt X must have the same cv-qualifiers (\tt const and-
1275 \tt volatile) that \tt T has, or the code will fail to-
1276 compile. Use qSharedPointerConstCast to cast away the constness.-
1277-
1278 \sa QWeakPointer::toStrongRef(), qSharedPointerCast(), qSharedPointerConstCast()-
1279*/-
1280-
1281-
1282/*!-
1283 \fn QWeakPointer<X> qWeakPointerCast(const QWeakPointer<T> &other)-
1284 \relates QWeakPointer-
1285-
1286 Returns a weak pointer to the pointer held by \a other, cast to-
1287 type \tt X. The types \tt T and \tt X must belong to one-
1288 hierarchy for the \tt static_cast to succeed.-
1289-
1290 Note that \tt X must have the same cv-qualifiers (\tt const and-
1291 \tt volatile) that \tt T has, or the code will fail to-
1292 compile. Use qSharedPointerConstCast to cast away the constness.-
1293*/-
1294-
1295#include <qset.h>-
1296#include <qmutex.h>-
1297-
1298#if !defined(QT_NO_QOBJECT)-
1299#include "private/qobject_p.h"-
1300-
1301QT_BEGIN_NAMESPACE-
1302-
1303/*!-
1304 \internal-
1305 This function is called for a just-created QObject \a obj, to enable-
1306 the use of QSharedPointer and QWeakPointer in the future.-
1307 */-
1308void QtSharedPointer::ExternalRefCountData::setQObjectShared(const QObject *, bool)-
1309{}-
1310-
1311/*!-
1312 \internal-
1313 This function is called when a QSharedPointer is created from a QWeakPointer-
1314-
1315 We check that the QWeakPointer was really created from a QSharedPointer, and-
1316 not from a QObject.-
1317*/-
1318void QtSharedPointer::ExternalRefCountData::checkQObjectShared(const QObject *)-
1319{-
1320 if (strongref.load() < 0)
strongref.load() < 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSharedPointer
FALSEevaluated 60 times by 5 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkAccessManager_And_QProgressDialog
  • tst_QNetworkDiskCache
  • tst_QSharedPointer
  • tst_Spdy
2-60
1321 qWarning("QSharedPointer: cannot create a QSharedPointer from a QObject-tracking QWeakPointer");
executed 2 times by 1 test: QMessageLogger(__FILE__, 1321, __PRETTY_FUNCTION__).warning("QSharedPointer: cannot create a QSharedPointer from a QObject-tracking QWeakPointer");
Executed by:
  • tst_QSharedPointer
2
1322}
executed 62 times by 5 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkAccessManager_And_QProgressDialog
  • tst_QNetworkDiskCache
  • tst_QSharedPointer
  • tst_Spdy
62
1323-
1324QtSharedPointer::ExternalRefCountData *QtSharedPointer::ExternalRefCountData::getAndRef(const QObject *obj)-
1325{-
1326 Q_ASSERT(obj);-
1327 QObjectPrivate *d = QObjectPrivate::get(const_cast<QObject *>(obj));-
1328 Q_ASSERT_X(!d->wasDeleted, "QWeakPointer", "Detected QWeakPointer creation in a QObject being deleted");-
1329-
1330 ExternalRefCountData *that = d->sharedRefcount.load();-
1331 if (that) {
thatDescription
TRUEevaluated 216197 times by 224 tests
Evaluated by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAnimationGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QBrush
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • ...
FALSEevaluated 68632 times by 243 tests
Evaluated by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAbstractTextDocumentLayout
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAnimationGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QBrush
  • tst_QBuffer
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • ...
68632-216197
1332 that->weakref.ref();-
1333 return that;
executed 216197 times by 224 tests: return that;
Executed by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAnimationGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QBrush
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • ...
216197
1334 }-
1335-
1336 // we can create the refcount data because it doesn't exist-
1337 ExternalRefCountData *x = new ExternalRefCountData(Qt::Uninitialized);-
1338 x->strongref.store(-1);-
1339 x->weakref.store(2); // the QWeakPointer that called us plus the QObject itself-
1340 if (!d->sharedRefcount.testAndSetRelease(0, x)) {
!d->sharedRefc...tRelease(0, x)Description
TRUEnever evaluated
FALSEevaluated 68632 times by 243 tests
Evaluated by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAbstractTextDocumentLayout
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAnimationGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QBrush
  • tst_QBuffer
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • ...
0-68632
1341 delete x;-
1342 x = d->sharedRefcount.loadAcquire();-
1343 x->weakref.ref();-
1344 }
never executed: end of block
0
1345 return x;
executed 68632 times by 243 tests: return x;
Executed by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAbstractTextDocumentLayout
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAnimationGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QBrush
  • tst_QBuffer
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • ...
68632
1346}-
1347-
1348/**-
1349 \internal-
1350 Returns a QSharedPointer<QObject> if the variant contains-
1351 a QSharedPointer<T> where T inherits QObject. Otherwise the behaviour is undefined.-
1352*/-
1353QSharedPointer<QObject> QtSharedPointer::sharedPointerFromVariant_internal(const QVariant &variant)-
1354{-
1355 Q_ASSERT(QMetaType::typeFlags(variant.userType()) & QMetaType::SharedPointerToQObject);-
1356 return *reinterpret_cast<const QSharedPointer<QObject>*>(variant.constData());
executed 8 times by 2 tests: return *reinterpret_cast<const QSharedPointer<QObject>*>(variant.constData());
Executed by:
  • tst_QMetaType
  • tst_QSharedPointer
8
1357}-
1358-
1359/**-
1360 \internal-
1361 Returns a QWeakPointer<QObject> if the variant contains-
1362 a QWeakPointer<T> where T inherits QObject. Otherwise the behaviour is undefined.-
1363*/-
1364QWeakPointer<QObject> QtSharedPointer::weakPointerFromVariant_internal(const QVariant &variant)-
1365{-
1366 Q_ASSERT(QMetaType::typeFlags(variant.userType()) & QMetaType::WeakPointerToQObject || QMetaType::typeFlags(variant.userType()) & QMetaType::TrackingPointerToQObject);-
1367 return *reinterpret_cast<const QWeakPointer<QObject>*>(variant.constData());
executed 21 times by 3 tests: return *reinterpret_cast<const QWeakPointer<QObject>*>(variant.constData());
Executed by:
  • tst_QMetaType
  • tst_QPointer
  • tst_QSharedPointer
21
1368}-
1369-
1370QT_END_NAMESPACE-
1371-
1372#endif-
1373-
1374-
1375-
1376//# define QT_SHARED_POINTER_BACKTRACE_SUPPORT-
1377# ifdef QT_SHARED_POINTER_BACKTRACE_SUPPORT-
1378# if defined(__GLIBC__) && (__GLIBC__ >= 2) && !defined(__UCLIBC__) && !defined(QT_LINUXBASE)-
1379# define BACKTRACE_SUPPORTED-
1380# elif defined(Q_OS_MAC)-
1381# define BACKTRACE_SUPPORTED-
1382# endif-
1383# endif-
1384-
1385# if defined(BACKTRACE_SUPPORTED)-
1386# include <sys/types.h>-
1387# include <execinfo.h>-
1388# include <stdio.h>-
1389# include <unistd.h>-
1390# include <sys/wait.h>-
1391-
1392QT_BEGIN_NAMESPACE-
1393-
1394static inline QByteArray saveBacktrace() __attribute__((always_inline));-
1395static inline QByteArray saveBacktrace()-
1396{-
1397 static const int maxFrames = 32;-
1398-
1399 QByteArray stacktrace;-
1400 stacktrace.resize(sizeof(void*) * maxFrames);-
1401 int stack_size = backtrace((void**)stacktrace.data(), maxFrames);-
1402 stacktrace.resize(sizeof(void*) * stack_size);-
1403-
1404 return stacktrace;-
1405}-
1406-
1407static void printBacktrace(QByteArray stacktrace)-
1408{-
1409 void *const *stack = (void *const *)stacktrace.constData();-
1410 int stack_size = stacktrace.size() / sizeof(void*);-
1411 char **stack_symbols = backtrace_symbols(stack, stack_size);-
1412-
1413 int filter[2];-
1414 pid_t child = -1;-
1415 if (pipe(filter) != -1)-
1416 child = fork();-
1417 if (child == 0) {-
1418 // child process-
1419 dup2(fileno(stderr), fileno(stdout));-
1420 dup2(filter[0], fileno(stdin));-
1421 close(filter[0]);-
1422 close(filter[1]);-
1423 execlp("c++filt", "c++filt", "-n", NULL);-
1424-
1425 // execlp failed-
1426 execl("/bin/cat", "/bin/cat", NULL);-
1427 _exit(127);-
1428 }-
1429-
1430 // parent process-
1431 close(filter[0]);-
1432 FILE *output;-
1433 if (child == -1) {-
1434 // failed forking-
1435 close(filter[1]);-
1436 output = stderr;-
1437 } else {-
1438 output = fdopen(filter[1], "w");-
1439 }-
1440-
1441 fprintf(stderr, "Backtrace of the first creation (most recent frame first):\n");-
1442 for (int i = 0; i < stack_size; ++i) {-
1443 if (strlen(stack_symbols[i]))-
1444 fprintf(output, "#%-2d %s\n", i, stack_symbols[i]);-
1445 else-
1446 fprintf(output, "#%-2d %p\n", i, stack[i]);-
1447 }-
1448-
1449 if (child != -1) {-
1450 fclose(output);-
1451 waitpid(child, 0, 0);-
1452 }-
1453}-
1454-
1455QT_END_NAMESPACE-
1456-
1457# endif // BACKTRACE_SUPPORTED-
1458-
1459namespace {-
1460 QT_USE_NAMESPACE-
1461 struct Data {-
1462 const volatile void *pointer;-
1463# ifdef BACKTRACE_SUPPORTED-
1464 QByteArray backtrace;-
1465# endif-
1466 };-
1467-
1468 class KnownPointers-
1469 {-
1470 public:-
1471 QMutex mutex;-
1472 QHash<const void *, Data> dPointers;-
1473 QHash<const volatile void *, const void *> dataPointers;-
1474 };-
1475}-
1476-
1477Q_GLOBAL_STATIC(KnownPointers, knownPointers)
executed 2 times by 2 tests: end of block
Executed by:
  • tst_qsharedpointer - unknown status
  • tst_qsharedpointer_and_qwidget - unknown status
executed 2 times by 2 tests: guard.store(QtGlobalStatic::Destroyed);
Executed by:
  • tst_qsharedpointer - unknown status
  • tst_qsharedpointer_and_qwidget - unknown status
executed 384 times by 2 tests: return &holder.value;
Executed by:
  • tst_QSharedPointer
  • tst_QSharedPointer_and_QWidget
guard.load() =...c::InitializedDescription
TRUEevaluated 2 times by 2 tests
Evaluated by:
  • tst_qsharedpointer - unknown status
  • tst_qsharedpointer_and_qwidget - unknown status
FALSEnever evaluated
0-384
1478-
1479QT_BEGIN_NAMESPACE-
1480-
1481namespace QtSharedPointer {-
1482 Q_AUTOTEST_EXPORT void internalSafetyCheckCleanCheck();-
1483}-
1484-
1485/*!-
1486 \internal-
1487*/-
1488void QtSharedPointer::internalSafetyCheckAdd(const void *d_ptr, const volatile void *ptr)-
1489{-
1490 KnownPointers *const kp = knownPointers();-
1491 if (!kp)
!kpDescription
TRUEnever evaluated
FALSEevaluated 132 times by 1 test
Evaluated by:
  • tst_QSharedPointer
0-132
1492 return; // end-game: the application is being destroyed already
never executed: return;
0
1493-
1494 QMutexLocker lock(&kp->mutex);-
1495 Q_ASSERT(!kp->dPointers.contains(d_ptr));-
1496-
1497 //qDebug("Adding d=%p value=%p", d_ptr, ptr);-
1498-
1499 const void *other_d_ptr = kp->dataPointers.value(ptr, 0);-
1500 if (other_d_ptr) {
other_d_ptrDescription
TRUEnever evaluated
FALSEevaluated 132 times by 1 test
Evaluated by:
  • tst_QSharedPointer
0-132
1501# ifdef BACKTRACE_SUPPORTED-
1502 printBacktrace(knownPointers()->dPointers.value(other_d_ptr).backtrace);-
1503# endif-
1504 qFatal("QSharedPointer: internal self-check failed: pointer %p was already tracked "-
1505 "by another QSharedPointer object %p", ptr, other_d_ptr);-
1506 }
never executed: end of block
0
1507-
1508 Data data;-
1509 data.pointer = ptr;-
1510# ifdef BACKTRACE_SUPPORTED-
1511 data.backtrace = saveBacktrace();-
1512# endif-
1513-
1514 kp->dPointers.insert(d_ptr, data);-
1515 kp->dataPointers.insert(ptr, d_ptr);-
1516 Q_ASSERT(kp->dPointers.size() == kp->dataPointers.size());-
1517}
executed 132 times by 1 test: end of block
Executed by:
  • tst_QSharedPointer
132
1518-
1519/*!-
1520 \internal-
1521*/-
1522void QtSharedPointer::internalSafetyCheckRemove(const void *d_ptr)-
1523{-
1524 KnownPointers *const kp = knownPointers();-
1525 if (!kp)
!kpDescription
TRUEnever evaluated
FALSEevaluated 132 times by 1 test
Evaluated by:
  • tst_QSharedPointer
0-132
1526 return; // end-game: the application is being destroyed already
never executed: return;
0
1527-
1528 QMutexLocker lock(&kp->mutex);-
1529-
1530 QHash<const void *, Data>::iterator it = kp->dPointers.find(d_ptr);-
1531 if (it == kp->dPointers.end()) {
it == kp->dPointers.end()Description
TRUEnever evaluated
FALSEevaluated 132 times by 1 test
Evaluated by:
  • tst_QSharedPointer
0-132
1532 qFatal("QSharedPointer: internal self-check inconsistency: pointer %p was not tracked. "-
1533 "To use QT_SHAREDPOINTER_TRACK_POINTERS, you have to enable it throughout "-
1534 "in your code.", d_ptr);-
1535 }
never executed: end of block
0
1536-
1537 QHash<const volatile void *, const void *>::iterator it2 = kp->dataPointers.find(it->pointer);-
1538 Q_ASSERT(it2 != kp->dataPointers.end());-
1539-
1540 //qDebug("Removing d=%p value=%p", d_ptr, it->pointer);-
1541-
1542 // remove entries-
1543 kp->dataPointers.erase(it2);-
1544 kp->dPointers.erase(it);-
1545 Q_ASSERT(kp->dPointers.size() == kp->dataPointers.size());-
1546}
executed 132 times by 1 test: end of block
Executed by:
  • tst_QSharedPointer
132
1547-
1548/*!-
1549 \internal-
1550 Called by the QSharedPointer autotest-
1551*/-
1552void QtSharedPointer::internalSafetyCheckCleanCheck()-
1553{-
1554# ifdef QT_BUILD_INTERNAL-
1555 KnownPointers *const kp = knownPointers();-
1556 Q_ASSERT_X(kp, "internalSafetyCheckSelfCheck()", "Called after global statics deletion!");-
1557-
1558 if (kp->dPointers.size() != kp->dataPointers.size())
kp->dPointers....ointers.size()Description
TRUEnever evaluated
FALSEevaluated 120 times by 2 tests
Evaluated by:
  • tst_QSharedPointer
  • tst_QSharedPointer_and_QWidget
0-120
1559 qFatal("Internal consistency error: the number of pointers is not equal!");
never executed: QMessageLogger(__FILE__, 1559, __PRETTY_FUNCTION__).fatal("Internal consistency error: the number of pointers is not equal!");
0
1560-
1561 if (!kp->dPointers.isEmpty())
!kp->dPointers.isEmpty()Description
TRUEnever evaluated
FALSEevaluated 120 times by 2 tests
Evaluated by:
  • tst_QSharedPointer
  • tst_QSharedPointer_and_QWidget
0-120
1562 qFatal("Pointer cleaning failed: %d entries remaining", kp->dPointers.size());
never executed: QMessageLogger(__FILE__, 1562, __PRETTY_FUNCTION__).fatal("Pointer cleaning failed: %d entries remaining", kp->dPointers.size());
0
1563# endif-
1564}
executed 120 times by 2 tests: end of block
Executed by:
  • tst_QSharedPointer
  • tst_QSharedPointer_and_QWidget
120
1565-
1566QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9