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

Commit

Permalink
Use unique_ptr for Source objects (fixes #904)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jul 2, 2015
1 parent 9361cbf commit c357ce5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/mbgl/renderer/painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ std::vector<RenderItem> Painter::determineRenderOrder(const Style& style) {
continue;
}

util::ptr<Source> source = style.getSource(layer.bucket->source);
Source* source = style.getSource(layer.bucket->source);
if (!source) {
Log::Warning(Event::Render, "can't find source for layer '%s'", layer.id.c_str());
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void Style::recalculate(float z, TimePoint now) {
continue;
}

util::ptr<Source> source = getSource(layer->bucket->source);
Source* source = getSource(layer->bucket->source);
if (!source) {
continue;
}
Expand All @@ -122,12 +122,12 @@ void Style::recalculate(float z, TimePoint now) {
}
}

util::ptr<Source> Style::getSource(const std::string& id) const {
const auto it = std::find_if(sources.begin(), sources.end(), [&](util::ptr<Source> source) {
Source* Style::getSource(const std::string& id) const {
const auto it = std::find_if(sources.begin(), sources.end(), [&](const auto& source) {
return source->info.source_id == id;
});

return it != sources.end() ? *it : nullptr;
return it != sources.end() ? it->get() : nullptr;
}

void Style::setDefaultTransitionDuration(Duration duration) {
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ class Style : public GlyphStore::Observer,
return lastError;
}

util::ptr<Source> getSource(const std::string& id) const;
Source* getSource(const std::string& id) const;

std::unique_ptr<GlyphStore> glyphStore;
std::unique_ptr<GlyphAtlas> glyphAtlas;
util::ptr<Sprite> sprite;
std::unique_ptr<SpriteAtlas> spriteAtlas;
std::unique_ptr<LineAtlas> lineAtlas;

std::vector<util::ptr<Source>> sources;
std::vector<std::unique_ptr<Source>> sources;
std::vector<util::ptr<StyleLayer>> layers;

private:
Expand Down
18 changes: 9 additions & 9 deletions src/mbgl/style/style_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ void StyleParser::parse(JSVal document) {
// create shape annotations source
const std::string& shapeID = AnnotationManager::ShapeLayerID;

util::ptr<Source> shapeAnnotationsSource = std::make_shared<Source>();
sourcesMap.emplace(shapeID, shapeAnnotationsSource);
sources.emplace_back(shapeAnnotationsSource);
std::unique_ptr<Source> shapeAnnotationsSource = std::make_unique<Source>();
shapeAnnotationsSource->info.type = SourceType::Annotations;
shapeAnnotationsSource->info.source_id = shapeID;
sourcesMap.emplace(shapeID, shapeAnnotationsSource.get());
sources.emplace_back(std::move(shapeAnnotationsSource));

// create point annotations layer
const std::string& pointID = AnnotationManager::PointLayerID;
Expand All @@ -70,12 +70,12 @@ void StyleParser::parse(JSVal document) {
parseLayout(iconOverlap, pointBucket);

// create point annotations source & connect to bucket & layer
util::ptr<Source> pointAnnotationsSource = std::make_shared<Source>();
sourcesMap.emplace(pointID, pointAnnotationsSource);
sources.emplace_back(pointAnnotationsSource);
std::unique_ptr<Source> pointAnnotationsSource = std::make_unique<Source>();
pointAnnotationsSource->info.type = SourceType::Annotations;
pointAnnotationsSource->info.source_id = pointID;
pointAnnotationsLayer->bucket = pointBucket;
sourcesMap.emplace(pointID, pointAnnotationsSource.get());
sources.emplace_back(std::move(pointAnnotationsSource));
}

if (document.HasMember("sprite")) {
Expand Down Expand Up @@ -227,14 +227,14 @@ 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() };
util::ptr<Source> source = std::make_shared<Source>();
std::unique_ptr<Source> source = std::make_unique<Source>();
parseRenderProperty<SourceTypeClass>(itr->value, source->info.type, "type");
parseRenderProperty(itr->value, source->info.url, "url");
parseRenderProperty(itr->value, source->info.tile_size, "tileSize");
source->info.source_id = name;
source->info.parseTileJSONProperties(itr->value);
sources.emplace_back(source);
sourcesMap.emplace(name, source);
sourcesMap.emplace(name, source.get());
sources.emplace_back(std::move(source));
}
} else {
Log::Warning(Event::ParseStyle, "sources must be an object");
Expand Down
8 changes: 4 additions & 4 deletions src/mbgl/style/style_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class StyleParser {

void parse(JSVal document);

std::vector<util::ptr<Source>> getSources() {
return sources;
std::vector<std::unique_ptr<Source>>&& getSources() {
return std::move(sources);
}

std::vector<util::ptr<StyleLayer>> getLayers() {
Expand Down Expand Up @@ -103,10 +103,10 @@ class StyleParser {
private:
std::unordered_map<std::string, const rapidjson::Value *> constants;

std::vector<util::ptr<Source>> sources;
std::vector<std::unique_ptr<Source>> sources;
std::vector<util::ptr<StyleLayer>> layers;

std::unordered_map<std::string, const util::ptr<Source>> sourcesMap;
std::unordered_map<std::string, const Source*> sourcesMap;
std::unordered_map<std::string, std::pair<JSVal, util::ptr<StyleLayer>>> layersMap;

// Store a stack of layers we're parsing right now. This is to prevent reference cycles.
Expand Down

0 comments on commit c357ce5

Please sign in to comment.