QExplicitlySharedDataPointer Class▲
-
Header: QExplicitlySharedDataPointer
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
Detailed Description▲
QExplicitlySharedDataPointer<T> makes writing your own explicitly shared classes easy. QExplicitlySharedDataPointer implements thread-safe reference counting, ensuring that adding QExplicitlySharedDataPointers to your 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 Employee example in QSharedDataPointer, modified to use explicit sharing as explained in the discussion 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▲
See also QSharedData, QSharedDataPointer
Member Type Documentation▲
QExplicitlySharedDataPointer::Type▲
This is the type of the shared data object. The d pointer points to an object of this type.
Member Function Documentation▲
QExplicitlySharedDataPointer::QExplicitlySharedDataPointer()▲
[explicit] QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(T *data)▲
QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T> &o)▲
QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<X> &o)▲
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.
relying on such static_cast is potentially dangerous, because it allows code like this to compile:
QExplicitlySharedDataPointer&
lt;Base&
gt; base(new
Base);
QExplicitlySharedDataPointer&
lt;Derived&
gt; derived(base); // !!! DANGER !!!
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.
[since 5.2] QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(QExplicitlySharedDataPointer<T> &&o)▲
This function was introduced in Qt 5.2.
QExplicitlySharedDataPointer::~QExplicitlySharedDataPointer()▲
[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<T>::clone() for an explanation of how to use it.
const T *QExplicitlySharedDataPointer::constData() const▲
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.
[since 6.0] T *QExplicitlySharedDataPointer::get() const▲
Same as data(). This function is provided for STL compatibility.
This function was introduced in Qt 6.0.
[since 6.0] void QExplicitlySharedDataPointer::reset(T *ptr = nullptr)▲
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.
This function was introduced in Qt 6.0.
void QExplicitlySharedDataPointer::swap(QExplicitlySharedDataPointer<T> &other)▲
Swap this instance's explicitly shared data pointer with the explicitly shared data pointer in other.
[since 5.12] 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.)
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.
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.
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=(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<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.
[since 5.2] QExplicitlySharedDataPointer<T> &QExplicitlySharedDataPointer::operator=(QExplicitlySharedDataPointer<T> &&other)▲
Move-assigns other to this QExplicitlySharedDataPointer instance.
This function was introduced in Qt 5.2.
Related Non-Members▲
[explicit] bool operator!=(const QExplicitlySharedDataPointer<T> &lhs, const QExplicitlySharedDataPointer<T> &rhs)▲
Returns true if lhs and rhs do not have the same d pointer.
[explicit] bool operator!=(const T *ptr, const QExplicitlySharedDataPointer<T> &rhs)▲
Returns true if the d pointer of rhs is not ptr.
[explicit] bool operator==(const QExplicitlySharedDataPointer<T> &lhs, const QExplicitlySharedDataPointer<T> &rhs)▲
Returns true if lhs and rhs have the same d pointer.
[explicit] bool operator==(const T *ptr, const QExplicitlySharedDataPointer<T> &rhs)▲
Returns true if the d pointer of rhs is ptr.