Use unique_ptr for Source objects (fixes #904)

This commit is contained in:
John Firebaugh
2015-07-02 12:27:22 -07:00
parent 9361cbfd5f
commit c357ce540a
5 changed files with 20 additions and 20 deletions

View File

@@ -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;

View File

@@ -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;
}
@@ -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) {

View File

@@ -62,7 +62,7 @@ public:
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;
@@ -70,7 +70,7 @@ public:
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:

View File

@@ -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;
@@ -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")) {
@@ -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");

View File

@@ -35,8 +35,8 @@ public:
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() {
@@ -103,10 +103,10 @@ private:
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.