From a0dfa8c4d29a005009d04dcb52452efb9d74e26e Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Mon, 26 Sep 2022 17:38:14 +0200 Subject: Replace qExchange calls with std::exchange qExchange is one of the few remaining functionalities that have not been moved out of qglobal. Given that std::exchange exists in the standard, we can simply move to it everywhere... ...if it weren't for the fact that std::exchange is only constexpr in C++20, and only has its noexceptness specified in (most likely) C++23. Still, we want to move to the existing std functionality where possible, to allow the removal of qglobal includes in lieu of something more fine-grained in the future. So leave any constexpr calls[1] alone for now (and observe that none of our current usages cares about the conditional noexceptness), but replace everything else. [1] QScopedValueRollback' ctor and QExplicitlySharedDataPointerV2::take Task-number: QTBUG-99313 Change-Id: I599cb9846cf319c7ffd3457130938347a75aad25 Reviewed-by: Thiago Macieira --- src/corelib/io/qdebug.h | 2 +- src/corelib/itemmodels/qabstractitemmodel.h | 2 +- src/corelib/kernel/qbasictimer.h | 2 +- src/corelib/kernel/qobjectdefs.h | 2 +- src/corelib/kernel/qproperty.cpp | 2 +- src/corelib/kernel/qpropertyprivate.h | 2 +- src/corelib/plugin/qfactoryloader.cpp | 2 +- src/corelib/serialization/qcborvalue.h | 2 +- src/corelib/text/qregularexpression.cpp | 2 +- src/corelib/thread/qsemaphore.h | 2 +- src/corelib/tools/qhash.h | 8 ++++---- src/corelib/tools/qscopedpointer.h | 4 ++-- src/corelib/tools/qscopeguard.h | 2 +- src/corelib/tools/qshareddata.h | 10 +++++----- src/corelib/tools/qshareddata_impl.h | 2 +- src/gui/image/qicon.h | 2 +- src/gui/image/qimage.h | 2 +- src/gui/kernel/qcursor.h | 2 +- src/gui/kernel/qpalette.h | 2 +- src/gui/painting/qpen.h | 2 +- src/gui/painting/qregion.h | 2 +- src/gui/text/qfontengine_p.h | 4 ++-- src/network/access/qhttpprotocolhandler.cpp | 6 +++--- src/network/access/qnetworkreplyimpl.cpp | 2 +- src/network/kernel/qhostinfo.h | 2 +- 25 files changed, 36 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index b7cdd67568..f8fd877010 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -72,7 +72,7 @@ public: explicit QDebug(QString *string) : stream(new Stream(string)) {} explicit QDebug(QtMsgType t) : stream(new Stream(t)) {} QDebug(const QDebug &o) : stream(o.stream) { ++stream->ref; } - QDebug(QDebug &&other) noexcept : stream{qExchange(other.stream, nullptr)} {} + QDebug(QDebug &&other) noexcept : stream{std::exchange(other.stream, nullptr)} {} inline QDebug &operator=(const QDebug &other); QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QDebug) ~QDebug(); diff --git a/src/corelib/itemmodels/qabstractitemmodel.h b/src/corelib/itemmodels/qabstractitemmodel.h index dbeb237797..e56412241a 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.h +++ b/src/corelib/itemmodels/qabstractitemmodel.h @@ -184,7 +184,7 @@ public: { return !operator==(other); } QPersistentModelIndex &operator=(const QPersistentModelIndex &other); inline QPersistentModelIndex(QPersistentModelIndex &&other) noexcept - : d(qExchange(other.d, nullptr)) {} + : d(std::exchange(other.d, nullptr)) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPersistentModelIndex) void swap(QPersistentModelIndex &other) noexcept { qt_ptr_swap(d, other.d); } bool operator==(const QModelIndex &other) const; diff --git a/src/corelib/kernel/qbasictimer.h b/src/corelib/kernel/qbasictimer.h index 13d48822f5..b39a78d293 100644 --- a/src/corelib/kernel/qbasictimer.h +++ b/src/corelib/kernel/qbasictimer.h @@ -22,7 +22,7 @@ public: inline ~QBasicTimer() { if (id) stop(); } QBasicTimer(QBasicTimer &&other) noexcept - : id{qExchange(other.id, 0)} + : id{std::exchange(other.id, 0)} {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QBasicTimer) diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h index 458860209b..845e44882d 100644 --- a/src/corelib/kernel/qobjectdefs.h +++ b/src/corelib/kernel/qobjectdefs.h @@ -600,7 +600,7 @@ public: operator RestrictedBool() const { return d_ptr && isConnected_helper() ? &Connection::d_ptr : nullptr; } #endif - Connection(Connection &&other) noexcept : d_ptr(qExchange(other.d_ptr, nullptr)) {} + Connection(Connection &&other) noexcept : d_ptr(std::exchange(other.d_ptr, nullptr)) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(Connection) void swap(Connection &other) noexcept { qt_ptr_swap(d_ptr, other.d_ptr); } }; diff --git a/src/corelib/kernel/qproperty.cpp b/src/corelib/kernel/qproperty.cpp index a6435830f9..37c5b8d013 100644 --- a/src/corelib/kernel/qproperty.cpp +++ b/src/corelib/kernel/qproperty.cpp @@ -25,7 +25,7 @@ void QPropertyBindingPrivatePtr::reset(QtPrivate::RefCounted *ptr) noexcept if (ptr != d) { if (ptr) ptr->ref++; - auto *old = qExchange(d, ptr); + auto *old = std::exchange(d, ptr); if (old && (--old->ref == 0)) QPropertyBindingPrivate::destroyAndFreeMemory(static_cast(d)); } diff --git a/src/corelib/kernel/qpropertyprivate.h b/src/corelib/kernel/qpropertyprivate.h index 1356a3702e..966821fa67 100644 --- a/src/corelib/kernel/qpropertyprivate.h +++ b/src/corelib/kernel/qpropertyprivate.h @@ -82,7 +82,7 @@ public: reset(o); return *this; } - QPropertyBindingPrivatePtr(QPropertyBindingPrivatePtr &&o) noexcept : d(qExchange(o.d, nullptr)) {} + QPropertyBindingPrivatePtr(QPropertyBindingPrivatePtr &&o) noexcept : d(std::exchange(o.d, nullptr)) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QPropertyBindingPrivatePtr) operator bool () const noexcept { return d != nullptr; } diff --git a/src/corelib/plugin/qfactoryloader.cpp b/src/corelib/plugin/qfactoryloader.cpp index 4ee87d0dab..06015127d6 100644 --- a/src/corelib/plugin/qfactoryloader.cpp +++ b/src/corelib/plugin/qfactoryloader.cpp @@ -319,7 +319,7 @@ void QFactoryLoader::setExtraSearchPath(const QString &path) return; // nothing to do QMutexLocker locker(&qt_factoryloader_global->mutex); - QString oldPath = qExchange(d->extraSearchPath, path); + QString oldPath = std::exchange(d->extraSearchPath, path); if (oldPath.isEmpty()) { // easy case, just update this directory d->updateSinglePath(d->extraSearchPath); diff --git a/src/corelib/serialization/qcborvalue.h b/src/corelib/serialization/qcborvalue.h index ff4a0fd104..b95d25d6fd 100644 --- a/src/corelib/serialization/qcborvalue.h +++ b/src/corelib/serialization/qcborvalue.h @@ -141,7 +141,7 @@ public: QCborValue(const QCborValue &other) noexcept; QCborValue(QCborValue &&other) noexcept - : n(other.n), container(qExchange(other.container, nullptr)), t(qExchange(other.t, Undefined)) + : n(other.n), container(std::exchange(other.container, nullptr)), t(std::exchange(other.t, Undefined)) { } QCborValue &operator=(const QCborValue &other) noexcept; diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp index 866f3976af..0aa9087727 100644 --- a/src/corelib/text/qregularexpression.cpp +++ b/src/corelib/text/qregularexpression.cpp @@ -2671,7 +2671,7 @@ QRegularExpressionMatch QRegularExpressionMatchIterator::next() } d.detach(); - return qExchange(d->next, d->next.d.constData()->nextMatch()); + return std::exchange(d->next, d->next.d.constData()->nextMatch()); } /*! diff --git a/src/corelib/thread/qsemaphore.h b/src/corelib/thread/qsemaphore.h index 3df0765902..8823468dbc 100644 --- a/src/corelib/thread/qsemaphore.h +++ b/src/corelib/thread/qsemaphore.h @@ -82,7 +82,7 @@ public: QSemaphore *cancel() noexcept { - return qExchange(m_sem, nullptr); + return std::exchange(m_sem, nullptr); } private: diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 5075459531..9f06b5d36e 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -172,7 +172,7 @@ struct MultiNode MultiNode(MultiNode &&other) : key(other.key), - value(qExchange(other.value, nullptr)) + value(std::exchange(other.value, nullptr)) { } @@ -203,7 +203,7 @@ struct MultiNode void insertMulti(Args &&... args) { Chain *e = new Chain{ T(std::forward(args)...), nullptr }; - e->next = qExchange(value, e); + e->next = std::exchange(value, e); } template void emplaceValue(Args &&... args) @@ -1412,8 +1412,8 @@ public: return *this; } QMultiHash(QMultiHash &&other) noexcept - : d(qExchange(other.d, nullptr)), - m_size(qExchange(other.m_size, 0)) + : d(std::exchange(other.d, nullptr)), + m_size(std::exchange(other.m_size, 0)) { } QMultiHash &operator=(QMultiHash &&other) noexcept(std::is_nothrow_destructible::value) diff --git a/src/corelib/tools/qscopedpointer.h b/src/corelib/tools/qscopedpointer.h index 1ad7acb0f4..1637bb40a5 100644 --- a/src/corelib/tools/qscopedpointer.h +++ b/src/corelib/tools/qscopedpointer.h @@ -120,7 +120,7 @@ public: { if (d == other) return; - T *oldD = qExchange(d, other); + T *oldD = std::exchange(d, other); Cleanup::cleanup(oldD); } @@ -128,7 +128,7 @@ public: QT_DEPRECATED_VERSION_X_6_1("Use std::unique_ptr instead, and call release().") T *take() noexcept { - T *oldD = qExchange(d, nullptr); + T *oldD = std::exchange(d, nullptr); return oldD; } #endif diff --git a/src/corelib/tools/qscopeguard.h b/src/corelib/tools/qscopeguard.h index 2639b41222..5bd202ce33 100644 --- a/src/corelib/tools/qscopeguard.h +++ b/src/corelib/tools/qscopeguard.h @@ -28,7 +28,7 @@ public: QScopeGuard(QScopeGuard &&other) noexcept : m_func(std::move(other.m_func)) - , m_invoke(qExchange(other.m_invoke, false)) + , m_invoke(std::exchange(other.m_invoke, false)) { } diff --git a/src/corelib/tools/qshareddata.h b/src/corelib/tools/qshareddata.h index be848e2ef8..813e9d6c7a 100644 --- a/src/corelib/tools/qshareddata.h +++ b/src/corelib/tools/qshareddata.h @@ -66,7 +66,7 @@ public: if (ptr != d) { if (ptr) ptr->ref.ref(); - T *old = qExchange(d, ptr); + T *old = std::exchange(d, ptr); if (old && !old->ref.deref()) delete old; } @@ -82,7 +82,7 @@ public: reset(o); return *this; } - QSharedDataPointer(QSharedDataPointer &&o) noexcept : d(qExchange(o.d, nullptr)) {} + QSharedDataPointer(QSharedDataPointer &&o) noexcept : d(std::exchange(o.d, nullptr)) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSharedDataPointer) operator bool () const noexcept { return d != nullptr; } @@ -135,7 +135,7 @@ public: T *data() const noexcept { return d; } T *get() const noexcept { return d; } const T *constData() const noexcept { return d; } - T *take() noexcept { return qExchange(d, nullptr); } + T *take() noexcept { return std::exchange(d, nullptr); } void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); } @@ -163,7 +163,7 @@ public: if (ptr != d) { if (ptr) ptr->ref.ref(); - T *old = qExchange(d, ptr); + T *old = std::exchange(d, ptr); if (old && !old->ref.deref()) delete old; } @@ -179,7 +179,7 @@ public: reset(o); return *this; } - QExplicitlySharedDataPointer(QExplicitlySharedDataPointer &&o) noexcept : d(qExchange(o.d, nullptr)) {} + QExplicitlySharedDataPointer(QExplicitlySharedDataPointer &&o) noexcept : d(std::exchange(o.d, nullptr)) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QExplicitlySharedDataPointer) operator bool () const noexcept { return d != nullptr; } diff --git a/src/corelib/tools/qshareddata_impl.h b/src/corelib/tools/qshareddata_impl.h index 61a9d1d105..a0216397cc 100644 --- a/src/corelib/tools/qshareddata_impl.h +++ b/src/corelib/tools/qshareddata_impl.h @@ -51,7 +51,7 @@ public: } QExplicitlySharedDataPointerV2(QExplicitlySharedDataPointerV2 &&other) noexcept - : d(qExchange(other.d, nullptr)) + : d(std::exchange(other.d, nullptr)) { } diff --git a/src/gui/image/qicon.h b/src/gui/image/qicon.h index 63816220dd..22f63b1ecb 100644 --- a/src/gui/image/qicon.h +++ b/src/gui/image/qicon.h @@ -26,7 +26,7 @@ public: QIcon(const QPixmap &pixmap); QIcon(const QIcon &other); QIcon(QIcon &&other) noexcept - : d(qExchange(other.d, nullptr)) + : d(std::exchange(other.d, nullptr)) {} explicit QIcon(const QString &fileName); // file or resource name explicit QIcon(QIconEngine *engine); diff --git a/src/gui/image/qimage.h b/src/gui/image/qimage.h index aa58af4a76..86c49ac28a 100644 --- a/src/gui/image/qimage.h +++ b/src/gui/image/qimage.h @@ -96,7 +96,7 @@ public: QImage(const QImage &); QImage(QImage &&other) noexcept - : QPaintDevice(), d(qExchange(other.d, nullptr)) + : QPaintDevice(), d(std::exchange(other.d, nullptr)) {} ~QImage(); diff --git a/src/gui/kernel/qcursor.h b/src/gui/kernel/qcursor.h index 1d94d65ce4..3aeef52094 100644 --- a/src/gui/kernel/qcursor.h +++ b/src/gui/kernel/qcursor.h @@ -51,7 +51,7 @@ public: QCursor(const QCursor &cursor); ~QCursor(); QCursor &operator=(const QCursor &cursor); - QCursor(QCursor &&other) noexcept : d(qExchange(other.d, nullptr)) {} + QCursor(QCursor &&other) noexcept : d(std::exchange(other.d, nullptr)) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QCursor) void swap(QCursor &other) noexcept { qt_ptr_swap(d, other.d); } diff --git a/src/gui/kernel/qpalette.h b/src/gui/kernel/qpalette.h index a9b1b59eb5..0a6dc0b381 100644 --- a/src/gui/kernel/qpalette.h +++ b/src/gui/kernel/qpalette.h @@ -32,7 +32,7 @@ public: ~QPalette(); QPalette &operator=(const QPalette &palette); QPalette(QPalette &&other) noexcept - : d(qExchange(other.d, nullptr)), currentGroup(other.currentGroup) + : d(std::exchange(other.d, nullptr)), currentGroup(other.currentGroup) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPalette) diff --git a/src/gui/painting/qpen.h b/src/gui/painting/qpen.h index d8f13df388..2f38098496 100644 --- a/src/gui/painting/qpen.h +++ b/src/gui/painting/qpen.h @@ -35,7 +35,7 @@ public: QPen &operator=(const QPen &pen) noexcept; QPen(QPen &&other) noexcept - : d(qExchange(other.d, nullptr)) {} + : d(std::exchange(other.d, nullptr)) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPen) void swap(QPen &other) noexcept { qt_ptr_swap(d, other.d); } diff --git a/src/gui/painting/qregion.h b/src/gui/painting/qregion.h index c946287411..b0051b6067 100644 --- a/src/gui/painting/qregion.h +++ b/src/gui/painting/qregion.h @@ -34,7 +34,7 @@ public: QRegion(const QPolygon &pa, Qt::FillRule fillRule = Qt::OddEvenFill); QRegion(const QRegion ®ion); QRegion(QRegion &&other) noexcept - : d(qExchange(other.d, const_cast(&shared_empty))) {} + : d(std::exchange(other.d, const_cast(&shared_empty))) {} QRegion(const QBitmap &bitmap); ~QRegion(); QRegion &operator=(const QRegion &); diff --git a/src/gui/text/qfontengine_p.h b/src/gui/text/qfontengine_p.h index c614e004f0..dbad0d95f9 100644 --- a/src/gui/text/qfontengine_p.h +++ b/src/gui/text/qfontengine_p.h @@ -277,8 +277,8 @@ public: explicit Holder(void *p, qt_destroy_func_t d) : ptr(p), destroy_func(d) {} ~Holder() { if (ptr && destroy_func) destroy_func(ptr); } Holder(Holder &&other) noexcept - : ptr(qExchange(other.ptr, nullptr)), - destroy_func(qExchange(other.destroy_func, nullptr)) + : ptr(std::exchange(other.ptr, nullptr)), + destroy_func(std::exchange(other.destroy_func, nullptr)) { } QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(Holder) diff --git a/src/network/access/qhttpprotocolhandler.cpp b/src/network/access/qhttpprotocolhandler.cpp index 36102909df..b2576a85e6 100644 --- a/src/network/access/qhttpprotocolhandler.cpp +++ b/src/network/access/qhttpprotocolhandler.cpp @@ -298,7 +298,7 @@ bool QHttpProtocolHandler::sendRequest() sendRequest(); //recurse } else { // no data to send: just send the HTTP headers - m_socket->write(qExchange(m_header, {})); + m_socket->write(std::exchange(m_header, {})); QMetaObject::invokeMethod(m_reply, "requestSent", Qt::QueuedConnection); m_channel->state = QHttpNetworkConnectionChannel::WaitingState; // now wait for response sendRequest(); //recurse @@ -314,7 +314,7 @@ bool QHttpProtocolHandler::sendRequest() // the upload device might have no data to send, but we still have to send the headers, // do it now. if (!m_header.isEmpty()) - m_socket->write(qExchange(m_header, {})); + m_socket->write(std::exchange(m_header, {})); if (uploadByteDevice) emit m_reply->dataSendProgress(m_channel->written, m_channel->bytesTotal); m_channel->state = QHttpNetworkConnectionChannel::WaitingState; // now wait for response @@ -371,7 +371,7 @@ bool QHttpProtocolHandler::sendRequest() // assemble header and data and send them together const qint64 headerSize = m_header.size(); m_header.append(readPointer, currentReadSize); - currentWriteSize = m_socket->write(qExchange(m_header, {})); + currentWriteSize = m_socket->write(std::exchange(m_header, {})); if (currentWriteSize != -1) currentWriteSize -= headerSize; QMetaObject::invokeMethod(m_reply, "requestSent", Qt::QueuedConnection); diff --git a/src/network/access/qnetworkreplyimpl.cpp b/src/network/access/qnetworkreplyimpl.cpp index a1fedb4cd3..2c713e69fd 100644 --- a/src/network/access/qnetworkreplyimpl.cpp +++ b/src/network/access/qnetworkreplyimpl.cpp @@ -281,7 +281,7 @@ void QNetworkReplyImplPrivate::handleNotifications() if (notificationHandlingPaused) return; - for (InternalNotifications notification : qExchange(pendingNotifications, {})) { + for (InternalNotifications notification : std::exchange(pendingNotifications, {})) { if (state != Working) return; switch (notification) { diff --git a/src/network/kernel/qhostinfo.h b/src/network/kernel/qhostinfo.h index 6ee8aff081..eac23ac296 100644 --- a/src/network/kernel/qhostinfo.h +++ b/src/network/kernel/qhostinfo.h @@ -26,7 +26,7 @@ public: explicit QHostInfo(int lookupId = -1); QHostInfo(const QHostInfo &d); - QHostInfo(QHostInfo &&other) noexcept : d_ptr(qExchange(other.d_ptr, nullptr)) {} + QHostInfo(QHostInfo &&other) noexcept : d_ptr(std::exchange(other.d_ptr, nullptr)) {} QHostInfo &operator=(const QHostInfo &d); QHostInfo &operator=(QHostInfo &&other) noexcept { swap(other); return *this; } ~QHostInfo(); -- cgit v1.2.3