diff options
author | Ivan Solovev <[email protected]> | 2025-06-16 17:55:12 +0200 |
---|---|---|
committer | Ivan Solovev <[email protected]> | 2025-06-18 20:06:05 +0000 |
commit | 68210484d95b0f944092c1380aafa7325c9ca891 (patch) | |
tree | e5f589843f1619b0a989057dd2bc1dc6e325fa47 | |
parent | f036bd0edadf0f59757f4a8fa695706273eb72d2 (diff) |
QDBusMessage: add move-constructor
Since the class was not desigend to support a nullptr d_ptr, this
change requires modifications in the destructor and copy-ctor.
The change in destructor is straightforward - simply add a check
that d_ptr is not null.
The copy-constructor was using a qAtomicAssign() helper, which
was relying on the fact that the passed pointers are not null, so
we cannot use it anymore. Use copy-and-swap instead.
The other methods do not require any changes, because the
moved-from object can only be destroyed or assigned-to.
[ChangeLog][QtDBus][QDBusMessage] Added move constructor.
Change-Id: Ic8a0d913b9cf2f881369f7ad4f3a88c1f3fb345f
Reviewed-by: Marc Mutz <[email protected]>
-rw-r--r-- | src/dbus/qdbusmessage.cpp | 14 | ||||
-rw-r--r-- | src/dbus/qdbusmessage.h | 1 | ||||
-rw-r--r-- | tests/auto/dbus/qdbusmessage/tst_qdbusmessage.cpp | 26 |
3 files changed, 39 insertions, 2 deletions
diff --git a/src/dbus/qdbusmessage.cpp b/src/dbus/qdbusmessage.cpp index 24a80528f65..b10266ac8b7 100644 --- a/src/dbus/qdbusmessage.cpp +++ b/src/dbus/qdbusmessage.cpp @@ -520,6 +520,15 @@ QDBusMessage::QDBusMessage() } /*! + \fn QDBusMessage::QDBusMessage(QDBusMessage &&other) + \since 6.11 + + Moves \a other into this object. + + \include qdbusmessage.cpp partially-formed +*/ + +/*! Constructs a copy of the object given by \a other. Note: QDBusMessage objects are shared. Modifications made to the @@ -537,7 +546,7 @@ QDBusMessage::QDBusMessage(const QDBusMessage &other) */ QDBusMessage::~QDBusMessage() { - if (!d_ptr->ref.deref()) + if (d_ptr && !d_ptr->ref.deref()) delete d_ptr; } @@ -562,7 +571,8 @@ QDBusMessage::~QDBusMessage() */ QDBusMessage &QDBusMessage::operator=(const QDBusMessage &other) { - qAtomicAssign(d_ptr, other.d_ptr); + QDBusMessage copy(other); + swap(copy); return *this; } diff --git a/src/dbus/qdbusmessage.h b/src/dbus/qdbusmessage.h index f6db3bc21e6..608b9779d22 100644 --- a/src/dbus/qdbusmessage.h +++ b/src/dbus/qdbusmessage.h @@ -31,6 +31,7 @@ public: QDBusMessage(); QDBusMessage(const QDBusMessage &other); + QDBusMessage(QDBusMessage &&other) noexcept : d_ptr(std::exchange(other.d_ptr, nullptr)) {} QDBusMessage &operator=(QDBusMessage &&other) noexcept { swap(other); return *this; } QDBusMessage &operator=(const QDBusMessage &other); ~QDBusMessage(); diff --git a/tests/auto/dbus/qdbusmessage/tst_qdbusmessage.cpp b/tests/auto/dbus/qdbusmessage/tst_qdbusmessage.cpp index 475dbcec0bb..bc2e28e7d24 100644 --- a/tests/auto/dbus/qdbusmessage/tst_qdbusmessage.cpp +++ b/tests/auto/dbus/qdbusmessage/tst_qdbusmessage.cpp @@ -23,6 +23,7 @@ class tst_QDBusMessage : public QObject Q_OBJECT private Q_SLOTS: void moveAssign(); + void moveConstruct(); }; void tst_QDBusMessage::moveAssign() @@ -49,6 +50,31 @@ void tst_QDBusMessage::moveAssign() } } +void tst_QDBusMessage::moveConstruct() +{ + QDBusMessage referenceMessage = + QDBusMessage::createMethodCall("org.kde.selftest"_L1, "/org/kde/selftest"_L1, + "org.kde.selftest"_L1, "Ping"_L1); + referenceMessage << "ping"_L1; + + // moved-from object can be destroyed... + { + QDBusMessage copy = referenceMessage; + QDBusMessage other = std::move(copy); + COMPARE_MESSAGES(other, referenceMessage); + } + // ... or assigned-to + { + QDBusMessage copy = referenceMessage; + QDBusMessage other = std::move(copy); + COMPARE_MESSAGES(other, referenceMessage); + other << "other_arg"_L1; // also affects referenceMessage + copy = other; + COMPARE_MESSAGES(copy, other); + COMPARE_MESSAGES(copy, referenceMessage); + } +} + #undef COMPARE_MESSAGES QTEST_MAIN(tst_QDBusMessage) |