summaryrefslogtreecommitdiff
path: root/src/mbgl/style/style.cpp
diff options
context:
space:
mode:
authorJesse Bounds <jesse@rebounds.net>2016-11-11 14:40:04 -0800
committerGitHub <noreply@github.com>2016-11-11 14:40:04 -0800
commitedf40bb6d9f0ee3731f39de597ce9a167571ea5b (patch)
treeb0a310487d4c3f53dde0b554ff16f1b1cacee9fe /src/mbgl/style/style.cpp
parent39b0bc0a402248e29791f93aeab5a6234bf80862 (diff)
downloadqtlocation-mapboxgl-edf40bb6d9f0ee3731f39de597ce9a167571ea5b.tar.gz
[core] Return source and layer ownership (#7014)
When a source or layer is removed transfer ownership back to the caller so it can (optionally) take it. Preserve the behavior that removing a CustomLayer triggers deinitialization. Deinitialize all custom layers when a style is destroyed in case those layers are not explicitly removed.
Diffstat (limited to 'src/mbgl/style/style.cpp')
-rw-r--r--src/mbgl/style/style.cpp26
1 files changed, 23 insertions, 3 deletions
diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp
index 57e2580d4d..0b3d782d06 100644
--- a/src/mbgl/style/style.cpp
+++ b/src/mbgl/style/style.cpp
@@ -51,6 +51,12 @@ Style::~Style() {
source->baseImpl->setObserver(nullptr);
}
+ for (const auto& layer : layers) {
+ if (CustomLayer* customLayer = layer->as<CustomLayer>()) {
+ customLayer->impl->deinitialize();
+ }
+ }
+
glyphAtlas->setObserver(nullptr);
spriteAtlas->setObserver(nullptr);
}
@@ -134,7 +140,7 @@ void Style::addSource(std::unique_ptr<Source> source) {
sources.emplace_back(std::move(source));
}
-void Style::removeSource(const std::string& id) {
+std::unique_ptr<Source> Style::removeSource(const std::string& id) {
auto it = std::find_if(sources.begin(), sources.end(), [&](const auto& source) {
return source->getID() == id;
});
@@ -143,8 +149,11 @@ void Style::removeSource(const std::string& id) {
throw std::runtime_error("no such source");
}
+ auto source = std::move(*it);
sources.erase(it);
updateBatch.sourceIDs.erase(id);
+
+ return source;
}
std::vector<const Layer*> Style::getLayers() const {
@@ -185,11 +194,22 @@ Layer* Style::addLayer(std::unique_ptr<Layer> layer, optional<std::string> befor
return layers.emplace(before ? findLayer(*before) : layers.end(), std::move(layer))->get();
}
-void Style::removeLayer(const std::string& id) {
- auto it = findLayer(id);
+std::unique_ptr<Layer> Style::removeLayer(const std::string& id) {
+ auto it = std::find_if(layers.begin(), layers.end(), [&](const auto& layer) {
+ return layer->baseImpl->id == id;
+ });
+
if (it == layers.end())
throw std::runtime_error("no such layer");
+
+ auto layer = std::move(*it);
+
+ if (CustomLayer* customLayer = layer->as<CustomLayer>()) {
+ customLayer->impl->deinitialize();
+ }
+
layers.erase(it);
+ return layer;
}
std::string Style::getName() const {