CopperSpice API  1.9.2
QSharedPointer< T > Class Template Reference

Stores a pointer to a potentially shared object. More...

Public Typedefs

using element_type = typename std::shared_ptr< T >::element_type
 
using ElementType = element_type
 
using pointer = element_type *
 
using Pointer = pointer
 
using weak_type = QWeakPointer< T >
 
using WeakType = weak_type
 

Public Methods

constexpr QSharedPointer () noexcept
 
 QSharedPointer (const QSharedPointer< T > &other) = default
 
template<typename U >
 QSharedPointer (const QSharedPointer< U > &p) noexcept
 
template<typename U >
 QSharedPointer (const QWeakPointer< U > &p)
 
template<typename U >
 QSharedPointer (const std::shared_ptr< U > &p) noexcept
 
 QSharedPointer (QSharedPointer< T > &&other) = default
 
template<typename U >
 QSharedPointer (QSharedPointer< U > &&p) noexcept
 
template<typename U , typename Deleter >
 QSharedPointer (QUniquePointer< U, Deleter > &&p)
 
constexpr QSharedPointer (std::nullptr_t) noexcept
 
template<typename Deleter >
 QSharedPointer (std::nullptr_t, Deleter d)
 
template<typename Deleter , typename Alloc >
 QSharedPointer (std::nullptr_t, Deleter d, Alloc a)
 
template<typename U >
 QSharedPointer (std::shared_ptr< U > &&p) noexcept
 
template<typename U >
 QSharedPointer (U *p)
 
template<typename U , typename Deleter >
 QSharedPointer (U *p, Deleter d)
 
template<typename U , typename Deleter , typename Alloc >
 QSharedPointer (U *p, Deleter d, Alloc a)
 
 ~QSharedPointer ()
 
void clear ()
 
template<typename U >
QSharedPointer< U > constCast () const
 
QSharedPointer< T > create (Args &&...args)
 
T * data () const
 
template<typename U >
QSharedPointer< U > dynamicCast () const
 
bool isNull () const
 
template<typename U >
QSharedPointer< U > objectCast () const
 
 operator bool () const
 
bool operator! () const
 
T & operator* () const
 
T * operator-> () const
 
QSharedPointer & operator= (const QSharedPointer< T > &other) = default
 
template<typename U >
QSharedPointer & operator= (const QSharedPointer< U > &p) noexcept
 
template<typename U >
QSharedPointer & operator= (const QWeakPointer< U > &p)
 
QSharedPointer & operator= (QSharedPointer< T > &&other) = default
 
template<typename U >
QSharedPointer & operator= (QSharedPointer< U > &&p) noexcept
 
void reset ()
 
void reset (U *value)
 
void reset (U *value, Deleter d)
 
void reset (U *value, Deleter d, Alloc a)
 
template<typename U >
QSharedPointer< U > staticCast () const
 
void swap (QSharedPointer &other)
 
QWeakPointer< T > toWeakRef () const
 

Related Functions

These are not member functions

bool operator!= (const QSharedPointer< T1 > &ptr1, const QSharedPointer< T2 > &ptr2)
 
bool operator!= (const QSharedPointer< T1 > &ptr1, const T2 *ptr2)
 
bool operator!= (const T1 *ptr1, const QSharedPointer< T2 > &ptr2)
 
bool operator< (const QSharedPointer< T1 > &ptr1, const QSharedPointer< T2 > ptr2)
 
bool operator<= (const QSharedPointer< T1 > &ptr1, const QSharedPointer< T2 > ptr2)
 
bool operator== (const QSharedPointer< T1 > &ptr1, const QSharedPointer< T2 > &ptr2)
 
bool operator== (const QSharedPointer< T1 > &ptr1, const T2 *ptr2)
 
bool operator== (const T1 *ptr1, const QSharedPointer< T2 > &ptr2)
 
bool operator> (const QSharedPointer< T1 > &ptr1, const QSharedPointer< T2 > ptr2)
 
bool operator>= (const QSharedPointer< T1 > &ptr1, const QSharedPointer< T2 > ptr2)
 
QSharedPointer< T > QMakeShared (Args &&...args)
 
QSharedPointer< U > qSharedPointerConstCast (const QSharedPointer< T > &other)
 
