From f820a71a5023278bc084ba422a82f6f212d6048c Mon Sep 17 00:00:00 2001 From: David Redondo Date: Tue, 22 Mar 2022 09:27:07 +0100 Subject: [PATCH] QWaylandClientExtension: Allow specifying destructor for wayland objects While the template takes care of creating proxies automatically, destroying them is harder since an interface can have 0 to multiple destructors. However in the most common case there is only one or always calling one is sufficient. An additional template parameter is introduced that allows user code to specify a callable taking a pointer to the scanner generated class that should be called when the wayland object is to be be destroyed. This is done when the global is removed or upon destruction of the C++ object itself. The clientextension test is changed how it can be used. Since it works via a non-type template parameter a pointer to a (member) function can be passed or when compiled in c++20 mode a lambda or for example a function object. This new functionality is opt-in and the default behavior is unchanged. The default value is nullptr used as a tag to do not enable the new behavior. Change-Id: I8043f7106fec0cd6f2a79682e5872cfa8da3d11b Reviewed-by: David Edmundson --- .../wayland/global/qwaylandclientextension.h | 23 ++++++++++++++++--- .../clientextension/tst_clientextension.cpp | 13 ++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/plugins/platforms/wayland/global/qwaylandclientextension.h b/src/plugins/platforms/wayland/global/qwaylandclientextension.h index 8fe74e37902..c57549c34a8 100644 --- a/src/plugins/platforms/wayland/global/qwaylandclientextension.h +++ b/src/plugins/platforms/wayland/global/qwaylandclientextension.h @@ -45,14 +45,31 @@ protected Q_SLOTS: void initialize(); }; -template + +template class Q_WAYLANDCLIENT_EXPORT QWaylandClientExtensionTemplate : public QWaylandClientExtension { Q_DECLARE_PRIVATE(QWaylandClientExtensionTemplate) + public: - QWaylandClientExtensionTemplate(const int ver) : - QWaylandClientExtension(ver) + QWaylandClientExtensionTemplate(const int ver) : QWaylandClientExtension(ver) { + if constexpr (destruct != nullptr) { + connect(this, &QWaylandClientExtensionTemplate::activeChanged, this, [this] { + if (!isActive()) { + std::invoke(destruct, static_cast(this)); + } + }); + } + } + + ~QWaylandClientExtensionTemplate() + { + if constexpr (destruct != nullptr) { + if (isActive()) { + std::invoke(destruct, static_cast(this)); + } + } } const struct wl_interface *extensionInterface() const override diff --git a/tests/auto/wayland/clientextension/tst_clientextension.cpp b/tests/auto/wayland/clientextension/tst_clientextension.cpp index 91b02e3b249..c1cd1cb1fa3 100644 --- a/tests/auto/wayland/clientextension/tst_clientextension.cpp +++ b/tests/auto/wayland/clientextension/tst_clientextension.cpp @@ -14,17 +14,12 @@ using namespace MockCompositor; -class TestExtension : public QWaylandClientExtensionTemplate, - public QtWayland::test_interface +class TestExtension + : public QWaylandClientExtensionTemplate, + public QtWayland::test_interface { public: - TestExtension() : QWaylandClientExtensionTemplate(1) { } - ~TestExtension() - { - if (object()) { - release(); - } - } + TestExtension() : QWaylandClientExtensionTemplate(1){}; void initialize() { QWaylandClientExtension::initialize(); } };