summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJesse Bounds <jesse@rebounds.net>2016-11-11 14:40:04 -0800
committerJesse Bounds <jesse@rebounds.net>2016-11-13 19:56:29 -0800
commit96a7a4402ef5eac2fa17ec7b52ef24e3b10bbf29 (patch)
tree18fa208f1f5d086a8e6d8ac728b49d923c45b076 /src
parent82fcd58121f272fea8d115ae0419791a78884324 (diff)
downloadqtlocation-mapboxgl-96a7a4402ef5eac2fa17ec7b52ef24e3b10bbf29.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')
-rw-r--r--src/mbgl/map/map.cpp13
-rw-r--r--src/mbgl/style/layers/custom_layer_impl.cpp12
-rw-r--r--src/mbgl/style/layers/custom_layer_impl.hpp1
-rw-r--r--src/mbgl/style/style.cpp26
-rw-r--r--src/mbgl/style/style.hpp4
5 files changed, 41 insertions, 15 deletions
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp
index 903f90c540..bd1de6f180 100644
--- a/src/mbgl/map/map.cpp
+++ b/src/mbgl/map/map.cpp
@@ -818,11 +818,12 @@ void Map::addSource(std::unique_ptr<style::Source> source) {
}
}
-void Map::removeSource(const std::string& sourceID) {
+std::unique_ptr<Source> Map::removeSource(const std::string& sourceID) {
if (impl->style) {
impl->styleMutated = true;
- impl->style->removeSource(sourceID);
+ return impl->style->removeSource(sourceID);
}
+ return nullptr;
}
style::Layer* Map::getLayer(const std::string& layerID) {
@@ -847,18 +848,20 @@ void Map::addLayer(std::unique_ptr<Layer> layer, const optional<std::string>& be
impl->backend.deactivate();
}
-void Map::removeLayer(const std::string& id) {
+std::unique_ptr<Layer> Map::removeLayer(const std::string& id) {
if (!impl->style) {
- return;
+ return nullptr;
}
impl->styleMutated = true;
impl->backend.activate();
- impl->style->removeLayer(id);
+ auto removedLayer = impl->style->removeLayer(id);
impl->onUpdate(Update::Classes);
impl->backend.deactivate();
+
+ return removedLayer;
}
void Map::addImage(const std::string& name, std::unique_ptr<const SpriteImage> image) {
diff --git a/src/mbgl/style/layers/custom_layer_impl.cpp b/src/mbgl/style/layers/custom_layer_impl.cpp
index a0686e353c..8896d04ca1 100644
--- a/src/mbgl/style/layers/custom_layer_impl.cpp
+++ b/src/mbgl/style/layers/custom_layer_impl.cpp
@@ -23,11 +23,7 @@ CustomLayer::Impl::Impl(const CustomLayer::Impl& other)
// Don't copy anything else.
}
-CustomLayer::Impl::~Impl() {
- if (deinitializeFn) {
- deinitializeFn(context);
- }
-}
+CustomLayer::Impl::~Impl() = default;
std::unique_ptr<Layer> CustomLayer::Impl::clone() const {
return std::make_unique<CustomLayer>(*this);
@@ -43,6 +39,12 @@ void CustomLayer::Impl::initialize() {
initializeFn(context);
}
+void CustomLayer::Impl::deinitialize() {
+ if (deinitializeFn) {
+ deinitializeFn(context);
+ }
+}
+
void CustomLayer::Impl::render(const TransformState& state) const {
assert(renderFn);
diff --git a/src/mbgl/style/layers/custom_layer_impl.hpp b/src/mbgl/style/layers/custom_layer_impl.hpp
index ffa892ddf8..b5b626ca5e 100644
--- a/src/mbgl/style/layers/custom_layer_impl.hpp
+++ b/src/mbgl/style/layers/custom_layer_impl.hpp
@@ -21,6 +21,7 @@ public:
~Impl() final;
void initialize();
+ void deinitialize();
void render(const TransformState&) const;
private:
diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp
index 91483e156a..05dbc0843f 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 {
diff --git a/src/mbgl/style/style.hpp b/src/mbgl/style/style.hpp
index 00e30d3977..2875311c63 100644
--- a/src/mbgl/style/style.hpp
+++ b/src/mbgl/style/style.hpp
@@ -66,13 +66,13 @@ public:
Source* getSource(const std::string& id) const;
void addSource(std::unique_ptr<Source>);
- void removeSource(const std::string& sourceID);
+ std::unique_ptr<Source> removeSource(const std::string& sourceID);
std::vector<const Layer*> getLayers() const;
Layer* getLayer(const std::string& id) const;
Layer* addLayer(std::unique_ptr<Layer>,
optional<std::string> beforeLayerID = {});
- void removeLayer(const std::string& layerID);
+ std::unique_ptr<Layer> removeLayer(const std::string& layerID);
std::string getName() const;
LatLng getDefaultLatLng() const;