QSharedPointer< U > qSharedPointerDynamicCast (const QSharedPointer< T > &other)
 
QSharedPointer< U > qSharedPointerObjectCast (const QSharedPointer< T > &other)
 
QSharedPointer< U > qSharedPointerStaticCast (const QSharedPointer< T > &other)
 

Detailed Description

template<typename T>
class QSharedPointer< T >

The QSharedPointer class stores a pointer to a potentially shared object. This class maintains a shared reference count which indicates how many shared pointers are pointing to the current object. Any pointer class which takes responsibility for the lifetime of the object it points to is considered a smart pointer.

A QSharedPointer object can be constructed from a raw pointer, another QSharedPointer object, or by a QWeakPointer object. When this class is destroyed the object it points to will be deleted if no other shared pointers are pointing to the given object.

Thread Safety

QSharedPointer and QWeakPointer are thread safe which means the internal pointer is updated atomically. Different threads can access the QSharedPointer or QWeakPointer pointing to the same object at the same time without a locking mechanism. While this shared pointer class is thread safe it can not provide a guarantee about the object it points to.

See also
QSharedDataPointer, QScopedPointer, QScopedArrayPointer, QWeakPointer

Member Typedef Documentation

template<typename T >
QSharedPointer< T >::element_type

Typedef for std::shared_ptr<T>::element_type.

template<typename T >
QSharedPointer< T >::ElementType

Typedef for element_type.

template<typename T >
QSharedPointer< T >::pointer

Typedef for element_type *.

template<typename T >
QSharedPointer< T >::Pointer

Typedef for pointer.

template<typename T >
QSharedPointer< T >::weak_type

Typedef for QWeakPointer<T>.

template<typename T >
QSharedPointer< T >::WeakType

Typedef for weak_type.

Constructor & Destructor Documentation

template<typename T >
constexpr QSharedPointer< T >::QSharedPointer ( )
constexprnoexcept

Creates a new QSharedPointer which contains a nullptr.

template<typename T >
constexpr QSharedPointer< T >::QSharedPointer ( std::nullptr_t  )
constexprnoexcept

Creates a new QSharedPointer which contains a nullptr.

template<typename T >
template<typename U >
QSharedPointer< T >::QSharedPointer ( U *  p)
explicit

Creates a new QSharedPointer which points to p. Ownership of the object is transferred to the new QSharedPointer.

template<typename T >
template<typename U , typename Deleter >
QSharedPointer< T >::QSharedPointer ( U *  p,
Deleter  d 
)

Creates a new QSharedPointer which points to the given raw pointer p. Ownership of the object is transferred to the new QSharedPointer. The value for d is a deleter object and will be used when QSharedPointer is being destroyed.

template<typename T >
template<typename Deleter >
QSharedPointer< T >::QSharedPointer ( std::nullptr_t  ,
Deleter  d 
)

Creates a new QSharedPointer which contains a nullptr. The value for d is a deleter object and will be used when QSharedPointer is being destroyed.

template<typename T >
template<typename U , typename Deleter , typename Alloc >
QSharedPointer< T >::QSharedPointer ( U *  p,
Deleter  d,
Alloc  a 
)

Creates a new QSharedPointer which points to p. Ownership of the object is transferred to the new QSharedPointer. The value for d is a deleter object and will be used when QSharedPointer is being destroyed. The value for a is an allocator object and will be used when QSharedPointer needs to allocate memory.

template<typename T >
template<typename Deleter , typename Alloc >
QSharedPointer< T >::QSharedPointer ( std::nullptr_t  ,
Deleter  d,
Alloc  a 
)

Creates a new QSharedPointer which contains a nullptr. The value for d is a deleter object and will be used when QSharedPointer is being destroyed. The value for a is an allocator object and will be used when QSharedPointer needs to allocate memory.

template<typename T >
template<typename U , typename Deleter >
QSharedPointer< T >::QSharedPointer ( QUniquePointer< U, Deleter > &&  p)

Creates a new QSharedPointer using the given unique pointer p. The unique pointer will be set to a nullptr.

template<typename T >
template<typename U >
QSharedPointer< T >::QSharedPointer ( const std::shared_ptr< U > &  p)
noexcept

Creates a new QSharedPointer using the given C++ Standard Library shared pointer p.

