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

\inmodule QtCore More...

#include <qshareddata.h>

+ Collaboration diagram for QExplicitlySharedDataPointer< T >:

Public Types

typedef T Type
 This is the type of the shared data object.
 
typedef T * pointer
 

Public Member Functions

T & operator* () const
 Provides access to the shared data object's members.
 
T * operator-> () noexcept
 Provides access to the shared data object's members.
 
T * operator-> () const noexcept
 Provides const access to the shared data object's members.
 
 operator T* ()
 
 operator const T * () const noexcept
 
T * data () const noexcept
 Returns a pointer to the shared data object.
 
T * get () const noexcept
 
const T * constData () const noexcept
 Returns a const pointer to the shared data object.
 
T * take () 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.
 
void reset (T *ptr=nullptr) noexcept
 
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.
 
 operator bool () const noexcept
 Returns true if the {d pointer} of this is not null.
 
bool operator! () const noexcept
 Returns true if the {d pointer} of this is \nullptr.
 
void swap (QExplicitlySharedDataPointer &other) noexcept
 Swap this instance's explicitly shared data pointer with the explicitly shared data pointer in other.
 

Protected Member Functions

T * clone ()
 

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

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 129 of file qshareddata.h.

Member Typedef Documentation

◆ pointer

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

Definition at line 133 of file qshareddata.h.

◆ Type

template<typename 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 132 of file qshareddata.h.

Constructor & Destructor Documentation

◆ QExplicitlySharedDataPointer() [1/6]

template<typename T >
template< class T > QExplicitlySharedDataPointer< T >::QExplicitlySharedDataPointer ( )
inlinenoexcept

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

Definition at line 148 of file qshareddata.h.

◆ ~QExplicitlySharedDataPointer()

template<typename T >
template< class 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 149 of file qshareddata.h.

References d.

◆ QExplicitlySharedDataPointer() [2/6]

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

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

Definition at line 152 of file qshareddata.h.

References d.

◆ QExplicitlySharedDataPointer() [3/6]

template<typename T >
Q_NODISCARD_CTOR QExplicitlySharedDataPointer< T >::QExplicitlySharedDataPointer ( T * data,
QAdoptSharedDataTag  )
inlinenoexcept

Definition at line 155 of file qshareddata.h.

References d.

◆ QExplicitlySharedDataPointer() [4/6]

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

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 158 of file qshareddata.h.

References d.

◆ QExplicitlySharedDataPointer() [5/6]

template<typename T >
template<typename X >
template< class T > template< class X > 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.

However, if the macro {QT_ENABLE_QEXPLICITLYSHAREDDATAPOINTER_STATICCAST} is defined before including the {QExplicitlySharedDataPointer} header, then the {d pointer} of o undergoes a {static_cast} to the type {T *}. The result of the cast is then set as the {d pointer} of {this}, and the reference count of the shared data object is incremented.

Warning
relying on such {static_cast} is potentially dangerous, because it allows code like this to compile:
QExplicitlySharedDataPointer<Base> base(new Base);
QExplicitlySharedDataPointer<Derived> derived(base); // !!! DANGER !!!
static const uint base
Definition qurlidna.cpp:20
QExplicitlySharedDataPointer< Derived > derived(base)

Starting from Qt 5.4 the cast is disabled by default. It is possible to enable it back by defining the {QT_ENABLE_QEXPLICITLYSHAREDDATAPOINTER_STATICCAST} macro, and therefore to allow old code (that relied on this feature) to compile without modifications.

Definition at line 163 of file qshareddata.h.

References d.

◆ QExplicitlySharedDataPointer() [6/6]

template<typename T >
template< class T > QExplicitlySharedDataPointer< T >::QExplicitlySharedDataPointer ( QExplicitlySharedDataPointer< T > && o)
inlinenoexcept

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

Since
5.2

Definition at line 193 of file qshareddata.h.

References d.

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 237 of file qshareddata.h.

References d.

◆ constData()

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

Returns a const pointer to the shared data object.

See also
data()

Definition at line 142 of file qshareddata.h.

References d.

Referenced by QPlaceContent::d_func(), QColorTransformPrivate::isIdentity(), and QRegularExpressionMatchIterator::next().

+ Here is the caller graph for this function:

◆ data()

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

Returns a pointer to the shared data object.

Definition at line 140 of file qshareddata.h.

References d.

