diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-01-28 16:29:54 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-01-29 14:50:15 -0800 |
commit | b41a73e1c1dfea982df4a0326d45a48babc22dd4 (patch) | |
tree | fc744d5953ad6ecf8e52d5653c499760b148cb5c /src | |
parent | 849e8b32b4b2febc63ad3df8539b483ccc67ac63 (diff) | |
download | qtlocation-mapboxgl-b41a73e1c1dfea982df4a0326d45a48babc22dd4.tar.gz |
[core] Rationalize Resource initialization
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/map/map_context.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/map/raster_tile_data.cpp | 15 | ||||
-rw-r--r-- | src/mbgl/map/source.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/map/vector_tile.cpp | 14 | ||||
-rw-r--r-- | src/mbgl/sprite/sprite_store.cpp | 36 | ||||
-rw-r--r-- | src/mbgl/storage/resource.cpp | 82 | ||||
-rw-r--r-- | src/mbgl/text/glyph_pbf.cpp | 9 | ||||
-rw-r--r-- | src/mbgl/util/url.cpp | 24 | ||||
-rw-r--r-- | src/mbgl/util/url.hpp | 4 |
9 files changed, 102 insertions, 86 deletions
diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 821741e87e..dcb4bd0a9e 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -104,7 +104,7 @@ void MapContext::setStyleURL(const std::string& url) { } FileSource* fs = util::ThreadContext::getFileSource(); - styleRequest = fs->request({ Resource::Kind::Style, styleURL }, [this, base](Response res) { + styleRequest = fs->request(Resource::style(styleURL), [this, base](Response res) { if (res.error) { if (res.error->reason == Response::Error::Reason::NotFound && util::mapbox::isMapboxURL(styleURL)) { diff --git a/src/mbgl/map/raster_tile_data.cpp b/src/mbgl/map/raster_tile_data.cpp index fa0bd298b1..f9f5480197 100644 --- a/src/mbgl/map/raster_tile_data.cpp +++ b/src/mbgl/map/raster_tile_data.cpp @@ -5,7 +5,6 @@ #include <mbgl/storage/file_source.hpp> #include <mbgl/util/worker.hpp> #include <mbgl/util/work_request.hpp> -#include <mbgl/util/url.hpp> using namespace mbgl; @@ -20,19 +19,7 @@ RasterTileData::RasterTileData(const TileID& id_, worker(worker_) { state = State::loading; - Resource resource { - Resource::Kind::Tile, - util::templateTileURL(urlTemplate, id, pixelRatio) - }; - - resource.tileData = Resource::TileData { - urlTemplate, - pixelRatio, - id.x, - id.y, - id.z - }; - + const Resource resource = Resource::tile(urlTemplate, pixelRatio, id.x, id.y, id.sourceZ); req = util::ThreadContext::getFileSource()->request(resource, [callback, this](Response res) { if (res.error) { std::exception_ptr error; diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index fbe276f324..65b9de7abb 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -91,7 +91,7 @@ void Source::load() { // URL may either be a TileJSON file, or a GeoJSON file. FileSource* fs = util::ThreadContext::getFileSource(); - req = fs->request({ Resource::Kind::Source, url }, [this](Response res) { + req = fs->request(Resource::source(url), [this](Response res) { if (res.error) { observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message))); return; diff --git a/src/mbgl/map/vector_tile.cpp b/src/mbgl/map/vector_tile.cpp index cfe78d228d..f837714b8d 100644 --- a/src/mbgl/map/vector_tile.cpp +++ b/src/mbgl/map/vector_tile.cpp @@ -185,19 +185,7 @@ VectorTileMonitor::VectorTileMonitor(const TileID& tileID_, float pixelRatio_, c } std::unique_ptr<FileRequest> VectorTileMonitor::monitorTile(const GeometryTileMonitor::Callback& callback) { - Resource resource { - Resource::Kind::Tile, - util::templateTileURL(urlTemplate, tileID, pixelRatio) - }; - - resource.tileData = Resource::TileData { - urlTemplate, - pixelRatio, - tileID.x, - tileID.y, - tileID.z - }; - + const Resource resource = Resource::tile(urlTemplate, pixelRatio, tileID.x, tileID.y, tileID.sourceZ); return util::ThreadContext::getFileSource()->request(resource, [callback, this](Response res) { if (res.notModified) { // We got the same data again. Abort early. diff --git a/src/mbgl/sprite/sprite_store.cpp b/src/mbgl/sprite/sprite_store.cpp index e051b969a1..15a345cc4d 100644 --- a/src/mbgl/sprite/sprite_store.cpp +++ b/src/mbgl/sprite/sprite_store.cpp @@ -32,14 +32,10 @@ void SpriteStore::setURL(const std::string& url) { return; } - std::string spriteURL(url + (pixelRatio > 1 ? "@2x" : "") + ".png"); - std::string jsonURL(url + (pixelRatio > 1 ? "@2x" : "") + ".json"); - loader = std::make_unique<Loader>(); FileSource* fs = util::ThreadContext::getFileSource(); - loader->jsonRequest = fs->request({ Resource::Kind::SpriteJSON, jsonURL }, - [this, jsonURL](Response res) { + loader->jsonRequest = fs->request(Resource::spriteJSON(url, pixelRatio), [this](Response res) { if (res.error) { observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message))); return; @@ -57,24 +53,22 @@ void SpriteStore::setURL(const std::string& url) { } }); - loader->spriteRequest = - fs->request({ Resource::Kind::SpriteImage, spriteURL }, - [this, spriteURL](Response res) { - if (res.error) { - observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message))); - return; - } + loader->spriteRequest = fs->request(Resource::spriteImage(url, pixelRatio), [this](Response res) { + if (res.error) { + observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message))); + return; + } - if (res.notModified) { - // We got the same data back as last time. Abort early. - return; - } + if (res.notModified) { + // We got the same data back as last time. Abort early. + return; + } - if (!loader->image || *loader->image != *res.data) { - loader->image = res.data; - emitSpriteLoadedIfComplete(); - } - }); + if (!loader->image || *loader->image != *res.data) { + loader->image = res.data; + emitSpriteLoadedIfComplete(); + } + }); } void SpriteStore::emitSpriteLoadedIfComplete() { diff --git a/src/mbgl/storage/resource.cpp b/src/mbgl/storage/resource.cpp new file mode 100644 index 0000000000..9cdedb59eb --- /dev/null +++ b/src/mbgl/storage/resource.cpp @@ -0,0 +1,82 @@ +#include <mbgl/storage/resource.hpp> +#include <mbgl/util/string.hpp> +#include <mbgl/util/token.hpp> +#include <mbgl/util/url.hpp> + +namespace mbgl { + +Resource Resource::style(const std::string& url) { + return Resource { + Resource::Kind::Style, + url + }; +} + +Resource Resource::source(const std::string& url) { + return Resource { + Resource::Kind::Source, + url + }; +} + +Resource Resource::spriteImage(const std::string& base, float pixelRatio) { + return Resource { + Resource::Kind::SpriteImage, + base + (pixelRatio > 1 ? "@2x" : "") + ".png" + }; +} + +Resource Resource::spriteJSON(const std::string& base, float pixelRatio) { + return Resource { + Resource::Kind::SpriteJSON, + base + (pixelRatio > 1 ? "@2x" : "") + ".json" + }; +} + +Resource Resource::glyphs(const std::string& urlTemplate, const std::string& fontStack, const std::pair<uint16_t, uint16_t>& glyphRange) { + return Resource { + Resource::Kind::Glyphs, + util::replaceTokens(urlTemplate, [&](const std::string& token) { + if (token == "fontstack") { + return util::percentEncode(fontStack); + } else if (token == "range") { + return util::toString(glyphRange.first) + "-" + util::toString(glyphRange.second); + } else { + return std::string(); + } + }) + }; +} + +Resource Resource::tile(const std::string& urlTemplate, float pixelRatio, int32_t x, int32_t y, int8_t z) { + return Resource { + Resource::Kind::Tile, + util::replaceTokens(urlTemplate, [&](const std::string& token) { + if (token == "z") { + return util::toString(z); + } else if (token == "x") { + return util::toString(x); + } else if (token == "y") { + return util::toString(y); + } else if (token == "prefix") { + std::string prefix{ 2 }; + prefix[0] = "0123456789abcdef"[x % 16]; + prefix[1] = "0123456789abcdef"[y % 16]; + return prefix; + } else if (token == "ratio") { + return std::string(pixelRatio > 1.0 ? "@2x" : ""); + } else { + return std::string(); + } + }), + Resource::TileData { + urlTemplate, + pixelRatio, + x, + y, + z + } + }; +} + +} diff --git a/src/mbgl/text/glyph_pbf.cpp b/src/mbgl/text/glyph_pbf.cpp index 37b7cfc9fb..0dfed6b430 100644 --- a/src/mbgl/text/glyph_pbf.cpp +++ b/src/mbgl/text/glyph_pbf.cpp @@ -67,15 +67,8 @@ GlyphPBF::GlyphPBF(GlyphStore* store, GlyphStore::Observer* observer_) : parsed(false), observer(observer_) { - // Load the glyph set URL - std::string url = util::replaceTokens(store->getURL(), [&](const std::string &name) -> std::string { - if (name == "fontstack") return util::percentEncode(fontStack); - if (name == "range") return util::toString(glyphRange.first) + "-" + util::toString(glyphRange.second); - return ""; - }); - FileSource* fs = util::ThreadContext::getFileSource(); - req = fs->request({ Resource::Kind::Glyphs, url }, [this, store, fontStack, glyphRange](Response res) { + req = fs->request(Resource::glyphs(store->getURL(), fontStack, glyphRange), [this, store, fontStack, glyphRange](Response res) { if (res.error) { observer->onGlyphsError(fontStack, glyphRange, std::make_exception_ptr(std::runtime_error(res.error->message))); return; diff --git a/src/mbgl/util/url.cpp b/src/mbgl/util/url.cpp index 85d8bc5bd0..bf6fc70ff5 100644 --- a/src/mbgl/util/url.cpp +++ b/src/mbgl/util/url.cpp @@ -1,7 +1,4 @@ #include <mbgl/util/url.hpp> -#include <mbgl/util/token.hpp> -#include <mbgl/util/string.hpp> -#include <mbgl/map/tile_id.hpp> #include <cctype> #include <iomanip> @@ -50,26 +47,5 @@ std::string percentDecode(const std::string& input) { return decoded; } -std::string templateTileURL(const std::string& url, const TileID& id, float pixelRatio) { - return util::replaceTokens(url, [&](const std::string& token) -> std::string { - if (token == "z") { - return util::toString(id.sourceZ); - } else if (token == "x") { - return util::toString(id.x); - } else if (token == "y") { - return util::toString(id.y); - } else if (token == "prefix") { - std::string prefix{ 2 }; - prefix[0] = "0123456789abcdef"[id.x % 16]; - prefix[1] = "0123456789abcdef"[id.y % 16]; - return prefix; - } else if (token == "ratio") { - return pixelRatio > 1.0 ? "@2x" : ""; - } else { - return ""; - } - }); -} - } // namespace util } // namespace mbgl diff --git a/src/mbgl/util/url.hpp b/src/mbgl/util/url.hpp index 550fff8aa1..8baa94be1d 100644 --- a/src/mbgl/util/url.hpp +++ b/src/mbgl/util/url.hpp @@ -5,15 +5,11 @@ namespace mbgl { -class TileID; - namespace util { std::string percentEncode(const std::string&); std::string percentDecode(const std::string&); -std::string templateTileURL(const std::string& url, const TileID& id, float pixelRatio = 1); - } // namespace util } // namespace mbgl |