From 19129cbd023345569b24f1b9863e0ee32ae61182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 17 Sep 2014 16:01:35 +0200 Subject: [PATCH] make SourceInfo object noncopyable and use a shared_pointer for it --- include/mbgl/map/raster_tile_data.hpp | 2 +- include/mbgl/map/source.hpp | 4 ++-- include/mbgl/map/tile_data.hpp | 4 ++-- include/mbgl/map/vector_tile_data.hpp | 2 +- include/mbgl/style/style_source.hpp | 7 ++++--- src/map/raster_tile_data.cpp | 2 +- src/map/source.cpp | 22 +++++++++++----------- src/map/tile_data.cpp | 6 +++--- src/map/tile_parser.cpp | 4 ++-- src/map/vector_tile_data.cpp | 4 ++-- src/style/style_parser.cpp | 10 +++++----- 11 files changed, 34 insertions(+), 33 deletions(-) diff --git a/include/mbgl/map/raster_tile_data.hpp b/include/mbgl/map/raster_tile_data.hpp index 329bbf463..14833c0d8 100644 --- a/include/mbgl/map/raster_tile_data.hpp +++ b/include/mbgl/map/raster_tile_data.hpp @@ -16,7 +16,7 @@ class RasterTileData : public TileData { friend class TileParser; public: - RasterTileData(Tile::ID id, Map &map, const SourceInfo &source); + RasterTileData(Tile::ID id, Map &map, const util::ptr &source); ~RasterTileData(); virtual void parse(); diff --git a/include/mbgl/map/source.hpp b/include/mbgl/map/source.hpp index 471165f37..e74ddb990 100644 --- a/include/mbgl/map/source.hpp +++ b/include/mbgl/map/source.hpp @@ -25,7 +25,7 @@ struct box; class Source : public std::enable_shared_from_this, private util::noncopyable { public: - Source(SourceInfo& info); + Source(const util::ptr& info); void load(Map &map); @@ -54,7 +54,7 @@ private: double getZoom(const TransformState &state) const; - SourceInfo& info; + util::ptr info; bool loaded = false; // Stores the time when this source was most recently updated. diff --git a/include/mbgl/map/tile_data.hpp b/include/mbgl/map/tile_data.hpp index 803f0447b..07cf19c5c 100644 --- a/include/mbgl/map/tile_data.hpp +++ b/include/mbgl/map/tile_data.hpp @@ -40,7 +40,7 @@ public: }; public: - TileData(Tile::ID id, Map &map, const SourceInfo &source); + TileData(Tile::ID id, Map &map, const util::ptr &source); ~TileData(); void request(); @@ -68,7 +68,7 @@ protected: Map ↦ public: - const SourceInfo &source; + util::ptr source; protected: std::unique_ptr req; diff --git a/include/mbgl/map/vector_tile_data.hpp b/include/mbgl/map/vector_tile_data.hpp index 3f80c0080..9de666c84 100644 --- a/include/mbgl/map/vector_tile_data.hpp +++ b/include/mbgl/map/vector_tile_data.hpp @@ -26,7 +26,7 @@ class VectorTileData : public TileData { friend class TileParser; public: - VectorTileData(Tile::ID id, Map &map, const SourceInfo &source); + VectorTileData(Tile::ID id, Map &map, const util::ptr &source); ~VectorTileData(); virtual void beforeParse(); diff --git a/include/mbgl/style/style_source.hpp b/include/mbgl/style/style_source.hpp index 4b89a6557..00c48431a 100644 --- a/include/mbgl/style/style_source.hpp +++ b/include/mbgl/style/style_source.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -12,7 +13,7 @@ namespace mbgl { class Source; -class SourceInfo { +class SourceInfo : private util::noncopyable { public: SourceType type = SourceType::Vector; std::string url; @@ -30,12 +31,12 @@ public: class StyleSource : public std::enable_shared_from_this { public: - SourceInfo info; + util::ptr info; bool enabled = false; util::ptr source; - StyleSource(const SourceInfo &info) + StyleSource(const util::ptr &info) : info(info) {} }; diff --git a/src/map/raster_tile_data.cpp b/src/map/raster_tile_data.cpp index 2f09a5fec..e7725b7ab 100644 --- a/src/map/raster_tile_data.cpp +++ b/src/map/raster_tile_data.cpp @@ -5,7 +5,7 @@ using namespace mbgl; -RasterTileData::RasterTileData(Tile::ID id, Map &map, const SourceInfo &source) +RasterTileData::RasterTileData(Tile::ID id, Map &map, const util::ptr &source) : TileData(id, map, source), bucket(map.getTexturepool(), properties) { } diff --git a/src/map/source.cpp b/src/map/source.cpp index 348d99d3b..36f1a71c8 100644 --- a/src/map/source.cpp +++ b/src/map/source.cpp @@ -20,7 +20,7 @@ namespace mbgl { -Source::Source(SourceInfo& info) +Source::Source(const util::ptr& info) : info(info) { } @@ -29,12 +29,12 @@ Source::Source(SourceInfo& info) // The reason this isn't part of the constructor is that calling shared_from_this() in // the constructor fails. void Source::load(Map& map) { - if (info.url.empty()) { + if (info->url.empty()) { loaded = true; return; } - std::string url = util::mapbox::normalizeSourceURL(info.url, map.getAccessToken()); + std::string url = util::mapbox::normalizeSourceURL(info->url, map.getAccessToken()); util::ptr source = shared_from_this(); map.getFileSource()->request(ResourceType::JSON, url)->onload([source, &map](const Response &res) { @@ -51,7 +51,7 @@ void Source::load(Map& map) { return; } - source->info.parseTileJSONProperties(d); + source->info->parseTileJSONProperties(d); source->loaded = true; map.update(); @@ -184,9 +184,9 @@ TileData::State Source::addTile(Map &map, const Tile::ID& id) { if (!new_tile.data) { // If we don't find working tile data, we're just going to load it. - if (info.type == SourceType::Vector) { + if (info->type == SourceType::Vector) { new_tile.data = std::make_shared(normalized_id, map, info); - } else if (info.type == SourceType::Raster) { + } else if (info->type == SourceType::Raster) { new_tile.data = std::make_shared(normalized_id, map, info); } else { throw std::runtime_error("source type not implemented"); @@ -200,7 +200,7 @@ TileData::State Source::addTile(Map &map, const Tile::ID& id) { } double Source::getZoom(const TransformState& state) const { - double offset = std::log(util::tileSize / info.tile_size) / std::log(2); + double offset = std::log(util::tileSize / info->tile_size) / std::log(2); offset += (state.getPixelRatio() > 1.0 ? 1 :0); return state.getZoom() + offset; } @@ -212,8 +212,8 @@ int32_t Source::coveringZoomLevel(const TransformState& state) const { std::forward_list Source::coveringTiles(const TransformState& state) const { int32_t z = coveringZoomLevel(state); - if (z < info.min_zoom) return {{}}; - if (z > info.max_zoom) z = info.max_zoom; + if (z < info->min_zoom) return {{}}; + if (z > info->max_zoom) z = info->max_zoom; // Map four viewport corners to pixel coordinates box points = state.cornersToBox(z); @@ -286,8 +286,8 @@ bool Source::updateTiles(Map &map) { std::forward_list required = coveringTiles(map.getState()); // Determine the overzooming/underzooming amounts. - int32_t minCoveringZoom = util::clamp(zoom - 10, info.min_zoom, info.max_zoom); - int32_t maxCoveringZoom = util::clamp(zoom + 1, info.min_zoom, info.max_zoom); + int32_t minCoveringZoom = util::clamp(zoom - 10, info->min_zoom, info->max_zoom); + int32_t maxCoveringZoom = util::clamp(zoom + 1, info->min_zoom, info->max_zoom); // Retain is a list of tiles that we shouldn't delete, even if they are not // the most ideal tile for the current viewport. This may include tiles like diff --git a/src/map/tile_data.cpp b/src/map/tile_data.cpp index ecb05d5b2..44e3826bf 100644 --- a/src/map/tile_data.cpp +++ b/src/map/tile_data.cpp @@ -9,7 +9,7 @@ using namespace mbgl; -TileData::TileData(Tile::ID id, Map &map, const SourceInfo &source) +TileData::TileData(Tile::ID id, Map &map, const util::ptr &source) : id(id), state(State::initial), map(map), @@ -29,10 +29,10 @@ const std::string TileData::toString() const { } void TileData::request() { - if (source.tiles.empty()) + if (source->tiles.empty()) return; - std::string url = source.tiles[(id.x + id.y) % source.tiles.size()]; + std::string url = source->tiles[(id.x + id.y) % source->tiles.size()]; url = util::replaceTokens(url, [&](const std::string &token) -> std::string { if (token == "z") return std::to_string(id.z); if (token == "x") return std::to_string(id.x); diff --git a/src/map/tile_parser.cpp b/src/map/tile_parser.cpp index 4a92dd5e1..3dc5cb9ce 100644 --- a/src/map/tile_parser.cpp +++ b/src/map/tile_parser.cpp @@ -50,7 +50,7 @@ TileParser::TileParser(const std::string &data, VectorTileData &tile, glyphStore(glyphStore_), spriteAtlas(spriteAtlas_), sprite(sprite_), - collision(std::make_unique(tile.id.z, 4096, tile.source.tile_size, tile.depth)) { + collision(std::make_unique(tile.id.z, 4096, tile.source->tile_size, tile.depth)) { assert(&tile != nullptr); assert(style); assert(glyphAtlas); @@ -110,7 +110,7 @@ std::unique_ptr TileParser::createBucket(util::ptr bucket_d } // Skip this bucket if we are to not render this - if (tile.id.z < std::floor(bucket_desc->min_zoom) && std::floor(bucket_desc->min_zoom) < tile.source.max_zoom) return nullptr; + if (tile.id.z < std::floor(bucket_desc->min_zoom) && std::floor(bucket_desc->min_zoom) < tile.source->max_zoom) return nullptr; if (tile.id.z >= std::ceil(bucket_desc->max_zoom)) return nullptr; auto layer_it = vector_data.layers.find(bucket_desc->source_layer); diff --git a/src/map/vector_tile_data.cpp b/src/map/vector_tile_data.cpp index 4ad8fa869..e34ed9418 100644 --- a/src/map/vector_tile_data.cpp +++ b/src/map/vector_tile_data.cpp @@ -8,9 +8,9 @@ using namespace mbgl; -VectorTileData::VectorTileData(Tile::ID id, Map &map, const SourceInfo &source) +VectorTileData::VectorTileData(Tile::ID id, Map &map, const util::ptr &source) : TileData(id, map, source), - depth(id.z >= source.max_zoom ? map.getMaxZoom() - id.z : 1) { + depth(id.z >= source->max_zoom ? map.getMaxZoom() - id.z : 1) { } VectorTileData::~VectorTileData() { diff --git a/src/style/style_parser.cpp b/src/style/style_parser.cpp index 3eed16151..c2247d51b 100644 --- a/src/style/style_parser.cpp +++ b/src/style/style_parser.cpp @@ -176,12 +176,12 @@ void StyleParser::parseSources(JSVal value) { rapidjson::Value::ConstMemberIterator itr = value.MemberBegin(); for (; itr != value.MemberEnd(); ++itr) { std::string name { itr->name.GetString(), itr->name.GetStringLength() }; - SourceInfo info; + util::ptr info = std::make_shared(); - parseRenderProperty(itr->value, info.type, "type"); - parseRenderProperty(itr->value, info.url, "url"); - parseRenderProperty(itr->value, info.tile_size, "tileSize"); - info.parseTileJSONProperties(itr->value); + parseRenderProperty(itr->value, info->type, "type"); + parseRenderProperty(itr->value, info->url, "url"); + parseRenderProperty(itr->value, info->tile_size, "tileSize"); + info->parseTileJSONProperties(itr->value); sources.emplace(std::move(name), std::make_shared(info)); }