CopperSpice API  1.8.1
QSharedPointer< T > Class Template Reference

Holds a strong reference to a shared pointer. More...

Public Typedefs

typedef const value_typeconst_pointer
 
typedef const value_typeconst_reference
 
typedef qptrdiff difference_type
 
typedef T element_type
 
typedef value_typepointer
 
typedef value_typereference
 
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)
 

Detailed Description

template<class T>
class QSharedPointer< T >

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.

Thread-Safety

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.

Other Pointer Classes

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.

Optional pointer tracking

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.

See also
QSharedDataPointer, QWeakPointer, QScopedPointer

Member Typedef Documentation

template<class T >
QSharedPointer< T >::const_pointer

Typedef for const T *.

template<class T >
QSharedPointer< T >::const_reference

Typedef for const T &.

template<class T >
QSharedPointer< T >::difference_type

Typedef for integral type used to represent the distance between two elements.

template<class T >
QSharedPointer< T >::element_type

Typedef for T.

template<class T >
QSharedPointer< T >::pointer

Typedef for T *.

template<class T >
QSharedPointer< T >::reference

Typedef for T &.

template<class T >
QSharedPointer< T >::Type

Typedef for T.

template<class T >
QSharedPointer< T >::value_type

Typedef for T.

Constructor & Destructor Documentation

template<class T >
QSharedPointer< T >::QSharedPointer ( )
inline

Creates a QSharedPointer which is a nullptr.

template<class T >
QSharedPointer< T >::~QSharedPointer ( )
inline

Destroys this QSharedPointer. If this is the last QSharedPointer which refers to the managed object, the object will be destroyed.

template<class T >
QSharedPointer< T >::QSharedPointer ( T *  ptr)
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.

template<class T >
template<typename Deleter >
QSharedPointer< T >::QSharedPointer ( T *  ptr,
Deleter  deleter 
)
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.

static void doDeleteLater(MyObject *obj) {
obj->deleteLater();
}
void otherFunction() {
QSharedPointer<MyObject> obj = QSharedPointer<MyObject>(new MyObject, doDeleteLater); // line A
// continue using obj
obj.clear(); // calls obj->deleteLater();
}

The line of code above marked "line A" can be replaced by the following, which is functionally equivalent.

See also
clear()
template<class T >
QSharedPointer< T >::QSharedPointer ( const QSharedPointer< T > &  other)
inline

Creates a QSharedPointer which shares ownership with other.

template<class T >
template<class X >
QSharedPointer< T >::QSharedPointer ( const QSharedPointer< X > &  other)
inline

Creates a QSharedPointer which shares ownership with other. The template parameter X must inherit from T.

template<class T >
template<class X >
QSharedPointer< T >::QSharedPointer ( const QWeakPointer< X > &  other)
inline

Creates a QSharedPointer by promoting the weak reference other to a strong reference. The template parameter X must inherit from T.

See also
QWeakPointer::toStrongRef()

Method Documentation

template<class T >
void QSharedPointer< T >::clear ( )
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.

template<class T >
template<class X >
QSharedPointer< X > QSharedPointer< T >::constCast ( ) const
inline

Performs a const_cast from T to X and returns a QSharedPointer that shares the reference count.

See also
isNull(), qSharedPointerConstCast()
template<class T >
template<typename... Args>
QSharedPointer< T > QSharedPointer< T >::create ( Args &&...  arguments)
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.

template<class T >
T * QSharedPointer< T >::data ( ) const
inline

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.
template<class T >
template<class X >
QSharedPointer< X > QSharedPointer< T >::dynamicCast ( ) const
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.

See also
qSharedPointerDynamicCast()
template<class T >
bool QSharedPointer< T >::isNull ( ) const
inline

Returns true if this object is holding a reference to a nullptr.

template<class T >
template<class X >
QSharedPointer< X > QSharedPointer< T >::objectCast ( ) const
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.

See also
qSharedPointerObjectCast()
template<class T >
QSharedPointer< T >::operator RestrictedBool ( ) const
inline

Returns true if this object is not a nullptr. This method is suitable for use in an "if statement".

