diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2018-11-15 15:47:25 +0200 |
---|---|---|
committer | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2018-11-15 18:32:31 +0200 |
commit | 33818233cf49ffa7238a3d1b2bbf9fc126cbc253 (patch) | |
tree | 86833d95d8bf7abbcb8e6965aaba65d666ffff52 /platform | |
parent | 73dcfe2d0d3475ad9584969c7908ba681a3f5bfa (diff) | |
download | qtlocation-mapboxgl-33818233cf49ffa7238a3d1b2bbf9fc126cbc253.tar.gz |
[core][android] Introduce mbgl::style::LayerTypeInfo
The `LayerTypeInfo` contains static meta data about certain layer type.
Each layer module should have a single immutable `LayerTypeInfo` instance
for the represented layer type. Both `LayerImpl` and `LayerFactory` from the
module always refer to the same `LayerTypeInfo` instance, so address of this
instance can be used as a layer module Id during the process life time.
Diffstat (limited to 'platform')
13 files changed, 83 insertions, 57 deletions
diff --git a/platform/android/src/style/layers/background_layer.cpp b/platform/android/src/style/layers/background_layer.cpp index c0d3f39bc4..e28fd0798b 100644 --- a/platform/android/src/style/layers/background_layer.cpp +++ b/platform/android/src/style/layers/background_layer.cpp @@ -109,12 +109,12 @@ namespace android { } // namespace jni::Local<jni::Object<Layer>> BackgroundJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Layer& layer) { - assert(layer.baseImpl->getLayerFactory() == this); + assert(layer.baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new BackgroundLayer(map, toBackgroundLayer(layer))); } jni::Local<jni::Object<Layer>> BackgroundJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr<mbgl::style::Layer> layer) { - assert(layer->baseImpl->getLayerFactory() == this); + assert(layer->baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new BackgroundLayer(map, std::unique_ptr<mbgl::style::BackgroundLayer>(static_cast<mbgl::style::BackgroundLayer*>(layer.release())))); } diff --git a/platform/android/src/style/layers/circle_layer.cpp b/platform/android/src/style/layers/circle_layer.cpp index 13f3a4cb03..f460031dcd 100644 --- a/platform/android/src/style/layers/circle_layer.cpp +++ b/platform/android/src/style/layers/circle_layer.cpp @@ -214,12 +214,12 @@ namespace android { } // namespace jni::Local<jni::Object<Layer>> CircleJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Layer& layer) { - assert(layer.baseImpl->getLayerFactory() == this); + assert(layer.baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new CircleLayer(map, toCircleLayer(layer))); } jni::Local<jni::Object<Layer>> CircleJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr<mbgl::style::Layer> layer) { - assert(layer->baseImpl->getLayerFactory() == this); + assert(layer->baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new CircleLayer(map, std::unique_ptr<mbgl::style::CircleLayer>(static_cast<mbgl::style::CircleLayer*>(layer.release())))); } diff --git a/platform/android/src/style/layers/fill_extrusion_layer.cpp b/platform/android/src/style/layers/fill_extrusion_layer.cpp index fd67b391ac..7767a900d0 100644 --- a/platform/android/src/style/layers/fill_extrusion_layer.cpp +++ b/platform/android/src/style/layers/fill_extrusion_layer.cpp @@ -168,12 +168,12 @@ namespace android { } // namespace jni::Local<jni::Object<Layer>> FillExtrusionJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Layer& layer) { - assert(layer.baseImpl->getLayerFactory() == this); + assert(layer.baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new FillExtrusionLayer(map, toFillExtrusionLayer(layer))); } jni::Local<jni::Object<Layer>> FillExtrusionJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr<mbgl::style::Layer> layer) { - assert(layer->baseImpl->getLayerFactory() == this); + assert(layer->baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new FillExtrusionLayer(map, std::unique_ptr<mbgl::style::FillExtrusionLayer>(static_cast<mbgl::style::FillExtrusionLayer*>(layer.release())))); } diff --git a/platform/android/src/style/layers/fill_layer.cpp b/platform/android/src/style/layers/fill_layer.cpp index 252771038a..c54f4cd39d 100644 --- a/platform/android/src/style/layers/fill_layer.cpp +++ b/platform/android/src/style/layers/fill_layer.cpp @@ -155,12 +155,12 @@ namespace android { } // namespace jni::Local<jni::Object<Layer>> FillJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Layer& layer) { - assert(layer.baseImpl->getLayerFactory() == this); + assert(layer.baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new FillLayer(map, toFillLayer(layer))); } jni::Local<jni::Object<Layer>> FillJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr<mbgl::style::Layer> layer) { - assert(layer->baseImpl->getLayerFactory() == this); + assert(layer->baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new FillLayer(map, std::unique_ptr<mbgl::style::FillLayer>(static_cast<mbgl::style::FillLayer*>(layer.release())))); } diff --git a/platform/android/src/style/layers/heatmap_layer.cpp b/platform/android/src/style/layers/heatmap_layer.cpp index d478746f09..509ab5d707 100644 --- a/platform/android/src/style/layers/heatmap_layer.cpp +++ b/platform/android/src/style/layers/heatmap_layer.cpp @@ -123,12 +123,12 @@ namespace android { } // namespace jni::Local<jni::Object<Layer>> HeatmapJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Layer& layer) { - assert(layer.baseImpl->getLayerFactory() == this); + assert(layer.baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new HeatmapLayer(map, toHeatmapLayer(layer))); } jni::Local<jni::Object<Layer>> HeatmapJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr<mbgl::style::Layer> layer) { - assert(layer->baseImpl->getLayerFactory() == this); + assert(layer->baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new HeatmapLayer(map, std::unique_ptr<mbgl::style::HeatmapLayer>(static_cast<mbgl::style::HeatmapLayer*>(layer.release())))); } diff --git a/platform/android/src/style/layers/hillshade_layer.cpp b/platform/android/src/style/layers/hillshade_layer.cpp index 3c2ab33d43..bc25f666a0 100644 --- a/platform/android/src/style/layers/hillshade_layer.cpp +++ b/platform/android/src/style/layers/hillshade_layer.cpp @@ -137,12 +137,12 @@ namespace android { } // namespace jni::Local<jni::Object<Layer>> HillshadeJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Layer& layer) { - assert(layer.baseImpl->getLayerFactory() == this); + assert(layer.baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new HillshadeLayer(map, toHillshadeLayer(layer))); } jni::Local<jni::Object<Layer>> HillshadeJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr<mbgl::style::Layer> layer) { - assert(layer->baseImpl->getLayerFactory() == this); + assert(layer->baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new HillshadeLayer(map, std::unique_ptr<mbgl::style::HillshadeLayer>(static_cast<mbgl::style::HillshadeLayer*>(layer.release())))); } diff --git a/platform/android/src/style/layers/layer.cpp.ejs b/platform/android/src/style/layers/layer.cpp.ejs index 1489d83a5e..c81fc5ab39 100644 --- a/platform/android/src/style/layers/layer.cpp.ejs +++ b/platform/android/src/style/layers/layer.cpp.ejs @@ -101,12 +101,12 @@ namespace android { } // namespace jni::Local<jni::Object<Layer>> <%- camelize(type) %>JavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Layer& layer) { - assert(layer.baseImpl->getLayerFactory() == this); + assert(layer.baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new <%- camelize(type) %>Layer(map, to<%- camelize(type) %>Layer(layer))); } jni::Local<jni::Object<Layer>> <%- camelize(type) %>JavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr<mbgl::style::Layer> layer) { - assert(layer->baseImpl->getLayerFactory() == this); + assert(layer->baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new <%- camelize(type) %>Layer(map, std::unique_ptr<mbgl::style::<%- camelize(type) %>Layer>(static_cast<mbgl::style::<%- camelize(type) %>Layer*>(layer.release())))); } diff --git a/platform/android/src/style/layers/layer_manager.cpp b/platform/android/src/style/layers/layer_manager.cpp index 72bc317607..9ad62121e6 100644 --- a/platform/android/src/style/layers/layer_manager.cpp +++ b/platform/android/src/style/layers/layer_manager.cpp @@ -34,16 +34,16 @@ namespace mbgl { namespace android { LayerManagerAndroid::LayerManagerAndroid() { - factories.emplace_back(std::unique_ptr<JavaLayerPeerFactory>(new FillJavaLayerPeerFactory)); - factories.emplace_back(std::unique_ptr<JavaLayerPeerFactory>(new LineJavaLayerPeerFactory)); - factories.emplace_back(std::unique_ptr<JavaLayerPeerFactory>(new CircleJavaLayerPeerFactory)); - factories.emplace_back(std::unique_ptr<JavaLayerPeerFactory>(new SymbolJavaLayerPeerFactory)); - factories.emplace_back(std::unique_ptr<JavaLayerPeerFactory>(new RasterJavaLayerPeerFactory)); - factories.emplace_back(std::unique_ptr<JavaLayerPeerFactory>(new BackgroundJavaLayerPeerFactory)); - factories.emplace_back(std::unique_ptr<JavaLayerPeerFactory>(new HillshadeJavaLayerPeerFactory)); - factories.emplace_back(std::unique_ptr<JavaLayerPeerFactory>(new FillExtrusionJavaLayerPeerFactory)); - factories.emplace_back(std::unique_ptr<JavaLayerPeerFactory>(new HeatmapJavaLayerPeerFactory)); - factories.emplace_back(std::unique_ptr<JavaLayerPeerFactory>(new CustomJavaLayerPeerFactory)); + addLayerType(std::make_unique<FillJavaLayerPeerFactory>()); + addLayerType(std::make_unique<LineJavaLayerPeerFactory>()); + addLayerType(std::make_unique<CircleJavaLayerPeerFactory>()); + addLayerType(std::make_unique<SymbolJavaLayerPeerFactory>()); + addLayerType(std::make_unique<RasterJavaLayerPeerFactory>()); + addLayerType(std::make_unique<BackgroundJavaLayerPeerFactory>()); + addLayerType(std::make_unique<HillshadeJavaLayerPeerFactory>()); + addLayerType(std::make_unique<FillExtrusionJavaLayerPeerFactory>()); + addLayerType(std::make_unique<HeatmapJavaLayerPeerFactory>()); + addLayerType(std::make_unique<CustomJavaLayerPeerFactory>()); } LayerManagerAndroid::~LayerManagerAndroid() = default; @@ -69,11 +69,20 @@ void LayerManagerAndroid::registerNative(jni::JNIEnv& env) { } } +void LayerManagerAndroid::addLayerType(std::unique_ptr<JavaLayerPeerFactory> factory) { + auto* coreFactory = factory->getLayerFactory(); + std::string type{coreFactory->getTypeInfo()->type}; + if (!type.empty()) { + typeToFactory.emplace(std::make_pair(std::move(type), coreFactory)); + } + factories.emplace_back(std::move(factory)); +} + JavaLayerPeerFactory* LayerManagerAndroid::getPeerFactory(mbgl::style::Layer* layer) { - auto* layerFactory = layer->baseImpl->getLayerFactory(); - assert(layerFactory); + auto* typeInfo = layer->baseImpl->getTypeInfo(); + assert(typeInfo); for (const auto& factory: factories) { - if (factory->getLayerFactory() == layerFactory) { + if (factory->getLayerFactory()->getTypeInfo() == typeInfo) { return factory.get(); } } @@ -81,16 +90,17 @@ JavaLayerPeerFactory* LayerManagerAndroid::getPeerFactory(mbgl::style::Layer* la return nullptr; } -std::unique_ptr<style::Layer> LayerManagerAndroid::createLayer(const std::string& type, const std::string& id, const style::conversion::Convertible& value, style::conversion::Error& error) noexcept{ - for (const auto& factory: factories) { - auto* layerFactory = factory->getLayerFactory(); - if (layerFactory->supportsType(type)) { - if (auto layer = layerFactory->createLayer(id, value)) { - return layer; - } +std::unique_ptr<style::Layer> LayerManagerAndroid::createLayer(const std::string& type, + const std::string& id, + const style::conversion::Convertible& value, + style::conversion::Error& error) noexcept { + auto search = typeToFactory.find(type); + if (search != typeToFactory.end()) { + auto layer = search->second->createLayer(id, value); + if (!layer) { error.message = "Error parsing a layer of type: " + type; - return nullptr; } + return layer; } error.message = "Unsupported layer type: " + type; return nullptr; diff --git a/platform/android/src/style/layers/layer_manager.hpp b/platform/android/src/style/layers/layer_manager.hpp index cec9d39d8c..396521cc4a 100644 --- a/platform/android/src/style/layers/layer_manager.hpp +++ b/platform/android/src/style/layers/layer_manager.hpp @@ -28,11 +28,13 @@ public: private: LayerManagerAndroid(); + void addLayerType(std::unique_ptr<JavaLayerPeerFactory>); JavaLayerPeerFactory* getPeerFactory(mbgl::style::Layer*); // mbgl:style::LayerManager overrides. std::unique_ptr<style::Layer> createLayer(const std::string& type, const std::string& id, const style::conversion::Convertible& value, style::conversion::Error& error) noexcept final; std::vector<std::unique_ptr<JavaLayerPeerFactory>> factories; + std::map<std::string, style::LayerFactory*> typeToFactory; }; } diff --git a/platform/android/src/style/layers/line_layer.cpp b/platform/android/src/style/layers/line_layer.cpp index 69b745b89c..73fcd5f991 100644 --- a/platform/android/src/style/layers/line_layer.cpp +++ b/platform/android/src/style/layers/line_layer.cpp @@ -247,12 +247,12 @@ namespace android { } // namespace jni::Local<jni::Object<Layer>> LineJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Layer& layer) { - assert(layer.baseImpl->getLayerFactory() == this); + assert(layer.baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new LineLayer(map, toLineLayer(layer))); } jni::Local<jni::Object<Layer>> LineJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr<mbgl::style::Layer> layer) { - assert(layer->baseImpl->getLayerFactory() == this); + assert(layer->baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new LineLayer(map, std::unique_ptr<mbgl::style::LineLayer>(static_cast<mbgl::style::LineLayer*>(layer.release())))); } diff --git a/platform/android/src/style/layers/raster_layer.cpp b/platform/android/src/style/layers/raster_layer.cpp index 00286c0f16..b12267525f 100644 --- a/platform/android/src/style/layers/raster_layer.cpp +++ b/platform/android/src/style/layers/raster_layer.cpp @@ -173,12 +173,12 @@ namespace android { } // namespace jni::Local<jni::Object<Layer>> RasterJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Layer& layer) { - assert(layer.baseImpl->getLayerFactory() == this); + assert(layer.baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new RasterLayer(map, toRasterLayer(layer))); } jni::Local<jni::Object<Layer>> RasterJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr<mbgl::style::Layer> layer) { - assert(layer->baseImpl->getLayerFactory() == this); + assert(layer->baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new RasterLayer(map, std::unique_ptr<mbgl::style::RasterLayer>(static_cast<mbgl::style::RasterLayer*>(layer.release())))); } diff --git a/platform/android/src/style/layers/symbol_layer.cpp b/platform/android/src/style/layers/symbol_layer.cpp index 646e02ce32..61e4d59326 100644 --- a/platform/android/src/style/layers/symbol_layer.cpp +++ b/platform/android/src/style/layers/symbol_layer.cpp @@ -466,12 +466,12 @@ namespace android { } // namespace jni::Local<jni::Object<Layer>> SymbolJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, mbgl::style::Layer& layer) { - assert(layer.baseImpl->getLayerFactory() == this); + assert(layer.baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new SymbolLayer(map, toSymbolLayer(layer))); } jni::Local<jni::Object<Layer>> SymbolJavaLayerPeerFactory::createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr<mbgl::style::Layer> layer) { - assert(layer->baseImpl->getLayerFactory() == this); + assert(layer->baseImpl->getTypeInfo() == getTypeInfo()); return createJavaPeer(env, new SymbolLayer(map, std::unique_ptr<mbgl::style::SymbolLayer>(static_cast<mbgl::style::SymbolLayer*>(layer.release())))); } diff --git a/platform/default/layer_manager.cpp b/platform/default/layer_manager.cpp index 919bc9e64e..0a27c0b512 100644 --- a/platform/default/layer_manager.cpp +++ b/platform/default/layer_manager.cpp @@ -2,6 +2,7 @@ #include <mbgl/style/layers/symbol_layer.hpp> #include <mbgl/style/layers/background_layer.hpp> #include <mbgl/style/layers/circle_layer.hpp> +#include <mbgl/style/layers/custom_layer.hpp> #include <mbgl/style/layers/fill_extrusion_layer.hpp> #include <mbgl/style/layers/fill_layer.hpp> #include <mbgl/style/layers/heatmap_layer.hpp> @@ -10,6 +11,7 @@ #include <mbgl/style/layers/raster_layer.hpp> #include <mbgl/style/layers/symbol_layer.hpp> +#include <map> #include <memory> #include <vector> @@ -19,36 +21,48 @@ namespace style { class LayerManagerBase : public LayerManager { public: LayerManagerBase(); + private: + void addLayerType(std::unique_ptr<LayerFactory>); // LayerManager overrides. std::unique_ptr<Layer> createLayer(const std::string& type, const std::string& id, const conversion::Convertible& value, conversion::Error& error) noexcept final; + std::vector<std::unique_ptr<LayerFactory>> factories; + std::map<std::string, LayerFactory*> typeToFactory; }; LayerManagerBase::LayerManagerBase() { - factories.emplace_back(std::unique_ptr<LayerFactory>(new FillLayerFactory)); - factories.emplace_back(std::unique_ptr<LayerFactory>(new LineLayerFactory)); - factories.emplace_back(std::unique_ptr<LayerFactory>(new CircleLayerFactory)); - factories.emplace_back(std::unique_ptr<LayerFactory>(new SymbolLayerFactory)); - factories.emplace_back(std::unique_ptr<LayerFactory>(new RasterLayerFactory)); - factories.emplace_back(std::unique_ptr<LayerFactory>(new BackgroundLayerFactory)); - factories.emplace_back(std::unique_ptr<LayerFactory>(new HillshadeLayerFactory)); - factories.emplace_back(std::unique_ptr<LayerFactory>(new FillExtrusionLayerFactory)); - factories.emplace_back(std::unique_ptr<LayerFactory>(new HeatmapLayerFactory)); + addLayerType(std::make_unique<FillLayerFactory>()); + addLayerType(std::make_unique<LineLayerFactory>()); + addLayerType(std::make_unique<CircleLayerFactory>()); + addLayerType(std::make_unique<SymbolLayerFactory>()); + addLayerType(std::make_unique<RasterLayerFactory>()); + addLayerType(std::make_unique<BackgroundLayerFactory>()); + addLayerType(std::make_unique<HillshadeLayerFactory>()); + addLayerType(std::make_unique<FillExtrusionLayerFactory>()); + addLayerType(std::make_unique<HeatmapLayerFactory>()); + addLayerType(std::make_unique<CustomLayerFactory>()); +} + +void LayerManagerBase::addLayerType(std::unique_ptr<LayerFactory> factory) { + std::string type{factory->getTypeInfo()->type}; + if (!type.empty()) { + typeToFactory.emplace(std::make_pair(std::move(type), factory.get())); + } + factories.emplace_back(std::move(factory)); } std::unique_ptr<Layer> LayerManagerBase::createLayer(const std::string& type, const std::string& id, const conversion::Convertible& value, conversion::Error& error) noexcept { - for (const auto& factory: factories) { - if (factory->supportsType(type)) { - auto layer = factory->createLayer(id, value); - if (!layer) { - error.message = "Error parsing a layer of type: " + type; - } - return layer; + auto search = typeToFactory.find(type); + if (search != typeToFactory.end()) { + auto layer = search->second->createLayer(id, value); + if (!layer) { + error.message = "Error parsing a layer of type: " + type; } + return layer; } error.message = "Unsupported layer type: " + type; return nullptr; |