summaryrefslogtreecommitdiff
path: root/src/mbgl/sourcemanager/raster_source_factory.cpp
blob: a06595059d95779f809d896d9b34f72635f4380b (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
#include <mbgl/renderer/sources/render_raster_source.hpp>
#include <mbgl/sourcemanager/raster_source_factory.hpp>
#include <mbgl/style/conversion/url_or_tileset.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/sources/raster_source.hpp>
#include <mbgl/style/sources/raster_source_impl.hpp>

namespace mbgl {

const style::SourceTypeInfo* RasterSourceFactory::getTypeInfo() const noexcept {
    return style::RasterSource::Impl::staticTypeInfo();
}

std::unique_ptr<style::Source> RasterSourceFactory::createSource(const std::string& id,
                                                                 const style::conversion::Convertible& value,
                                                                 style::conversion::Error& error) noexcept {
    optional<variant<std::string, Tileset>> urlOrTileset =
        style::conversion::convert<variant<std::string, Tileset>>(value, error);

    if (!urlOrTileset) {
        return nullptr;
    }

    uint16_t tileSize = util::tileSize;
    auto tileSizeValue = objectMember(value, "tileSize");
    if (tileSizeValue) {
        optional<float> size = toNumber(*tileSizeValue);
        if (!size || *size < 0 || *size > std::numeric_limits<uint16_t>::max()) {
            error.message = "invalid tileSize";
            return nullptr;
        }
        tileSize = *size;
    }

    return {std::make_unique<style::RasterSource>(id, std::move(*urlOrTileset), tileSize)};
}

std::unique_ptr<RenderSource> RasterSourceFactory::createRenderSource(Immutable<style::Source::Impl> impl) noexcept {
    assert(impl->getTypeInfo() == getTypeInfo());
    return std::make_unique<RenderRasterSource>(staticImmutableCast<style::RasterSource::Impl>(impl));
}

} // namespace mbgl