diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2016-05-23 16:38:28 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2016-06-10 12:42:14 +0200 |
commit | 4357fed93bedfa4db675bfcae7bddfa07627ea56 (patch) | |
tree | c4059550c2f498d0d74b6c5620e4731188a0173b | |
parent | 971807d3b09892dbb6901461d1ce9adb3552a22d (diff) | |
download | qtlocation-mapboxgl-4357fed93bedfa4db675bfcae7bddfa07627ea56.tar.gz |
[core] merge similar implementation of VectorTileSource and ImageTileSource
-rw-r--r-- | src/mbgl/tile/annotation_tile_source.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/tile/annotation_tile_source.hpp | 2 | ||||
-rw-r--r-- | src/mbgl/tile/file_based_tile_source.hpp | 22 | ||||
-rw-r--r-- | src/mbgl/tile/file_based_tile_source_impl.hpp | 27 | ||||
-rw-r--r-- | src/mbgl/tile/geojson_tile_source.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/tile/geojson_tile_source.hpp | 2 | ||||
-rw-r--r-- | src/mbgl/tile/geometry_tile_data.cpp | 12 | ||||
-rw-r--r-- | src/mbgl/tile/geometry_tile_data.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/tile/image_tile_source.cpp | 16 | ||||
-rw-r--r-- | src/mbgl/tile/image_tile_source.hpp | 13 | ||||
-rw-r--r-- | src/mbgl/tile/raster_tile_data.cpp | 14 | ||||
-rw-r--r-- | src/mbgl/tile/raster_tile_data.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/tile/tile_source.hpp | 14 | ||||
-rw-r--r-- | src/mbgl/tile/vector_tile_source.cpp | 23 | ||||
-rw-r--r-- | src/mbgl/tile/vector_tile_source.hpp | 15 |
15 files changed, 101 insertions, 73 deletions
diff --git a/src/mbgl/tile/annotation_tile_source.cpp b/src/mbgl/tile/annotation_tile_source.cpp index 143bcc23f5..1d638d18f5 100644 --- a/src/mbgl/tile/annotation_tile_source.cpp +++ b/src/mbgl/tile/annotation_tile_source.cpp @@ -18,7 +18,7 @@ AnnotationTileSource::~AnnotationTileSource() { } void AnnotationTileSource::update(std::unique_ptr<GeometryTile> tile) { - tileData.setData(nullptr, std::move(tile), {}, {}); + tileData.setData(std::move(tile), {}, {}); } } // namespace mbgl diff --git a/src/mbgl/tile/annotation_tile_source.hpp b/src/mbgl/tile/annotation_tile_source.hpp index 8d7a1d8a1f..c06ee4a374 100644 --- a/src/mbgl/tile/annotation_tile_source.hpp +++ b/src/mbgl/tile/annotation_tile_source.hpp @@ -10,7 +10,7 @@ namespace mbgl { class AnnotationManager; class GeometryTile; -class AnnotationTileSource : public GeometryTileSource { +class AnnotationTileSource final : public GeometryTileSource { public: AnnotationTileSource(GeometryTileData&, const OverscaledTileID&, AnnotationManager&); ~AnnotationTileSource(); diff --git a/src/mbgl/tile/file_based_tile_source.hpp b/src/mbgl/tile/file_based_tile_source.hpp new file mode 100644 index 0000000000..e4099ed316 --- /dev/null +++ b/src/mbgl/tile/file_based_tile_source.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include <mbgl/storage/resource.hpp> + +namespace mbgl { + +class FileSource; +class AsyncRequest; + +template <typename T, typename I> +class FileBasedTileSource : public T { +protected: + FileBasedTileSource(typename T::data_type&, const Resource&, FileSource&); + virtual ~FileBasedTileSource() = default; + +protected: + const Resource resource; + FileSource& fileSource; + std::unique_ptr<AsyncRequest> request; +}; + +} // namespace mbgl diff --git a/src/mbgl/tile/file_based_tile_source_impl.hpp b/src/mbgl/tile/file_based_tile_source_impl.hpp new file mode 100644 index 0000000000..1527f3b13a --- /dev/null +++ b/src/mbgl/tile/file_based_tile_source_impl.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include <mbgl/tile/file_based_tile_source.hpp> +#include <mbgl/storage/file_source.hpp> + +namespace mbgl { + +template <typename T, typename I> +FileBasedTileSource<T, I>::FileBasedTileSource(typename T::data_type& tileData_, + const Resource& resource_, + FileSource& fileSource_) + : T(tileData_), resource(resource_), fileSource(fileSource_) { + request = fileSource.request(resource, [this](Response res) { + if (res.error) { + T::tileData.setError(std::make_exception_ptr(std::runtime_error(res.error->message))); + } else if (res.notModified) { + return; + } else if (res.noContent) { + T::tileData.setData(nullptr, res.modified, res.expires); + } else { + + T::tileData.setData(I::parseData(res.data), res.modified, res.expires); + } + }); +} + +} // namespace mbgl diff --git a/src/mbgl/tile/geojson_tile_source.cpp b/src/mbgl/tile/geojson_tile_source.cpp index 3daf7cd02e..fb526fd9fb 100644 --- a/src/mbgl/tile/geojson_tile_source.cpp +++ b/src/mbgl/tile/geojson_tile_source.cpp @@ -92,7 +92,7 @@ void GeoJSONTileSource::update() { if (geojsonvt) { auto tile = convertTile( geojsonvt->getTile(tileID.canonical.z, tileID.canonical.x, tileID.canonical.y)); - tileData.setData(nullptr, std::move(tile), {}, {}); + tileData.setData(std::move(tile), {}, {}); } } diff --git a/src/mbgl/tile/geojson_tile_source.hpp b/src/mbgl/tile/geojson_tile_source.hpp index bfa5f87252..faae757eb4 100644 --- a/src/mbgl/tile/geojson_tile_source.hpp +++ b/src/mbgl/tile/geojson_tile_source.hpp @@ -14,7 +14,7 @@ class GeoJSONVT; namespace mbgl { -class GeoJSONTileSource : public GeometryTileSource { +class GeoJSONTileSource final : public GeometryTileSource { public: GeoJSONTileSource(GeometryTileData&, mapbox::geojsonvt::GeoJSONVT*, const OverscaledTileID&); virtual ~GeoJSONTileSource(); diff --git a/src/mbgl/tile/geometry_tile_data.cpp b/src/mbgl/tile/geometry_tile_data.cpp index aefd327ff0..1f1bca6530 100644 --- a/src/mbgl/tile/geometry_tile_data.cpp +++ b/src/mbgl/tile/geometry_tile_data.cpp @@ -29,15 +29,13 @@ GeometryTileData::GeometryTileData(const OverscaledTileID& id_, mode_) { } -void GeometryTileData::setData(std::exception_ptr err, - std::unique_ptr<GeometryTile> tile, +void GeometryTileData::setError(std::exception_ptr err) { + observer->onTileError(*this, err); +} + +void GeometryTileData::setData(std::unique_ptr<GeometryTile> tile, optional<Timestamp> modified_, optional<Timestamp> expires_) { - if (err) { - observer->onTileError(*this, err); - return; - } - modified = modified_; expires = expires_; diff --git a/src/mbgl/tile/geometry_tile_data.hpp b/src/mbgl/tile/geometry_tile_data.hpp index d4eef956a5..4e13278dc5 100644 --- a/src/mbgl/tile/geometry_tile_data.hpp +++ b/src/mbgl/tile/geometry_tile_data.hpp @@ -29,8 +29,9 @@ public: ~GeometryTileData(); - void setData(std::exception_ptr err, - std::unique_ptr<GeometryTile> tile, + void setError(std::exception_ptr err); + + void setData(std::unique_ptr<GeometryTile> tile, optional<Timestamp> modified_, optional<Timestamp> expires_); diff --git a/src/mbgl/tile/image_tile_source.cpp b/src/mbgl/tile/image_tile_source.cpp index e552c0e315..477c00dd19 100644 --- a/src/mbgl/tile/image_tile_source.cpp +++ b/src/mbgl/tile/image_tile_source.cpp @@ -1,25 +1,13 @@ #include <mbgl/tile/image_tile_source.hpp> +#include <mbgl/tile/file_based_tile_source_impl.hpp> #include <mbgl/tile/raster_tile_data.hpp> -#include <mbgl/storage/file_source.hpp> namespace mbgl { ImageTileSource::ImageTileSource(RasterTileData& tileData_, const Resource& resource_, FileSource& fileSource_) - : RasterTileSource(tileData_), resource(resource_), fileSource(fileSource_) { - request = fileSource.request(resource, [this](Response res) { - if (res.error) { - tileData.setData(std::make_exception_ptr(std::runtime_error(res.error->message)), - nullptr, res.modified, res.expires); - } else if (res.notModified) { - return; - } else if (res.noContent) { - tileData.setData(nullptr, nullptr, res.modified, res.expires); - } else { - tileData.setData(nullptr, res.data, res.modified, res.expires); - } - }); + : FileBasedTileSource<RasterTileSource, ImageTileSource>(tileData_, resource_, fileSource_) { } } // namespace mbgl diff --git a/src/mbgl/tile/image_tile_source.hpp b/src/mbgl/tile/image_tile_source.hpp index 8b4e9c0980..56b3f7ba87 100644 --- a/src/mbgl/tile/image_tile_source.hpp +++ b/src/mbgl/tile/image_tile_source.hpp @@ -1,23 +1,22 @@ #pragma once #include <mbgl/tile/tile_source.hpp> -#include <mbgl/tile/tile_id.hpp> -#include <mbgl/storage/resource.hpp> +#include <mbgl/tile/file_based_tile_source.hpp> namespace mbgl { class FileSource; class AsyncRequest; -class ImageTileSource : public RasterTileSource { +class ImageTileSource final : public FileBasedTileSource<RasterTileSource, ImageTileSource> { public: ImageTileSource(RasterTileData&, const Resource&, FileSource&); virtual ~ImageTileSource() = default; -private: - const Resource resource; - FileSource& fileSource; - std::unique_ptr<AsyncRequest> request; + static std::shared_ptr<const std::string> parseData(std::shared_ptr<const std::string> data) { + return data; + } + }; } // namespace mbgl diff --git a/src/mbgl/tile/raster_tile_data.cpp b/src/mbgl/tile/raster_tile_data.cpp index 4702f60f48..dade0357c1 100644 --- a/src/mbgl/tile/raster_tile_data.cpp +++ b/src/mbgl/tile/raster_tile_data.cpp @@ -17,15 +17,13 @@ RasterTileData::RasterTileData(const OverscaledTileID& id_, worker(worker_) { } -void RasterTileData::setData(std::exception_ptr err, - std::shared_ptr<const std::string> data, - optional<Timestamp> modified_, - optional<Timestamp> expires_) { - if (err) { - observer->onTileError(*this, err); - return; - } +void RasterTileData::setError(std::exception_ptr err) { + observer->onTileError(*this, err); +} +void RasterTileData::setData(std::shared_ptr<const std::string> data, + optional<Timestamp> modified_, + optional<Timestamp> expires_) { modified = modified_; expires = expires_; diff --git a/src/mbgl/tile/raster_tile_data.hpp b/src/mbgl/tile/raster_tile_data.hpp index 17e1465ebc..f5c82d4e0d 100644 --- a/src/mbgl/tile/raster_tile_data.hpp +++ b/src/mbgl/tile/raster_tile_data.hpp @@ -21,8 +21,9 @@ public: Worker&); ~RasterTileData(); - void setData(std::exception_ptr err, - std::shared_ptr<const std::string> data, + void setError(std::exception_ptr err); + + void setData(std::shared_ptr<const std::string> data, optional<Timestamp> modified_, optional<Timestamp> expires_); diff --git a/src/mbgl/tile/tile_source.hpp b/src/mbgl/tile/tile_source.hpp index 294ddd0d6b..ba4555129d 100644 --- a/src/mbgl/tile/tile_source.hpp +++ b/src/mbgl/tile/tile_source.hpp @@ -13,25 +13,31 @@ class GeometryTileData; class RasterTileData; class GeometryTileSource : public TileSource { +public: + using data_type = GeometryTileData; + protected: - GeometryTileSource(GeometryTileData& tileData_) : tileData(tileData_) {} + GeometryTileSource(data_type& tileData_) : tileData(tileData_) {} public: virtual ~GeometryTileSource() = default; protected: - GeometryTileData& tileData; + data_type& tileData; }; class RasterTileSource : public TileSource { +public: + using data_type = RasterTileData; + protected: - RasterTileSource(RasterTileData& tileData_) : tileData(tileData_) {}; + RasterTileSource(data_type& tileData_) : tileData(tileData_) {}; public: virtual ~RasterTileSource() = default; protected: - RasterTileData& tileData; + data_type& tileData; }; } // namespace mbgl diff --git a/src/mbgl/tile/vector_tile_source.cpp b/src/mbgl/tile/vector_tile_source.cpp index 2ee0827b14..2439b5f011 100644 --- a/src/mbgl/tile/vector_tile_source.cpp +++ b/src/mbgl/tile/vector_tile_source.cpp @@ -1,27 +1,18 @@ #include <mbgl/tile/vector_tile_source.hpp> -#include <mbgl/tile/vector_tile.hpp> +#include <mbgl/tile/file_based_tile_source_impl.hpp> #include <mbgl/tile/geometry_tile_data.hpp> -#include <mbgl/storage/file_source.hpp> +#include <mbgl/tile/vector_tile.hpp> namespace mbgl { VectorTileSource::VectorTileSource(GeometryTileData& tileData_, const Resource& resource_, FileSource& fileSource_) - : GeometryTileSource(tileData_), resource(resource_), fileSource(fileSource_) { - request = fileSource.request(resource, [this](Response res) { - if (res.error) { - tileData.setData(std::make_exception_ptr(std::runtime_error(res.error->message)), - nullptr, res.modified, res.expires); - } else if (res.notModified) { - return; - } else if (res.noContent) { - tileData.setData(nullptr, nullptr, res.modified, res.expires); - } else { - tileData.setData(nullptr, std::make_unique<VectorTile>(res.data), res.modified, - res.expires); - } - }); + : FileBasedTileSource<GeometryTileSource, VectorTileSource>(tileData_, resource_, fileSource_) { +} + +std::unique_ptr<GeometryTile> VectorTileSource::parseData(std::shared_ptr<const std::string> data) { + return data ? std::make_unique<VectorTile>(data) : nullptr; } } // namespace mbgl diff --git a/src/mbgl/tile/vector_tile_source.hpp b/src/mbgl/tile/vector_tile_source.hpp index fb64c7512e..a73a80ae12 100644 --- a/src/mbgl/tile/vector_tile_source.hpp +++ b/src/mbgl/tile/vector_tile_source.hpp @@ -1,23 +1,20 @@ #pragma once #include <mbgl/tile/tile_source.hpp> -#include <mbgl/tile/tile_id.hpp> -#include <mbgl/storage/resource.hpp> +#include <mbgl/tile/file_based_tile_source.hpp> + +#include <memory> namespace mbgl { -class FileSource; -class AsyncRequest; +class GeometryTile; -class VectorTileSource : public GeometryTileSource { +class VectorTileSource final : public FileBasedTileSource<GeometryTileSource, VectorTileSource> { public: VectorTileSource(GeometryTileData&, const Resource&, FileSource&); virtual ~VectorTileSource() = default; -private: - const Resource resource; - FileSource& fileSource; - std::unique_ptr<AsyncRequest> request; + static std::unique_ptr<GeometryTile> parseData(std::shared_ptr<const std::string>); }; } // namespace mbgl |