template<typename T >
template<typename U >
QSharedPointer< T >::QSharedPointer ( std::shared_ptr< U > &&  p)
noexcept

Creates a new QSharedPointer by moving from the given C++ Standard Library shared pointer p.

template<typename T >
QSharedPointer< T >::~QSharedPointer ( )

Destroys this QSharedPointer.

If the current QSharedPointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
QSharedPointer< T >::QSharedPointer ( const QSharedPointer< T > &  other)
default

Copy constructs a new QSharedPointer from other. The current shared pointer shares ownership with other.

template<typename T >
QSharedPointer< T >::QSharedPointer ( QSharedPointer< T > &&  other)
default

Move constructs a new QSharedPointer from other.

template<typename T >
template<typename U >
QSharedPointer< T >::QSharedPointer ( const QSharedPointer< U > &  p)
noexcept

Copy constructs a new QSharedPointer from p. This shared pointer shares ownership with other. The template parameter U must inherit from T.

template<typename T >
template<typename U >
QSharedPointer< T >::QSharedPointer ( QSharedPointer< U > &&  p)
noexcept

Creates a QSharedPointer which shares ownership with p. The template parameter U must inherit from T.

template<typename T >
template<typename U >
QSharedPointer< T >::QSharedPointer ( const QWeakPointer< U > &  p)
explicit

Creates a QSharedPointer by converting the weak pointer p to a shared pointer. The template parameter U must inherit from T.

See also
QWeakPointer::toStrongRef()

Method Documentation

template<typename T >
void QSharedPointer< T >::clear ( )

Set the current QSharedPointer to a nullptr.

If the current QSharedPointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
template<typename U >
QSharedPointer< U > QSharedPointer< T >::constCast ( ) const

Returns a QSharedPointer to the current object. The shared pointer is cast from type T to type U using a const_cast.

See also
dynamicCast(), staticCast()
template<typename T >
QSharedPointer< T > QSharedPointer< T >::create ( Args &&...  args)

Creates a new QSharedPointer pointing to a new object of type T. This method can be more efficient than calling new to create the object and using the constructor which accepts a raw pointer.

int arg1 = 5;
bool arg2 = true;
// preferred
// equiveleant, usually slower
T *rawPtr = new T(arg1, arg2);
QSharedPointer<T> ptrB(rawPtr);
template<typename T >
T * QSharedPointer< T >::data ( ) const

Returns the value of the pointer referenced by this object.

Note
Do not delete the object pointed to by the raw pointer returned by this method. This will cause undefined behavior.
template<typename T >
template<typename U >
QSharedPointer< U > QSharedPointer< T >::dynamicCast ( ) const

Returns a QSharedPointer to the current object. The returned shared pointer is cast from type T to type U using a dynamic_cast. If the cast fails this method will return a nullptr.

The U must have the same cv-qualifiers (const and volatile) that T has. If you need to cast away either of these qualifiers then use constCast().

See also
constCast(), staticCast()
template<typename T >
bool QSharedPointer< T >::isNull ( ) const

Returns true if the current QSharedPointer contains a nullptr.

template<typename T >
template<typename U >
QSharedPointer< U > QSharedPointer< T >::objectCast ( ) const

Equivalent to calling dynamicCast<U>().

template<typename T >
QSharedPointer< T >::operator bool ( ) const

Returns true if the current QSharedPointer is not a nullptr. This method is called when a shared pointer is used in an "if statement" or another context where a boolean value is expected. Since this method is explicit it is not possible to assign a QSharedPointer to a boolean.

// example 1
if (mySharedPtr) {
// ...
}
// example 2
bool isTest_A = mySharedPtr; // will *not* compile
bool isTest_B = (mySharedPtr != nullptr); // compiles, valid code
bool isTest_C = bool(mySharedPtr); // compiles, valid code
template<typename T >
bool QSharedPointer< T >::operator! ( ) const

Returns true if this object is a nullptr. This method is called when a shared pointer is used in an "if statement" or another context where a boolean value is expected.

if (! mySharedPtr) {
// ...
}
See also
operator bool()
template<typename T >
T & QSharedPointer< T >::operator* ( ) const

Returns a reference to the object the current QSharedPointer is pointing to.

template<typename T >
T * QSharedPointer< T >::operator-> ( ) const

