From 099fcc3b3c72b117e93d4da39ee6f70c6c5a2bc8 Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Mon, 2 Apr 2018 16:15:25 -0700 Subject: Use a host interface for CustomLayer instead of function pointers (#11553) Use a host interface for CustomLayer instead of function pointers Co-authored-by: Julian Rex --- platform/qt/src/qmapboxgl.cpp | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'platform/qt/src/qmapboxgl.cpp') diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 414b65255c..2e736c0aa2 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -1343,20 +1343,43 @@ void QMapboxGL::removeSource(const QString& id) this API and is not officially supported. Use at your own risk. */ void QMapboxGL::addCustomLayer(const QString &id, - QMapbox::CustomLayerInitializeFunction initFn, - QMapbox::CustomLayerRenderFunction renderFn, - QMapbox::CustomLayerDeinitializeFunction deinitFn, - void *context, + QScopedPointer& host, const QString& before) { + class HostWrapper : public mbgl::style::CustomLayerHost { + public: + QScopedPointer ptr; + HostWrapper(QScopedPointer& p) + : ptr(p.take()) { + } + + void initialize() { + ptr->initialize(); + } + + void render(const mbgl::style::CustomLayerRenderParameters& params) { + QMapbox::CustomLayerRenderParameters renderParams; + renderParams.width = params.width; + renderParams.height = params.height; + renderParams.latitude = params.latitude; + renderParams.longitude = params.longitude; + renderParams.zoom = params.zoom; + renderParams.bearing = params.bearing; + renderParams.pitch = params.pitch; + renderParams.fieldOfView = params.fieldOfView; + ptr->render(renderParams); + } + + void contextLost() { } + + void deinitialize() { + ptr->deinitialize(); + } + }; + d_ptr->mapObj->getStyle().addLayer(std::make_unique( id.toStdString(), - reinterpret_cast(initFn), - // This cast is safe as long as both mbgl:: and QMapbox:: - // CustomLayerRenderParameters members remains the same. - (mbgl::style::CustomLayerRenderFunction)renderFn, - reinterpret_cast(deinitFn), - context), + std::make_unique(host)), before.isEmpty() ? mbgl::optional() : mbgl::optional(before.toStdString())); } -- cgit v1.2.1