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 17:48:36 +0200 |
commit | ff09c94d0825f5dbe7dddce52b64e74d61978976 (patch) | |
tree | 86833d95d8bf7abbcb8e6965aaba65d666ffff52 /platform/default/layer_manager.cpp | |
parent | 73dcfe2d0d3475ad9584969c7908ba681a3f5bfa (diff) | |
download | qtlocation-mapboxgl-upstream/mikhail_LayerTypeInfo.tar.gz |
[core][android] Introduce mbgl::style::LayerTypeInfoupstream/mikhail_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/default/layer_manager.cpp')
-rw-r--r-- | platform/default/layer_manager.cpp | 46 |
1 files changed, 30 insertions, 16 deletions
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; |