QtBase  v6.3.1
Public Types | Public Member Functions | Friends | Related Functions | List of all members
QWeakPointer< T > Class Template Reference

The QWeakPointer class holds a weak reference to a shared pointer. More...

#include <qsharedpointer_impl.h>

Public Types

typedef T element_type
 
typedef T value_type
 
typedef value_typepointer
 
typedef const value_typeconst_pointer
 
typedef value_typereference
 
typedef const value_typeconst_reference
 
typedef qptrdiff difference_type
 

Public Member Functions

bool isNull () const noexcept
 
 operator bool () const noexcept
 
bool operator! () const noexcept
 
constexpr QWeakPointer () noexcept
 
 ~QWeakPointer ()
 
 QWeakPointer (const QWeakPointer &other) noexcept
 
 QWeakPointer (QWeakPointer &&other) noexcept
 
template<class X , IfCompatible< X > = true>
 QWeakPointer (QWeakPointer< X > &&other) noexcept
 
template<class X , IfCompatible< X > = true>
QWeakPointeroperator= (QWeakPointer< X > &&other) noexcept
 
QWeakPointeroperator= (const QWeakPointer &other) noexcept
 
void swap (QWeakPointer &other) noexcept
 
 QWeakPointer (const QSharedPointer< T > &o)
 
QWeakPointeroperator= (const QSharedPointer< T > &o)
 
template<class X , IfCompatible< X > = true>
 QWeakPointer (const QWeakPointer< X > &o)
 
template<class X , IfCompatible< X > = true>
QWeakPointeroperator= (const QWeakPointer< X > &o)
 
template<class X , IfCompatible< X > = true>
 QWeakPointer (const QSharedPointer< X > &o)
 
template<class X , IfCompatible< X > = true>
QWeakPointeroperator= (const QSharedPointer< X > &o)
 
void clear ()
 
QSharedPointer< TtoStrongRef () const
 
QSharedPointer< Tlock () const
 
template<class X >
bool operator== (const QWeakPointer< X > &o) const noexcept
 
template<class X >
bool operator!= (const QWeakPointer< X > &o) const noexcept
 
template<class X >
bool operator== (const QSharedPointer< X > &o) const noexcept
 
template<class X >
bool operator!= (const QSharedPointer< X > &o) const noexcept
 

Friends

struct QtPrivate::EnableInternalData
 
template<class X >
class QSharedPointer
 
template<class X >
class QWeakPointer
 
template<class X >
class QPointer
 
template<typename X >
bool operator== (const QSharedPointer< X > &p1, const QWeakPointer &p2) noexcept
 
template<typename X >
bool operator!= (const QSharedPointer< X > &p1, const QWeakPointer &p2) noexcept
 
bool operator== (const QWeakPointer &p, std::nullptr_t)
 
bool operator== (std::nullptr_t, const QWeakPointer &p)
 
bool operator!= (const QWeakPointer &p, std::nullptr_t)
 
bool operator!= (std::nullptr_t, const QWeakPointer &p)
 

Related Functions

(Note that these are not member functions.)

template< class T > template< class X > bool operator== (const QSharedPointer< T > &ptr1, const QWeakPointer< X > &ptr2)
 
template< class T > template< class X > bool operator!= (const QSharedPointer< T > &ptr1, const QWeakPointer< X > &ptr2)
 
template< class T > template< class X > bool operator== (const QWeakPointer< T > &ptr1, const QSharedPointer< X > &ptr2)
 
template< class T > bool operator== (const QWeakPointer< T > &lhs, std::nullptr_t)
 
template< class T > bool operator== (std::nullptr_t, const QWeakPointer< T > &rhs)
 
template< class T > bool operator!= (const QWeakPointer< T > &lhs, std::nullptr_t)
 
template< class T > bool operator!= (std::nullptr_t, const QWeakPointer< T > &rhs)
 
template< class T > template< class X > bool operator!= (const QWeakPointer< T > &ptr1, const QSharedPointer< X > &ptr2)
 
template< class X > template< class T > QSharedPointer< XqSharedPointerCast (const QWeakPointer< T > &other)
 
template< class X > template< class T > QSharedPointer< XqSharedPointerDynamicCast (const QWeakPointer< T > &src)
 
template< class X > template< class T > QSharedPointer< XqSharedPointerConstCast (const QWeakPointer< T > &src)
 
template< class X > template< class T > QSharedPointer< XqSharedPointerObjectCast (const QWeakPointer< T > &src)
 The qSharedPointerObjectCast function is for casting a shared pointer. More...
 
