summaryrefslogtreecommitdiff
path: root/platform/default/layer_manager.cpp
blob: 0a27c0b512aeb4cb6c86db97cf36ef2b8bd581dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <mbgl/style/layer.hpp>
#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>
#include <mbgl/style/layers/hillshade_layer.hpp>
#include <mbgl/style/layers/line_layer.hpp>
#include <mbgl/style/layers/raster_layer.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>

#include <map>
#include <memory>
#include <vector>

namespace mbgl {
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() {
    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 {
    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;
}

// static 
LayerManager* LayerManager::get() noexcept {
    static LayerManagerBase impl;
    return &impl;
}

} // namespace style
} // namespace mbgl