diff options
Diffstat (limited to 'src/corelib/tools/qarraydatapointer.h')
-rw-r--r-- | src/corelib/tools/qarraydatapointer.h | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index eb20330f0f..6657d40cf9 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -27,27 +27,39 @@ public: typedef typename std::conditional<pass_parameter_by_value, T, const T &>::type parameter_type; + Q_NODISCARD_CTOR constexpr QArrayDataPointer() noexcept : d(nullptr), ptr(nullptr), size(0) { } + Q_NODISCARD_CTOR QArrayDataPointer(const QArrayDataPointer &other) noexcept : d(other.d), ptr(other.ptr), size(other.size) { ref(); } + Q_NODISCARD_CTOR constexpr QArrayDataPointer(Data *header, T *adata, qsizetype n = 0) noexcept : d(header), ptr(adata), size(n) { } - explicit QArrayDataPointer(QPair<QTypedArrayData<T> *, T *> adata, qsizetype n = 0) noexcept + Q_NODISCARD_CTOR + explicit QArrayDataPointer(std::pair<QTypedArrayData<T> *, T *> adata, qsizetype n = 0) noexcept : d(adata.first), ptr(adata.second), size(n) { } + Q_NODISCARD_CTOR explicit + QArrayDataPointer(qsizetype alloc, qsizetype n = 0, + QArrayData::AllocationOption option = QArrayData::KeepSize) + : QArrayDataPointer(Data::allocate(alloc, option), n) + { + } + + Q_NODISCARD_CTOR static QArrayDataPointer fromRawData(const T *rawData, qsizetype length) noexcept { Q_ASSERT(rawData || !length); @@ -61,12 +73,12 @@ public: return *this; } + Q_NODISCARD_CTOR QArrayDataPointer(QArrayDataPointer &&other) noexcept - : d(other.d), ptr(other.ptr), size(other.size) + : d(std::exchange(other.d, nullptr)), + ptr(std::exchange(other.ptr, nullptr)), + size(std::exchange(other.size, 0)) { - other.d = nullptr; - other.ptr = nullptr; - other.size = 0; } QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QArrayDataPointer) @@ -320,13 +332,11 @@ public: if constexpr (IsFwdIt) { const qsizetype n = std::distance(first, last); if (needsDetach() || n > constAllocatedCapacity()) { - QArrayDataPointer allocated(Data::allocate(detachCapacity(n))); - Q_CHECK_PTR(allocated.data()); + QArrayDataPointer allocated(detachCapacity(n)); swap(allocated); } } else if (needsDetach()) { - QArrayDataPointer allocated(Data::allocate(allocatedCapacity())); - Q_CHECK_PTR(allocated.data()); + QArrayDataPointer allocated(allocatedCapacity()); swap(allocated); // We don't want to copy data that we know we'll overwrite } @@ -402,6 +412,26 @@ public: size = dst - begin(); } + QArrayDataPointer sliced(qsizetype pos, qsizetype n) const & + { + QArrayDataPointer result(n); + std::uninitialized_copy_n(begin() + pos, n, result.begin()); + result.size = n; + return result; + } + + QArrayDataPointer sliced(qsizetype pos, qsizetype n) && + { + if (needsDetach()) + return sliced(pos, n); + T *newBeginning = begin() + pos; + std::destroy(begin(), newBeginning); + std::destroy(newBeginning + n, end()); + setBegin(newBeginning); + size = n; + return std::move(*this); + } + // forwards from QArrayData qsizetype allocatedCapacity() noexcept { return d ? d->allocatedCapacity() : 0; } qsizetype constAllocatedCapacity() const noexcept { return d ? d->constAllocatedCapacity() : 0; } |