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 <thiago.macieira@intel.com>
This commit is contained in:
Fabian Kosmale 2022-09-26 17:38:14 +02:00
parent d06d22fbc0
commit a0dfa8c4d2
25 changed files with 36 additions and 36 deletions

View File

@ -72,7 +72,7 @@ public:
explicit QDebug(QString *string) : stream(new Stream(string)) {} explicit QDebug(QString *string) : stream(new Stream(string)) {}
explicit QDebug(QtMsgType t) : stream(new Stream(t)) {} explicit QDebug(QtMsgType t) : stream(new Stream(t)) {}
QDebug(const QDebug &o) : stream(o.stream) { ++stream->ref; } 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); inline QDebug &operator=(const QDebug &other);
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QDebug) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QDebug)
~QDebug(); ~QDebug();

View File

@ -184,7 +184,7 @@ public:
{ return !operator==(other); } { return !operator==(other); }
QPersistentModelIndex &operator=(const QPersistentModelIndex &other); QPersistentModelIndex &operator=(const QPersistentModelIndex &other);
inline QPersistentModelIndex(QPersistentModelIndex &&other) noexcept 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) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPersistentModelIndex)
void swap(QPersistentModelIndex &other) noexcept { qt_ptr_swap(d, other.d); } void swap(QPersistentModelIndex &other) noexcept { qt_ptr_swap(d, other.d); }
bool operator==(const QModelIndex &other) const; bool operator==(const QModelIndex &other) const;

View File

@ -22,7 +22,7 @@ public:
inline ~QBasicTimer() { if (id) stop(); } inline ~QBasicTimer() { if (id) stop(); }
QBasicTimer(QBasicTimer &&other) noexcept 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) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QBasicTimer)

View File

@ -600,7 +600,7 @@ public:
operator RestrictedBool() const { return d_ptr && isConnected_helper() ? &Connection::d_ptr : nullptr; } operator RestrictedBool() const { return d_ptr && isConnected_helper() ? &Connection::d_ptr : nullptr; }
#endif #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) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(Connection)
void swap(Connection &other) noexcept { qt_ptr_swap(d_ptr, other.d_ptr); } void swap(Connection &other) noexcept { qt_ptr_swap(d_ptr, other.d_ptr); }
}; };

View File

@ -25,7 +25,7 @@ void QPropertyBindingPrivatePtr::reset(QtPrivate::RefCounted *ptr) noexcept
if (ptr != d) { if (ptr != d) {
if (ptr) if (ptr)
ptr->ref++; ptr->ref++;
auto *old = qExchange(d, ptr); auto *old = std::exchange(d, ptr);
if (old && (--old->ref == 0)) if (old && (--old->ref == 0))
QPropertyBindingPrivate::destroyAndFreeMemory(static_cast<QPropertyBindingPrivate *>(d)); QPropertyBindingPrivate::destroyAndFreeMemory(static_cast<QPropertyBindingPrivate *>(d));
} }

View File

@ -82,7 +82,7 @@ public:
reset(o); reset(o);
return *this; 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) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QPropertyBindingPrivatePtr)
operator bool () const noexcept { return d != nullptr; } operator bool () const noexcept { return d != nullptr; }

View File

