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(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();

View File

@ -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;

View File

@ -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)

View File

@ -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); }
};

View File

@ -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<QPropertyBindingPrivate *>(d));
}

View File

@ -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; }

View File

@ -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);

View File

@ -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;

View File

@ -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());
}
/*!

View File

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

View File

@ -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>(args)...), nullptr };
e->next = qExchange(value, e);
e->next = std::exchange(value, e);
}
template<typename ...Args>
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<Node>::value)

View File

@ -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

View File

@ -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))
{
}

View File

@ -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; }

View File

@ -51,7 +51,7 @@ public:
}
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 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);

View File

@ -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();

View File

@ -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); }

View File

@ -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)

View File

@ -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); }

View File

@ -34,7 +34,7 @@ public:
QRegion(const QPolygon &pa, Qt::FillRule fillRule = Qt::OddEvenFill);
QRegion(const QRegion &region);
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();
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) {}
~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)

View File

@ -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);

View File

@ -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) {

View File

@ -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();