QExplicitlySharedDataPointer 类

template <typename T> class QExplicitlySharedDataPointer

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

头: #include <QExplicitlySharedDataPointer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意: 此类的所有函数 可重入 .

公共类型

Type

公共函数

QExplicitlySharedDataPointer ()
QExplicitlySharedDataPointer (T * data )
QExplicitlySharedDataPointer (const QExplicitlySharedDataPointer<X> & o )
QExplicitlySharedDataPointer (const QExplicitlySharedDataPointer<T> & o )
QExplicitlySharedDataPointer (QExplicitlySharedDataPointer<T> && o )
~QExplicitlySharedDataPointer ()
const T * constData () const
T * data () const
void detach ()
(从 6.0 起) T * get () const
(从 6.0 起) void reset (T * ptr = nullptr)
void swap (QExplicitlySharedDataPointer<T> & other )
T * take ()
bool operator bool () const
bool operator! () const
T & operator* () const
T * operator-> ()
T * operator-> () const
QExplicitlySharedDataPointer<T> & operator= (const QExplicitlySharedDataPointer<T> & o )
QExplicitlySharedDataPointer<T> & operator= (T * o )
QExplicitlySharedDataPointer<T> & operator= (QExplicitlySharedDataPointer<T> && other )

保护函数

T * clone ()
bool operator!= (const QExplicitlySharedDataPointer<T> & lhs , const QExplicitlySharedDataPointer<T> & rhs )
bool operator!= (const T * ptr , const QExplicitlySharedDataPointer<T> & rhs )
bool operator== (const QExplicitlySharedDataPointer<T> & lhs , const QExplicitlySharedDataPointer<T> & rhs )
bool operator== (const T * ptr , const QExplicitlySharedDataPointer<T> & rhs )

详细描述

QExplicitlySharedDataPointer<T> makes writing your own explicitly shared classes easy. QExplicitlySharedDataPointer implements thread-safe reference counting, ensuring that adding QExplicitlySharedDataPointers to your 可重入 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 隐式 vs 明确共享 .

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

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

另请参阅 QSharedData and QSharedDataPointer .

成员类型文档编制

QExplicitlySharedDataPointer:: Type

这是共享数据对象的类型。 d pointer 指向此类型的对象。

成员函数文档编制

[noexcept] QExplicitlySharedDataPointer:: QExplicitlySharedDataPointer ()

Constructs a QExplicitlySharedDataPointer initialized with nullptr as d pointer .

[explicit noexcept] QExplicitlySharedDataPointer:: QExplicitlySharedDataPointer ( T * data )

Constructs a QExplicitlySharedDataPointer with d pointer 设为 data 并递增 data 的引用计数。

[noexcept] template <typename X> 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.

默认情况下, 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<Base> base(new Base);
    QExplicitlySharedDataPointer<Derived> 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.

[noexcept] QExplicitlySharedDataPointer:: QExplicitlySharedDataPointer (const QExplicitlySharedDataPointer < T > & o )

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

[noexcept] QExplicitlySharedDataPointer:: QExplicitlySharedDataPointer ( QExplicitlySharedDataPointer < T > && o )

Move-constructs a QExplicitlySharedDataPointer instance, making it point at the same object that o 所指向的。

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.

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

[noexcept] const T *QExplicitlySharedDataPointer:: constData () const

返回共享数据对象的 const 指针。

另请参阅 data ().

[noexcept] T *QExplicitlySharedDataPointer:: data () const

返回共享数据对象指针。

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.

因为 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 代替。

[noexcept, since 6.0] T *QExplicitlySharedDataPointer:: get () const

如同 data ()。此函数为兼容 STL (标准模板库) 提供。

该函数在 Qt 6.0 引入。

[noexcept, since 6.0] void QExplicitlySharedDataPointer:: reset ( T * ptr = nullptr)

设置 d pointer of this to ptr 并递增 ptr 的引用计数若 ptr 不是 nullptr . The reference count of the old shared data object is decremented, and the object deleted if the reference count reaches 0.

该函数在 Qt 6.0 引入。

[noexcept] void QExplicitlySharedDataPointer:: swap ( QExplicitlySharedDataPointer < T > & other )

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

[noexcept] T *QExplicitlySharedDataPointer:: take ()

Returns a pointer to the shared object, and resets this 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.

[noexcept] bool QExplicitlySharedDataPointer:: operator bool () const

返回 true d pointer of this is not null.

[noexcept] bool QExplicitlySharedDataPointer:: operator! () const

返回 true d pointer of this is nullptr .

T &QExplicitlySharedDataPointer:: operator* () const

Provides access to the shared data object's members.

[noexcept] T *QExplicitlySharedDataPointer:: operator-> ()

Provides access to the shared data object's members.

[noexcept] T *QExplicitlySharedDataPointer:: operator-> () const

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

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

设置 d pointer of this 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.

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

设置 d pointer of this to o 并递增 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.

[noexcept] QExplicitlySharedDataPointer < T > &QExplicitlySharedDataPointer:: operator= ( QExplicitlySharedDataPointer < T > && other )

移动赋值 other 到此 QExplicitlySharedDataPointer 实例。

相关非成员

[noexcept] bool operator!= (const QExplicitlySharedDataPointer < T > & lhs , const QExplicitlySharedDataPointer < T > & rhs )

返回 true if lhs and rhs do not 有相同 d pointer .

[noexcept] bool operator!= (const T * ptr , const QExplicitlySharedDataPointer < T > & rhs )

返回 true d pointer of rhs is not ptr .

[noexcept] bool operator== (const QExplicitlySharedDataPointer < T > & lhs , const QExplicitlySharedDataPointer < T > & rhs )

返回 true if lhs and rhs 有相同 d pointer .

[noexcept] bool operator== (const T * ptr , const QExplicitlySharedDataPointer < T > & rhs )

返回 true d pointer of rhs is ptr .