template< class X > template< class T > QWeakPointer< XqWeakPointerCast (const QWeakPointer< T > &src)
 

Detailed Description

template<class T>
class QWeakPointer< T >

The QWeakPointer class holds a weak reference to a shared pointer.

\inmodule QtCore

Since
4.5 \reentrant

The QWeakPointer is an automatic weak reference to a pointer in C++. It cannot be used to dereference the pointer directly, but it can be used to verify if the pointer has been deleted or not in another context.

QWeakPointer objects can only be created by assignment from a QSharedPointer.

It's important to note that QWeakPointer provides no automatic casting operators to prevent mistakes from happening. Even though QWeakPointer tracks a pointer, it should not be considered a pointer itself, since it doesn't guarantee that the pointed object remains valid.

Therefore, to access the pointer that QWeakPointer is tracking, you must first promote it to QSharedPointer and verify if the resulting object is null or not. QSharedPointer guarantees that the object isn't deleted, so if you obtain a non-null object, you may use the pointer. See QWeakPointer::toStrongRef() for an example.

\omit

Definition at line 555 of file qsharedpointer_impl.h.

Member Typedef Documentation

◆ const_pointer

template<class T >
typedef const value_type* QWeakPointer< T >::const_pointer

Definition at line 565 of file qsharedpointer_impl.h.

◆ const_reference

template<class T >
typedef const value_type& QWeakPointer< T >::const_reference

Definition at line 567 of file qsharedpointer_impl.h.

◆ difference_type

template<class T >
typedef qptrdiff QWeakPointer< T >::difference_type

Definition at line 568 of file qsharedpointer_impl.h.

◆ element_type

template<class T >
typedef T QWeakPointer< T >::element_type

Definition at line 562 of file qsharedpointer_impl.h.

◆ pointer

template<class T >
typedef value_type* QWeakPointer< T >::pointer

Definition at line 564 of file qsharedpointer_impl.h.

◆ reference

template<class T >
typedef value_type& QWeakPointer< T >::reference

Definition at line 566 of file qsharedpointer_impl.h.

◆ value_type

template<class T >
typedef T QWeakPointer< T >::value_type

Definition at line 563 of file qsharedpointer_impl.h.

Constructor & Destructor Documentation

◆ QWeakPointer() [1/7]

template<class T >
constexpr QWeakPointer< T >::QWeakPointer ( )
inlineconstexprnoexcept

Definition at line 574 of file qsharedpointer_impl.h.

◆ ~QWeakPointer()

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

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

Definition at line 575 of file qsharedpointer_impl.h.

Here is the call graph for this function:

◆ QWeakPointer() [2/7]

template<class T >
template< class T > QWeakPointer< T >::QWeakPointer ( const QWeakPointer< T > &  other)
inlinenoexcept

Creates a QWeakPointer that holds a weak reference to the pointer referenced by other.

If \tt T is a derived type of the template parameter of this class, QWeakPointer will perform an automatic cast. Otherwise, you will get a compiler error.

Definition at line 577 of file qsharedpointer_impl.h.

Here is the call graph for this function:

◆ QWeakPointer() [3/7]

template<class T >
QWeakPointer< T >::QWeakPointer ( QWeakPointer< T > &&  other)
inlinenoexcept

Definition at line 579 of file qsharedpointer_impl.h.

Here is the call graph for this function:

◆ QWeakPointer() [4/7]

template<class T >
template<class X , IfCompatible< X > = true>
QWeakPointer< T >::QWeakPointer ( QWeakPointer< X > &&  other)
inlinenoexcept

Definition at line 588 of file qsharedpointer_impl.h.

Here is the call graph for this function:

◆ QWeakPointer() [5/7]

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

Creates a QWeakPointer that holds a weak reference to the pointer referenced by other.

If \tt T is a derived type of the template parameter of this class, QWeakPointer will perform an automatic cast. Otherwise, you will get a compiler error.

Definition at line 616 of file qsharedpointer_impl.h.

Here is the call graph for this function:

◆ QWeakPointer() [6/7]

template<class T >
template<class X , IfCompatible< X > = true>
QWeakPointer< T >::QWeakPointer ( const QWeakPointer< X > &  o)
inline

Definition at line 625 of file qsharedpointer_impl.h.

◆ QWeakPointer() [7/7]

template<class T >
template<class X , IfCompatible< X > = true>
QWeakPointer< T >::QWeakPointer ( const QSharedPointer< X > &  o)
inline

Definition at line 638 of file qsharedpointer_impl.h.

Member Function Documentation

