[ios, macos] Take back source and layer ownership on removal (#7048)

If a source or layer is removed from the style, recapture the
unique pointer ownership. This makes it safe to add back sources
and layers after they have been removed.
This commit is contained in:
Jesse Bounds
2016-11-14 15:50:33 -08:00
committed by GitHub
parent 56c2272b7d
commit 1c9d314236
13 changed files with 219 additions and 128 deletions

View File

@@ -32,6 +32,31 @@
}
#pragma mark - Adding to and removing from a map view
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::BackgroundLayer> &>(removedLayer));
self.rawLayer = _pendingLayer.get();
}
#pragma mark - Accessing the Paint Attributes
- (void)setBackgroundColor:(MGLStyleValue<MGLColor *> *)backgroundColor {
@@ -65,21 +90,4 @@
}
#pragma mark - Add style layer to map
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
@end

View File

@@ -53,6 +53,31 @@
return [NSPredicate mgl_predicateWithFilter:self.rawLayer->getFilter()];
}
#pragma mark - Adding to and removing from a map view
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::CircleLayer> &>(removedLayer));
self.rawLayer = _pendingLayer.get();
}
#pragma mark - Accessing the Paint Attributes
- (void)setCircleRadius:(MGLStyleValue<NSNumber *> *)circleRadius {
@@ -126,21 +151,4 @@
}
#pragma mark - Add style layer to map
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
@end

View File

@@ -53,6 +53,31 @@
return [NSPredicate mgl_predicateWithFilter:self.rawLayer->getFilter()];
}
#pragma mark - Adding to and removing from a map view
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::FillLayer> &>(removedLayer));
self.rawLayer = _pendingLayer.get();
}
#pragma mark - Accessing the Paint Attributes
- (void)setFillAntialias:(MGLStyleValue<NSNumber *> *)fillAntialias {
@@ -126,21 +151,4 @@
}
#pragma mark - Add style layer to map
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
@end

View File

@@ -64,6 +64,14 @@ const MGLGeoJSONSourceOption MGLGeoJSONSourceOptionSimplificationTolerance = @"M
mapView.mbglMap->addSource(std::move(_pendingSource));
}
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedSource = mapView.mbglMap->removeSource(self.identifier.UTF8String);
_pendingSource = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::GeoJSONSource> &>(removedSource));
self.rawSource = _pendingSource.get();
}
- (void)commonInit
{
auto source = std::make_unique<mbgl::style::GeoJSONSource>(self.identifier.UTF8String, self.geoJSONOptions);

View File

@@ -53,6 +53,31 @@
return [NSPredicate mgl_predicateWithFilter:self.rawLayer->getFilter()];
}
#pragma mark - Adding to and removing from a map view
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::LineLayer> &>(removedLayer));
self.rawLayer = _pendingLayer.get();
}
#pragma mark - Accessing the Layout Attributes
- (void)setLineCap:(MGLStyleValue<NSValue *> *)lineCap {
@@ -198,21 +223,4 @@
}
#pragma mark - Add style layer to map
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
@end

View File

@@ -65,4 +65,12 @@
mapView.mbglMap->addSource(std::move(_pendingSource));
}
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedSource = mapView.mbglMap->removeSource(self.identifier.UTF8String);
_pendingSource = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::RasterSource> &>(removedSource));
self.rawSource = _pendingSource.get();
}
@end

View File

@@ -32,6 +32,31 @@
}
#pragma mark - Adding to and removing from a map view
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::RasterLayer> &>(removedLayer));
self.rawLayer = _pendingLayer.get();
}
#pragma mark - Accessing the Paint Attributes
- (void)setRasterOpacity:(MGLStyleValue<NSNumber *> *)rasterOpacity {
@@ -105,21 +130,4 @@
}
#pragma mark - Add style layer to map
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
@end

View File

@@ -28,4 +28,13 @@
*/
- (void)addToMapView:(MGLMapView *)mapView;
/**
Removes the mbgl source that this object represents from the mbgl map.
When a mbgl source is removed, ownership of the object is transferred back
to the `MGLSource` instance and the unique_ptr reference is valid again. It is
safe to add the source back to the style after it is removed.
*/
- (void)removeFromMapView:(MGLMapView *)mapView;
@end

View File

@@ -168,7 +168,7 @@ static NSURL *MGLStyleURL_emerald;
- (void)removeLayer:(MGLStyleLayer *)layer
{
self.mapView.mbglMap->removeLayer(layer.identifier.UTF8String);
[layer removeFromMapView:self.mapView];
}
- (void)addLayer:(MGLStyleLayer *)layer
@@ -209,12 +209,7 @@ static NSURL *MGLStyleURL_emerald;
- (void)removeSource:(MGLSource *)source
{
self.mapView.mbglMap->removeSource(source.identifier.UTF8String);
// Once a mbgl source is removed from the map, ownership does not return
// to the MGL source. Therefore, the rawSource pointer is set to NULL
// so that the implementation of MGL source can avoid using it again.
source.rawSource = NULL;
[source removeFromMapView:self.mapView];
}
- (NS_ARRAY_OF(NSString *) *)styleClasses

View File

@@ -73,6 +73,31 @@
}
<% } -%>
#pragma mark - Adding to and removing from a map view
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::<%- camelize(type) %>Layer> &>(removedLayer));
self.rawLayer = _pendingLayer.get();
}
<% if (layoutProperties.length) { -%>
#pragma mark - Accessing the Layout Attributes
@@ -106,21 +131,4 @@
<% } -%>
<% } -%>
#pragma mark - Add style layer to map
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
@end

View File

@@ -29,11 +29,18 @@
*/
- (void)addToMapView:(MGLMapView *)mapView;
/**
Removes the mbgl style layer that this object represents from the mbgl map.
When a mbgl style layer is removed, ownership of the object is transferred back
to the `MGLStyleLayer` instance and the unique_ptr reference is valid again. It
is safe to add the layer back to the style after it is removed.
*/
- (void)removeFromMapView:(MGLMapView *)mapView;
/**
Adds the mbgl style layer that this object represents to the mbgl map below the specified `otherLayer`.
Once a mbgl style layer is added, ownership of the object is transferred to the
`mbgl::Map` and this object no longer has an active unique_ptr reference to the
`mbgl::style::Layer`.

View File

@@ -53,6 +53,31 @@
return [NSPredicate mgl_predicateWithFilter:self.rawLayer->getFilter()];
}
#pragma mark - Adding to and removing from a map view
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::SymbolLayer> &>(removedLayer));
self.rawLayer = _pendingLayer.get();
}
#pragma mark - Accessing the Layout Attributes
- (void)setSymbolPlacement:(MGLStyleValue<NSValue *> *)symbolPlacement {
@@ -538,21 +563,4 @@
}
#pragma mark - Add style layer to map
- (void)addToMapView:(MGLMapView *)mapView
{
[self addToMapView:mapView belowLayer:nil];
}
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
{
if (otherLayer) {
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId);
} else {
mapView.mbglMap->addLayer(std::move(_pendingLayer));
}
}
@end

View File

@@ -64,4 +64,12 @@ static NSString *MGLVectorSourceType = @"vector";
mapView.mbglMap->addSource(std::move(_pendingSource));
}
- (void)removeFromMapView:(MGLMapView *)mapView
{
auto removedSource = mapView.mbglMap->removeSource(self.identifier.UTF8String);
_pendingSource = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::VectorSource> &>(removedSource));
self.rawSource = _pendingSource.get();
}
@end