QExplicitlySharedDataPointer Class | Qt Core 5.15.19 (original) (raw)

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

This class was introduced in Qt 4.4.

Member Function Documentation

QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(QExplicitlySharedDataPointer<T> &&o)

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

This function was introduced in Qt 5.2.

template QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<X> &o)

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:

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.

QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T> &o)

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.

QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(T *data)

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

QExplicitlySharedDataPointer::QExplicitlySharedDataPointer()

Constructs a QExplicitlySharedDataPointer initialized with nullptr as d pointer.

QExplicitlySharedDataPointer<T> &QExplicitlySharedDataPointer::operator=(QExplicitlySharedDataPointer<T> &&other)

Move-assigns other to this QExplicitlySharedDataPointer instance.

This function was introduced in Qt 5.2.

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

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.

QExplicitlySharedDataPointer::~QExplicitlySharedDataPointer()

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.

[protected] T *QExplicitlySharedDataPointer::clone()

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::clone() for an explanation of how to use it.

This function was introduced in Qt 4.5.

const T *QExplicitlySharedDataPointer::constData() const

Returns a const pointer to the shared data object.

See also data().

T *QExplicitlySharedDataPointer::data() const

Returns a pointer to the shared data object.

void QExplicitlySharedDataPointer::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.

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.

void QExplicitlySharedDataPointer::reset()

Resets this to be null - i.e., this function sets the d pointer of this to nullptr, but first it decrements the reference count of the shared data object and deletes the shared data object if the reference count became 0.

void QExplicitlySharedDataPointer::swap(QExplicitlySharedDataPointer<T> &other)

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

T *QExplicitlySharedDataPointer::take()

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 was introduced in Qt 5.12.

bool QExplicitlySharedDataPointer::operator bool() const

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

bool QExplicitlySharedDataPointer::operator!() const

Returns true if the d pointer of this is nullptr.

bool QExplicitlySharedDataPointer::operator!=(const QExplicitlySharedDataPointer<T> &other) const

Returns true if other and this do not have the same d pointer.

bool QExplicitlySharedDataPointer::operator!=(const T *ptr) const

Returns true if the d pointer of this is not ptr.

T &QExplicitlySharedDataPointer::operator*() const

Provides access to the shared data object's members.

T *QExplicitlySharedDataPointer::operator->()

Provides access to the shared data object's members.

T *QExplicitlySharedDataPointer::operator->() const

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

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

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.

bool QExplicitlySharedDataPointer::operator==(const QExplicitlySharedDataPointer<T> &other) const

Returns true if other and this have the same d pointer.

bool QExplicitlySharedDataPointer::operator==(const T *ptr) const

Returns true if the d pointer of this is ptr.