◆ clear()

template<class T >
template< class T > void QWeakPointer< T >::clear ( )
inline

Clears this QWeakPointer object, dropping the reference that it may have had to the pointer.

Definition at line 648 of file qsharedpointer_impl.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isNull()

template<class T >
template< class T > bool QWeakPointer< T >::isNull ( ) const
inlinenoexcept

Returns true if this object refers to \nullptr.

Note that, due to the nature of weak references, the pointer that QWeakPointer references can become \nullptr at any moment, so the value returned from this function can change from false to true from one call to the next.

Definition at line 570 of file qsharedpointer_impl.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock()

template<class T >
template< class T > QSharedPointer< T > QWeakPointer< T >::lock ( ) const
inline
Since
5.4

Same as toStrongRef().

This function is provided for API compatibility with std::weak_ptr.

Definition at line 652 of file qsharedpointer_impl.h.

Here is the call graph for this function:

◆ operator bool()

template<class T >
template< class T > QWeakPointer< T >::operator bool ( ) const
inlineexplicitnoexcept

Returns true if the contained pointer is not \nullptr. This function is suitable for use in \tt if-constructs, like:

Note that, due to the nature of weak references, the pointer that QWeakPointer references can become \nullptr at any moment, so the value returned from this function can change from true to false from one call to the next.

See also
isNull()

Definition at line 571 of file qsharedpointer_impl.h.

Here is the call graph for this function:

◆ operator!()

template<class T >
template< class T > bool QWeakPointer< T >::operator! ( ) const
inlinenoexcept

Returns true if this object refers to \nullptr. This function is suitable for use in \tt if-constructs, like:

Note that, due to the nature of weak references, the pointer that QWeakPointer references can become \nullptr at any moment, so the value returned from this function can change from false to true from one call to the next.

See also
isNull()

Definition at line 572 of file qsharedpointer_impl.h.

Here is the call graph for this function:

◆ operator!=() [1/2]

template<class T >
template<class X >
bool QWeakPointer< T >::operator!= ( const QSharedPointer< X > &  o) const
inlinenoexcept

Definition at line 667 of file qsharedpointer_impl.h.

◆ operator!=() [2/2]

template<class T >
template<class X >
bool QWeakPointer< T >::operator!= ( const QWeakPointer< X > &  o) const
inlinenoexcept

Definition at line 659 of file qsharedpointer_impl.h.

◆ operator=() [1/5]

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

Makes this object share other's pointer. The current pointer reference is discarded but is not deleted.

If \tt T is a derived type of the template parameter of this class, QWeakPointer will perform an automatic cast. Otherwise, you will get a compiler error.

Definition at line 618 of file qsharedpointer_impl.h.

◆ operator=() [2/5]

template<class T >
template<class X , IfCompatible< X > = true>
QWeakPointer& QWeakPointer< T >::operator= ( const QSharedPointer< X > &  o)
inline

Definition at line 642 of file qsharedpointer_impl.h.

◆ operator=() [3/5]

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

Makes this object share other's pointer. The current pointer reference is discarded but is not deleted.

If \tt T is a derived type of the template parameter of this class, QWeakPointer will perform an automatic cast. Otherwise, you will get a compiler error.

Definition at line 603 of file qsharedpointer_impl.h.

◆ operator=() [4/5]

template<class T >
template<class X , IfCompatible< X > = true>
QWeakPointer& QWeakPointer< T >::operator= ( const QWeakPointer< X > &  o)
inline

Definition at line 629 of file qsharedpointer_impl.h.

◆ operator=() [5/5]

template<class T >
template<class X , IfCompatible< X > = true>
QWeakPointer& QWeakPointer< T >::operator= ( QWeakPointer< X > &&  other)
inlinenoexcept

Definition at line 596 of file qsharedpointer_impl.h.

Here is the call graph for this function:

◆ operator==() [1/2]

template<class T >
template<class X >
bool QWeakPointer< T >::operator== ( const QSharedPointer< X > &  o) const
inlinenoexcept

Definition at line 663 of file qsharedpointer_impl.h.

◆ operator==() [2/2]

template<class T >
template<class X >
bool QWeakPointer< T >::operator== ( const QWeakPointer< X > &  o) const
inlinenoexcept

Definition at line 655 of file qsharedpointer_impl.h.

◆ swap()

template<class T >
template< class T > void QWeakPointer< T >::swap ( QWeakPointer< T > &  other)
inlinenoexcept
Since
5.4

Swaps this weak pointer instance with other. This function is very fast and never fails.

