summaryrefslogtreecommitdiff
path: root/include/mbgl/style/source.hpp
blob: ceec12149a409f2e668fc9b068ab4c5523aa25c9 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#pragma once

#include <mapbox/std/weak.hpp>
#include <mapbox/util/type_wrapper.hpp>
#include <mbgl/style/source_data.hpp>
#include <mbgl/style/source_parameters.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/tile/tile_kind.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/immutable.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/variant.hpp>
#include <memory>
#include <string>

namespace mbgl {

class FileSource;
class Tileset;

namespace style {

class VectorSource;
class RasterSource;
class RasterDEMSource;
class GeoJSONSource;
class SourceObserver;
struct LayerTypeInfo;

/**
 * @brief Holds static data for a certain source type.
 */
struct SourceTypeInfo {
    /**
     * @brief contains the source type as defined in the style specification;
     */
    const char* type;

    /**
     * @brief specifies whether the source supports tile prefetching;
     */
    const enum class TilePrefetch : bool { No, Yes } tilePrefetch;

    /**
     * @brief specifies whether the source operates with the loaded tile set;
     */
    const enum class TileSet : bool { No, Yes } tileSet;

    /**
     * @brief specifies source's tile kind;
     */
    const optional<TileKind> tileKind;
};

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

    virtual ~Source();

    std::string getID() const;
    optional<std::string> getAttribution() const;

    // The data from the volatile sources are not stored in a persistent storage.
    bool isVolatile() const noexcept;
    void setVolatile(bool) noexcept;

    // Private implementation
    class Impl;
    Immutable<Impl> baseImpl;

    void setObserver(SourceObserver*);
    SourceObserver* observer = nullptr;

    virtual void loadDescription(FileSource&) = 0;

    virtual void setSourceData(SourceData) {}

    virtual SourceDataResult getSourceData() const { return {}; }

    virtual const variant<std::string, Tileset>* getURLOrTileset() const { return nullptr; }

    virtual void setSourceParameters(SourceParameters) {}

    virtual void invalidateTile(const CanonicalTileID&) {}
    virtual void invalidateRegion(const LatLngBounds&) {}

    void setPrefetchZoomDelta(optional<uint8_t> delta) noexcept;
    optional<uint8_t> getPrefetchZoomDelta() const noexcept;

    int32_t getCoveringZoomLevel(double z) const noexcept;

    uint16_t getTileSize() const;

    // If the given source supports loading tiles from a server,
    // sets the minimum tile update interval.
    // Update network requests that are more frequent than the
    // minimum tile update interval are suppressed.
    //
    // Default value is `Duration::zero()`.
    void setMinimumTileUpdateInterval(Duration) noexcept;
    Duration getMinimumTileUpdateInterval() const noexcept;

    // Sets a limit for how much a parent tile can be overscaled.
    //
    // When a set of tiles for a current zoom level is being rendered and some of the
    // ideal tiles that cover the screen are not yet loaded, parent tile could be
    // used instead. This might introduce unwanted rendering side-effects, especially
    // for raster tiles that are overscaled multiple times.
    //
    // For example, an overscale factor of 3 would mean that on zoom level 3, the
    // minimum zoom level of a parent tile that could be used in place of an ideal
    // tile during rendering would be zoom 0. By default, no limit is set, so any
    // parent tile may be used.
    void setMaxOverscaleFactorForParentTiles(optional<uint8_t> overscaleFactor) noexcept;
    optional<uint8_t> getMaxOverscaleFactorForParentTiles() const noexcept;

    void dumpDebugLogs() const;

    virtual bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const = 0;

    bool loaded = false;

    // 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.
    mapbox::base::TypeWrapper peer;

    virtual mapbox::base::WeakPtr<Source> makeWeakPtr() = 0;

    const SourceTypeInfo* getTypeInfo() const noexcept;

    virtual Value serialize() const;

protected:
    explicit Source(Immutable<Impl>);

    void serializeUrlOrTileSet(Value&, const variant<std::string, Tileset>*) const;
    void serializeTileSet(Value&, const mbgl::Tileset&) const;

    virtual Mutable<Impl> createMutable() const noexcept = 0;
};

} // namespace style
} // namespace mbgl