From 900568cfb0b84a298395f4d84488fd9323552c63 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 14 Jun 2016 16:07:21 -0700 Subject: [core] Runtime source API: private impls --- src/mbgl/style/sources/geojson_source.cpp | 113 ------------------------ src/mbgl/style/sources/geojson_source.hpp | 48 ---------- src/mbgl/style/sources/geojson_source_impl.cpp | 117 +++++++++++++++++++++++++ src/mbgl/style/sources/geojson_source_impl.hpp | 50 +++++++++++ src/mbgl/style/sources/raster_source.cpp | 40 --------- src/mbgl/style/sources/raster_source.hpp | 19 ---- src/mbgl/style/sources/raster_source_impl.cpp | 40 +++++++++ src/mbgl/style/sources/raster_source_impl.hpp | 20 +++++ src/mbgl/style/sources/vector_source.cpp | 25 ------ src/mbgl/style/sources/vector_source.hpp | 19 ---- src/mbgl/style/sources/vector_source_impl.cpp | 25 ++++++ src/mbgl/style/sources/vector_source_impl.hpp | 20 +++++ 12 files changed, 272 insertions(+), 264 deletions(-) delete mode 100644 src/mbgl/style/sources/geojson_source.cpp delete mode 100644 src/mbgl/style/sources/geojson_source.hpp create mode 100644 src/mbgl/style/sources/geojson_source_impl.cpp create mode 100644 src/mbgl/style/sources/geojson_source_impl.hpp delete mode 100644 src/mbgl/style/sources/raster_source.cpp delete mode 100644 src/mbgl/style/sources/raster_source.hpp create mode 100644 src/mbgl/style/sources/raster_source_impl.cpp create mode 100644 src/mbgl/style/sources/raster_source_impl.hpp delete mode 100644 src/mbgl/style/sources/vector_source.cpp delete mode 100644 src/mbgl/style/sources/vector_source.hpp create mode 100644 src/mbgl/style/sources/vector_source_impl.cpp create mode 100644 src/mbgl/style/sources/vector_source_impl.hpp (limited to 'src/mbgl/style/sources') diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp deleted file mode 100644 index e06b00ec99..0000000000 --- a/src/mbgl/style/sources/geojson_source.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include - -namespace mbgl { -namespace style { - -std::unique_ptr GeoJSONSource::parseGeoJSON(const JSValue& value) { - using namespace mapbox::geojsonvt; - - Options options; - options.buffer = util::EXTENT / util::tileSize * 128; - options.extent = util::EXTENT; - - try { - return std::make_unique(Convert::convert(value, 0), options); - } catch (const std::exception& ex) { - Log::Error(Event::ParseStyle, "Failed to parse GeoJSON data: %s", ex.what()); - // Create an empty GeoJSON VT object to make sure we're not infinitely waiting for - // tiles to load. - return std::make_unique(std::vector{}, options); - } -} - -std::unique_ptr GeoJSONSource::parse(const std::string& id, const JSValue& value) { - // We should probably split this up to have URLs in the url property, and actual data - // in the data property. Until then, we're going to detect the content based on the - // object type. - if (!value.HasMember("data")) { - Log::Error(Event::ParseStyle, "GeoJSON source must have a data value"); - return nullptr; - } - - const JSValue& dataVal = value["data"]; - if (dataVal.IsString()) { - return std::make_unique(id, std::string(dataVal.GetString(), dataVal.GetStringLength())); - } else if (dataVal.IsObject()) { - return std::make_unique(id, parseGeoJSON(dataVal)); - } else { - Log::Error(Event::ParseStyle, "GeoJSON data must be a URL or an object"); - return nullptr; - } -} - -GeoJSONSource::GeoJSONSource(std::string id_, variant urlOrGeoJSON_) - : Source(SourceType::GeoJSON, std::move(id_)), - urlOrGeoJSON(std::move(urlOrGeoJSON_)) { -} - -GeoJSONSource::~GeoJSONSource() = default; - -void GeoJSONSource::load(FileSource& fileSource) { - if (urlOrGeoJSON.is()) { - loaded = true; - return; - } - - if (req) { - return; - } - - const std::string& url = urlOrGeoJSON.get(); - req = fileSource.request(Resource::source(url), [this](Response res) { - if (res.error) { - observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message))); - } else if (res.notModified) { - return; - } else if (res.noContent) { - observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("unexpectedly empty GeoJSON"))); - } else { - rapidjson::GenericDocument, rapidjson::CrtAllocator> d; - d.Parse<0>(res.data->c_str()); - - if (d.HasParseError()) { - std::stringstream message; - message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError()); - observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(message.str()))); - return; - } - - invalidateTiles(); - - urlOrGeoJSON = parseGeoJSON(d); - loaded = true; - - observer->onSourceLoaded(*this); - } - }); -} - -Range GeoJSONSource::getZoomRange() { - assert(loaded); - return { 0, urlOrGeoJSON.get()->options.maxZoom }; -} - -std::unique_ptr GeoJSONSource::createTile(const OverscaledTileID& tileID, - const UpdateParameters& parameters) { - assert(loaded); - return std::make_unique(tileID, id, parameters, *urlOrGeoJSON.get()); -} - -} // namespace style -} // namespace mbgl diff --git a/src/mbgl/style/sources/geojson_source.hpp b/src/mbgl/style/sources/geojson_source.hpp deleted file mode 100644 index 490dae48b8..0000000000 --- a/src/mbgl/style/sources/geojson_source.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace mapbox { -namespace geojsonvt { -class GeoJSONVT; -} // namespace geojsonvt -} // namespace mapbox - -namespace mbgl { - -class AsyncRequest; - -namespace style { - -class GeoJSONSource : public Source { -public: - using GeoJSON = std::unique_ptr; - - static std::unique_ptr parse(const std::string& id, const JSValue&); - static GeoJSON parseGeoJSON(const JSValue&); - - GeoJSONSource(std::string id, variant urlOrGeoJSON); - ~GeoJSONSource() final; - - void load(FileSource&) final; - - uint16_t getTileSize() const final { - return util::tileSize; - } - - const variant& getURLOrGeoJSON() const { - return urlOrGeoJSON; - } - -private: - Range getZoomRange() final; - std::unique_ptr createTile(const OverscaledTileID&, const UpdateParameters&) final; - - variant urlOrGeoJSON; - std::unique_ptr req; -}; - -} // namespace style -} // namespace mbgl diff --git a/src/mbgl/style/sources/geojson_source_impl.cpp b/src/mbgl/style/sources/geojson_source_impl.cpp new file mode 100644 index 0000000000..f58e0fc62b --- /dev/null +++ b/src/mbgl/style/sources/geojson_source_impl.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include + +namespace mbgl { +namespace style { + +std::unique_ptr GeoJSONSource::Impl::parseGeoJSON(const JSValue& value) { + using namespace mapbox::geojsonvt; + + Options options; + options.buffer = util::EXTENT / util::tileSize * 128; + options.extent = util::EXTENT; + + try { + return std::make_unique(Convert::convert(value, 0), options); + } catch (const std::exception& ex) { + Log::Error(Event::ParseStyle, "Failed to parse GeoJSON data: %s", ex.what()); + // Create an empty GeoJSON VT object to make sure we're not infinitely waiting for + // tiles to load. + return std::make_unique(std::vector{}, options); + } +} + +std::unique_ptr GeoJSONSource::Impl::parse(const std::string& id, const JSValue& value) { + // We should probably split this up to have URLs in the url property, and actual data + // in the data property. Until then, we're going to detect the content based on the + // object type. + if (!value.HasMember("data")) { + Log::Error(Event::ParseStyle, "GeoJSON source must have a data value"); + return nullptr; + } + + const JSValue& dataVal = value["data"]; + if (dataVal.IsString()) { + return std::make_unique([&] (Source& base) { + return std::make_unique(id, base, std::string(dataVal.GetString(), dataVal.GetStringLength())); + }); + } else if (dataVal.IsObject()) { + return std::make_unique([&] (Source& base) { + return std::make_unique(id, base, parseGeoJSON(dataVal)); + }); + } else { + Log::Error(Event::ParseStyle, "GeoJSON data must be a URL or an object"); + return nullptr; + } +} + +GeoJSONSource::Impl::Impl(std::string id_, Source& base_, variant urlOrGeoJSON_) + : Source::Impl(SourceType::GeoJSON, std::move(id_), base_), + urlOrGeoJSON(std::move(urlOrGeoJSON_)) { +} + +GeoJSONSource::Impl::~Impl() = default; + +void GeoJSONSource::Impl::load(FileSource& fileSource) { + if (urlOrGeoJSON.is()) { + loaded = true; + return; + } + + if (req) { + return; + } + + const std::string& url = urlOrGeoJSON.get(); + req = fileSource.request(Resource::source(url), [this](Response res) { + if (res.error) { + observer->onSourceError(base, std::make_exception_ptr(std::runtime_error(res.error->message))); + } else if (res.notModified) { + return; + } else if (res.noContent) { + observer->onSourceError(base, std::make_exception_ptr(std::runtime_error("unexpectedly empty GeoJSON"))); + } else { + rapidjson::GenericDocument, rapidjson::CrtAllocator> d; + d.Parse<0>(res.data->c_str()); + + if (d.HasParseError()) { + std::stringstream message; + message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError()); + observer->onSourceError(base, std::make_exception_ptr(std::runtime_error(message.str()))); + return; + } + + invalidateTiles(); + + urlOrGeoJSON = parseGeoJSON(d); + loaded = true; + + observer->onSourceLoaded(base); + } + }); +} + +Range GeoJSONSource::Impl::getZoomRange() { + assert(loaded); + return { 0, urlOrGeoJSON.get()->options.maxZoom }; +} + +std::unique_ptr GeoJSONSource::Impl::createTile(const OverscaledTileID& tileID, + const UpdateParameters& parameters) { + assert(loaded); + return std::make_unique(tileID, base.getID(), parameters, *urlOrGeoJSON.get()); +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/geojson_source_impl.hpp b/src/mbgl/style/sources/geojson_source_impl.hpp new file mode 100644 index 0000000000..350045b27c --- /dev/null +++ b/src/mbgl/style/sources/geojson_source_impl.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include +#include + +namespace mapbox { +namespace geojsonvt { +class GeoJSONVT; +} // namespace geojsonvt +} // namespace mapbox + +namespace mbgl { + +class AsyncRequest; + +namespace style { + +class GeoJSONSource::Impl : public Source::Impl { +public: + using GeoJSON = std::unique_ptr; + + static std::unique_ptr parse(const std::string& id, const JSValue&); + static GeoJSON parseGeoJSON(const JSValue&); + + Impl(std::string id, Source&, + variant urlOrGeoJSON); + ~Impl() final; + + void load(FileSource&) final; + + uint16_t getTileSize() const final { + return util::tileSize; + } + + const variant& getURLOrGeoJSON() const { + return urlOrGeoJSON; + } + +private: + Range getZoomRange() final; + std::unique_ptr createTile(const OverscaledTileID&, const UpdateParameters&) final; + + variant urlOrGeoJSON; + std::unique_ptr req; +}; + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/raster_source.cpp b/src/mbgl/style/sources/raster_source.cpp deleted file mode 100644 index 9f873a3065..0000000000 --- a/src/mbgl/style/sources/raster_source.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include - -namespace mbgl { -namespace style { - -std::unique_ptr RasterSource::parse(std::string id, const JSValue& value) { - optional> urlOrTileset = TileSource::parseURLOrTileset(value); - if (!urlOrTileset) { - return nullptr; - } - - uint16_t tileSize = util::tileSize; - if (value.HasMember("tileSize")) { - const JSValue& tileSizeVal = value["tileSize"]; - if (tileSizeVal.IsNumber() && tileSizeVal.GetUint64() <= std::numeric_limits::max()) { - tileSize = tileSizeVal.GetUint64(); - } else { - Log::Error(Event::ParseStyle, "invalid tileSize"); - return nullptr; - } - } - - return std::make_unique(std::move(id), std::move(*urlOrTileset), tileSize); -} - -RasterSource::RasterSource(std::string id_, - variant urlOrTileset_, - uint16_t tileSize_) - : TileSource(SourceType::Raster, std::move(id_), std::move(urlOrTileset_), tileSize_) { -} - -std::unique_ptr RasterSource::createTile(const OverscaledTileID& tileID, - const UpdateParameters& parameters) { - return std::make_unique(tileID, parameters, tileset); -} - -} // namespace style -} // namespace mbgl diff --git a/src/mbgl/style/sources/raster_source.hpp b/src/mbgl/style/sources/raster_source.hpp deleted file mode 100644 index 27b2276df5..0000000000 --- a/src/mbgl/style/sources/raster_source.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -namespace mbgl { -namespace style { - -class RasterSource : public TileSource { -public: - static std::unique_ptr parse(std::string id, const JSValue&); - - RasterSource(std::string id, variant, uint16_t tileSize); - -private: - std::unique_ptr createTile(const OverscaledTileID&, const UpdateParameters&) final; -}; - -} // namespace style -} // namespace mbgl diff --git a/src/mbgl/style/sources/raster_source_impl.cpp b/src/mbgl/style/sources/raster_source_impl.cpp new file mode 100644 index 0000000000..a6e19b4757 --- /dev/null +++ b/src/mbgl/style/sources/raster_source_impl.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +namespace mbgl { +namespace style { + +std::unique_ptr RasterSource::Impl::parse(std::string id, const JSValue& value) { + optional> urlOrTileset = TileSourceImpl::parseURLOrTileset(value); + if (!urlOrTileset) { + return nullptr; + } + + uint16_t tileSize = util::tileSize; + if (value.HasMember("tileSize")) { + const JSValue& tileSizeVal = value["tileSize"]; + if (tileSizeVal.IsNumber() && tileSizeVal.GetUint64() <= std::numeric_limits::max()) { + tileSize = tileSizeVal.GetUint64(); + } else { + Log::Error(Event::ParseStyle, "invalid tileSize"); + return nullptr; + } + } + + return std::make_unique(std::move(id), std::move(*urlOrTileset), tileSize); +} + +RasterSource::Impl::Impl(std::string id_, Source& base_, + variant urlOrTileset_, + uint16_t tileSize_) + : TileSourceImpl(SourceType::Raster, std::move(id_), base_, std::move(urlOrTileset_), tileSize_) { +} + +std::unique_ptr RasterSource::Impl::createTile(const OverscaledTileID& tileID, + const UpdateParameters& parameters) { + return std::make_unique(tileID, parameters, tileset); +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/raster_source_impl.hpp b/src/mbgl/style/sources/raster_source_impl.hpp new file mode 100644 index 0000000000..2222b13082 --- /dev/null +++ b/src/mbgl/style/sources/raster_source_impl.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace mbgl { +namespace style { + +class RasterSource::Impl : public TileSourceImpl { +public: + static std::unique_ptr parse(std::string id, const JSValue&); + + Impl(std::string id, Source&, variant, uint16_t tileSize); + +private: + std::unique_ptr createTile(const OverscaledTileID&, const UpdateParameters&) final; +}; + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/vector_source.cpp b/src/mbgl/style/sources/vector_source.cpp deleted file mode 100644 index 3f8f840b38..0000000000 --- a/src/mbgl/style/sources/vector_source.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -namespace mbgl { -namespace style { - -std::unique_ptr VectorSource::parse(std::string id, const JSValue& value) { - optional> urlOrTileset = TileSource::parseURLOrTileset(value); - if (!urlOrTileset) { - return nullptr; - } - return std::make_unique(std::move(id), std::move(*urlOrTileset)); -} - -VectorSource::VectorSource(std::string id_, variant urlOrTileset_) - : TileSource(SourceType::Vector, std::move(id_), std::move(urlOrTileset_), util::tileSize) { -} - -std::unique_ptr VectorSource::createTile(const OverscaledTileID& tileID, - const UpdateParameters& parameters) { - return std::make_unique(tileID, id, parameters, tileset); -} - -} // namespace style -} // namespace mbgl diff --git a/src/mbgl/style/sources/vector_source.hpp b/src/mbgl/style/sources/vector_source.hpp deleted file mode 100644 index ced7d80471..0000000000 --- a/src/mbgl/style/sources/vector_source.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -namespace mbgl { -namespace style { - -class VectorSource : public TileSource { -public: - static std::unique_ptr parse(std::string id, const JSValue&); - - VectorSource(std::string id, variant); - -private: - std::unique_ptr createTile(const OverscaledTileID&, const UpdateParameters&) final; -}; - -} // namespace style -} // namespace mbgl diff --git a/src/mbgl/style/sources/vector_source_impl.cpp b/src/mbgl/style/sources/vector_source_impl.cpp new file mode 100644 index 0000000000..28e14f3e16 --- /dev/null +++ b/src/mbgl/style/sources/vector_source_impl.cpp @@ -0,0 +1,25 @@ +#include +#include + +namespace mbgl { +namespace style { + +std::unique_ptr VectorSource::Impl::parse(std::string id, const JSValue& value) { + optional> urlOrTileset = TileSourceImpl::parseURLOrTileset(value); + if (!urlOrTileset) { + return nullptr; + } + return std::make_unique(std::move(id), std::move(*urlOrTileset)); +} + +VectorSource::Impl::Impl(std::string id_, Source& base_, variant urlOrTileset_) + : TileSourceImpl(SourceType::Vector, std::move(id_), base_, std::move(urlOrTileset_), util::tileSize) { +} + +std::unique_ptr VectorSource::Impl::createTile(const OverscaledTileID& tileID, + const UpdateParameters& parameters) { + return std::make_unique(tileID, base.getID(), parameters, tileset); +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/vector_source_impl.hpp b/src/mbgl/style/sources/vector_source_impl.hpp new file mode 100644 index 0000000000..4a6703e5c0 --- /dev/null +++ b/src/mbgl/style/sources/vector_source_impl.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace mbgl { +namespace style { + +class VectorSource::Impl : public TileSourceImpl { +public: + static std::unique_ptr parse(std::string id, const JSValue&); + + Impl(std::string id, Source&, variant); + +private: + std::unique_ptr createTile(const OverscaledTileID&, const UpdateParameters&) final; +}; + +} // namespace style +} // namespace mbgl -- cgit v1.2.1