if (mySharedPtr) {
...
}
See also
isNull()
template<class T >
bool QSharedPointer< T >::operator! ( ) const
inline

Returns true if this object is a nullptr. This method is suitable for use in an "if statement".

if (! mySharedPtr) {
...
}
See also
isNull()
template<class T >
T & QSharedPointer< T >::operator* ( ) const
inline

Provides access to the shared pointer's members.

See also
isNull()
template<class T >
T * QSharedPointer< T >::operator-> ( ) const
inline

Provides access to the shared pointer's members.

See also
isNull()
template<class T >
QSharedPointer< T > & QSharedPointer< T >::operator= ( const QSharedPointer< T > &  other)
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.

template<class T >
template<class X >
QSharedPointer< T > & QSharedPointer< T >::operator= ( const QSharedPointer< X > &  other)
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.

template<class T >
template<class X >
QSharedPointer< T > & QSharedPointer< T >::operator= ( const QWeakPointer< X > &  other)
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.

template<class T >
QSharedPointer< T > & QSharedPointer< T >::operator= ( QSharedPointer< T > &&  other)
inline

Move assigns from other and returns a reference to this object.

template<class T >
void QSharedPointer< T >::reset ( )
inline

Same as clear().

template<class T >
void QSharedPointer< T >::reset ( T *  value)
inline

Resets this QSharedPointer object to point to value instead. This method is equivalent to calling the following code.

QSharedPointer<T> ptr(value);
this->swap(ptr);
template<class T >
template<typename Deleter >
void QSharedPointer< T >::reset ( T *  value,
Deleter  deleter 
)
inline

Resets this QSharedPointer object to point to value and updates the Deleter to deleter. This method is equivalent to calling the following code.

QSharedPointer<T> ptr(value, deleter);
this->swap(ptr);
template<class T >
template<class X >
QSharedPointer< X > QSharedPointer< T >::staticCast ( ) const
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.

See also
dynamicCast(), constCast(), qSharedPointerCast()
template<class T >
void QSharedPointer< T >::swap ( QSharedPointer< T > &  other)
inline

Swaps this shared pointer instance with other. This method is very fast and never fails.

template<class T >
QWeakPointer< T > QSharedPointer< T >::toWeakRef ( ) const
inline

Returns a weak reference object that shares the pointer referenced by this object.

See also
QWeakPointer::QWeakPointer()

Friends And Related Function Documentation

bool operator!= ( const QSharedPointer< T > &  ptr1,
const QSharedPointer< X > &  ptr2 
)
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.

bool operator!= ( const QSharedPointer< T > &  ptr1,
const X *  ptr2 
)
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.

bool operator!= ( const T *  ptr1,
const QSharedPointer< X > &  ptr2 
)
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.

bool operator== ( const QSharedPointer< T > &  ptr1,
const QSharedPointer< X > &  ptr2 
)
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.

bool operator== ( const QSharedPointer< T > &  ptr1,
const X *  ptr2 
)
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.

bool operator== ( const T *  ptr1,
const QSharedPointer< X > &  ptr2 
)
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.

QSharedPointer< T > QMakeShared ( Args &&...  args)
related

This function creates a new object and returns a shared pointer to the newly created object in a single operation.

QSharedPointer<MyFileClass> filePtr = QMakeShared<MyFileClass>( myFileName.path() );
QSharedPointer< X > qSharedPointerCast ( const QSharedPointer< T > &  other)
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.

See also
QSharedPointer::staticCast(), qSharedPointerDynamicCast(), qSharedPointerConstCast()
QSharedPointer< X > qSharedPointerConstCast ( const QSharedPointer< T > &  other)
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.

See also
QSharedPointer::constCast(), qSharedPointerCast(), qSharedPointerDynamicCast()
QSharedPointer< X > qSharedPointerDynamicCast ( const QSharedPointer< T > &  other)
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.

See also
QSharedPointer::dynamicCast(), qSharedPointerCast(), qSharedPointerConstCast()
QSharedPointer< X > qSharedPointerObjectCast ( const QSharedPointer< T > &  other)
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.

See also
QSharedPointer::objectCast(), qSharedPointerCast(), qSharedPointerConstCast()