Referenced by QQmlValueTypeProxyBinding::~QQmlValueTypeProxyBinding(), QQmlValueTypeProxyBinding::binding(), QFontMetrics::boundingRect(), QFontMetricsF::boundingRect(), QFontMetricsF::boundingRect(), QFontMetrics::boundingRect(), QFontMetrics::boundingRect(), QFontMetricsF::boundingRect(), QQmlIncubator::clear(), QSGTextMaskMaterial::compare(), QPdfPageNavigator::currentLink(), QPlaceContent::d_func(), QFontMetrics::elidedText(), QFontMetricsF::elidedText(), QRegularExpression::errorString(), QCborMap::find(), QCborMap::find(), QCborMap::find(), QCborMap::find(), QRawFont::fromFont(), QFontPrivate::get(), QGlyphRunPrivate::get(), QRawFontPrivate::get(), QSGTextMaskMaterial::glyphCache(), QFontMetrics::horizontalAdvance(), QFontMetrics::horizontalAdvance(), QFontMetricsF::horizontalAdvance(), QFontMetricsF::horizontalAdvance(), QDomDocumentPrivate::implementation(), QCborArray::insert(), QCborArray::insert(), QRegularExpression::isValid(), QRegularExpression::match(), QRegularExpression::matchView(), QQmlAnyBinding::operator=(), QRegularExpression::optimize(), QRegularExpression::patternErrorOffset(), QQmlValueTypeProxyBinding::removeBindings(), QQmlValueTypeProxyBinding::setEnabled(), QFontPrivate::smallCapsFontPrivate(), QQmlValueTypeProxyBinding::subBindings(), QFontMetrics::tightBoundingRect(), QFontMetricsF::tightBoundingRect(), QFontMetrics::tightBoundingRect(), QFontMetricsF::tightBoundingRect(), and QCborArray::toJsonArray().

+ Here is the caller graph for this function:

◆ detach()

template<typename T >
template< class 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 145 of file qshareddata.h.

References d.

Referenced by QHostAddress::clear(), QNetworkRequestFactory::clearAttribute(), QNetworkRequestFactory::clearAttributes(), QNetworkRequestFactory::clearBearerToken(), QNetworkRequestFactory::clearCommonHeaders(), QNetworkRequestFactory::clearPassword(), QNetworkRequestFactory::clearQueryParameters(), QNetworkRequestFactory::clearUserName(), QPicture::detach(), QColorSpace::detach(), QPageRanges::detach(), QPlaceContent::detach(), QMediaTimeRange::detach(), QVideoFrameFormat::detach(), QGeoAreaMonitorInfo::detach(), QGeoPositionInfo::detach(), QGeoSatelliteInfo::detach(), QFontPrivate::detachButKeepEngineData(), QQuickIcon::ensureRelativeSourceResolved(), QRawFont::loadFromData(), QRegularExpressionMatchIterator::next(), QStorageInfo::refresh(), QGeoPositionInfo::removeAttribute(), QGeoSatelliteInfo::removeAttribute(), QQuickIcon::resetCache(), QQuickIcon::resetColor(), QQuickIcon::resetHeight(), QQuickIcon::resetName(), QQuickIcon::resetSource(), QQuickIcon::resetWidth(), QQuickIcon::resolve(), QHostAddress::setAddress(), QHostAddress::setAddress(), QHostAddress::setAddress(), QHostAddress::setAddress(), QGeoAreaMonitorInfo::setArea(), QGeoPositionInfo::setAttribute(), QGeoSatelliteInfo::setAttribute(), QNetworkRequestFactory::setAttribute(), QNetworkRequestFactory::setBaseUrl(), QNetworkRequestFactory::setBearerToken(), QPageLayout::setBottomMargin(), QPalette::setBrush(), QQuickIcon::setCache(), QQuickIcon::setColor(), QNetworkRequestFactory::setCommonHeaders(), QGeoPositionInfo::setCoordinate(), QGeoAreaMonitorInfo::setExpiration(), QQuickIcon::setHeight(), QPageLayout::setLeftMargin(), QPageLayout::setMargins(), QPageLayout::setMinimumMargins(), QPageLayout::setMode(), QQuickIcon::setName(), QGeoAreaMonitorInfo::setName(), QGeoAreaMonitorInfo::setNotificationParameters(), QPageLayout::setOrientation(), QPageLayout::setPageSize(), QNetworkRequestFactory::setPassword(), QStorageInfo::setPath(), QRegularExpression::setPattern(), QRegularExpression::setPatternOptions(), QGeoAreaMonitorInfo::setPersistent(), QRawFont::setPixelSize(), QNetworkRequestFactory::setPriority(), QNetworkRequestFactory::setQueryParameters(), QPageLayout::setRightMargin(), QGeoSatelliteInfo::setSatelliteIdentifier(), QGeoSatelliteInfo::setSatelliteSystem(), QHostAddress::setScopeId(), QGeoSatelliteInfo::setSignalStrength(), QQuickIcon::setSource(), QGeoPositionInfo::setTimestamp(), QPageLayout::setTopMargin(), QNetworkRequestFactory::setTransferTimeout(), QPageLayout::setUnits(), QNetworkRequestFactory::setUserName(), and QQuickIcon::setWidth().

