CopperSpice API  1.9.1
QWeakPointer< T > Class Template Reference

Stores a weak 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
 

Public Methods

 QWeakPointer ()
 
template<typename U >
 QWeakPointer (const QSharedPointer< U > &p)
 
 QWeakPointer (const QWeakPointer< T > &other)
 
template<typename U >
 QWeakPointer (const QWeakPointer< U > &other)
 
 QWeakPointer (QWeakPointer< T > &&other)
 
 QWeakPointer (T *p)
 
 ~QWeakPointer ()
 
void clear () noexcept
 
Pointer data () const noexcept
 
bool expired () const noexcept
 
bool isNull () const
 
 operator bool () const noexcept
 
bool operator! () const
 
template<typename U >
QWeakPointer & operator= (const QSharedPointer< U > &p) noexcept
 
QWeakPointer & operator= (const QWeakPointer< T > &other)
 
QWeakPointer & operator= (QWeakPointer< T > &&other)
 
template<typename U >
bool owner_before (const QSharedPointer< U > &p) const noexcept
 
template<typename U >
bool owner_before (const QWeakPointer< U > &p) const noexcept
 
QSharedPointer< T > toStrongRef () const noexcept
 

Related Functions

These are not member functions

bool operator!= (const QSharedPointer< T1 > &ptr1, const QWeakPointer< T2 > &ptr2)
 
bool operator!= (const QWeakPointer< T > &ptr1, std::nullptr_t)
 
bool operator!= (const QWeakPointer< T1 > &ptr1, const QSharedPointer< T2 > &ptr2)
 
bool operator!= (std::nullptr_t, const QWeakPointer< T > &ptr2)
 
bool operator== (const QSharedPointer< T1 > &ptr1, const QWeakPointer< T2 > &ptr2)
 
bool operator== (const QWeakPointer< T > &ptr1, std::nullptr_t)
 
bool operator== (const QWeakPointer< T1 > &ptr1, const QSharedPointer< T2 > &ptr2)
 
bool operator== (std::nullptr_t, const QWeakPointer< T > &ptr2)
 

Detailed Description

template<typename T>
class QWeakPointer< T >

The QWeakPointer class stores a weak pointer to a potentially shared object. A weak pointer is a smart pointer which does take ownership or manage the lifetime of the shared object. A CsWeakPointer can only be created by calling QSharedPointer::toWeakRef() or using the constructor in QWeakPointer which accepts a QSharedPointer.

To access the object which QWeakPointer points to, convert it to QSharedPointer and ensure the shared pointer is not a nullptr. If the shared pointer is not a nullptr, the object will not be deleted while the shared pointer is in scope.

QWeakPointer also provides a data() method which returns a raw pointer to the object. This method should only be used if your code can absolutely guarantee the object will not get deleted.

See also
QSharedPointer

Member Typedef Documentation

template<typename T >
QWeakPointer< T >::element_type

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

template<typename T >
QWeakPointer< T >::ElementType

Typedef for element_type.

template<typename T >
QWeakPointer< T >::pointer

Typedef for element_type *.

template<typename T >
QWeakPointer< T >::Pointer

Typedef for pointer.

Constructor & Destructor Documentation

template<typename T >
QWeakPointer< T >::QWeakPointer ( )

Creates a new QWeakPointer which contains a nullptr.

template<typename T >
template<typename U >
QWeakPointer< T >::QWeakPointer ( const QWeakPointer< U > &  other)

Creates a new QWeakPointer which points to the same object as other. This constructor is only available if U* can be converted to a T*.

template<typename T >
QWeakPointer< T >::QWeakPointer ( T *  p)

Creates a new QWeakPointer which points to p. This constructor is only available if T is a class which inherits from QObject.

This constructor uses the internal shared pointer in QObject pointed to by p, to create the new weak pointer.

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

Destroys this QWeakPointer object. The pointer referenced by this object will not be deleted.

template<typename T >
QWeakPointer< T >::QWeakPointer ( const QWeakPointer< T > &  other)

Copy constructs a new QWeakPointer from other.

template<typename T >
QWeakPointer< T >::QWeakPointer ( QWeakPointer< T > &&  other)

Move constructs a new QWeakPointer from other.

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

Creates a new QWeakPointer which contains a weak reference to the shared pointer p.

Method Documentation

template<typename T >
void QWeakPointer< T >::clear ( )
noexcept

Sets this QWeakPointer object to a nullptr.

template<typename T >
Pointer QWeakPointer< T >::data ( ) const
noexcept

Returns the value of the raw pointer in this QWeakPointer. If the object has already been deleted this method will return a nullptr. Dereferencing this raw pointer is only permissible if the code can absolutely guarantee the object will not be deleted before the dereference occurs.

See also
toStrongRef()
template<typename T >
bool QWeakPointer< T >::expired ( ) const
noexcept

Equivalent to calling isNull().

template<typename T >
bool QWeakPointer< T >::isNull ( ) const

Returns true if this object contains a reference to a null pointer. Since the object the QWeakPointer references can be deleted at any time, a value of true does not guarantee the object is still alive after calling this method.

template<typename T >
QWeakPointer< T >::operator bool ( ) const
explicitnoexcept

Returns true if the current QWeakPointer 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 (myWeakPtr) {
...
}
// example 2
bool isTest_A = myWeakPtr; // will *not* compile
bool isTest_B = (myWeakPtr != nullptr); // compiles, valid code
bool isTest_C = bool(myWeakPtr); // compiles, valid code
template<typename T >
bool QWeakPointer< 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 (! myWeakPtr) {
...
}
template<typename T >
template<typename U >
QWeakPointer & QWeakPointer< T >::operator= ( const QSharedPointer< U > &  p)
noexcept

Copy assigns from the shared pointer p and returns a reference to this object.

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

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

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

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

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

Returns true if the current pointer is less than p, otherwise returns false.

template<typename T >
template<typename U >
bool QWeakPointer< T >::owner_before ( const QWeakPointer< U > &  p) const
noexcept

Returns true if the current pointer is less than p, otherwise returns false.

template<typename T >
QSharedPointer< T > QWeakPointer< T >::toStrongRef ( ) const
noexcept

Converts the current weak pointer to a shared pointer and returns the QSharedPointer. This method can fail if the object the QWeakPointer is pointing to has been deleted. To prevent undefined behavior verify the return value by calling QSharedPointer::isNull() before dereferencing.

if (ptr != nullptr) {
qDebug() << "Pointer is valid and points to an existing object";
}
See also
QSharedPointer::QSharedPointer()

Friends And Related Function Documentation

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

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

bool operator!= ( const QWeakPointer< T > &  ptr1,
std::nullptr_t   
)
related

Returns true if ptr1 is not a nullptr, otherwise returns false.

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

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

bool operator!= ( std::nullptr_t  ,
const QWeakPointer< T > &  ptr2 
)
related

Returns true if ptr2 is not a nullptr, otherwise returns false.

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

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

bool operator== ( const QWeakPointer< T > &  ptr1,
std::nullptr_t   
)
related

Returns true if ptr1 is a nullptr, otherwise returns false.

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

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

bool operator== ( std::nullptr_t  ,
const QWeakPointer< T > &  ptr2 
)
related

Returns true if ptr2 is a nullptr, otherwise returns false.