From 0ff6f175ecf7b60529346ddbc2740a8419d07195 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 3 Apr 2014 11:02:08 +0200 Subject: [PATCH] Add QNonContiguousByteDeviceFactory::createShared() for more efficient creation of QNonContiguousByteDevices held in shared pointers. Use the new functions in QNetworkAccessBackend::createUploadByteDevice() and QNetworkReplyHttpImplPrivate::createUploadByteDevice(). Change-Id: I8a3c76f7c8d5926850303992c77e9382a39a55e8 Reviewed-by: Richard J. Moore --- src/corelib/io/qnoncontiguousbytedevice.cpp | 39 ++++++++++++++++++++ src/corelib/io/qnoncontiguousbytedevice_p.h | 6 +++ src/network/access/qnetworkaccessbackend.cpp | 4 +- src/network/access/qnetworkreplyhttpimpl.cpp | 4 +- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/corelib/io/qnoncontiguousbytedevice.cpp b/src/corelib/io/qnoncontiguousbytedevice.cpp index b1e4dd7584..4c5fb38c67 100644 --- a/src/corelib/io/qnoncontiguousbytedevice.cpp +++ b/src/corelib/io/qnoncontiguousbytedevice.cpp @@ -505,6 +505,25 @@ QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(QIODevice *dev return new QNonContiguousByteDeviceIoDeviceImpl(device); // FIXME } +/*! + Create a QNonContiguousByteDevice out of a QIODevice, return it in a QSharedPointer. + For QFile, QBuffer and all other QIODevice, sequential or not. + + \internal +*/ +QSharedPointer QNonContiguousByteDeviceFactory::createShared(QIODevice *device) +{ + // shortcut if it is a QBuffer + if (QBuffer *buffer = qobject_cast(device)) + return QSharedPointer::create(buffer); + + // ### FIXME special case if device is a QFile that supports map() + // then we can actually deal with the file without using read/peek + + // generic QIODevice + return QSharedPointer::create(device); // FIXME +} + /*! \fn static QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(QSharedPointer ringBuffer) @@ -517,6 +536,16 @@ QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(QSharedPointer return new QNonContiguousByteDeviceRingBufferImpl(ringBuffer); } +/*! + Create a QNonContiguousByteDevice out of a QRingBuffer, return it in a QSharedPointer. + + \internal +*/ +QSharedPointer QNonContiguousByteDeviceFactory::createShared(QSharedPointer ringBuffer) +{ + return QSharedPointer::create(qMove(ringBuffer)); +} + /*! \fn static QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(QByteArray *byteArray) @@ -529,6 +558,16 @@ QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(QByteArray *by return new QNonContiguousByteDeviceByteArrayImpl(byteArray); } +/*! + Create a QNonContiguousByteDevice out of a QByteArray. + + \internal +*/ +QSharedPointer QNonContiguousByteDeviceFactory::createShared(QByteArray *byteArray) +{ + return QSharedPointer::create(byteArray); +} + /*! \fn static QIODevice* QNonContiguousByteDeviceFactory::wrap(QNonContiguousByteDevice* byteDevice) diff --git a/src/corelib/io/qnoncontiguousbytedevice_p.h b/src/corelib/io/qnoncontiguousbytedevice_p.h index 4606ac2686..1e746db80f 100644 --- a/src/corelib/io/qnoncontiguousbytedevice_p.h +++ b/src/corelib/io/qnoncontiguousbytedevice_p.h @@ -90,8 +90,14 @@ class Q_CORE_EXPORT QNonContiguousByteDeviceFactory { public: static QNonContiguousByteDevice* create(QIODevice *device); + static QSharedPointer createShared(QIODevice *device); + static QNonContiguousByteDevice* create(QByteArray *byteArray); + static QSharedPointer createShared(QByteArray *byteArray); + static QNonContiguousByteDevice* create(QSharedPointer ringBuffer); + static QSharedPointer createShared(QSharedPointer ringBuffer); + static QIODevice* wrap(QNonContiguousByteDevice* byteDevice); }; diff --git a/src/network/access/qnetworkaccessbackend.cpp b/src/network/access/qnetworkaccessbackend.cpp index 3c5e1e80a8..f387559cb3 100644 --- a/src/network/access/qnetworkaccessbackend.cpp +++ b/src/network/access/qnetworkaccessbackend.cpp @@ -130,9 +130,9 @@ QStringList QNetworkAccessManagerPrivate::backendSupportedSchemes() const QNonContiguousByteDevice* QNetworkAccessBackend::createUploadByteDevice() { if (reply->outgoingDataBuffer) - uploadByteDevice = QSharedPointer(QNonContiguousByteDeviceFactory::create(reply->outgoingDataBuffer)); + uploadByteDevice = QNonContiguousByteDeviceFactory::createShared(reply->outgoingDataBuffer); else if (reply->outgoingData) { - uploadByteDevice = QSharedPointer(QNonContiguousByteDeviceFactory::create(reply->outgoingData)); + uploadByteDevice = QNonContiguousByteDeviceFactory::createShared(reply->outgoingData); } else { return 0; } diff --git a/src/network/access/qnetworkreplyhttpimpl.cpp b/src/network/access/qnetworkreplyhttpimpl.cpp index 7dc8e6ac73..bf49b97b30 100644 --- a/src/network/access/qnetworkreplyhttpimpl.cpp +++ b/src/network/access/qnetworkreplyhttpimpl.cpp @@ -1844,9 +1844,9 @@ QNonContiguousByteDevice* QNetworkReplyHttpImplPrivate::createUploadByteDevice() Q_Q(QNetworkReplyHttpImpl); if (outgoingDataBuffer) - uploadByteDevice = QSharedPointer(QNonContiguousByteDeviceFactory::create(outgoingDataBuffer)); + uploadByteDevice = QNonContiguousByteDeviceFactory::createShared(outgoingDataBuffer); else if (outgoingData) { - uploadByteDevice = QSharedPointer(QNonContiguousByteDeviceFactory::create(outgoingData)); + uploadByteDevice = QNonContiguousByteDeviceFactory::createShared(outgoingData); } else { return 0; }