QSharedDataPointer Class
template <typename T> class QSharedDataPointer
The QSharedDataPointer class represents a pointer to an implicitly shared object. More…
| Header | #include <QSharedDataPointer> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Functions
| QSharedDataPointer() | |
| QSharedDataPointer(T *data) | |
| QSharedDataPointer(QAdoptSharedDataTag, T *data) | |
| QSharedDataPointer(const QSharedDataPointer &o) | |
| QSharedDataPointer(QSharedDataPointer &&o) | |
| QSharedDataPointer & | operator=(const QSharedDataPointer &o) |
| QSharedDataPointer & | operator=(QSharedDataPointer &&o) |
| ~QSharedDataPointer() | |
| void | detach() |
| T & | operator*() |
| const T & | operator*() const |
| T * | operator->() |
| const T * | operator->() const |
| operator bool() const | |
| T * | data() |
| const T * | data() const |
| T * | get() |
| const T * | get() const |
| const T * | constData() const |
| void | reset(T *p = nullptr) |
| void | swap(QSharedDataPointer &o) |
| bool | operator!() const |
| T * | take() |
Protected Functions
| T * | clone() |
Detailed Description
QSharedDataPointer<T> holds a pointer to a QSharedData-derived object and maintains its reference count. Copying a QSharedDataPointer shares the data; calling a non-const function, or the non-const operator->(), operator*() or data(), detaches first, making a private copy of the data if it is shared (copy-on-write).
See also QSharedData and QExplicitlySharedDataPointer.
Differences from Qt
The conversion to bool is implicit.
Member Function Documentation
QSharedDataPointer::QSharedDataPointer()
Constructs a null pointer.
QSharedDataPointer::QSharedDataPointer(T *data)
Constructs a pointer that shares data and increments its reference count. A null data gives a null pointer.
QSharedDataPointer::QSharedDataPointer(QAdoptSharedDataTag, T *data)
Constructs a pointer that adopts data without changing its reference count. The first argument is the tag that selects this constructor.
QSharedDataPointer::QSharedDataPointer(const QSharedDataPointer &o)
Constructs a copy of o that shares its data and increments the reference count.
QSharedDataPointer::QSharedDataPointer(QSharedDataPointer &&o)
Move-constructs a pointer from o, leaving o null.
QSharedDataPointer::~QSharedDataPointer()
Destroys the pointer, decrementing the reference count and deleting the data if this was the last reference.
T * QSharedDataPointer::clone()
Returns a new copy of the shared object, for detach(). The default copy-constructs T; specialize it for a class that needs another way, such as a virtual clone():
template <> Shape::Data *QSharedDataPointer<Shape::Data>::clone() { return d->clone(); }
const T * QSharedDataPointer::constData() const
Returns a const pointer to the data without detaching.
See also data().
T * QSharedDataPointer::data()
Returns a pointer to the data, detaching first if it is shared.
See also constData().
const T * QSharedDataPointer::data() const
This is an overloaded function.
Returns a const pointer to the data without detaching.
void QSharedDataPointer::detach()
Detaches from shared data. If the data is shared with another pointer, it is copied and this pointer refers to the copy. Does nothing for a null pointer.
T * QSharedDataPointer::get()
Returns a pointer to the data, detaching first if it is shared. This is the same as data().
This function was introduced in 6.0.
const T * QSharedDataPointer::get() const
This is an overloaded function.
Returns a const pointer to the data without detaching.
This function was introduced in 6.0.
QSharedDataPointer::operator bool() const
Returns true if the pointer is not null; otherwise returns false.
bool QSharedDataPointer::operator!() const
Returns true if the pointer is null.
T & QSharedDataPointer::operator*()
Returns a reference to the data, detaching first if it is shared.
const T & QSharedDataPointer::operator*() const
This is an overloaded function.
Returns a const reference to the data without detaching.
T * QSharedDataPointer::operator->()
Returns a pointer to the data, detaching first if it is shared.
const T * QSharedDataPointer::operator->() const
This is an overloaded function.
Returns a const pointer to the data without detaching.
QSharedDataPointer & QSharedDataPointer::operator=(const QSharedDataPointer &o)
Assigns o to this pointer, which then shares the data of o, and returns a reference to this pointer.
QSharedDataPointer & QSharedDataPointer::operator=(QSharedDataPointer &&o)
Move-assigns o to this pointer, leaving o null, and returns a reference to this pointer.
void QSharedDataPointer::reset(T *p = nullptr)
Resets the pointer to share p, releasing the current data. The default is a null pointer.
void QSharedDataPointer::swap(QSharedDataPointer &o)
Swaps this pointer with o. This operation is very fast and never fails.
T * QSharedDataPointer::take()
Returns the shared object and makes this pointer null, without changing the reference count; the caller now holds that reference.
This function was introduced in 6.0.