From 33818233cf49ffa7238a3d1b2bbf9fc126cbc253 Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Thu, 15 Nov 2018 15:47:25 +0200 Subject: [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. --- platform/default/layer_manager.cpp | 46 +++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 16 deletions(-) (limited to 'platform/default') 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 #include #include +#include #include #include #include @@ -10,6 +11,7 @@ #include #include +#include #include #include @@ -19,36 +21,48 @@ namespace style { class LayerManagerBase : public LayerManager { public: LayerManagerBase(); + private: + void addLayerType(std::unique_ptr); // LayerManager overrides. std::unique_ptr createLayer(const std::string& type, const std::string& id, const conversion::Convertible& value, conversion::Error& error) noexcept final; + std::vector> factories; + std::map typeToFactory; }; LayerManagerBase::LayerManagerBase() { - factories.emplace_back(std::unique_ptr(new FillLayerFactory)); - factories.emplace_back(std::unique_ptr(new LineLayerFactory)); - factories.emplace_back(std::unique_ptr(new CircleLayerFactory)); - factories.emplace_back(std::unique_ptr(new SymbolLayerFactory)); - factories.emplace_back(std::unique_ptr(new RasterLayerFactory)); - factories.emplace_back(std::unique_ptr(new BackgroundLayerFactory)); - factories.emplace_back(std::unique_ptr(new HillshadeLayerFactory)); - factories.emplace_back(std::unique_ptr(new FillExtrusionLayerFactory)); - factories.emplace_back(std::unique_ptr(new HeatmapLayerFactory)); + addLayerType(std::make_unique()); + addLayerType(std::make_unique()); + addLayerType(std::make_unique()); + addLayerType(std::make_unique()); + addLayerType(std::make_unique()); + addLayerType(std::make_unique()); + addLayerType(std::make_unique()); + addLayerType(std::make_unique()); + addLayerType(std::make_unique()); + addLayerType(std::make_unique()); +} + +void LayerManagerBase::addLayerType(std::unique_ptr 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 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; -- cgit v1.2.1