Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QExplicitlySharedDataPointer< T > Class Template Reference

\inmodule QtCore More...

#include <qshareddata.h>

Inheritance diagram for QExplicitlySharedDataPointer< T >:
Collaboration diagram for QExplicitlySharedDataPointer< T >:

Public Types

typedef T Type
 This is the type of the shared data object.
typedef T * pointer
Public Types inherited from QSharedDataPointerBase< QExplicitlySharedDataPointer, T >
using Type
using pointer

Public Member Functions

 operator T* ()
 operator const T * () const noexcept
T * data () const noexcept
 Returns a pointer to the shared data object.
T * get () const noexcept
void detach ()
 If the shared data object's reference count is greater than 1, this function creates a deep copy of the shared data object and sets the {d pointer} of this to the copy.
Q_NODISCARD_CTOR QExplicitlySharedDataPointer () noexcept
 Constructs a QExplicitlySharedDataPointer initialized with \nullptr as {d pointer}.
 ~QExplicitlySharedDataPointer ()
 Decrements the reference count of the shared data object.
Q_NODISCARD_CTOR QExplicitlySharedDataPointer (T *data) noexcept
 Constructs a QExplicitlySharedDataPointer with {d pointer} set to data and increments {data}'s reference count.
Q_NODISCARD_CTOR QExplicitlySharedDataPointer (T *data, QAdoptSharedDataTag) noexcept
Q_NODISCARD_CTOR QExplicitlySharedDataPointer (const QExplicitlySharedDataPointer &o) noexcept
 This standard copy constructor sets the {d pointer} of this to the {d pointer} in o and increments the reference count of the shared data object.
template<typename X>
Q_NODISCARD_CTOR QExplicitlySharedDataPointer (const QExplicitlySharedDataPointer< X > &o) noexcept
 This copy constructor is different in that it allows o to be a different type of explicitly shared data pointer but one that has a compatible shared data object.
QExplicitlySharedDataPointeroperator= (const QExplicitlySharedDataPointer &o) noexcept
 Sets the {d pointer} of this to the {d pointer} of o and increments the reference count of the shared data object.
QExplicitlySharedDataPointeroperator= (T *o) noexcept
 Sets the {d pointer} of this to o and increments {o}'s reference count.
Q_NODISCARD_CTOR QExplicitlySharedDataPointer (QExplicitlySharedDataPointer &&o) noexcept
 Move-constructs a QExplicitlySharedDataPointer instance, making it point at the same object that o was pointing to.
void detach ()
void detach ()
void swap (Self &other) noexcept
void reset (T *ptr=nullptr) noexcept
Public Member Functions inherited from QSharedDataPointerBase< QExplicitlySharedDataPointer, T >
void detach ()
T & operator* ()
constT & operator* () const
T * operator-> ()
constT * operator-> () const noexcept
 operator T* ()
 operator const T * () const noexcept
T * data ()
T * get ()
const T * data () const noexcept
const T * get () const noexcept
const T * constData () const noexcept
T * take () noexcept
void reset (T *ptr=nullptr) noexcept
 operator bool () const noexcept
bool operator! () const noexcept
void swap (Self &other) noexcept

Protected Member Functions

T * clone ()
Protected Member Functions inherited from QSharedDataPointerBase< QExplicitlySharedDataPointer, T >
constexpr QSharedDataPointerBase (T *ptr=nullptr) noexcept
void detach_helper ()

Static Protected Member Functions

template<typename... Args>
static T * create (Args &&... args)
static void destroy (T *ptr)

Additional Inherited Members

Protected Attributes inherited from QSharedDataPointerBase< QExplicitlySharedDataPointer, T >
Qt::totally_ordered_wrapper< T * > d

Detailed Description

template<typename T>
class QExplicitlySharedDataPointer< T >

\inmodule QtCore

The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object.

Since
4.4 \reentrant

\compares strong \compareswith strong T* std::nullptr_t \endcompareswith

QExplicitlySharedDataPointer<T> makes writing your own explicitly shared classes easy. QExplicitlySharedDataPointer implements \l {thread-safe} reference counting, ensuring that adding QExplicitlySharedDataPointers to your \l {reentrant} classes won't make them non-reentrant.

Except for one big difference, QExplicitlySharedDataPointer is just like QSharedDataPointer. The big difference is that member functions of QExplicitlySharedDataPointer {do not} do the automatic {copy on write} operation (detach()) that non-const members of QSharedDataPointer do before allowing the shared data object to be modified. There is a detach() function available, but if you really want to detach(), you have to call it yourself. This means that QExplicitlySharedDataPointers behave like regular C++ pointers, except that by doing reference counting and not deleting the shared data object until the reference count is 0, they avoid the dangling pointer problem.

It is instructive to compare QExplicitlySharedDataPointer with QSharedDataPointer by way of an example. Consider the \l {Employee example} in QSharedDataPointer, modified to use explicit sharing as explained in the discussion \l {Implicit vs Explicit Sharing}.

Note that if you use this class but find you are calling detach() a lot, you probably should be using QSharedDataPointer instead.

In the member function documentation, {d pointer} always refers to the internal pointer to the shared data object.

See also
QSharedData, QSharedDataPointer

Definition at line 225 of file qshareddata.h.

Member Typedef Documentation

◆ pointer

template<typename T>
typedef T* QExplicitlySharedDataPointer< T >::pointer

Definition at line 231 of file qshareddata.h.

◆ Type

template<typename T>
typedef T QExplicitlySharedDataPointer< T >::Type

This is the type of the shared data object.

The {d pointer} points to an object of this type.

Definition at line 230 of file qshareddata.h.

Constructor & Destructor Documentation

◆ QExplicitlySharedDataPointer() [1/6]

