[core] merge similar implementation of VectorTileSource and ImageTileSource
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
22
src/mbgl/tile/file_based_tile_source.hpp
Normal file
22
src/mbgl/tile/file_based_tile_source.hpp
Normal file
@@ -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
|
||||
27
src/mbgl/tile/file_based_tile_source_impl.hpp
Normal file
27
src/mbgl/tile/file_based_tile_source_impl.hpp
Normal file
@@ -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
|
||||
@@ -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), {}, {});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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_;
|
||||
|
||||
|
||||
@@ -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_);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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_;
|
||||
|
||||
|
||||
@@ -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_);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user