Commit 9c944c81 authored by Vlad Zahorodnii's avatar Vlad Zahorodnii

Introduce resource_cast<> helper function

Getting the object associated with the particular wl_resource is not
difficult, but it involves a pretty reasonable amount of boilerplate
code.

This change, introduces a helper function with an intend to reduce the
amount of boilerplate code.

It can be used as resource_cast<const ObjectPrivate *>(resource) or
just simply resource_cast<ObjectPrivate *>(resource).
parent 2e3de7f2
......@@ -6,7 +6,7 @@
#include "datacontrolsource_v1_interface.h"
#include "datacontroldevicemanager_v1_interface.h"
#include "clientconnection.h"
#include "resource_p.h"
#include "utils.h"
// Qt
#include <QStringList>
// Wayland
......@@ -86,8 +86,10 @@ wl_client *DataControlSourceV1Interface::client() const
DataControlSourceV1Interface *DataControlSourceV1Interface::get(wl_resource *native)
{
auto priv = static_cast<DataControlSourceV1InterfacePrivate*>(QtWaylandServer::zwlr_data_control_source_v1::Resource::fromResource(native)->object());
return priv->q;
if (auto sourcePrivate = resource_cast<DataControlSourceV1InterfacePrivate *>(native)) {
return sourcePrivate->q;
}
return nullptr;
}
}
......@@ -7,7 +7,7 @@
#include "datasource_interface.h"
#include "datadevicemanager_interface.h"
#include "clientconnection.h"
#include "resource_p.h"
#include "utils.h"
// Qt
#include <QStringList>
// Wayland
......@@ -129,11 +129,10 @@ QStringList DataSourceInterface::mimeTypes() const
DataSourceInterface *DataSourceInterface::get(wl_resource *native)
{
if (!native) {
return nullptr;
if (auto sourcePrivate = resource_cast<DataSourceInterfacePrivate *>(native)) {
return sourcePrivate->q;
}
auto priv = static_cast<DataSourceInterfacePrivate*>(QtWaylandServer::wl_data_source::Resource::fromResource(native)->object());
return priv->q;
return nullptr;
}
DataDeviceManagerInterface::DnDActions DataSourceInterface::supportedDragAndDropActions() const
......
......@@ -6,6 +6,7 @@
#include "primaryselectionsource_v1_interface.h"
#include "primaryselectiondevicemanager_v1_interface.h"
#include "clientconnection.h"
#include "utils.h"
// Qt
#include <QStringList>
// Wayland
......@@ -84,8 +85,10 @@ wl_client *PrimarySelectionSourceV1Interface::client() const
PrimarySelectionSourceV1Interface *PrimarySelectionSourceV1Interface::get(wl_resource *native)
{
auto priv = static_cast<PrimarySelectionSourceV1InterfacePrivate*>(QtWaylandServer::zwp_primary_selection_source_v1::Resource::fromResource(native)->object());
return priv->q;
if (auto sourcePrivate = resource_cast<PrimarySelectionSourceV1InterfacePrivate *>(native)) {
return sourcePrivate->q;
}
return nullptr;
}
}
......@@ -6,6 +6,7 @@
*/
#include "region_interface.h"
#include "compositor_interface.h"
#include "utils.h"
#include "qwayland-server-wayland.h"
......@@ -72,8 +73,8 @@ QRegion RegionInterface::region() const
RegionInterface *RegionInterface::get(wl_resource *native)
{
if (auto region = RegionInterfacePrivate::Resource::fromResource(native)) {
return static_cast<RegionInterfacePrivate *>(region->object())->q;
if (auto regionPrivate = resource_cast<RegionInterfacePrivate *>(native)) {
return regionPrivate->q;
}
return nullptr;
}
......
......@@ -16,6 +16,7 @@
#include "subcompositor_interface.h"
#include "subsurface_interface_p.h"
#include "surfacerole_p.h"
#include "utils.h"
// std
#include <algorithm>
......@@ -735,8 +736,8 @@ QPoint SurfaceInterface::offset() const
SurfaceInterface *SurfaceInterface::get(wl_resource *native)
{
if (auto surface = SurfaceInterfacePrivate::Resource::fromResource(native)) {
return static_cast<SurfaceInterfacePrivate *>(surface->object())->q;
if (auto surfacePrivate = resource_cast<SurfaceInterfacePrivate *>(native)) {
return surfacePrivate->q;
}
return nullptr;
}
......
......@@ -11,6 +11,9 @@
#include <QRegion>
#include <limits>
#include <type_traits>
struct wl_resource;
namespace KWaylandServer
{
......@@ -26,4 +29,14 @@ inline QRegion KWAYLANDSERVER_EXPORT infiniteRegion()
std::numeric_limits<int>::max());
}
template <typename T>
T resource_cast(::wl_resource *resource)
{
using ObjectType = std::remove_pointer_t<std::remove_cv_t<T>>;
if (auto resourceContainer = ObjectType::Resource::fromResource(resource)) {
return static_cast<T>(resourceContainer->object());
}
return T();
}
} // namespace KWaylandServer
......@@ -10,6 +10,7 @@
#include "display.h"
#include "output_interface.h"
#include "seat_interface.h"
#include "utils.h"
#include <QTimer>
......@@ -302,8 +303,9 @@ QRect XdgSurfaceInterface::windowGeometry() const
XdgSurfaceInterface *XdgSurfaceInterface::get(::wl_resource *resource)
{
if (auto surface = QtWaylandServer::xdg_surface::Resource::fromResource(resource))
return static_cast<XdgSurfaceInterfacePrivate *>(surface->object())->q;
if (auto surfacePrivate = resource_cast<XdgSurfaceInterfacePrivate *>(resource)) {
return surfacePrivate->q;
}
return nullptr;
}
......@@ -513,9 +515,7 @@ XdgToplevelInterfacePrivate *XdgToplevelInterfacePrivate::get(XdgToplevelInterfa
XdgToplevelInterfacePrivate *XdgToplevelInterfacePrivate::get(wl_resource *resource)
{
if (auto toplevel = QtWaylandServer::xdg_toplevel::Resource::fromResource(resource))
return static_cast<XdgToplevelInterfacePrivate *>(toplevel->object());
return nullptr;
return resource_cast<XdgToplevelInterfacePrivate *>(resource);
}
XdgToplevelInterface::XdgToplevelInterface(XdgSurfaceInterface *surface, ::wl_resource *resource)
......@@ -628,8 +628,8 @@ void XdgToplevelInterface::sendClose()
XdgToplevelInterface *XdgToplevelInterface::get(::wl_resource *resource)
{
if (auto toplevel = QtWaylandServer::xdg_toplevel::Resource::fromResource(resource)) {
return static_cast<XdgToplevelInterfacePrivate *>(toplevel->object())->q;
if (auto toplevelPrivate = resource_cast<XdgToplevelInterfacePrivate *>(resource)) {
return toplevelPrivate->q;
}
return nullptr;
}
......@@ -763,8 +763,9 @@ void XdgPopupInterface::sendPopupDone()
XdgPopupInterface *XdgPopupInterface::get(::wl_resource *resource)
{
if (auto popup = QtWaylandServer::xdg_popup::Resource::fromResource(resource))
return static_cast<XdgPopupInterfacePrivate *>(popup->object())->q;
if (auto popupPrivate = resource_cast<XdgPopupInterfacePrivate *>(resource)) {
return popupPrivate->q;
}
return nullptr;
}
......@@ -776,9 +777,7 @@ XdgPositionerPrivate::XdgPositionerPrivate(::wl_resource *resource)
XdgPositionerPrivate *XdgPositionerPrivate::get(wl_resource *resource)
{
if (auto positioner = QtWaylandServer::xdg_positioner::Resource::fromResource(resource))
return static_cast<XdgPositionerPrivate *>(positioner->object());
return nullptr;
return resource_cast<XdgPositionerPrivate *>(resource);
}
void XdgPositionerPrivate::xdg_positioner_destroy_resource(Resource *resource)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment