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>
This commit is contained in:
parent
5e864b671e
commit
f820a71a50
@ -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
|
||||
|
@ -14,17 +14,12 @@
|
||||
|
||||
using namespace MockCompositor;
|
||||
|
||||
class TestExtension : public QWaylandClientExtensionTemplate<TestExtension>,
|
||||
class TestExtension
|
||||
: public QWaylandClientExtensionTemplate<TestExtension, &QtWayland::test_interface::release>,
|
||||
public QtWayland::test_interface
|
||||
{
|
||||
public:
|
||||
TestExtension() : QWaylandClientExtensionTemplate<TestExtension>(1) { }
|
||||
~TestExtension()
|
||||
{
|
||||
if (object()) {
|
||||
release();
|
||||
}
|
||||
}
|
||||
TestExtension() : QWaylandClientExtensionTemplate(1){};
|
||||
void initialize() { QWaylandClientExtension::initialize(); }
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user