Qt 4.8
|
The QSharedPointer class holds a strong reference to a shared pointer. More...
#include <qsharedpointer.h>
Public Functions | |
void | clear () |
Clears this QSharedPointer object, dropping the reference that it may have had to the pointer. More... | |
template<class X > | |
QSharedPointer< X > | constCast () const |
Performs a const_cast from this pointer's type to X and returns a QSharedPointer that shares the reference. More... | |
T * | data () const |
Returns the value of the pointer referenced by this object. More... | |
template<class X > | |
QSharedPointer< X > | dynamicCast () const |
Performs a dynamic cast from this pointer's type to X and returns a QSharedPointer that shares the reference. More... | |
bool | isNull () const |
Returns true if this object is holding a reference to a null pointer. More... | |
template<class X > | |
QSharedPointer< X > | objectCast () const |
Performs a qobject_cast() from this pointer's type to X and returns a QSharedPointer that shares the reference. More... | |
operator bool () const | |
Returns true if this object is not null. More... | |
bool | operator! () const |
Returns true if this object is null. More... | |
T & | operator* () const |
Provides access to the shared pointer's members. More... | |
T * | operator-> () const |
Provides access to the shared pointer's members. More... | |
QSharedPointer< T > & | operator= (const QSharedPointer< T > &other) |
Makes this object share other's pointer. More... | |
QSharedPointer< T > & | operator= (const QWeakPointer< T > &other) |
Promotes other to a strong reference and makes this object share a reference to the pointer referenced by it. More... | |
QSharedPointer () | |
Creates a QSharedPointer that points to null (0). More... | |
QSharedPointer (T *ptr) | |
Creates a QSharedPointer that points to ptr. More... | |
QSharedPointer (T *ptr, Deleter d) | |
Creates a QSharedPointer that points to ptr. More... | |
QSharedPointer (const QSharedPointer< T > &other) | |
Creates a QSharedPointer object that shares other's pointer. More... | |
QSharedPointer (const QWeakPointer< T > &other) | |
Creates a QSharedPointer by promoting the weak reference other to strong reference and sharing its pointer. More... | |
template<class X > | |
QSharedPointer< X > | staticCast () const |
Performs a static cast from this pointer's type to X and returns a QSharedPointer that shares the reference. More... | |
QWeakPointer< T > | toWeakRef () const |
Returns a weak reference object that shares the pointer referenced by this object. More... | |
~QSharedPointer () | |
Destroys this QSharedPointer object. More... | |
Related Functions | |
(Note that these are not member functions.) | |
bool | operator!= (const QSharedPointer< T > &ptr1, const QSharedPointer< X > &ptr2) |
Returns true if the pointer referenced by ptr1 is not the same pointer as that referenced by ptr2. More... | |
bool | operator!= (const QSharedPointer< T > &ptr1, const X *ptr2) |
bool | operator!= (const T *ptr1, const QSharedPointer< X > &ptr2) |
bool | operator== (const QSharedPointer< T > &ptr1, const QSharedPointer< X > &ptr2) |
Returns true if the pointer referenced by ptr1 is the same pointer as that referenced by ptr2. More... | |
bool | operator== (const QSharedPointer< T > &ptr1, const X *ptr2) |
bool | operator== (const T *ptr1, const QSharedPointer< X > &ptr2) |
QSharedPointer< X > | qSharedPointerCast (const QSharedPointer< T > &other) |
Returns a shared pointer to the pointer held by other, cast to type X . More... | |
QSharedPointer< X > | qSharedPointerConstCast (const QSharedPointer< T > &other) |
Returns a shared pointer to the pointer held by other, cast to type X . More... | |
QSharedPointer< X > | qSharedPointerDynamicCast (const QSharedPointer< T > &other) |
Returns a shared pointer to the pointer held by other, using a dynamic cast to type X to obtain an internal pointer of the appropriate type. More... | |
QSharedPointer< X > | qSharedPointerObjectCast (const QSharedPointer< T > &other) |
The qSharedPointerObjectCast function is for casting a shared pointer. More... | |
The QSharedPointer class holds a strong reference to a shared pointer.
The QSharedPointer is an automatic, shared pointer in C++. It behaves exactly like a normal pointer for normal purposes, including respect for constness.
QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.
A QSharedPointer object can be created from a normal pointer, another QSharedPointer object or by promoting a QWeakPointer object to a strong reference.
QSharedPointer and QWeakPointer are thread-safe and operate atomically on the pointer value. Different threads can also access the QSharedPointer or QWeakPointer pointing to the same object at the same time without need for locking mechanisms.
It should be noted that, while the pointer value can be accessed in this manner, QSharedPointer and QWeakPointer provide no guarantee about the object being pointed to. Thread-safety and reentrancy rules for that object still apply.
Qt also provides two other pointer wrapper classes: QPointer and QSharedDataPointer. They are incompatible with one another, since each has its very different use case.
QSharedPointer holds a shared pointer by means of an external reference count (i.e., a reference counter placed outside the object). Like its name indicates, the pointer value is shared among all instances of QSharedPointer and QWeakPointer. The contents of the object pointed to by the pointer should not be considered shared, however: there is only one object. For that reason, QSharedPointer does not provide a way to detach or make copies of the pointed object.
QSharedDataPointer, on the other hand, holds a pointer to shared data (i.e., a class derived from QSharedData). It does so by means of an internal reference count, placed in the QSharedData base class. This class can, therefore, detach based on the type of access made to the data being guarded: if it's a non-const access, it creates a copy atomically for the operation to complete.
QExplicitlySharedDataPointer is a variant of QSharedDataPointer, except that it only detaches if QExplicitlySharedDataPointer::detach() is explicitly called (hence the name).
QScopedPointer simply holds a pointer to a heap allocated object and deletes it in its destructor. This class is useful when an object needs to be heap allocated and deleted, but no more. QScopedPointer is lightweight, it makes no use of additional structure or reference counting.
Finally, QPointer holds a pointer to a QObject-derived object, but it does so weakly. QPointer can be replaced by QWeakPointer in almost all cases, since they have the same functionality. See QWeakPointers tracking a QObject for more information.
A feature of QSharedPointer that can be enabled at compile time for debugging purposes is a pointer tracking mechanism. When enabled, QSharedPointer registers in a global set all the pointers that it tracks. This allows one to catch mistakes like assigning the same pointer to two QSharedPointer objects.
This function is enabled by defining the QT_SHAREDPOINTER_TRACK_POINTERS
macro before including the QSharedPointer header.
It is safe to use this feature even with code compiled without the feature. QSharedPointer will ensure that the pointer is removed from the tracker even from code compiled without pointer tracking.
Note, however, that the pointer tracking feature has limitations on multiple- or virtual-inheritance (that is, in cases where two different pointer addresses can refer to the same object). In that case, if a pointer is cast to a different type and its value changes, QSharedPointer's pointer tracking mechanism may fail to detect that the object being tracked is the same.
QSharedPointer is in reality implemented by two ancestor classes: QtSharedPointer::Basic and QtSharedPointer::ExternalRefCount. The reason for having that split is now mostly legacy: in the beginning, QSharedPointer was meant to support both internal reference counting and external reference counting.
QtSharedPointer::Basic implements the basic functionality that is shared between internal- and external-reference counting. That is, it's mostly the accessor functions into QSharedPointer. Those are all inherited by QSharedPointer, which adds another level of shared functionality (the constructors and assignment operators). The Basic class has one member variable, which is the actual pointer being tracked.
QtSharedPointer::ExternalRefCount implements the actual reference counting and introduces the d-pointer for QSharedPointer. That d-pointer itself is shared with other QSharedPointer objects as well as QWeakPointer.
The reason for keeping the pointer value itself outside the d-pointer is because of multiple inheritance needs. If you have two QSharedPointer objects of different pointer types, but pointing to the same object in memory, it could happen that the pointer values are different. The differentPointers
autotest exemplifies this problem. The same thing could happen in the case of virtual inheritance: a pointer of class matching the virtual base has different address compared to the pointer of the complete object. See the virtualBaseDifferentPointers
autotest for this problem.
The d pointer is of type QtSharedPointer::ExternalRefCountData for simple QSharedPointer objects, but could be of a derived type in some cases. It is basically a reference-counted reference-counter.
This class is basically a reference-counted reference-counter. It has two members: strongref
and weakref
. The strong reference counter is controlling the lifetime of the object tracked by QSharedPointer. a positive value indicates that the object is alive. It's also the number of QSharedObject instances that are attached to this Data.
When the strong reference count decreases to zero, the object is deleted (see below for information on custom deleters). The strong reference count can also exceptionally be -1, indicating that there are no QSharedPointers attached to an object, which is tracked too. The only case where this is possible is that of QWeakPointers tracking a QObject.
The weak reference count controls the lifetime of the d-pointer itself. It can be thought of as an internal/intrusive reference count for ExternalRefCountData itself. This count is equal to the number of QSharedPointers and QWeakPointers that are tracking this object. (In case the object tracked derives from QObject, this number is increased by 1, since QObjectPrivate tracks it too).
ExternalRefCountData is a virtual class: it has a virtual destructor and a virtual destroy() function. The destroy() function is supposed to delete the object being tracked and return true if it does so. Otherwise, it returns false to indicate that the caller must simply call delete. This allows the normal use-case of QSharedPointer without custom deleters to use only one 12- or 16-byte (depending on whether it's a 32- or 64-bit architecture) external descriptor structure, without paying the price for the custom deleter that it isn't using.
::ExternalRefCountDataWithDestroyFn
This class is not used directly, per se. It only exists to enable the two classes that derive from it. It adds one member variable, which is a pointer to a function (which returns void and takes an ExternalRefCountData* as a parameter). It also overrides the destroy() function: it calls that function pointer with this
as parameter, and returns true.
That means when ExternalRefCountDataWithDestroyFn is used, the destroyer
field must be set to a valid function that will delete the object tracked.
This class also adds an operator delete function to ensure that it simply calls the global operator delete. That should be the behaviour in all compilers already, but to be on the safe side, this class ensures that no funny business happens.
On a 32-bit architecture, this class is 16 bytes in size, whereas it's 24 bytes on 64-bit. (On Itanium where function pointers contain the global pointer, it can be 32 bytes).
::ExternalRefCountWithCustomDeleter
This class derives from ExternalRefCountDataWithDestroyFn and is a template class. As template parameters, it has the type of the pointer being tracked (T
) and a Deleter
, which is anything. It adds two fields to its parent class, matching those template parameters: a member of type Deleter
and a member of type T*
.
The purpose of this class is to store the pointer to be deleted and the deleter code along with the d-pointer. This allows the last strong reference to call any arbitrary function that disposes of the object. For example, this allows calling QObject::deleteLater() on a given object. The pointer to the object is kept here to avoid the extra cost of keeping the deleter in the generic case.
This class is never instantiated directly: the constructors and destructor are private. Only the create() function may be called to return an object of this type. See below for construction details.
The size of this class depends on the size of Deleter
. If it's an empty functor (i.e., no members), ABIs generally assign it the size of 1. But given that it's followed by a pointer, up to 3 or 7 padding bytes may be inserted: in that case, the size of this class is 16+4+4 = 24 bytes on 32-bit architectures, or 24+8+8 = 40 bytes on 64-bit architectures (48 bytes on Itanium with global pointers stored). If Deleter
is a function pointer, the size should be the same as the empty structure case, except for Itanium where it may be 56 bytes due to another global pointer. If Deleter
is a pointer to a member function (PMF), the size will be even bigger and will depend on the ABI. For architectures using the Itanium C++ ABI, a PMF is twice the size of a normal pointer, or 24 bytes on Itanium itself. In that case, the size of this structure will be 16+8+4 = 28 bytes on 32-bit architectures, 24+16+8 = 48 bytes on 64-bit, and 32+24+8 = 64 bytes on Itanium.
(Values for Itanium consider an LP64 architecture; for ILP32, pointers are 32-bit in length, function pointers are 64-bit and PMF are 96-bit, so the sizes are slightly less)
::ExternalRefCountWithContiguousData
This class also derives from ExternalRefCountDataWithDestroyFn and it is also a template class. The template parameter is the type T
of the class which QSharedPointer tracks. It adds only one member to its parent, which is of type T
(the actual type, not a pointer to it).
The purpose of this class is to lay the T
object out next to the reference counts, saving one memory allocation per shared pointer. This is particularly interesting for small T
or for the cases when there are few if any QWeakPointer tracking the object. This class exists to implement the QSharedPointer::create() call.
Like ExternalRefCountWithCustomDeleter, this class is never instantiated directly. This class also provides a create() member that returns the pointer, and hides its constructors and destructor. (With C++0x, we'd delete them).
The size of this class depends on the size of T
.
Like explained above, these classes have private constructors. Moreover, they are not defined anywhere, so trying to call new ClassType
would result in a compilation or linker error. Instead, these classes must be constructed via their create() methods.
Instead of instantiating the class by the normal way, the create() method calls operator new
directly with the size of the class, then calls the parent class's constructor only (ExternalRefCountDataWithDestroyFn). This ensures that the inherited members are initialised properly, as well as the virtual table pointer, which must point to ExternalRefCountDataWithDestroyFn's virtual table. That way, we also ensure that the virtual destructor being called is ExternalRefCountDataWithDestroyFn's.
After initialising the base class, the ExternalRefCountWithCustomDeleter::create() function initialises the new members directly, by using the placement operator new
. In the case of the ExternalRefCountWithContiguousData::create() function, the address to the still-uninitialised T
member is saved for the callee to use. The member is only initialised in QSharedPointer::create(), so that we avoid having many variants of the internal functions according to the arguments in use for calling the constructor.
When initialising the parent class, the create() functions pass the address of the static deleter() member function. That is, when the virtual destroy() is called by QSharedPointer, the deleter() functions are called instead. These functions static_cast the ExternalRefCountData* parameter to their own type and execute their deletion: for the ExternalRefCountWithCustomDeleter::deleter() case, it runs the user's custom deleter, then destroys the deleter; for ExternalRefCountWithContiguousData::deleter, it simply calls the T
destructor directly.
By not calling the constructor of the derived classes, we avoid instantiating their virtual tables. Since these classes are template-based, there would be one virtual table per T
and Deleter
type. (This is what Qt 4.5 did.)
Instead, only one non-inline function is required per template, which is the deleter() static member. All the other functions can be inlined. What's more, the address of deleter() is calculated only in code, which can be resolved at link-time if the linker can determine that the function lies in the current application or library module (since these classes are not exported, that is the case for Windows or for builds with -fvisibility=hidden
).
In contrast, a virtual table would require at least 3 relocations to be resolved at module load-time, per module where these classes are used. (In the Itanium C++ ABI, there would be more relocations, due to the RTTI)
To ensure that pointers created with pointer-tracking enabled get un-tracked when destroyed, even if destroyed by code compiled without the feature, QSharedPointer modifies slightly the instructions of the previous sections.
When ExternalRefCountWithCustomDeleter or ExternalRefCountWithContiguousData are used, their create() functions will set the ExternalRefCountDataWithDestroyFn::destroyer function pointer to safetyCheckDeleter() instead. These static member functions simply call internalSafetyCheckRemove2() before passing control to the normal deleter() function.
If neither custom deleter nor QSharedPointer::create() are used, then QSharedPointer uses a custom deleter of its own: the normalDeleter() function, which simply calls delete
. By using a custom deleter, the safetyCheckDeleter() procedure described above kicks in.
Definition at line 62 of file qsharedpointer.h.
QSharedPointer< T >::QSharedPointer | ( | ) |
Creates a QSharedPointer that points to null (0).
|
explicit |
Creates a QSharedPointer that points to ptr.
The pointer ptr becomes managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.
QSharedPointer< T >::QSharedPointer | ( | T * | ptr, |
Deleter | deleter | ||
) |
Creates a QSharedPointer that points to ptr.
The pointer ptr becomes managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.
The deleter parameter specifies the custom deleter for this object. The custom deleter is called, instead of the operator delete(), when the strong reference count drops to 0. This is useful, for instance, for calling deleteLater() on a QObject instead:
It is also possible to specify a member function directly, as in:
QSharedPointer< T >::QSharedPointer | ( | const QSharedPointer< T > & | other | ) |
Creates a QSharedPointer object that shares other's pointer.
If T
is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.
QSharedPointer< T >::QSharedPointer | ( | const QWeakPointer< T > & | other | ) |
Creates a QSharedPointer by promoting the weak reference other to strong reference and sharing its pointer.
If T
is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.
|
inline |
Destroys this QSharedPointer object.
If it is the last reference to the pointer stored, this will delete the pointer as well.
Definition at line 80 of file qsharedpointer.h.
void QSharedPointer< T >::clear | ( | ) |
Clears this QSharedPointer object, dropping the reference that it may have had to the pointer.
If this was the last reference, then the pointer itself will be deleted.
Referenced by QSharedPointer< QNetworkSession >::~QSharedPointer().
Performs a const_cast
from this pointer's type to X
and returns a QSharedPointer that shares the reference.
This function can be used for up- and for down-casting, but is more useful for up-casting.
Referenced by QSharedPointer< QNetworkSession >::~QSharedPointer().
T * QSharedPointer< T >::data | ( | ) | const |
Returns the value of the pointer referenced by this object.
Note: do not delete the pointer returned by this function or pass it to another function that could delete it, including creating QSharedPointer or QWeakPointer objects.
Referenced by QNetworkAccessManagerPrivate::_q_networkSessionClosed(), QHostInfoPrivate::fromName(), QNetworkAccessHttpBackend::postRequest(), and QNetworkReplyImplPrivate::setDownloadBuffer().
Performs a dynamic cast from this pointer's type to X
and returns a QSharedPointer that shares the reference.
If this function is used to up-cast, then QSharedPointer will perform a dynamic_cast
, which means that if the object being pointed by this QSharedPointer is not of type X
, the returned object will be null.
Note: the template type X
must have the same const and volatile qualifiers as the template of this object, or the cast will fail. Use constCast() if you need to drop those qualifiers.
Referenced by QSharedPointer< QNetworkSession >::~QSharedPointer().
bool QSharedPointer< T >::isNull | ( | ) | const |
Returns true if this object is holding a reference to a null pointer.
Referenced by QHttpThreadDelegate::dataReadProgressSlot(), QSharedNetworkSessionManager::getSession(), QHttpThreadDelegate::readyReadSlot(), QNetworkAccessHttpBackend::replyDownloadMetaData(), and QDeclarativeAbstractBinding::weakPointer().
Performs a qobject_cast() from this pointer's type to X
and returns a QSharedPointer that shares the reference.
If this function is used to up-cast, then QSharedPointer will perform a qobject_cast
, which means that if the object being pointed by this QSharedPointer is not of type X
, the returned object will be null.
Note: the template type X
must have the same const and volatile qualifiers as the template of this object, or the cast will fail. Use constCast() if you need to drop those qualifiers.
Referenced by QSharedPointer< QNetworkSession >::~QSharedPointer().
QSharedPointer< T >::operator bool | ( | ) | const |
Returns true if this object is not null.
This function is suitable for use in if-constructs
, like:
bool QSharedPointer< T >::operator! | ( | ) | const |
Returns true if this object is null.
This function is suitable for use in if-constructs
, like:
T & QSharedPointer< T >::operator* | ( | ) | const |
Provides access to the shared pointer's members.
T * QSharedPointer< T >::operator-> | ( | ) | const |
Provides access to the shared pointer's members.
QSharedPointer & QSharedPointer< T >::operator= | ( | const QSharedPointer< T > & | other | ) |
Makes this object share other's pointer.
The current pointer reference is discarded and, if it was the last, the pointer will be deleted.
If T
is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.
Referenced by QSharedPointer< QNetworkSession >::~QSharedPointer().
QSharedPointer & QSharedPointer< T >::operator= | ( | const QWeakPointer< T > & | other | ) |
Promotes other to a strong reference and makes this object share a reference to the pointer referenced by it.
The current pointer reference is discarded and, if it was the last, the pointer will be deleted.
If T
is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.
Performs a static cast from this pointer's type to X
and returns a QSharedPointer that shares the reference.
This function can be used for up- and for down-casting, but is more useful for up-casting.
Note: the template type X
must have the same const and volatile qualifiers as the template of this object, or the cast will fail. Use constCast() if you need to drop those qualifiers.
Referenced by QSharedPointer< QNetworkSession >::~QSharedPointer().
QWeakPointer< T > QSharedPointer< T >::toWeakRef | ( | ) | const |
Returns a weak reference object that shares the pointer referenced by this object.
Referenced by QNetworkAccessManagerPrivate::createSession(), QDeclarativeAbstractBinding::weakPointer(), and QSharedPointer< QNetworkSession >::~QSharedPointer().
|
related |
Returns true if the pointer referenced by ptr1 is not the same pointer as that referenced by ptr2.
If ptr2's template parameter is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast
to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's, you will get a compiler error.
|
related |
Returns true if the pointer referenced by ptr1 is not the same pointer as ptr2.
If ptr2's type is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast
to ensure that the pointers being compared are equal. If ptr2's type is not a base or a derived type from this ptr1's, you will get a compiler error.
|
related |
Returns true if the pointer ptr1 is not the same pointer as that referenced by ptr2.
If ptr2's template parameter is different from ptr1's type, QSharedPointer will attempt to perform an automatic static_cast
to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's type, you will get a compiler error.
|
related |
Returns true if the pointer referenced by ptr1 is the same pointer as that referenced by ptr2.
If ptr2's template parameter is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast
to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's, you will get a compiler error.
|
related |
Returns true if the pointer referenced by ptr1 is the same pointer as ptr2.
If ptr2's type is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast
to ensure that the pointers being compared are equal. If ptr2's type is not a base or a derived type from this ptr1's, you will get a compiler error.
|
related |
Returns true if the pointer ptr1 is the same pointer as that referenced by ptr2.
If ptr2's template parameter is different from ptr1's type, QSharedPointer will attempt to perform an automatic static_cast
to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's type, you will get a compiler error.
|
related |
Returns a shared pointer to the pointer held by other, cast to type X
.
The types T
and X
must belong to one hierarchy for the static_cast
to succeed.
Note that X
must have the same cv-qualifiers (const
and volatile
) that T
has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.
|
related |
Returns a shared pointer to the pointer held by other, cast to type X
.
The types T
and X
must belong to one hierarchy for the const_cast
to succeed. The const
and volatile
differences between T
and X
are ignored.
|
related |
Returns a shared pointer to the pointer held by other, using a dynamic cast to type X
to obtain an internal pointer of the appropriate type.
If the dynamic_cast
fails, the object returned will be null.
Note that X
must have the same cv-qualifiers (const
and volatile
) that T
has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.
|
related |
The qSharedPointerObjectCast function is for casting a shared pointer.
Returns a shared pointer to the pointer held by other, using a qobject_cast() to type X
to obtain an internal pointer of the appropriate type. If the qobject_cast
fails, the object returned will be null.
Note that X
must have the same cv-qualifiers (const
and volatile
) that T
has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.