From a9034148980947b3408ff06f93bf81baff18601d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Wed, 19 Jun 2024 12:36:38 +0200 Subject: [PATCH] QNonContiguousByteDeviceByteArrayImpl: store value not pointer Storing a pointer was fine when used with the QBuffer version, but that means you have to keep the QBArray alive outside the class itself. Just store the value and it is a little easier to use. Changes in this cherry-pick: The dev patch chain has the test update in the wrong commit, so cherry-picked it along with this. And apparently I also forgot to commit a bug-fix to this commit as well, the bug anyway disappeared in dev when I deleted the class and redirected everything to the ByteArray class. Change-Id: Ic02e0b4627fde2b5fdd7e5fb69a94e06aa768ab9 Reviewed-by: Alexey Edelev Reviewed-by: Dennis Oberst (cherry picked from commit 67140afca21efae75aad35f63ca1d7211fe6ec3a) Reviewed-by: Alexey Edelev --- src/corelib/io/qnoncontiguousbytedevice.cpp | 21 ++++++++++--------- src/corelib/io/qnoncontiguousbytedevice_p.h | 9 ++++---- .../tst_qhttpnetworkconnection.cpp | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/corelib/io/qnoncontiguousbytedevice.cpp b/src/corelib/io/qnoncontiguousbytedevice.cpp index 260fea7969d..d108a00cf20 100644 --- a/src/corelib/io/qnoncontiguousbytedevice.cpp +++ b/src/corelib/io/qnoncontiguousbytedevice.cpp @@ -6,6 +6,8 @@ #include #include +#include + QT_BEGIN_NAMESPACE /*! @@ -102,10 +104,9 @@ QNonContiguousByteDevice::~QNonContiguousByteDevice() // FIXME we should scrap this whole implementation and instead change the ByteArrayImpl to be able to cope with sub-arrays? QNonContiguousByteDeviceBufferImpl::QNonContiguousByteDeviceBufferImpl(QBuffer *b) : QNonContiguousByteDevice(), - buffer(b), - byteArray(QByteArray::fromRawData(buffer->buffer().constData() + buffer->pos(), - buffer->size() - buffer->pos())), - arrayImpl(new QNonContiguousByteDeviceByteArrayImpl(&byteArray)) + byteArray(QByteArray::fromRawData(b->buffer().constData() + b->pos(), + b->buffer().size() - b->pos())), + arrayImpl(new QNonContiguousByteDeviceByteArrayImpl(b->buffer().sliced(b->pos()))) { arrayImpl->setParent(this); connect(arrayImpl, &QNonContiguousByteDevice::readyRead, this, @@ -143,8 +144,8 @@ qint64 QNonContiguousByteDeviceBufferImpl::size() const return arrayImpl->size(); } -QNonContiguousByteDeviceByteArrayImpl::QNonContiguousByteDeviceByteArrayImpl(QByteArray *ba) - : QNonContiguousByteDevice(), byteArray(ba), currentPosition(0) +QNonContiguousByteDeviceByteArrayImpl::QNonContiguousByteDeviceByteArrayImpl(QByteArray ba) + : QNonContiguousByteDevice(), byteArray(std::move(ba)), currentPosition(0) { } @@ -164,7 +165,7 @@ const char* QNonContiguousByteDeviceByteArrayImpl::readPointer(qint64 maximumLen else len = size() - currentPosition; - return byteArray->constData() + currentPosition; + return byteArray.constData() + currentPosition; } bool QNonContiguousByteDeviceByteArrayImpl::advanceReadPointer(qint64 amount) @@ -187,7 +188,7 @@ bool QNonContiguousByteDeviceByteArrayImpl::reset() qint64 QNonContiguousByteDeviceByteArrayImpl::size() const { - return byteArray->size(); + return byteArray.size(); } qint64 QNonContiguousByteDeviceByteArrayImpl::pos() const @@ -515,7 +516,7 @@ QNonContiguousByteDeviceFactory::createShared(std::shared_ptr ringB \internal */ -QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(QByteArray *byteArray) +QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(const QByteArray &byteArray) { return new QNonContiguousByteDeviceByteArrayImpl(byteArray); } @@ -526,7 +527,7 @@ QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(QByteArray *by \internal */ std::shared_ptr -QNonContiguousByteDeviceFactory::createShared(QByteArray *byteArray) +QNonContiguousByteDeviceFactory::createShared(const QByteArray &byteArray) { return std::make_shared(byteArray); } diff --git a/src/corelib/io/qnoncontiguousbytedevice_p.h b/src/corelib/io/qnoncontiguousbytedevice_p.h index eb75034c6a4..0220ac3c312 100644 --- a/src/corelib/io/qnoncontiguousbytedevice_p.h +++ b/src/corelib/io/qnoncontiguousbytedevice_p.h @@ -52,8 +52,8 @@ public: static QNonContiguousByteDevice *create(QIODevice *device); static std::shared_ptr createShared(QIODevice *device); - static QNonContiguousByteDevice *create(QByteArray *byteArray); - static std::shared_ptr createShared(QByteArray *byteArray); + static QNonContiguousByteDevice *create(const QByteArray &byteArray); + static std::shared_ptr createShared(const QByteArray &byteArray); static QNonContiguousByteDevice *create(std::shared_ptr ringBuffer); static std::shared_ptr createShared(std::shared_ptr ringBuffer); @@ -68,7 +68,7 @@ class QNonContiguousByteDeviceByteArrayImpl : public QNonContiguousByteDevice { Q_OBJECT public: - explicit QNonContiguousByteDeviceByteArrayImpl(QByteArray *ba); + explicit QNonContiguousByteDeviceByteArrayImpl(QByteArray ba); ~QNonContiguousByteDeviceByteArrayImpl(); const char *readPointer(qint64 maximumLength, qint64 &len) override; bool advanceReadPointer(qint64 amount) override; @@ -78,7 +78,7 @@ public: qint64 pos() const override; protected: - QByteArray *byteArray; + QByteArray byteArray; qint64 currentPosition; }; @@ -137,7 +137,6 @@ public: qint64 size() const override; protected: - QBuffer *buffer; QByteArray byteArray; QNonContiguousByteDeviceByteArrayImpl *arrayImpl; }; diff --git a/tests/auto/network/access/qhttpnetworkconnection/tst_qhttpnetworkconnection.cpp b/tests/auto/network/access/qhttpnetworkconnection/tst_qhttpnetworkconnection.cpp index decd4421649..134bc7413b4 100644 --- a/tests/auto/network/access/qhttpnetworkconnection/tst_qhttpnetworkconnection.cpp +++ b/tests/auto/network/access/qhttpnetworkconnection/tst_qhttpnetworkconnection.cpp @@ -249,7 +249,7 @@ void tst_QHttpNetworkConnection::put() QHttpNetworkRequest request(protocol + host + path, QHttpNetworkRequest::Put); QByteArray array = data.toLatin1(); - QNonContiguousByteDevice *bd = QNonContiguousByteDeviceFactory::create(&array); + QNonContiguousByteDevice *bd = QNonContiguousByteDeviceFactory::create(array); bd->setParent(this); request.setUploadByteDevice(bd); @@ -331,7 +331,7 @@ void tst_QHttpNetworkConnection::post() QHttpNetworkRequest request(protocol + host + path, QHttpNetworkRequest::Post); QByteArray array = data.toLatin1(); - QNonContiguousByteDevice *bd = QNonContiguousByteDeviceFactory::create(&array); + QNonContiguousByteDevice *bd = QNonContiguousByteDeviceFactory::create(array); bd->setParent(this); request.setUploadByteDevice(bd);