make SourceInfo object noncopyable and use a shared_pointer for it

This commit is contained in:
Konstantin Käfer
2014-09-17 16:01:35 +02:00
parent 111fccb76c
commit 19129cbd02
11 changed files with 34 additions and 33 deletions

View File

@@ -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<SourceInfo> &source);
~RasterTileData();
virtual void parse();

View File

@@ -25,7 +25,7 @@ struct box;
class Source : public std::enable_shared_from_this<Source>, private util::noncopyable {
public:
Source(SourceInfo& info);
Source(const util::ptr<SourceInfo>& info);
void load(Map &map);
@@ -54,7 +54,7 @@ private:
double getZoom(const TransformState &state) const;
SourceInfo& info;
util::ptr<SourceInfo> info;
bool loaded = false;
// Stores the time when this source was most recently updated.

View File

@@ -40,7 +40,7 @@ public:
};
public:
TileData(Tile::ID id, Map &map, const SourceInfo &source);
TileData(Tile::ID id, Map &map, const util::ptr<SourceInfo> &source);
~TileData();
void request();
@@ -68,7 +68,7 @@ protected:
Map &map;
public:
const SourceInfo &source;
util::ptr<SourceInfo> source;
protected:
std::unique_ptr<Request> req;

View File

@@ -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<SourceInfo> &source);
~VectorTileData();
virtual void beforeParse();

View File

@@ -3,6 +3,7 @@
#include <mbgl/style/types.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <rapidjson/document.h>
#include <vector>
@@ -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<StyleSource> {
public:
SourceInfo info;
util::ptr<SourceInfo> info;
bool enabled = false;
util::ptr<Source> source;
StyleSource(const SourceInfo &info)
StyleSource(const util::ptr<SourceInfo> &info)
: info(info)
{}
};

View File

@@ -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<SourceInfo> &source)
: TileData(id, map, source),
bucket(map.getTexturepool(), properties) {
}

View File

@@ -20,7 +20,7 @@
namespace mbgl {
Source::Source(SourceInfo& info)
Source::Source(const util::ptr<SourceInfo>& 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> 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<VectorTileData>(normalized_id, map, info);
} else if (info.type == SourceType::Raster) {
} else if (info->type == SourceType::Raster) {
new_tile.data = std::make_shared<RasterTileData>(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<Tile::ID> 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<Tile::ID> required = coveringTiles(map.getState());
// Determine the overzooming/underzooming amounts.
int32_t minCoveringZoom = util::clamp<int32_t>(zoom - 10, info.min_zoom, info.max_zoom);
int32_t maxCoveringZoom = util::clamp<int32_t>(zoom + 1, info.min_zoom, info.max_zoom);
int32_t minCoveringZoom = util::clamp<int32_t>(zoom - 10, info->min_zoom, info->max_zoom);
int32_t maxCoveringZoom = util::clamp<int32_t>(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

View File

@@ -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<SourceInfo> &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);

View File

@@ -50,7 +50,7 @@ TileParser::TileParser(const std::string &data, VectorTileData &tile,
glyphStore(glyphStore_),
spriteAtlas(spriteAtlas_),
sprite(sprite_),
collision(std::make_unique<Collision>(tile.id.z, 4096, tile.source.tile_size, tile.depth)) {
collision(std::make_unique<Collision>(tile.id.z, 4096, tile.source->tile_size, tile.depth)) {
assert(&tile != nullptr);
assert(style);
assert(glyphAtlas);
@@ -110,7 +110,7 @@ std::unique_ptr<Bucket> TileParser::createBucket(util::ptr<StyleBucket> 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);

View File

@@ -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<SourceInfo> &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() {

View File

@@ -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<SourceInfo> info = std::make_shared<SourceInfo>();
parseRenderProperty<SourceTypeClass>(itr->value, info.type, "type");
parseRenderProperty(itr->value, info.url, "url");
parseRenderProperty(itr->value, info.tile_size, "tileSize");
info.parseTileJSONProperties(itr->value);
parseRenderProperty<SourceTypeClass>(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<StyleSource>(info));
}