@ -319,7 +319,7 @@ void QFactoryLoader::setExtraSearchPath(const QString &path)
return; // nothing to do return; // nothing to do
QMutexLocker locker(&qt_factoryloader_global->mutex); QMutexLocker locker(&qt_factoryloader_global->mutex);
QString oldPath = qExchange(d->extraSearchPath, path); QString oldPath = std::exchange(d->extraSearchPath, path);
if (oldPath.isEmpty()) { if (oldPath.isEmpty()) {
// easy case, just update this directory // easy case, just update this directory
d->updateSinglePath(d->extraSearchPath); d->updateSinglePath(d->extraSearchPath);

View File

@ -141,7 +141,7 @@ public:
QCborValue(const QCborValue &other) noexcept; QCborValue(const QCborValue &other) noexcept;
QCborValue(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; QCborValue &operator=(const QCborValue &other) noexcept;

View File

@ -2671,7 +2671,7 @@ QRegularExpressionMatch QRegularExpressionMatchIterator::next()
} }
d.detach(); d.detach();
return qExchange(d->next, d->next.d.constData()->nextMatch()); return std::exchange(d->next, d->next.d.constData()->nextMatch());
} }
/*! /*!

View File

@ -82,7 +82,7 @@ public:
QSemaphore *cancel() noexcept QSemaphore *cancel() noexcept
{ {
return qExchange(m_sem, nullptr); return std::exchange(m_sem, nullptr);
} }
private: private:

View File

@ -172,7 +172,7 @@ struct MultiNode
MultiNode(MultiNode &&other) MultiNode(MultiNode &&other)
: key(other.key), : key(other.key),
value(qExchange(other.value, nullptr)) value(std::exchange(other.value, nullptr))
{ {
} }
@ -203,7 +203,7 @@ struct MultiNode
void insertMulti(Args &&... args) void insertMulti(Args &&... args)
{ {
Chain *e = new Chain{ T(std::forward<Args>(args)...), nullptr }; Chain *e = new Chain{ T(std::forward<Args>(args)...), nullptr };
e->next = qExchange(value, e); e->next = std::exchange(value, e);
} }
template<typename ...Args> template<typename ...Args>
void emplaceValue(Args &&... args) void emplaceValue(Args &&... args)
@ -1412,8 +1412,8 @@ public:
return *this; return *this;
} }
QMultiHash(QMultiHash &&other) noexcept QMultiHash(QMultiHash &&other) noexcept
: d(qExchange(other.d, nullptr)), : d(std::exchange(other.d, nullptr)),
m_size(qExchange(other.m_size, 0)) m_size(std::exchange(other.m_size, 0))
{ {
} }
QMultiHash &operator=(QMultiHash &&other) noexcept(std::is_nothrow_destructible<Node>::value) QMultiHash &operator=(QMultiHash &&other) noexcept(std::is_nothrow_destructible<Node>::value)

View File

@ -120,7 +120,7 @@ public:
{ {
if (d == other) if (d == other)
return; return;
T *oldD = qExchange(d, other); T *oldD = std::exchange(d, other);
Cleanup::cleanup(oldD); Cleanup::cleanup(oldD);
} }
@ -128,7 +128,7 @@ public:
QT_DEPRECATED_VERSION_X_6_1("Use std::unique_ptr instead, and call release().") QT_DEPRECATED_VERSION_X_6_1("Use std::unique_ptr instead, and call release().")
T *take() noexcept T *take() noexcept
{ {
T *oldD = qExchange(d, nullptr); T *oldD = std::exchange(d, nullptr);
return oldD; return oldD;
} }
#endif #endif

View File

@ -28,7 +28,7 @@ public:
QScopeGuard(QScopeGuard &&other) noexcept QScopeGuard(QScopeGuard &&other) noexcept
: m_func(std::move(other.m_func)) : m_func(std::move(other.m_func))
, m_invoke(qExchange(other.m_invoke, false)) , m_invoke(std::exchange(other.m_invoke, false))
{ {
} }

View File

@ -66,7 +66,7 @@ public:
if (ptr != d) { if (ptr != d) {
if (ptr) if (ptr)
ptr->ref.ref(); ptr->ref.ref();
T *old = qExchange(d, ptr); T *old = std::exchange(d, ptr);
if (old && !old->ref.deref()) if (old && !old->ref.deref())
delete old; delete old;
} }
@ -82,7 +82,7 @@ public:
reset(o); reset(o);
return *this; 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) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSharedDataPointer)
operator bool () const noexcept { return d != nullptr; } operator bool () const noexcept { return d != nullptr; }
@ -135,7 +135,7 @@ public:
T *data() const noexcept { return d; } T *data() const noexcept { return d; }
T *get() const noexcept { return d; } T *get() const noexcept { return d; }
const T *constData() 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(); } void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); }
@ -163,7 +163,7 @@ public:
if (ptr != d) { if (ptr != d) {
if (ptr) if (ptr)
ptr->ref.ref(); ptr->ref.ref();
T *old = qExchange(d, ptr); T *old = std::exchange(d, ptr);
if (old && !old->ref.deref()) if (old && !old->ref.deref())
delete old; delete old;
} }
@ -179,7 +179,7 @@ public:
reset(o); reset(o);
return *this; 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) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QExplicitlySharedDataPointer)
operator bool () const noexcept { return d != nullptr; } operator bool () const noexcept { return d != nullptr; }

View File

@ -51,7 +51,7 @@ public:
} }
QExplicitlySharedDataPointerV2(QExplicitlySharedDataPointerV2 &&other) noexcept QExplicitlySharedDataPointerV2(QExplicitlySharedDataPointerV2 &&other) noexcept
: d(qExchange(other.d, nullptr)) : d(std::exchange(other.d, nullptr))
{ {
} }

View File

@ -26,7 +26,7 @@ public:
QIcon(const QPixmap &pixmap); QIcon(const QPixmap &pixmap);
QIcon(const QIcon &other); QIcon(const QIcon &other);
QIcon(QIcon &&other) noexcept 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(const QString &fileName); // file or resource name
explicit QIcon(QIconEngine *engine); explicit QIcon(QIconEngine *engine);

View File

@ -96,7 +96,7 @@ public:
QImage(const QImage &); QImage(const QImage &);
QImage(QImage &&other) noexcept QImage(QImage &&other) noexcept
: QPaintDevice(), d(qExchange(other.d, nullptr)) : QPaintDevice(), d(std::exchange(other.d, nullptr))
{} {}
~QImage(); ~QImage();

View File

@ -51,7 +51,7 @@ public:
QCursor(const QCursor &cursor); QCursor(const QCursor &cursor);
~QCursor(); ~QCursor();
QCursor &operator=(const QCursor &cursor); 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) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QCursor)
void swap(QCursor &other) noexcept { qt_ptr_swap(d, other.d); } void swap(QCursor &other) noexcept { qt_ptr_swap(d, other.d); }

View File

@ -32,7 +32,7 @@ public:
~QPalette(); ~QPalette();
QPalette &operator=(const QPalette &palette); QPalette &operator=(const QPalette &palette);
QPalette(QPalette &&other) noexcept 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) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPalette)

View File

@ -35,7 +35,7 @@ public:
QPen &operator=(const QPen &pen) noexcept; QPen &operator=(const QPen &pen) noexcept;
QPen(QPen &&other) 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) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPen)
void swap(QPen &other) noexcept { qt_ptr_swap(d, other.d); } void swap(QPen &other) noexcept { qt_ptr_swap(d, other.d); }

View File

@ -34,7 +34,7 @@ public:
QRegion(const QPolygon &pa, Qt::FillRule fillRule = Qt::OddEvenFill); QRegion(const QPolygon &pa, Qt::FillRule fillRule = Qt::OddEvenFill);
QRegion(const QRegion &region); QRegion(const QRegion &region);
QRegion(QRegion &&other) noexcept QRegion(QRegion &&other) noexcept
: d(qExchange(other.d, const_cast<QRegionData*>(&shared_empty))) {} : d(std::exchange(other.d, const_cast<QRegionData*>(&shared_empty))) {}
QRegion(const QBitmap &bitmap); QRegion(const QBitmap &bitmap);
~QRegion(); ~QRegion();
QRegion &operator=(const QRegion &); QRegion &operator=(const QRegion &);

View File

@ -277,8 +277,8 @@ public:
explicit Holder(void *p, qt_destroy_func_t d) : ptr(p), destroy_func(d) {} explicit Holder(void *p, qt_destroy_func_t d) : ptr(p), destroy_func(d) {}
~Holder() { if (ptr && destroy_func) destroy_func(ptr); } ~Holder() { if (ptr && destroy_func) destroy_func(ptr); }
Holder(Holder &&other) noexcept Holder(Holder &&other) noexcept
: ptr(qExchange(other.ptr, nullptr)), : ptr(std::exchange(other.ptr, nullptr)),
destroy_func(qExchange(other.destroy_func, nullptr)) destroy_func(std::exchange(other.destroy_func, nullptr))
{ {
} }
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(Holder) QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(Holder)

View File

@ -298,7 +298,7 @@ bool QHttpProtocolHandler::sendRequest()
sendRequest(); //recurse sendRequest(); //recurse
} else { } else {
// no data to send: just send the HTTP headers // 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); QMetaObject::invokeMethod(m_reply, "requestSent", Qt::QueuedConnection);
m_channel->state = QHttpNetworkConnectionChannel::WaitingState; // now wait for response m_channel->state = QHttpNetworkConnectionChannel::WaitingState; // now wait for response
sendRequest(); //recurse 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, // the upload device might have no data to send, but we still have to send the headers,
// do it now. // do it now.
if (!m_header.isEmpty()) if (!m_header.isEmpty())
m_socket->write(qExchange(m_header, {})); m_socket->write(std::exchange(m_header, {}));
if (uploadByteDevice) if (uploadByteDevice)
emit m_reply->dataSendProgress(m_channel->written, m_channel->bytesTotal); emit m_reply->dataSendProgress(m_channel->written, m_channel->bytesTotal);
m_channel->state = QHttpNetworkConnectionChannel::WaitingState; // now wait for response m_channel->state = QHttpNetworkConnectionChannel::WaitingState; // now wait for response
@ -371,7 +371,7 @@ bool QHttpProtocolHandler::sendRequest()
// assemble header and data and send them together // assemble header and data and send them together
const qint64 headerSize = m_header.size(); const qint64 headerSize = m_header.size();
m_header.append(readPointer, currentReadSize); m_header.append(readPointer, currentReadSize);
currentWriteSize = m_socket->write(qExchange(m_header, {})); currentWriteSize = m_socket->write(std::exchange(m_header, {}));
if (currentWriteSize != -1) if (currentWriteSize != -1)
currentWriteSize -= headerSize; currentWriteSize -= headerSize;
QMetaObject::invokeMethod(m_reply, "requestSent", Qt::QueuedConnection); QMetaObject::invokeMethod(m_reply, "requestSent", Qt::QueuedConnection);

View File

@ -281,7 +281,7 @@ void QNetworkReplyImplPrivate::handleNotifications()
if (notificationHandlingPaused) if (notificationHandlingPaused)
return; return;
for (InternalNotifications notification : qExchange(pendingNotifications, {})) { for (InternalNotifications notification : std::exchange(pendingNotifications, {})) {
if (state != Working) if (state != Working)
return; return;
switch (notification) { switch (notification) {

View File

@ -26,7 +26,7 @@ public:
explicit QHostInfo(int lookupId = -1); explicit QHostInfo(int lookupId = -1);
QHostInfo(const QHostInfo &d); 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=(const QHostInfo &d);
QHostInfo &operator=(QHostInfo &&other) noexcept { swap(other); return *this; } QHostInfo &operator=(QHostInfo &&other) noexcept { swap(other); return *this; }
~QHostInfo(); ~QHostInfo();