Returns a pointer to the object the current QSharedPointer is pointing to.

template<typename T >
QSharedPointer & QSharedPointer< T >::operator= ( const QSharedPointer< T > &  other)
default

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

If the current QSharedPointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
template<typename U >
QSharedPointer & QSharedPointer< T >::operator= ( const QSharedPointer< U > &  p)
noexcept

Copy assigns from p and returns a reference to this object. The template parameter U must inherit from T.

If the current QSharedPointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
template<typename U >
QSharedPointer & QSharedPointer< T >::operator= ( const QWeakPointer< U > &  p)

Converts the weak pointer p to a shared pointer and then assigns it to the current QSharedPointer. The template parameter U must inherit from T.

If the current QSharedPointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
QSharedPointer & QSharedPointer< T >::operator= ( QSharedPointer< T > &&  other)
default

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

If the current QSharedPointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
template<typename U >
QSharedPointer & QSharedPointer< T >::operator= ( QSharedPointer< U > &&  p)
noexcept

Copy assigns from p and returns a reference to this object. The template parameter U must inherit from T.

If the current QSharedPointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
void QSharedPointer< T >::reset ( )

Equivalent to calling clear().

template<typename T >
void QSharedPointer< T >::reset ( U *  value)

Resets the current QSharedPointer object to point to value. The template parameter U must inherit from T.

template<typename T >
void QSharedPointer< T >::reset ( U *  value,
Deleter  d 
)

Resets the current QSharedPointer object to point to value and updates the Deleter to d. The template parameter U must inherit from T.

template<typename T >
void QSharedPointer< T >::reset ( U *  value,
Deleter  d,
Alloc  a 
)

Resets the current QSharedPointer object to point to value and updates the Deleter to d. The value for a is an allocator object and will be used when QSharedPointer needs to allocate memory. The template parameter U must inherit from T.

template<typename T >
template<typename U >
QSharedPointer< U > QSharedPointer< T >::staticCast ( ) const

Returns a QSharedPointer to the current object. The returned shared pointer is cast from type T to type U using a static_cast.

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

Swaps the current QSharedPointer with other. This method is very fast and never fails.

template<typename T >
QWeakPointer< T > QSharedPointer< T >::toWeakRef ( ) const

Returns a QWeakPointer which shares the internal reference count with the current QSharedPointer.

Friends And Related Function Documentation

bool operator!= ( const QSharedPointer< T1 > &  ptr1,
const QSharedPointer< T2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object.

bool operator!= ( const QSharedPointer< T1 > &  ptr1,
const T2 *  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object.

bool operator!= ( const T1 *  ptr1,
const QSharedPointer< T2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object.

bool operator< ( const QSharedPointer< T1 > &  ptr1,
const QSharedPointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is less than ptr2.

bool operator<= ( const QSharedPointer< T1 > &  ptr1,
const QSharedPointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is less than or equal to ptr2.

bool operator== ( const QSharedPointer< T1 > &  ptr1,
const QSharedPointer< T2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object.

bool operator== ( const QSharedPointer< T1 > &  ptr1,
const T2 *  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object.

bool operator== ( const T1 *  ptr1,
const QSharedPointer< T2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object.

bool operator> ( const QSharedPointer< T1 > &  ptr1,
const QSharedPointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is greater than ptr2.

bool operator>= ( const QSharedPointer< T1 > &  ptr1,
const QSharedPointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is greater than or equal to ptr2.

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

This function constructs a new object of type T and returns a shared pointer to the newly created object in a single operation.

QSharedPointer<MyFileClass> filePtr = QMakeShared<MyFileClass>( myFileName.path() );
QSharedPointer< U > qSharedPointerConstCast ( const QSharedPointer< T > &  other)
related

Equivalent to calling other.constCast<U>().

QSharedPointer< U > qSharedPointerDynamicCast ( const QSharedPointer< T > &  other)
related

Equivalent to calling other.dynamicCast<U>().

QSharedPointer< U > qSharedPointerObjectCast ( const QSharedPointer< T > &  other)
related

Equivalent to calling other.dynamicCast<U>() or other.objectCast<U>().

QSharedPointer< U > qSharedPointerStaticCast ( const QSharedPointer< T > &  other)
related

Equivalent to calling other.staticCast<U>().