![]() |
CopperSpice API
1.8.1
|
Holds a strong reference to a shared pointer. More...
Public Typedefs | |
typedef const value_type * | const_pointer |
typedef const value_type & | const_reference |
typedef qptrdiff | difference_type |
typedef T | element_type |
typedef value_type * | pointer |
typedef value_type & | reference |
typedef T | Type |
typedef T | value_type |
Public Methods | |
QSharedPointer () | |
QSharedPointer (const QSharedPointer< T > &other) | |
template<class X > | |
QSharedPointer (const QSharedPointer< X > &other) | |
template<class X > | |
QSharedPointer (const QWeakPointer< X > &other) | |
QSharedPointer (T *ptr) | |
template<typename Deleter > | |
QSharedPointer (T *ptr, Deleter deleter) | |
~QSharedPointer () | |
void | clear () |
template<class X > | |
QSharedPointer< X > | constCast () const |
T * | data () const |
template<class X > | |
QSharedPointer< X > | dynamicCast () const |
bool | isNull () const |
template<class X > | |
QSharedPointer< X > | objectCast () const |
operator RestrictedBool () const | |
bool | operator! () const |
T & | operator* () const |
T * | operator-> () const |
QSharedPointer< T > & | operator= (const QSharedPointer< T > &other) |
template<class X > | |
QSharedPointer< T > & | operator= (const QSharedPointer< X > &other) |
template<class X > | |
QSharedPointer< T > & | operator= (const QWeakPointer< X > &other) |
QSharedPointer< T > & | operator= (QSharedPointer< T > &&other) |
void | reset () |
void | reset (T *value) |
template<typename Deleter > | |
void | reset (T *value, Deleter deleter) |
template<class X > | |
QSharedPointer< X > | staticCast () const |
void | swap (QSharedPointer &other) |
QWeakPointer< T > | toWeakRef () const |
Static Public Methods | |
template<typename... Args> | |
static QSharedPointer< T > | create (Args &&...arguments) |
Related Functions | |
These are not member functions | |
bool | operator!= (const QSharedPointer< T > &ptr1, const QSharedPointer< X > &ptr2) |
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) |
bool | operator== (const QSharedPointer< T > &ptr1, const X *ptr2) |
bool | operator== (const T *ptr1, const QSharedPointer< X > &ptr2) |
QSharedPointer< T > | QMakeShared (Args &&...args) |
QSharedPointer< X > | qSharedPointerCast (const QSharedPointer< T > &other) |
QSharedPointer< X > | qSharedPointerConstCast (const QSharedPointer< T > &other) |
QSharedPointer< X > | qSharedPointerDynamicCast (const QSharedPointer< T > &other) |
QSharedPointer< X > | qSharedPointerObjectCast (const QSharedPointer< T > &other) |
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. A QSharedPointer object can be created from a normal pointer, another QSharedPointer object or by promoting a QWeakPointer object to a strong reference.
QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.
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 a locking mechanism.
While these shared pointers are thread safe they provide no guarantee about the object they point to. Thread-safety and conditional thread safety rules for the object they point to must be observed.
CopperSpice also provides two other pointer wrapper classes: QPointer and QSharedDataPointer. They are incompatible with one another since each has a 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 the 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.
In contrast, QSharedDataPointer 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 detach based on the type of access made to the data being guarded. If it is a non-const access then 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 the 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. Refer to QWeakPointer 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 developers to catch mistakes like assigning the same pointer to two QSharedPointer objects.
This 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.
The pointer tracking feature has limitations on multiple or virtual inheritance. 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< T >::const_pointer |
Typedef for const T *.
QSharedPointer< T >::const_reference |
Typedef for const T &.
QSharedPointer< T >::difference_type |
Typedef for integral type used to represent the distance between two elements.
QSharedPointer< T >::element_type |
Typedef for T.
QSharedPointer< T >::pointer |
Typedef for T *.
QSharedPointer< T >::reference |
Typedef for T &.
QSharedPointer< T >::Type |
Typedef for T.
QSharedPointer< T >::value_type |
Typedef for T.
|
inline |
Creates a QSharedPointer which is a nullptr.
|
inline |
Destroys this QSharedPointer. If this is the last QSharedPointer which refers to the managed object, the object will be destroyed.
|
inlineexplicit |
Creates a QSharedPointer which points to ptr. The pointer ptr is managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.
|
inline |
Creates a QSharedPointer that points to ptr. The pointer 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 calling deleteLater() on a QObject.
QMakeShared<T>() is the preferred and safer way to construct a QSharedPointer. In the following example it can not be used since there is a custom deleter function.
The line of code above marked "line A" can be replaced by the following, which is functionally equivalent.
|
inline |
Creates a QSharedPointer which shares ownership with other.
|
inline |
Creates a QSharedPointer which shares ownership with other. The template parameter X must inherit from T.
|
inline |
Creates a QSharedPointer by promoting the weak reference other to a strong reference. The template parameter X must inherit from T.
|
inline |
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.
|
inline |
Performs a const_cast from T to X and returns a QSharedPointer that shares the reference count.
|
inlinestatic |
Creates a QSharedPointer object and allocates a new item of type T. The QSharedPointer internals and the object are allocated in one single memory allocation, which could help reduce memory fragmentation in a long-running application.
This method will attempt to call a constructor for type T that can accept all the arguments passed in arguments. Arguments are perfectly forwarded.
|
inline |
Returns the value of the pointer referenced by this object.
|
inline |
Performs a dynamic cast from T 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 contain a nullptr.
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.
|
inline |
Returns true if this object is holding a reference to a nullptr.
|
inline |
Performs a qobject_cast() from T 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 a nullptr.
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.
|
inline |
Returns true if this object is not a nullptr. This method is suitable for use in an "if statement".
|
inline |
Returns true if this object is a nullptr. This method is suitable for use in an "if statement".
|
inline |
Provides access to the shared pointer's members.
|
inline |
Provides access to the shared pointer's members.
|
inline |
Copy assigns from other and returns a reference to this object. If the current pointer was the last reference the object QSharedPointer was pointing to is deleted.
|
inline |
Copy assigns from other and returns a reference to this object. If the current pointer was the last reference the object QSharedPointer was pointing to is deleted.
The template parameter X must inherit from T.
|
inline |
Promotes other to a strong reference and copies it to this QSharedPointer. If the current pointer was the last reference the object QSharedPointer was pointing to is deleted.
The template parameter X must inherit from T.
|
inline |
Move assigns from other and returns a reference to this object.
|
inline |
Same as clear().
|
inline |
Resets this QSharedPointer object to point to value instead. This method is equivalent to calling the following code.
|
inline |
Resets this QSharedPointer object to point to value and updates the Deleter to deleter. This method is equivalent to calling the following code.
|
inline |
Performs a static cast from T to X and returns a QSharedPointer that shares the reference.
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.
|
inline |
Swaps this shared pointer instance with other. This method is very fast and never fails.
|
inline |
Returns a weak reference object that shares the pointer referenced by this object.
|
related |
Returns true if the pointer referenced by ptr1 is not the same pointer as that referenced by ptr2.
If the template parameter for ptr2 is different from ptr1, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal.
|
related |
Returns true if the pointer referenced by ptr1 is not the same pointer as ptr2.
If the template parameter for ptr2 is different from ptr1, QSharedPointer will attempt to perform an automatic static_cast
to ensure that the pointers being compared are equal.
|
related |
Returns true if the pointer ptr1 is not the same pointer as that referenced by ptr2.
If the template parameter for ptr2 is different from ptr1, QSharedPointer will attempt to perform an automatic static_cast
to ensure that the pointers being compared are equal.
|
related |
Returns true if the pointer referenced by ptr1 is the same pointer as that referenced by ptr2.
If the template parameter for ptr2 is different from ptr1, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal.
|
related |
Returns true if the pointer referenced by ptr1 is the same pointer as ptr2.
If the template parameter for ptr2 is different from ptr1, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal.
|
related |
Returns true if the pointer ptr1 is the same pointer as the one referenced by ptr2.
If the template parameter for ptr2 is different from ptr1, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal.
|
related |
This function creates a new object and returns a shared pointer to the newly created object in a single operation.
|
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.
The 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. If the dynamic_cast fails, the object returned will contain a nullptr.
The 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 nullptr.
The 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.