[server] Add static SurfaceInterface *SurfaceInterface::get(quint32 id)

This commit is contained in:
Martin Gräßlin 2015-02-09 14:31:20 +01:00
parent 272904313b
commit 1927c3a1ae
4 changed files with 34 additions and 0 deletions

View file

@ -165,6 +165,10 @@ void TestWaylandSurface::testStaticAccessor()
QCOMPARE(KWayland::Client::Surface::all().first(), s1);
QCOMPARE(KWayland::Client::Surface::get(*s1), s1);
QVERIFY(serverSurfaceCreated.wait());
auto serverSurface1 = serverSurfaceCreated.first().first().value<KWayland::Server::SurfaceInterface*>();
QVERIFY(serverSurface1);
QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface1->resource()), serverSurface1);
QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface1->id()), serverSurface1);
QVERIFY(!s1->size().isValid());
QSignalSpy sizeChangedSpy(s1, SIGNAL(sizeChanged(QSize)));
@ -185,6 +189,12 @@ void TestWaylandSurface::testStaticAccessor()
QCOMPARE(KWayland::Client::Surface::get(*s2), s2);
serverSurfaceCreated.clear();
QVERIFY(serverSurfaceCreated.wait());
auto serverSurface2 = serverSurfaceCreated.first().first().value<KWayland::Server::SurfaceInterface*>();
QVERIFY(serverSurface2);
QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface1->resource()), serverSurface1);
QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface1->id()), serverSurface1);
QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface2->resource()), serverSurface2);
QCOMPARE(KWayland::Server::SurfaceInterface::get(serverSurface2->id()), serverSurface2);
// delete s2 again
delete s2;

View file

@ -54,6 +54,20 @@ public:
}
return reinterpret_cast<ResourceDerived*>((*it)->q);
}
template <typename ResourceDerived>
static ResourceDerived *get(quint32 id) {
static_assert(std::is_base_of<Resource, ResourceDerived>::value,
"ResourceDerived must be derived from Resource");
auto it = std::find_if(s_allResources.constBegin(), s_allResources.constEnd(),
[id](Private *p) {
return wl_resource_get_id(p->resource) == id;
}
);
if (it == s_allResources.constEnd()) {
return nullptr;
}
return reinterpret_cast<ResourceDerived*>((*it)->q);
}
protected:
explicit Private(Resource *q, Global *g, wl_resource *parentResource, const wl_interface *interface, const void *implementation);

View file

@ -419,6 +419,11 @@ SurfaceInterface *SurfaceInterface::get(wl_resource *native)
return Private::get<SurfaceInterface>(native);
}
SurfaceInterface *SurfaceInterface::get(quint32 id)
{
return Private::get<SurfaceInterface>(id);
}
QList< QPointer< SubSurfaceInterface > > SurfaceInterface::childSubSurfaces() const
{
Q_D();

View file

@ -69,6 +69,11 @@ public:
QList<QPointer<SubSurfaceInterface>> childSubSurfaces() const;
static SurfaceInterface *get(wl_resource *native);
/**
* @returns The SurfaceInterface with given @p id, if it exists, otherwise @c nullptr.
* @since 5.3
**/
static SurfaceInterface *get(quint32 id);
Q_SIGNALS:
void damaged(const QRegion&);