summaryrefslogtreecommitdiff
path: root/include/mbgl/style/layer.hpp
blob: f09eb0165aa5dcc91c40701eecd163c9020ddcad (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#pragma once

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/any.hpp>
#include <mbgl/style/layer_type.hpp>
#include <mbgl/style/types.hpp>

#include <memory>
#include <string>

namespace mbgl {
namespace style {

class FillLayer;
class LineLayer;
class CircleLayer;
class SymbolLayer;
class RasterLayer;
class BackgroundLayer;
class CustomLayer;
class FillExtrusionLayer;

/**
 * The runtime representation of a [layer](https://www.mapbox.com/mapbox-gl-style-spec/#layers) from the Mapbox Style
 * Specification.
 *
 * `Layer` is an abstract base class; concrete derived classes are provided for each layer type. `Layer` contains
 * functionality that is common to all layer types:
 *
 * * Runtime type information: type predicates and casting
 * * Accessors for properties common to all layer types: ID, visibility, etc.
 * * Cloning and copying
 *
 * All other functionality lives in the derived classes. To instantiate a layer, create an instance of the desired
 * type, passing the ID:
 *
 *     auto circleLayer = std::make_unique<CircleLayer>("my-circle-layer");
 */
class Layer : public mbgl::util::noncopyable {
public:
    class Impl;

protected:

    const LayerType type;
    Layer(LayerType, std::unique_ptr<Impl>);

public:

    virtual ~Layer();

    // Check whether this layer is of the given subtype.
    template <class T>
    bool is() const;

    // Dynamically cast this layer to the given subtype.
    template <class T>
    T* as() {
        return is<T>() ? reinterpret_cast<T*>(this) : nullptr;
    }

    template <class T>
    const T* as() const {
        return is<T>() ? reinterpret_cast<const T*>(this) : nullptr;
    }

    // Convenience method for dynamic dispatch on the concrete layer type. Using
    // method overloading, this allows consolidation of logic common to vector-based
    // layers (Fill, Line, Circle, or Symbol). For example:
    //
    //     struct Visitor {
    //         void operator()(CustomLayer&) { ... }
    //         void operator()(RasterLayer&) { ... }
    //         void operator()(BackgroundLayer&) { ... }
    //         template <class VectorLayer>
    //         void operator()(VectorLayer&) { ... }
    //     };
    //
    template <class V>
    auto accept(V&& visitor) {
        switch (type) {
        case LayerType::Fill:
            return visitor(*as<FillLayer>());
        case LayerType::Line:
            return visitor(*as<LineLayer>());
        case LayerType::Circle:
            return visitor(*as<CircleLayer>());
        case LayerType::Symbol:
            return visitor(*as<SymbolLayer>());
        case LayerType::Raster:
            return visitor(*as<RasterLayer>());
        case LayerType::Background:
            return visitor(*as<BackgroundLayer>());
        case LayerType::Custom:
            return visitor(*as<CustomLayer>());
        case LayerType::FillExtrusion:
            return visitor(*as<FillExtrusionLayer>());
        }
    }

    const std::string& getID() const;

    // Visibility
    VisibilityType getVisibility() const;
    void setVisibility(VisibilityType);

    // Zoom range
    float getMinZoom() const;
    void setMinZoom(float) const;
    float getMaxZoom() const;
    void setMaxZoom(float) const;

    // Private implementation
    const std::unique_ptr<Impl> baseImpl;

    // For use in SDK bindings, which store a reference to a platform-native peer
    // object here, so that separately-obtained references to this object share
    // identical platform-native peers.
    any peer;
};

} // namespace style
} // namespace mbgl