Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Convert Style to unique_ptr and pass by reference
Browse files Browse the repository at this point in the history
Fixes #1277
Fixes #1309
  • Loading branch information
jfirebaugh committed Apr 27, 2015
1 parent 2c2c718 commit 61a35ee
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 39 deletions.
13 changes: 1 addition & 12 deletions src/mbgl/map/live_tile_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using namespace mbgl;
LiveTileData::LiveTileData(const TileID& id_,
AnnotationManager& annotationManager_,
float mapMaxZoom,
util::ptr<Style> style_,
Style& style_,
GlyphAtlas& glyphAtlas_,
GlyphStore& glyphStore_,
SpriteAtlas& spriteAtlas_,
Expand All @@ -35,27 +35,16 @@ void LiveTileData::parse() {

if (tile) {
try {
if (!style) {
throw std::runtime_error("style isn't present in LiveTileData object anymore");
}

// Parsing creates state that is encapsulated in TileParser. While parsing,
// the TileParser object writes results into this objects. All other state
// is going to be discarded afterwards.
TileParser parser(*tile, *this, style, glyphAtlas, glyphStore, spriteAtlas, sprite);

// Clear the style so that we don't have a cycle in the shared_ptr references.
style.reset();

parser.parse();
} catch (const std::exception& ex) {
Log::Error(Event::ParseTile, "Live-parsing [%d/%d/%d] failed: %s", id.z, id.x, id.y, ex.what());
state = State::obsolete;
return;
}
} else {
// Clear the style so that we don't have a cycle in the shared_ptr references.
style.reset();
}

if (state != State::obsolete) {
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/map/live_tile_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class LiveTileData : public VectorTileData {
LiveTileData(const TileID&,
AnnotationManager&,
float mapMaxZoom,
util::ptr<Style>,
Style&,
GlyphAtlas&,
GlyphStore&,
SpriteAtlas&,
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/map/map_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void MapContext::loadStyleJSON(const std::string& json, const std::string& base)
assert(Environment::currentlyOn(ThreadType::Map));

sprite.reset();
style = std::make_shared<Style>();
style = util::make_unique<Style>();
style->base = base;
style->loadJSON((const uint8_t *)json.c_str());
style->cascade(data.getClasses());
Expand All @@ -162,7 +162,7 @@ void MapContext::updateTiles() {
assert(Environment::currentlyOn(ThreadType::Map));
if (!style) return;
for (const auto& source : style->sources) {
source->update(data, transformState, style, *glyphAtlas, *glyphStore, *spriteAtlas,
source->update(data, transformState, *style, *glyphAtlas, *glyphStore, *spriteAtlas,
getSprite(), *texturePool, [this]() {
assert(Environment::currentlyOn(ThreadType::Map));
triggerUpdate();
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/map/map_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ class MapContext {
std::unique_ptr<LineAtlas> lineAtlas;
std::unique_ptr<TexturePool> texturePool;
std::unique_ptr<Painter> painter;
std::unique_ptr<Style> style;

util::ptr<Sprite> sprite;
util::ptr<Style> style;

std::string styleURL;
std::string styleJSON;
Expand Down
10 changes: 5 additions & 5 deletions src/mbgl/map/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ TileData::State Source::hasTile(const TileID& id) {

TileData::State Source::addTile(MapData& data,
const TransformState& transformState,
util::ptr<Style> style,
Style& style,
GlyphAtlas& glyphAtlas,
GlyphStore& glyphStore,
SpriteAtlas& spriteAtlas,
Expand Down Expand Up @@ -269,15 +269,15 @@ TileData::State Source::addTile(MapData& data,
new_tile.data =
std::make_shared<VectorTileData>(normalized_id, data.transform.getMaxZoom(), style, glyphAtlas,
glyphStore, spriteAtlas, sprite, info);
new_tile.data->request(style->workers, transformState.getPixelRatio(), callback);
new_tile.data->request(style.workers, transformState.getPixelRatio(), callback);
} else if (info.type == SourceType::Raster) {
new_tile.data = std::make_shared<RasterTileData>(normalized_id, texturePool, info);
new_tile.data->request(style->workers, transformState.getPixelRatio(), callback);
new_tile.data->request(style.workers, transformState.getPixelRatio(), callback);
} else if (info.type == SourceType::Annotations) {
new_tile.data = std::make_shared<LiveTileData>(normalized_id, data.annotationManager,
data.transform.getMaxZoom(), style, glyphAtlas,
glyphStore, spriteAtlas, sprite, info);
new_tile.data->reparse(style->workers, callback);
new_tile.data->reparse(style.workers, callback);
} else {
throw std::runtime_error("source type not implemented");
}
Expand Down Expand Up @@ -368,7 +368,7 @@ bool Source::findLoadedParent(const TileID& id, int32_t minCoveringZoom, std::fo

void Source::update(MapData& data,
const TransformState& transformState,
util::ptr<Style> style,
Style& style,
GlyphAtlas& glyphAtlas,
GlyphStore& glyphStore,
SpriteAtlas& spriteAtlas,
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/map/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Source : public std::enable_shared_from_this<Source>, private util::noncop
void load(MapData&, Environment&, std::function<void()> callback);
void update(MapData&,
const TransformState&,
util::ptr<Style>,
Style&,
GlyphAtlas&,
GlyphStore&,
SpriteAtlas&,
Expand Down Expand Up @@ -95,7 +95,7 @@ class Source : public std::enable_shared_from_this<Source>, private util::noncop

TileData::State addTile(MapData&,
const TransformState&,
util::ptr<Style>,
Style&,
GlyphAtlas&,
GlyphStore&,
SpriteAtlas&,
Expand Down
5 changes: 2 additions & 3 deletions src/mbgl/map/tile_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TileParser::~TileParser() = default;

TileParser::TileParser(const GeometryTile& geometryTile_,
VectorTileData& tile_,
const util::ptr<const Style>& style_,
const Style& style_,
GlyphAtlas& glyphAtlas_,
GlyphStore& glyphStore_,
SpriteAtlas& spriteAtlas_,
Expand All @@ -35,15 +35,14 @@ TileParser::TileParser(const GeometryTile& geometryTile_,
spriteAtlas(spriteAtlas_),
sprite(sprite_),
collision(util::make_unique<Collision>(tile.id.z, 4096, tile.source.tile_size, tile.depth)) {
assert(style);
assert(sprite);
assert(collision);
}

bool TileParser::obsolete() const { return tile.state == TileData::State::obsolete; }

void TileParser::parse() {
for (const auto& layer_desc : style->layers) {
for (const auto& layer_desc : style.layers) {
// Cancel early when parsing.
if (obsolete()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/map/tile_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TileParser : private util::noncopyable {
public:
TileParser(const GeometryTile& geometryTile,
VectorTileData& tile,
const util::ptr<const Style>& style,
const Style& style,
GlyphAtlas& glyphAtlas,
GlyphStore& glyphStore,
SpriteAtlas& spriteAtlas,
Expand All @@ -60,7 +60,7 @@ class TileParser : private util::noncopyable {
VectorTileData& tile;

// Cross-thread shared data.
util::ptr<const Style> style;
const Style& style;
GlyphAtlas& glyphAtlas;
GlyphStore& glyphStore;
SpriteAtlas& spriteAtlas;
Expand Down
10 changes: 1 addition & 9 deletions src/mbgl/map/vector_tile_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using namespace mbgl;

VectorTileData::VectorTileData(const TileID& id_,
float mapMaxZoom,
util::ptr<Style> style_,
Style& style_,
GlyphAtlas& glyphAtlas_,
GlyphStore& glyphStore_,
SpriteAtlas& spriteAtlas_,
Expand All @@ -37,20 +37,12 @@ void VectorTileData::parse() {
}

try {
if (!style) {
throw std::runtime_error("style isn't present in VectorTileData object anymore");
}

// Parsing creates state that is encapsulated in TileParser. While parsing,
// the TileParser object writes results into this objects. All other state
// is going to be discarded afterwards.
VectorTile vectorTile(pbf((const uint8_t *)data.data(), data.size()));
const VectorTile* vt = &vectorTile;
TileParser parser(*vt, *this, style, glyphAtlas, glyphStore, spriteAtlas, sprite);

// Clear the style so that we don't have a cycle in the shared_ptr references.
style.reset();

parser.parse();
} catch (const std::exception& ex) {
Log::Error(Event::ParseTile, "Parsing [%d/%d/%d] failed: %s", id.z, id.x, id.y, ex.what());
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/map/vector_tile_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class VectorTileData : public TileData {
public:
VectorTileData(const TileID&,
float mapMaxZoom,
util::ptr<Style>,
Style&,
GlyphAtlas&,
GlyphStore&,
SpriteAtlas&,
Expand Down Expand Up @@ -59,7 +59,7 @@ class VectorTileData : public TileData {
GlyphStore& glyphStore;
SpriteAtlas& spriteAtlas;
util::ptr<Sprite> sprite;
util::ptr<Style> style;
Style& style;

public:
const float depth;
Expand Down

0 comments on commit 61a35ee

Please sign in to comment.