◆ get()

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

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

Definition at line 141 of file qshareddata.h.

References d.

Referenced by QColorSpacePrivate::get(), and QColorSpacePrivate::get().

+ Here is the caller graph for this function:

◆ operator bool()

template<typename T >
template< class T > QExplicitlySharedDataPointer< T >::operator bool ( ) const
inlinenoexcept

Returns true if the {d pointer} of this is not null.

Definition at line 196 of file qshareddata.h.

References d.

◆ operator const T *()

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

Definition at line 139 of file qshareddata.h.

◆ operator T*()

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

Definition at line 138 of file qshareddata.h.

◆ operator!()

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

Returns true if the {d pointer} of this is \nullptr.

Definition at line 197 of file qshareddata.h.

References d.

◆ operator*()

template<typename T >
template< class T > T & QExplicitlySharedDataPointer< T >::operator* ( ) const
inline

Provides access to the shared data object's members.

Definition at line 135 of file qshareddata.h.

References d.

◆ operator->() [1/2]

template<typename T >
template< class T > const T * QExplicitlySharedDataPointer< T >::operator-> ( ) const
inlinenoexcept

Provides const access to the shared data object's members.

Definition at line 137 of file qshareddata.h.

References d.

◆ operator->() [2/2]

template<typename T >
template< class T > T * QExplicitlySharedDataPointer< T >::operator-> ( )
inlinenoexcept

Provides access to the shared data object's members.

Definition at line 136 of file qshareddata.h.

References d.

◆ operator=() [1/2]

template<typename T >
template< class T > QExplicitlySharedDataPointer< T > & 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 182 of file qshareddata.h.

References o.

◆ operator=() [2/2]

template<typename T >
template< class 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 187 of file qshareddata.h.

References o.

◆ reset()

template<typename T >
template< class T > void QExplicitlySharedDataPointer< T >::reset ( T * ptr = nullptr)
inlinenoexcept
Since
6.0

Sets the {d pointer} of this to ptr and increments {ptr}'s reference count if ptr is not \nullptr. The reference count of the old shared data object is decremented, and the object deleted if the reference count reaches 0.

Definition at line 171 of file qshareddata.h.

References d, and ptr().

Referenced by QCborArray::clear(), QCborMap::clear(), QPageRanges::clear(), QDomDocumentPrivate::clear(), QPageRanges::detach(), QPageRanges::fromString(), and QJsonPrivate::Parser::parse().

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

◆ swap()

template<typename T >
template< class T > void QExplicitlySharedDataPointer< T >::swap ( QExplicitlySharedDataPointer< T > & other)
inlinenoexcept

Swap this instance's explicitly shared data pointer with the explicitly shared data pointer in other.

Definition at line 199 of file qshareddata.h.

References d, other(), and qt_ptr_swap().

Referenced by QCapturableWindow::swap(), QPdfLink::swap(), and QPdfSelection::swap().

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

◆ take()

template<typename T >
template< class T > T * QExplicitlySharedDataPointer< T >::take ( )
inlinenoexcept
Since
5.12

Returns a pointer to the shared object, and resets this to be \nullptr. (That is, this function sets the {d pointer} of this to \nullptr.)

Note
The reference count of the returned object will {not} be decremented. This function can be used together with the constructor that takes a QAdoptSharedDataTag tag object to transfer the shared data object without intervening atomic operations.

Definition at line 143 of file qshareddata.h.

References d.

Referenced by QCborContainerPrivate::clone(), and QJsonPrivate::Parser::parse().

+ Here is the caller graph for this function:

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