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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtbase/src/corelib/tools/qsharedpointer.cpp