diff options
author | David Redondo <qt@david-redondo.de> | 2022-03-22 09:27:07 +0100 |
---|---|---|
committer | David Redondo <qt@david-redondo.de> | 2023-04-11 16:17:42 +0200 |
commit | 42a6cdd6788d0b25cfc5844ed4982a4e9214e605 (patch) | |
tree | e83711efafb3159fbc4205ee8c6a40e72128e908 /src | |
parent | 9130a67c5981abc35be406da593f3a4b7f8e281f (diff) | |
download | qtwayland-42a6cdd6788d0b25cfc5844ed4982a4e9214e605.tar.gz |
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 <davidedmundson@kde.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/client/global/qwaylandclientextension.h | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/src/client/global/qwaylandclientextension.h b/src/client/global/qwaylandclientextension.h index 8fe74e37..c57549c3 100644 --- a/src/client/global/qwaylandclientextension.h +++ b/src/client/global/qwaylandclientextension.h @@ -45,14 +45,31 @@ protected Q_SLOTS: void initialize(); }; -template <typename T> + +template<typename T, auto destruct = nullptr> 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<T *>(this)); + } + }); + } + } + + ~QWaylandClientExtensionTemplate() + { + if constexpr (destruct != nullptr) { + if (isActive()) { + std::invoke(destruct, static_cast<T *>(this)); + } + } } const struct wl_interface *extensionInterface() const override |