Definition at line 610 of file qsharedpointer_impl.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toStrongRef()

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

Promotes this weak reference to a strong one and returns a QSharedPointer object holding that reference. When promoting to QSharedPointer, this function verifies if the object has been deleted already or not. If it hasn't, this function increases the reference count to the shared object, thus ensuring that it will not get deleted.

Since this function can fail to obtain a valid strong reference to the shared object, you should always verify if the conversion succeeded, by calling QSharedPointer::isNull() on the returned object.

For example, the following code promotes a QWeakPointer that was held to a strong reference and, if it succeeded, it prints the value of the integer that was held:

See also
QSharedPointer::QSharedPointer()

Definition at line 650 of file qsharedpointer_impl.h.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator!=() [1/7]

template<class T >
template< class T > template< class X > bool operator!= ( const QSharedPointer< T > &  ptr1,
const QWeakPointer< X > &  ptr2 
)
related

Returns true if ptr1 and ptr2 refer to distinct pointers.

If ptr2's template parameter is different from ptr1's, QSharedPointer will attempt to perform an automatic \tt 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.

◆ operator!= [2/7]

template<class T >
template<typename X >
bool operator!= ( const QSharedPointer< X > &  p1,
const QWeakPointer< T > &  p2 
)
friend

Definition at line 674 of file qsharedpointer_impl.h.

◆ operator!= [3/7]

template<class T >
template< class T > bool operator!= ( const QWeakPointer< T > &  p,
std::nullptr_t   
)
friend
Since
5.8

Returns true if lhs refers to a valid (i.e. non-null) pointer.

See also
QWeakPointer::isNull()

Definition at line 681 of file qsharedpointer_impl.h.

◆ operator!=() [4/7]

template<class T >
template< class T > bool operator!= ( const QWeakPointer< T > &  lhs,
std::nullptr_t   
)
related
Since
5.8

Returns true if lhs refers to a valid (i.e. non-null) pointer.

See also
QWeakPointer::isNull()

◆ operator!=() [5/7]

template<class T >
template< class T > template< class X > bool operator!= ( const QWeakPointer< T > &  ptr1,
const QSharedPointer< X > &  ptr2 
)
related

Returns true if ptr1 and ptr2 refer to distinct pointers.

If ptr2's template parameter is different from ptr1's, QSharedPointer will attempt to perform an automatic \tt 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.

◆ operator!= [6/7]

template<class T >
template< class T > bool operator!= ( std::nullptr_t  ,
const QWeakPointer< T > &  p 
)
friend
Since
5.8

Returns true if rhs refers to a valid (i.e. non-null) pointer.

See also
QWeakPointer::isNull()

Definition at line 683 of file qsharedpointer_impl.h.

◆ operator!=() [7/7]

template<class T >
template< class T > bool operator!= ( std::nullptr_t  ,
const QWeakPointer< T > &  rhs 
)
related
Since
5.8

Returns true if rhs refers to a valid (i.e. non-null) pointer.

See also
QWeakPointer::isNull()

◆ operator==() [1/7]

template<class T >
template< class T > template< class X > bool operator== ( const QSharedPointer< T > &  ptr1,
const QWeakPointer< X > &  ptr2 
)
related

Returns true if ptr1 and ptr2 refer to the same pointer.

If ptr2's template parameter is different from ptr1's, QSharedPointer will attempt to perform an automatic \tt 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.

◆ operator== [2/7]

template<class T >
template<typename X >
bool operator== ( const QSharedPointer< X > &  p1,
const QWeakPointer< T > &  p2 
)
friend

Definition at line 671 of file qsharedpointer_impl.h.

◆ operator== [3/7]

template<class T >
template< class T > bool operator== ( const QWeakPointer< T > &  p,
std::nullptr_t   
)
friend
Since
5.8

Returns true if lhs refers to \nullptr.

See also
QWeakPointer::isNull()

Definition at line 677 of file qsharedpointer_impl.h.

◆ operator==() [4/7]

template<class T >
template< class T > bool operator== ( const QWeakPointer< T > &  lhs,
std::nullptr_t   
)
related
Since
5.8

Returns true if lhs refers to \nullptr.

See also
QWeakPointer::isNull()

◆ operator==() [5/7]

template<class T >
template< class T > template< class X > bool operator== ( const QWeakPointer< T > &  ptr1,
const QSharedPointer< X > &  ptr2 
)
related

Returns true if ptr1 and ptr2 refer to the same pointer.

If ptr2's template parameter is different from ptr1's, QSharedPointer will attempt to perform an automatic \tt 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.