Constructs a QExplicitlySharedDataPointer initialized with \nullptr as {d pointer}.

Definition at line 256 of file qshareddata.h.

◆ ~QExplicitlySharedDataPointer()

template<typename T>
QExplicitlySharedDataPointer< T >::~QExplicitlySharedDataPointer ( )
inline

Decrements the reference count of the shared data object.

If the reference count becomes 0, the shared data object is deleted. This is then destroyed.

Definition at line 257 of file qshareddata.h.

◆ QExplicitlySharedDataPointer() [2/6]

template<typename T>
Q_NODISCARD_CTOR QExplicitlySharedDataPointer< T >::QExplicitlySharedDataPointer ( T * data)
inlineexplicitnoexcept

Constructs a QExplicitlySharedDataPointer with {d pointer} set to data and increments {data}'s reference count.

Definition at line 260 of file qshareddata.h.

◆ QExplicitlySharedDataPointer() [3/6]

Definition at line 263 of file qshareddata.h.

◆ QExplicitlySharedDataPointer() [4/6]

This standard copy constructor sets the {d pointer} of this to the {d pointer} in o and increments the reference count of the shared data object.

Definition at line 266 of file qshareddata.h.

◆ QExplicitlySharedDataPointer() [5/6]

template<typename T>
template<typename X>
Q_NODISCARD_CTOR QExplicitlySharedDataPointer< T >::QExplicitlySharedDataPointer ( const QExplicitlySharedDataPointer< X > & o)
inlinenoexcept

This copy constructor is different in that it allows o to be a different type of explicitly shared data pointer but one that has a compatible shared data object.

By default, the {d pointer} of o (of type {X *}) gets implicitly converted to the type {T *}; the result of this conversion is set as the {d pointer} of {this}, and the reference count of the shared data object is incremented.

Definition at line 271 of file qshareddata.h.

◆ QExplicitlySharedDataPointer() [6/6]

Move-constructs a QExplicitlySharedDataPointer instance, making it point at the same object that o was pointing to.

Since
5.2

Definition at line 289 of file qshareddata.h.

Member Function Documentation

◆ clone()

template<typename T>
Q_INLINE_TEMPLATE T * QExplicitlySharedDataPointer< T >::clone ( )
protected
Since
4.5

Creates and returns a deep copy of the current data. This function is called by detach() when the reference count is greater than 1 in order to create the new copy. This function uses the {operator new} and calls the copy constructor of the type T.

See QSharedDataPointer<T>::clone() for an explanation of how to use it.

Definition at line 328 of file qshareddata.h.

◆ create()

template<typename T>
template<typename... Args>
T * QExplicitlySharedDataPointer< T >::create ( Args &&... args)
inlinestaticprotected

Definition at line 308 of file qshareddata.h.

◆ data()

template<typename T>
T * QExplicitlySharedDataPointer< T >::data ( ) const
inlinenoexcept

Returns a pointer to the shared data object.

Definition at line 240 of file qshareddata.h.

◆ destroy()

template<typename T>
void QExplicitlySharedDataPointer< T >::destroy ( T * ptr)
inlinestaticprotected

Definition at line 310 of file qshareddata.h.

◆ detach() [1/3]

template<typename T>
void QExplicitlySharedDataPointer< T >::detach ( )
inline

If the shared data object's reference count is greater than 1, this function creates a deep copy of the shared data object and sets the {d pointer} of this to the copy.

Because QExplicitlySharedDataPointer does not do the automatic {copy on write} operations that members of QSharedDataPointer do, detach() is not called automatically anywhere in the member functions of this class. If you find that you are calling detach() everywhere in your code, consider using QSharedDataPointer instead.

Definition at line 253 of file qshareddata.h.

◆ detach() [2/3]

Definition at line 811 of file qhttpheaders.cpp.

◆ detach() [3/3]

Definition at line 811 of file qhttpheaders.cpp.

◆ get()

template<typename T>
T * QExplicitlySharedDataPointer< T >::get ( ) const
inlinenoexcept
Since
6.0

Same as data(). This function is provided for STL compatibility.

Definition at line 241 of file qshareddata.h.

◆ operator const T *()

template<typename T>
QExplicitlySharedDataPointer< T >::operator const T * ( ) const
inlineexplicitnoexcept

Definition at line 236 of file qshareddata.h.

◆ operator T*()

template<typename T>
QExplicitlySharedDataPointer< T >::operator T* ( )
inlineexplicit

Definition at line 235 of file qshareddata.h.

◆ operator=() [1/2]

template<typename T>
QExplicitlySharedDataPointer & QExplicitlySharedDataPointer< T >::operator= ( const QExplicitlySharedDataPointer< T > & o)
inlinenoexcept

Sets the {d pointer} of this to the {d pointer} of o and increments the reference count of the shared data object.

The reference count of the old shared data object of this is decremented. If the reference count of the old shared data object becomes 0, the old shared data object is deleted.

Definition at line 278 of file qshareddata.h.

◆ operator=() [2/2]

template<typename T>
QExplicitlySharedDataPointer & QExplicitlySharedDataPointer< T >::operator= ( T * o)
inlinenoexcept

Sets the {d pointer} of this to o and increments {o}'s reference count.

The reference count of the old shared data object of this is decremented. If the reference count of the old shared data object becomes 0, the old shared data object is deleted.

Definition at line 283 of file qshareddata.h.

◆ reset()

template<typename T>
void QSharedDataPointerBase< QExplicitlySharedDataPointer, T >::reset ( T * ptr = nullptr)
inlinenoexcept

Definition at line 84 of file qshareddata.h.

◆ swap()

template<typename T>
void QSharedDataPointerBase< QExplicitlySharedDataPointer, T >::swap ( Self & other)
inlinenoexcept

Definition at line 98 of file qshareddata.h.


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