◆ operator== [6/7]

template<class T >
template< class T > bool operator== ( std::nullptr_t  ,
const QWeakPointer< T > &  p 
)
friend
Since
5.8

Returns true if rhs refers to \nullptr.

See also
QWeakPointer::isNull()

Definition at line 679 of file qsharedpointer_impl.h.

◆ operator==() [7/7]

template<class T >
template< class T > bool operator== ( std::nullptr_t  ,
const QWeakPointer< T > &  rhs 
)
related
Since
5.8

Returns true if rhs refers to \nullptr.

See also
QWeakPointer::isNull()

◆ QPointer

template<class T >
template<class X >
friend class QPointer
friend

Definition at line 690 of file qsharedpointer_impl.h.

◆ QSharedPointer

template<class T >
template<class X >
friend class QSharedPointer
friend

Definition at line 688 of file qsharedpointer_impl.h.

◆ qSharedPointerCast()

template<class T >
template< class X > template< class T > QSharedPointer< X > qSharedPointerCast ( const QWeakPointer< T > &  other)
related

Returns a shared pointer to the pointer held by other, cast to type \tt X. The types \tt T and \tt X must belong to one hierarchy for the \tt static_cast to succeed.

The other object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function returns a null QSharedPointer.

Note that \tt X must have the same cv-qualifiers (\tt const and \tt volatile) that \tt T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.

See also
QWeakPointer::toStrongRef(), qSharedPointerDynamicCast(), qSharedPointerConstCast()

Definition at line 842 of file qsharedpointer_impl.h.

◆ qSharedPointerConstCast()

template<class T >
template< class X > template< class T > QSharedPointer< X > qSharedPointerConstCast ( const QWeakPointer< T > &  src)
related

Returns a shared pointer to the pointer held by src, cast to type \tt X. The types \tt T and \tt X must belong to one hierarchy for the \tt const_cast to succeed. The \tt const and \tt volatile differences between \tt T and \tt X are ignored.

The src object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function returns a null QSharedPointer.

See also
QWeakPointer::toStrongRef(), qSharedPointerCast(), qSharedPointerDynamicCast()

Definition at line 868 of file qsharedpointer_impl.h.

◆ qSharedPointerDynamicCast()

template<class T >
template< class X > template< class T > QSharedPointer< X > qSharedPointerDynamicCast ( const QWeakPointer< T > &  src)
related

Returns a shared pointer to the pointer held by src, using a dynamic cast to type \tt X to obtain an internal pointer of the appropriate type. If the \tt dynamic_cast fails, the object returned will be null.

The src object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function also returns a null QSharedPointer.

Note that \tt X must have the same cv-qualifiers (\tt const and \tt volatile) that \tt T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.

See also
QWeakPointer::toStrongRef(), qSharedPointerCast(), qSharedPointerConstCast()

Definition at line 856 of file qsharedpointer_impl.h.

◆ qSharedPointerObjectCast()

template<class T >
template< class X > template< class T > QSharedPointer< X > qSharedPointerObjectCast ( const QWeakPointer< T > &  src)
related

The qSharedPointerObjectCast function is for casting a shared pointer.

Since
4.6

Returns a shared pointer to the pointer held by src, using a \l qobject_cast() to type \tt X to obtain an internal pointer of the appropriate type. If the \tt qobject_cast fails, the object returned will be null.

The src object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function also returns a null QSharedPointer.

Note that \tt X must have the same cv-qualifiers (\tt const and \tt volatile) that \tt T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.

See also
QWeakPointer::toStrongRef(), qSharedPointerCast(), qSharedPointerConstCast()

Definition at line 888 of file qsharedpointer_impl.h.

◆ QtPrivate::EnableInternalData

template<class T >
friend struct QtPrivate::EnableInternalData
friend

Definition at line 687 of file qsharedpointer_impl.h.

◆ QWeakPointer

template<class T >
template<class X >
template< class T > QWeakPointer< T >::QWeakPointer
friend

Creates a QWeakPointer that points to nothing.

Definition at line 689 of file qsharedpointer_impl.h.

◆ qWeakPointerCast()

template<class T >
template< class X > template< class T > QWeakPointer< X > qWeakPointerCast ( const QWeakPointer< T > &  src)
related

Returns a weak pointer to the pointer held by src, cast to type \tt X. The types \tt T and \tt X must belong to one hierarchy for the \tt static_cast to succeed.

Note that \tt X must have the same cv-qualifiers (\tt const and \tt volatile) that \tt T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.


The documentation for this class was generated from the following files: