From 37a087c675b847f64dd73a570c11b4ddebac35fb Mon Sep 17 00:00:00 2001 From: Jesse Bounds Date: Wed, 30 Nov 2016 11:59:29 -0800 Subject: [PATCH] [ios, macos] Avoid unsafe cast when removing style layers (#7227) This updates the code generation for style layers to add a test to check that the style object returned from mbgl can be safely cast into the expected type so ownership can be returned to the caller when removing layers. This scenario was previously possible if a layer of type T was added before a layer of type U but both layers shared the same identifier. In that case, if the pointer to the second layer of type U was used to remove a layer, a runtime exception would occur since mbgl returns the layer with type T (the first one added). Now, an exception will still occur if layers with the same identifier are manipulated as described above but it is done as part of a validation check for all public style layer methods at the Darwin platform level. The NSException attempts to warn developers about using and reusing style identifiers to avoid this issue in the first place. --- .../darwin/src/MGLBackgroundStyleLayer.mm | 25 +- platform/darwin/src/MGLCircleStyleLayer.mm | 49 +++- platform/darwin/src/MGLFillStyleLayer.mm | 49 +++- platform/darwin/src/MGLLineStyleLayer.mm | 77 ++++++- platform/darwin/src/MGLRasterStyleLayer.mm | 41 +++- platform/darwin/src/MGLStyleLayer.mm | 16 +- platform/darwin/src/MGLStyleLayer.mm.ejs | 29 ++- platform/darwin/src/MGLStyleLayer_Private.h | 19 ++ platform/darwin/src/MGLSymbolStyleLayer.mm | 213 +++++++++++++++++- 9 files changed, 507 insertions(+), 11 deletions(-) diff --git a/platform/darwin/src/MGLBackgroundStyleLayer.mm b/platform/darwin/src/MGLBackgroundStyleLayer.mm index 59aa06910..c8f8b39b3 100644 --- a/platform/darwin/src/MGLBackgroundStyleLayer.mm +++ b/platform/darwin/src/MGLBackgroundStyleLayer.mm @@ -50,42 +50,65 @@ - (void)removeFromMapView:(MGLMapView *)mapView { + _pendingLayer = nullptr; + _rawLayer = nullptr; + auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String); if (!removedLayer) { return; } - _pendingLayer = std::move(reinterpret_cast &>(removedLayer)); + + mbgl::style::BackgroundLayer *layer = dynamic_cast(removedLayer.get()); + if (!layer) { + return; + } + + removedLayer.release(); + + _pendingLayer = std::unique_ptr(layer); _rawLayer = _pendingLayer.get(); } #pragma mark - Accessing the Paint Attributes - (void)setBackgroundColor:(MGLStyleValue *)backgroundColor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(backgroundColor); _rawLayer->setBackgroundColor(mbglValue); } - (MGLStyleValue *)backgroundColor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getBackgroundColor() ?: _rawLayer->getDefaultBackgroundColor(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setBackgroundOpacity:(MGLStyleValue *)backgroundOpacity { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(backgroundOpacity); _rawLayer->setBackgroundOpacity(mbglValue); } - (MGLStyleValue *)backgroundOpacity { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getBackgroundOpacity() ?: _rawLayer->getDefaultBackgroundOpacity(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setBackgroundPattern:(MGLStyleValue *)backgroundPattern { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(backgroundPattern); _rawLayer->setBackgroundPattern(mbglValue); } - (MGLStyleValue *)backgroundPattern { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getBackgroundPattern() ?: _rawLayer->getDefaultBackgroundPattern(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } diff --git a/platform/darwin/src/MGLCircleStyleLayer.mm b/platform/darwin/src/MGLCircleStyleLayer.mm index 1c903ab00..7768ff66c 100644 --- a/platform/darwin/src/MGLCircleStyleLayer.mm +++ b/platform/darwin/src/MGLCircleStyleLayer.mm @@ -45,22 +45,30 @@ namespace mbgl { } - (NSString *)sourceLayerIdentifier { + MGLAssertStyleLayerIsValid(); + auto layerID = _rawLayer->getSourceLayer(); return layerID.empty() ? nil : @(layerID.c_str()); } - (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier { + MGLAssertStyleLayerIsValid(); + _rawLayer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: ""); } - (void)setPredicate:(NSPredicate *)predicate { + MGLAssertStyleLayerIsValid(); + _rawLayer->setFilter(predicate.mgl_filter); } - (NSPredicate *)predicate { + MGLAssertStyleLayerIsValid(); + return [NSPredicate mgl_predicateWithFilter:_rawLayer->getFilter()]; } #pragma mark - Adding to and removing from a map view @@ -82,82 +90,121 @@ namespace mbgl { - (void)removeFromMapView:(MGLMapView *)mapView { + _pendingLayer = nullptr; + _rawLayer = nullptr; + auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String); if (!removedLayer) { return; } - _pendingLayer = std::move(reinterpret_cast &>(removedLayer)); + + mbgl::style::CircleLayer *layer = dynamic_cast(removedLayer.get()); + if (!layer) { + return; + } + + removedLayer.release(); + + _pendingLayer = std::unique_ptr(layer); _rawLayer = _pendingLayer.get(); } #pragma mark - Accessing the Paint Attributes - (void)setCircleBlur:(MGLStyleValue *)circleBlur { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(circleBlur); _rawLayer->setCircleBlur(mbglValue); } - (MGLStyleValue *)circleBlur { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleBlur() ?: _rawLayer->getDefaultCircleBlur(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setCircleColor:(MGLStyleValue *)circleColor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(circleColor); _rawLayer->setCircleColor(mbglValue); } - (MGLStyleValue *)circleColor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleColor() ?: _rawLayer->getDefaultCircleColor(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setCircleOpacity:(MGLStyleValue *)circleOpacity { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(circleOpacity); _rawLayer->setCircleOpacity(mbglValue); } - (MGLStyleValue *)circleOpacity { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleOpacity() ?: _rawLayer->getDefaultCircleOpacity(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setCirclePitchScale:(MGLStyleValue *)circlePitchScale { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(circlePitchScale); _rawLayer->setCirclePitchScale(mbglValue); } - (MGLStyleValue *)circlePitchScale { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCirclePitchScale() ?: _rawLayer->getDefaultCirclePitchScale(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setCircleRadius:(MGLStyleValue *)circleRadius { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(circleRadius); _rawLayer->setCircleRadius(mbglValue); } - (MGLStyleValue *)circleRadius { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleRadius() ?: _rawLayer->getDefaultCircleRadius(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setCircleTranslate:(MGLStyleValue *)circleTranslate { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer, NSValue *>().toPropertyValue(circleTranslate); _rawLayer->setCircleTranslate(mbglValue); } - (MGLStyleValue *)circleTranslate { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleTranslate() ?: _rawLayer->getDefaultCircleTranslate(); return MGLStyleValueTransformer, NSValue *>().toStyleValue(propertyValue); } - (void)setCircleTranslateAnchor:(MGLStyleValue *)circleTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(circleTranslateAnchor); _rawLayer->setCircleTranslateAnchor(mbglValue); } - (MGLStyleValue *)circleTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleTranslateAnchor() ?: _rawLayer->getDefaultCircleTranslateAnchor(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } diff --git a/platform/darwin/src/MGLFillStyleLayer.mm b/platform/darwin/src/MGLFillStyleLayer.mm index 6f9c68f21..d1a96f723 100644 --- a/platform/darwin/src/MGLFillStyleLayer.mm +++ b/platform/darwin/src/MGLFillStyleLayer.mm @@ -40,22 +40,30 @@ namespace mbgl { } - (NSString *)sourceLayerIdentifier { + MGLAssertStyleLayerIsValid(); + auto layerID = _rawLayer->getSourceLayer(); return layerID.empty() ? nil : @(layerID.c_str()); } - (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier { + MGLAssertStyleLayerIsValid(); + _rawLayer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: ""); } - (void)setPredicate:(NSPredicate *)predicate { + MGLAssertStyleLayerIsValid(); + _rawLayer->setFilter(predicate.mgl_filter); } - (NSPredicate *)predicate { + MGLAssertStyleLayerIsValid(); + return [NSPredicate mgl_predicateWithFilter:_rawLayer->getFilter()]; } #pragma mark - Adding to and removing from a map view @@ -77,82 +85,121 @@ namespace mbgl { - (void)removeFromMapView:(MGLMapView *)mapView { + _pendingLayer = nullptr; + _rawLayer = nullptr; + auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String); if (!removedLayer) { return; } - _pendingLayer = std::move(reinterpret_cast &>(removedLayer)); + + mbgl::style::FillLayer *layer = dynamic_cast(removedLayer.get()); + if (!layer) { + return; + } + + removedLayer.release(); + + _pendingLayer = std::unique_ptr(layer); _rawLayer = _pendingLayer.get(); } #pragma mark - Accessing the Paint Attributes - (void)setFillAntialias:(MGLStyleValue *)fillAntialias { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(fillAntialias); _rawLayer->setFillAntialias(mbglValue); } - (MGLStyleValue *)fillAntialias { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getFillAntialias() ?: _rawLayer->getDefaultFillAntialias(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setFillColor:(MGLStyleValue *)fillColor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(fillColor); _rawLayer->setFillColor(mbglValue); } - (MGLStyleValue *)fillColor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getFillColor() ?: _rawLayer->getDefaultFillColor(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setFillOpacity:(MGLStyleValue *)fillOpacity { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(fillOpacity); _rawLayer->setFillOpacity(mbglValue); } - (MGLStyleValue *)fillOpacity { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getFillOpacity() ?: _rawLayer->getDefaultFillOpacity(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setFillOutlineColor:(MGLStyleValue *)fillOutlineColor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(fillOutlineColor); _rawLayer->setFillOutlineColor(mbglValue); } - (MGLStyleValue *)fillOutlineColor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getFillOutlineColor() ?: _rawLayer->getDefaultFillOutlineColor(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setFillPattern:(MGLStyleValue *)fillPattern { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(fillPattern); _rawLayer->setFillPattern(mbglValue); } - (MGLStyleValue *)fillPattern { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getFillPattern() ?: _rawLayer->getDefaultFillPattern(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setFillTranslate:(MGLStyleValue *)fillTranslate { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer, NSValue *>().toPropertyValue(fillTranslate); _rawLayer->setFillTranslate(mbglValue); } - (MGLStyleValue *)fillTranslate { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getFillTranslate() ?: _rawLayer->getDefaultFillTranslate(); return MGLStyleValueTransformer, NSValue *>().toStyleValue(propertyValue); } - (void)setFillTranslateAnchor:(MGLStyleValue *)fillTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(fillTranslateAnchor); _rawLayer->setFillTranslateAnchor(mbglValue); } - (MGLStyleValue *)fillTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getFillTranslateAnchor() ?: _rawLayer->getDefaultFillTranslateAnchor(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } diff --git a/platform/darwin/src/MGLLineStyleLayer.mm b/platform/darwin/src/MGLLineStyleLayer.mm index c37d1b659..a7ed2fc10 100644 --- a/platform/darwin/src/MGLLineStyleLayer.mm +++ b/platform/darwin/src/MGLLineStyleLayer.mm @@ -52,22 +52,30 @@ namespace mbgl { } - (NSString *)sourceLayerIdentifier { + MGLAssertStyleLayerIsValid(); + auto layerID = _rawLayer->getSourceLayer(); return layerID.empty() ? nil : @(layerID.c_str()); } - (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier { + MGLAssertStyleLayerIsValid(); + _rawLayer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: ""); } - (void)setPredicate:(NSPredicate *)predicate { + MGLAssertStyleLayerIsValid(); + _rawLayer->setFilter(predicate.mgl_filter); } - (NSPredicate *)predicate { + MGLAssertStyleLayerIsValid(); + return [NSPredicate mgl_predicateWithFilter:_rawLayer->getFilter()]; } #pragma mark - Adding to and removing from a map view @@ -89,52 +97,79 @@ namespace mbgl { - (void)removeFromMapView:(MGLMapView *)mapView { + _pendingLayer = nullptr; + _rawLayer = nullptr; + auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String); if (!removedLayer) { return; } - _pendingLayer = std::move(reinterpret_cast &>(removedLayer)); + + mbgl::style::LineLayer *layer = dynamic_cast(removedLayer.get()); + if (!layer) { + return; + } + + removedLayer.release(); + + _pendingLayer = std::unique_ptr(layer); _rawLayer = _pendingLayer.get(); } #pragma mark - Accessing the Layout Attributes - (void)setLineCap:(MGLStyleValue *)lineCap { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(lineCap); _rawLayer->setLineCap(mbglValue); } - (MGLStyleValue *)lineCap { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineCap() ?: _rawLayer->getDefaultLineCap(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setLineJoin:(MGLStyleValue *)lineJoin { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(lineJoin); _rawLayer->setLineJoin(mbglValue); } - (MGLStyleValue *)lineJoin { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineJoin() ?: _rawLayer->getDefaultLineJoin(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setLineMiterLimit:(MGLStyleValue *)lineMiterLimit { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(lineMiterLimit); _rawLayer->setLineMiterLimit(mbglValue); } - (MGLStyleValue *)lineMiterLimit { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineMiterLimit() ?: _rawLayer->getDefaultLineMiterLimit(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setLineRoundLimit:(MGLStyleValue *)lineRoundLimit { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(lineRoundLimit); _rawLayer->setLineRoundLimit(mbglValue); } - (MGLStyleValue *)lineRoundLimit { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineRoundLimit() ?: _rawLayer->getDefaultLineRoundLimit(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } @@ -142,101 +177,141 @@ namespace mbgl { #pragma mark - Accessing the Paint Attributes - (void)setLineBlur:(MGLStyleValue *)lineBlur { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(lineBlur); _rawLayer->setLineBlur(mbglValue); } - (MGLStyleValue *)lineBlur { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineBlur() ?: _rawLayer->getDefaultLineBlur(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setLineColor:(MGLStyleValue *)lineColor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(lineColor); _rawLayer->setLineColor(mbglValue); } - (MGLStyleValue *)lineColor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineColor() ?: _rawLayer->getDefaultLineColor(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setLineDasharray:(MGLStyleValue *> *)lineDasharray { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer, NSArray *, float>().toPropertyValue(lineDasharray); _rawLayer->setLineDasharray(mbglValue); } - (MGLStyleValue *> *)lineDasharray { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineDasharray() ?: _rawLayer->getDefaultLineDasharray(); return MGLStyleValueTransformer, NSArray *, float>().toStyleValue(propertyValue); } - (void)setLineGapWidth:(MGLStyleValue *)lineGapWidth { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(lineGapWidth); _rawLayer->setLineGapWidth(mbglValue); } - (MGLStyleValue *)lineGapWidth { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineGapWidth() ?: _rawLayer->getDefaultLineGapWidth(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setLineOffset:(MGLStyleValue *)lineOffset { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(lineOffset); _rawLayer->setLineOffset(mbglValue); } - (MGLStyleValue *)lineOffset { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineOffset() ?: _rawLayer->getDefaultLineOffset(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setLineOpacity:(MGLStyleValue *)lineOpacity { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(lineOpacity); _rawLayer->setLineOpacity(mbglValue); } - (MGLStyleValue *)lineOpacity { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineOpacity() ?: _rawLayer->getDefaultLineOpacity(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setLinePattern:(MGLStyleValue *)linePattern { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(linePattern); _rawLayer->setLinePattern(mbglValue); } - (MGLStyleValue *)linePattern { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLinePattern() ?: _rawLayer->getDefaultLinePattern(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setLineTranslate:(MGLStyleValue *)lineTranslate { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer, NSValue *>().toPropertyValue(lineTranslate); _rawLayer->setLineTranslate(mbglValue); } - (MGLStyleValue *)lineTranslate { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineTranslate() ?: _rawLayer->getDefaultLineTranslate(); return MGLStyleValueTransformer, NSValue *>().toStyleValue(propertyValue); } - (void)setLineTranslateAnchor:(MGLStyleValue *)lineTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(lineTranslateAnchor); _rawLayer->setLineTranslateAnchor(mbglValue); } - (MGLStyleValue *)lineTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineTranslateAnchor() ?: _rawLayer->getDefaultLineTranslateAnchor(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setLineWidth:(MGLStyleValue *)lineWidth { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(lineWidth); _rawLayer->setLineWidth(mbglValue); } - (MGLStyleValue *)lineWidth { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getLineWidth() ?: _rawLayer->getDefaultLineWidth(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } diff --git a/platform/darwin/src/MGLRasterStyleLayer.mm b/platform/darwin/src/MGLRasterStyleLayer.mm index cd1d135ff..24dba21c7 100644 --- a/platform/darwin/src/MGLRasterStyleLayer.mm +++ b/platform/darwin/src/MGLRasterStyleLayer.mm @@ -49,82 +49,121 @@ - (void)removeFromMapView:(MGLMapView *)mapView { + _pendingLayer = nullptr; + _rawLayer = nullptr; + auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String); if (!removedLayer) { return; } - _pendingLayer = std::move(reinterpret_cast &>(removedLayer)); + + mbgl::style::RasterLayer *layer = dynamic_cast(removedLayer.get()); + if (!layer) { + return; + } + + removedLayer.release(); + + _pendingLayer = std::unique_ptr(layer); _rawLayer = _pendingLayer.get(); } #pragma mark - Accessing the Paint Attributes - (void)setMaximumRasterBrightness:(MGLStyleValue *)maximumRasterBrightness { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(maximumRasterBrightness); _rawLayer->setRasterBrightnessMax(mbglValue); } - (MGLStyleValue *)maximumRasterBrightness { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getRasterBrightnessMax() ?: _rawLayer->getDefaultRasterBrightnessMax(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setMinimumRasterBrightness:(MGLStyleValue *)minimumRasterBrightness { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(minimumRasterBrightness); _rawLayer->setRasterBrightnessMin(mbglValue); } - (MGLStyleValue *)minimumRasterBrightness { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getRasterBrightnessMin() ?: _rawLayer->getDefaultRasterBrightnessMin(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setRasterContrast:(MGLStyleValue *)rasterContrast { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(rasterContrast); _rawLayer->setRasterContrast(mbglValue); } - (MGLStyleValue *)rasterContrast { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getRasterContrast() ?: _rawLayer->getDefaultRasterContrast(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setRasterFadeDuration:(MGLStyleValue *)rasterFadeDuration { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(rasterFadeDuration); _rawLayer->setRasterFadeDuration(mbglValue); } - (MGLStyleValue *)rasterFadeDuration { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getRasterFadeDuration() ?: _rawLayer->getDefaultRasterFadeDuration(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setRasterHueRotate:(MGLStyleValue *)rasterHueRotate { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(rasterHueRotate); _rawLayer->setRasterHueRotate(mbglValue); } - (MGLStyleValue *)rasterHueRotate { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getRasterHueRotate() ?: _rawLayer->getDefaultRasterHueRotate(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setRasterOpacity:(MGLStyleValue *)rasterOpacity { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(rasterOpacity); _rawLayer->setRasterOpacity(mbglValue); } - (MGLStyleValue *)rasterOpacity { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getRasterOpacity() ?: _rawLayer->getDefaultRasterOpacity(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setRasterSaturation:(MGLStyleValue *)rasterSaturation { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(rasterSaturation); _rawLayer->setRasterSaturation(mbglValue); } - (MGLStyleValue *)rasterSaturation { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getRasterSaturation() ?: _rawLayer->getDefaultRasterSaturation(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } diff --git a/platform/darwin/src/MGLStyleLayer.mm b/platform/darwin/src/MGLStyleLayer.mm index d1943cd46..6d9dabf25 100644 --- a/platform/darwin/src/MGLStyleLayer.mm +++ b/platform/darwin/src/MGLStyleLayer.mm @@ -3,10 +3,6 @@ #include -@interface MGLStyleLayer () - -@end - @implementation MGLStyleLayer - (instancetype)initWithIdentifier:(NSString *)identifier @@ -19,6 +15,8 @@ - (void)setVisible:(BOOL)visible { + MGLAssertStyleLayerIsValid(); + mbgl::style::VisibilityType v = visible ? mbgl::style::VisibilityType::Visible : mbgl::style::VisibilityType::None; @@ -27,27 +25,37 @@ - (BOOL)isVisible { + MGLAssertStyleLayerIsValid(); + mbgl::style::VisibilityType v = self.rawLayer->getVisibility(); return (v == mbgl::style::VisibilityType::Visible); } - (void)setMaximumZoomLevel:(float)maximumZoomLevel { + MGLAssertStyleLayerIsValid(); + self.rawLayer->setMaxZoom(maximumZoomLevel); } - (float)maximumZoomLevel { + MGLAssertStyleLayerIsValid(); + return self.rawLayer->getMaxZoom(); } - (void)setMinimumZoomLevel:(float)minimumZoomLevel { + MGLAssertStyleLayerIsValid(); + self.rawLayer->setMinZoom(minimumZoomLevel); } - (float)minimumZoomLevel { + MGLAssertStyleLayerIsValid(); + return self.rawLayer->getMinZoom(); } diff --git a/platform/darwin/src/MGLStyleLayer.mm.ejs b/platform/darwin/src/MGLStyleLayer.mm.ejs index ae90b67d3..85a1af997 100644 --- a/platform/darwin/src/MGLStyleLayer.mm.ejs +++ b/platform/darwin/src/MGLStyleLayer.mm.ejs @@ -81,22 +81,30 @@ namespace mbgl { <% if (type !== 'background' && type !== 'raster') { -%> - (NSString *)sourceLayerIdentifier { + MGLAssertStyleLayerIsValid(); + auto layerID = _rawLayer->getSourceLayer(); return layerID.empty() ? nil : @(layerID.c_str()); } - (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier { + MGLAssertStyleLayerIsValid(); + _rawLayer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: ""); } - (void)setPredicate:(NSPredicate *)predicate { + MGLAssertStyleLayerIsValid(); + _rawLayer->setFilter(predicate.mgl_filter); } - (NSPredicate *)predicate { + MGLAssertStyleLayerIsValid(); + return [NSPredicate mgl_predicateWithFilter:_rawLayer->getFilter()]; } <% } -%> @@ -119,11 +127,22 @@ namespace mbgl { - (void)removeFromMapView:(MGLMapView *)mapView { + _pendingLayer = nullptr; + _rawLayer = nullptr; + auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String); if (!removedLayer) { return; } - _pendingLayer = std::move(reinterpret_castLayer> &>(removedLayer)); + + mbgl::style::<%- camelize(type) %>Layer *layer = dynamic_castLayer *>(removedLayer.get()); + if (!layer) { + return; + } + + removedLayer.release(); + + _pendingLayer = std::unique_ptrLayer>(layer); _rawLayer = _pendingLayer.get(); } @@ -132,6 +151,8 @@ namespace mbgl { <% for (const property of layoutProperties) { -%> - (void)set<%- camelize(property.name) %>:(MGLStyleValue<<%- propertyType(property, true) %>> *)<%- objCName(property) %> { + MGLAssertStyleLayerIsValid(); + <% if (property.type == "enum") { -%> auto mbglValue = MGLStyleValueTransformer, NSValue *, mbgl::style::<%- mbglType(property) %>, MGL<%- camelize(originalPropertyName(property)) %>>().toEnumPropertyValue(<%- objCName(property) %>); _rawLayer->set<%- camelize(originalPropertyName(property)) %>(mbglValue); @@ -142,6 +163,8 @@ namespace mbgl { } - (MGLStyleValue<<%- propertyType(property, true) %>> *)<%- objCName(property) %> { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->get<%- camelize(originalPropertyName(property)) %>() ?: _rawLayer->getDefault<%- camelize(originalPropertyName(property)) %>(); <% if (property.type == "enum") { -%> return MGLStyleValueTransformer, NSValue *, mbgl::style::<%- mbglType(property) %>, MGL<%- camelize(originalPropertyName(property)) %>>().toEnumStyleValue(propertyValue); @@ -157,6 +180,8 @@ namespace mbgl { <% for (const property of paintProperties) { -%> - (void)set<%- camelize(property.name) %>:(MGLStyleValue<<%- propertyType(property, true) %>> *)<%- objCName(property) %> { + MGLAssertStyleLayerIsValid(); + <% if (property.type == "enum") { -%> auto mbglValue = MGLStyleValueTransformer, NSValue *, mbgl::style::<%- mbglType(property) %>, MGL<%- camelize(originalPropertyName(property)) %>>().toEnumPropertyValue(<%- objCName(property) %>); _rawLayer->set<%- camelize(originalPropertyName(property)) %>(mbglValue); @@ -167,6 +192,8 @@ namespace mbgl { } - (MGLStyleValue<<%- propertyType(property, true) %>> *)<%- objCName(property) %> { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->get<%- camelize(originalPropertyName(property)) %>() ?: _rawLayer->getDefault<%- camelize(originalPropertyName(property)) %>(); <% if (property.type == "enum") { -%> return MGLStyleValueTransformer, NSValue *, mbgl::style::<%- mbglType(property) %>, MGL<%- camelize(originalPropertyName(property)) %>>().toEnumStyleValue(propertyValue); diff --git a/platform/darwin/src/MGLStyleLayer_Private.h b/platform/darwin/src/MGLStyleLayer_Private.h index bfce452a4..e723c8cf1 100644 --- a/platform/darwin/src/MGLStyleLayer_Private.h +++ b/platform/darwin/src/MGLStyleLayer_Private.h @@ -5,6 +5,25 @@ #include +/** + Assert that the style layer is valid. + + This macro should be used at the beginning of any public-facing instance method + of `MGLStyleLayer` and its subclasses. For private methods, an assertion is more appropriate. + */ +#define MGLAssertStyleLayerIsValid() \ + do { \ + if (!self.rawLayer) { \ + [NSException raise:@"Invalid style layer" \ + format: \ + @"-[MGLStyle removeLayer:] has been called " \ + @"with this instance but another style layer instance was added with the same identifer. It is an " \ + @"error to send any message to this layer since it cannot be recovered after removal due to the " \ + @"identifer collision. Use unique identifiers for all layer instances including layers of " \ + @"different types."]; \ + } \ + } while (NO); + @class MGLMapView; @interface MGLStyleLayer (Private) diff --git a/platform/darwin/src/MGLSymbolStyleLayer.mm b/platform/darwin/src/MGLSymbolStyleLayer.mm index 3e5834f7b..29fe32218 100644 --- a/platform/darwin/src/MGLSymbolStyleLayer.mm +++ b/platform/darwin/src/MGLSymbolStyleLayer.mm @@ -99,22 +99,30 @@ namespace mbgl { } - (NSString *)sourceLayerIdentifier { + MGLAssertStyleLayerIsValid(); + auto layerID = _rawLayer->getSourceLayer(); return layerID.empty() ? nil : @(layerID.c_str()); } - (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier { + MGLAssertStyleLayerIsValid(); + _rawLayer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: ""); } - (void)setPredicate:(NSPredicate *)predicate { + MGLAssertStyleLayerIsValid(); + _rawLayer->setFilter(predicate.mgl_filter); } - (NSPredicate *)predicate { + MGLAssertStyleLayerIsValid(); + return [NSPredicate mgl_predicateWithFilter:_rawLayer->getFilter()]; } #pragma mark - Adding to and removing from a map view @@ -136,352 +144,499 @@ namespace mbgl { - (void)removeFromMapView:(MGLMapView *)mapView { + _pendingLayer = nullptr; + _rawLayer = nullptr; + auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String); if (!removedLayer) { return; } - _pendingLayer = std::move(reinterpret_cast &>(removedLayer)); + + mbgl::style::SymbolLayer *layer = dynamic_cast(removedLayer.get()); + if (!layer) { + return; + } + + removedLayer.release(); + + _pendingLayer = std::unique_ptr(layer); _rawLayer = _pendingLayer.get(); } #pragma mark - Accessing the Layout Attributes - (void)setIconAllowOverlap:(MGLStyleValue *)iconAllowOverlap { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconAllowOverlap); _rawLayer->setIconAllowOverlap(mbglValue); } - (MGLStyleValue *)iconAllowOverlap { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconAllowOverlap() ?: _rawLayer->getDefaultIconAllowOverlap(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconIgnorePlacement:(MGLStyleValue *)iconIgnorePlacement { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconIgnorePlacement); _rawLayer->setIconIgnorePlacement(mbglValue); } - (MGLStyleValue *)iconIgnorePlacement { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconIgnorePlacement() ?: _rawLayer->getDefaultIconIgnorePlacement(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconImageName:(MGLStyleValue *)iconImageName { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconImageName); _rawLayer->setIconImage(mbglValue); } - (MGLStyleValue *)iconImageName { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconImage() ?: _rawLayer->getDefaultIconImage(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconKeepUpright:(MGLStyleValue *)iconKeepUpright { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconKeepUpright); _rawLayer->setIconKeepUpright(mbglValue); } - (MGLStyleValue *)iconKeepUpright { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconKeepUpright() ?: _rawLayer->getDefaultIconKeepUpright(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconOffset:(MGLStyleValue *)iconOffset { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer, NSValue *>().toPropertyValue(iconOffset); _rawLayer->setIconOffset(mbglValue); } - (MGLStyleValue *)iconOffset { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconOffset() ?: _rawLayer->getDefaultIconOffset(); return MGLStyleValueTransformer, NSValue *>().toStyleValue(propertyValue); } - (void)setIconOptional:(MGLStyleValue *)iconOptional { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconOptional); _rawLayer->setIconOptional(mbglValue); } - (MGLStyleValue *)iconOptional { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconOptional() ?: _rawLayer->getDefaultIconOptional(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconPadding:(MGLStyleValue *)iconPadding { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconPadding); _rawLayer->setIconPadding(mbglValue); } - (MGLStyleValue *)iconPadding { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconPadding() ?: _rawLayer->getDefaultIconPadding(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconRotate:(MGLStyleValue *)iconRotate { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconRotate); _rawLayer->setIconRotate(mbglValue); } - (MGLStyleValue *)iconRotate { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconRotate() ?: _rawLayer->getDefaultIconRotate(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconRotationAlignment:(MGLStyleValue *)iconRotationAlignment { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(iconRotationAlignment); _rawLayer->setIconRotationAlignment(mbglValue); } - (MGLStyleValue *)iconRotationAlignment { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconRotationAlignment() ?: _rawLayer->getDefaultIconRotationAlignment(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setIconSize:(MGLStyleValue *)iconSize { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconSize); _rawLayer->setIconSize(mbglValue); } - (MGLStyleValue *)iconSize { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconSize() ?: _rawLayer->getDefaultIconSize(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconTextFit:(MGLStyleValue *)iconTextFit { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(iconTextFit); _rawLayer->setIconTextFit(mbglValue); } - (MGLStyleValue *)iconTextFit { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconTextFit() ?: _rawLayer->getDefaultIconTextFit(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setIconTextFitPadding:(MGLStyleValue *)iconTextFitPadding { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer, NSValue *>().toPropertyValue(iconTextFitPadding); _rawLayer->setIconTextFitPadding(mbglValue); } - (MGLStyleValue *)iconTextFitPadding { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconTextFitPadding() ?: _rawLayer->getDefaultIconTextFitPadding(); return MGLStyleValueTransformer, NSValue *>().toStyleValue(propertyValue); } - (void)setSymbolAvoidEdges:(MGLStyleValue *)symbolAvoidEdges { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(symbolAvoidEdges); _rawLayer->setSymbolAvoidEdges(mbglValue); } - (MGLStyleValue *)symbolAvoidEdges { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getSymbolAvoidEdges() ?: _rawLayer->getDefaultSymbolAvoidEdges(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setSymbolPlacement:(MGLStyleValue *)symbolPlacement { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(symbolPlacement); _rawLayer->setSymbolPlacement(mbglValue); } - (MGLStyleValue *)symbolPlacement { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getSymbolPlacement() ?: _rawLayer->getDefaultSymbolPlacement(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setSymbolSpacing:(MGLStyleValue *)symbolSpacing { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(symbolSpacing); _rawLayer->setSymbolSpacing(mbglValue); } - (MGLStyleValue *)symbolSpacing { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getSymbolSpacing() ?: _rawLayer->getDefaultSymbolSpacing(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextAllowOverlap:(MGLStyleValue *)textAllowOverlap { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textAllowOverlap); _rawLayer->setTextAllowOverlap(mbglValue); } - (MGLStyleValue *)textAllowOverlap { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextAllowOverlap() ?: _rawLayer->getDefaultTextAllowOverlap(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextAnchor:(MGLStyleValue *)textAnchor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(textAnchor); _rawLayer->setTextAnchor(mbglValue); } - (MGLStyleValue *)textAnchor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextAnchor() ?: _rawLayer->getDefaultTextAnchor(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setTextField:(MGLStyleValue *)textField { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textField); _rawLayer->setTextField(mbglValue); } - (MGLStyleValue *)textField { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextField() ?: _rawLayer->getDefaultTextField(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextFont:(MGLStyleValue *> *)textFont { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer, NSArray *, std::string>().toPropertyValue(textFont); _rawLayer->setTextFont(mbglValue); } - (MGLStyleValue *> *)textFont { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextFont() ?: _rawLayer->getDefaultTextFont(); return MGLStyleValueTransformer, NSArray *, std::string>().toStyleValue(propertyValue); } - (void)setTextIgnorePlacement:(MGLStyleValue *)textIgnorePlacement { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textIgnorePlacement); _rawLayer->setTextIgnorePlacement(mbglValue); } - (MGLStyleValue *)textIgnorePlacement { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextIgnorePlacement() ?: _rawLayer->getDefaultTextIgnorePlacement(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextJustify:(MGLStyleValue *)textJustify { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(textJustify); _rawLayer->setTextJustify(mbglValue); } - (MGLStyleValue *)textJustify { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextJustify() ?: _rawLayer->getDefaultTextJustify(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setTextKeepUpright:(MGLStyleValue *)textKeepUpright { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textKeepUpright); _rawLayer->setTextKeepUpright(mbglValue); } - (MGLStyleValue *)textKeepUpright { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextKeepUpright() ?: _rawLayer->getDefaultTextKeepUpright(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextLetterSpacing:(MGLStyleValue *)textLetterSpacing { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textLetterSpacing); _rawLayer->setTextLetterSpacing(mbglValue); } - (MGLStyleValue *)textLetterSpacing { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextLetterSpacing() ?: _rawLayer->getDefaultTextLetterSpacing(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextLineHeight:(MGLStyleValue *)textLineHeight { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textLineHeight); _rawLayer->setTextLineHeight(mbglValue); } - (MGLStyleValue *)textLineHeight { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextLineHeight() ?: _rawLayer->getDefaultTextLineHeight(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextMaxAngle:(MGLStyleValue *)textMaxAngle { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textMaxAngle); _rawLayer->setTextMaxAngle(mbglValue); } - (MGLStyleValue *)textMaxAngle { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextMaxAngle() ?: _rawLayer->getDefaultTextMaxAngle(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextMaxWidth:(MGLStyleValue *)textMaxWidth { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textMaxWidth); _rawLayer->setTextMaxWidth(mbglValue); } - (MGLStyleValue *)textMaxWidth { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextMaxWidth() ?: _rawLayer->getDefaultTextMaxWidth(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextOffset:(MGLStyleValue *)textOffset { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer, NSValue *>().toPropertyValue(textOffset); _rawLayer->setTextOffset(mbglValue); } - (MGLStyleValue *)textOffset { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextOffset() ?: _rawLayer->getDefaultTextOffset(); return MGLStyleValueTransformer, NSValue *>().toStyleValue(propertyValue); } - (void)setTextOptional:(MGLStyleValue *)textOptional { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textOptional); _rawLayer->setTextOptional(mbglValue); } - (MGLStyleValue *)textOptional { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextOptional() ?: _rawLayer->getDefaultTextOptional(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextPadding:(MGLStyleValue *)textPadding { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textPadding); _rawLayer->setTextPadding(mbglValue); } - (MGLStyleValue *)textPadding { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextPadding() ?: _rawLayer->getDefaultTextPadding(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextPitchAlignment:(MGLStyleValue *)textPitchAlignment { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(textPitchAlignment); _rawLayer->setTextPitchAlignment(mbglValue); } - (MGLStyleValue *)textPitchAlignment { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextPitchAlignment() ?: _rawLayer->getDefaultTextPitchAlignment(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setTextRotate:(MGLStyleValue *)textRotate { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textRotate); _rawLayer->setTextRotate(mbglValue); } - (MGLStyleValue *)textRotate { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextRotate() ?: _rawLayer->getDefaultTextRotate(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextRotationAlignment:(MGLStyleValue *)textRotationAlignment { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(textRotationAlignment); _rawLayer->setTextRotationAlignment(mbglValue); } - (MGLStyleValue *)textRotationAlignment { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextRotationAlignment() ?: _rawLayer->getDefaultTextRotationAlignment(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setTextSize:(MGLStyleValue *)textSize { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textSize); _rawLayer->setTextSize(mbglValue); } - (MGLStyleValue *)textSize { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextSize() ?: _rawLayer->getDefaultTextSize(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextTransform:(MGLStyleValue *)textTransform { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(textTransform); _rawLayer->setTextTransform(mbglValue); } - (MGLStyleValue *)textTransform { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextTransform() ?: _rawLayer->getDefaultTextTransform(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } @@ -489,141 +644,197 @@ namespace mbgl { #pragma mark - Accessing the Paint Attributes - (void)setIconColor:(MGLStyleValue *)iconColor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconColor); _rawLayer->setIconColor(mbglValue); } - (MGLStyleValue *)iconColor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconColor() ?: _rawLayer->getDefaultIconColor(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconHaloBlur:(MGLStyleValue *)iconHaloBlur { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconHaloBlur); _rawLayer->setIconHaloBlur(mbglValue); } - (MGLStyleValue *)iconHaloBlur { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconHaloBlur() ?: _rawLayer->getDefaultIconHaloBlur(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconHaloColor:(MGLStyleValue *)iconHaloColor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconHaloColor); _rawLayer->setIconHaloColor(mbglValue); } - (MGLStyleValue *)iconHaloColor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconHaloColor() ?: _rawLayer->getDefaultIconHaloColor(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconHaloWidth:(MGLStyleValue *)iconHaloWidth { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconHaloWidth); _rawLayer->setIconHaloWidth(mbglValue); } - (MGLStyleValue *)iconHaloWidth { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconHaloWidth() ?: _rawLayer->getDefaultIconHaloWidth(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconOpacity:(MGLStyleValue *)iconOpacity { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(iconOpacity); _rawLayer->setIconOpacity(mbglValue); } - (MGLStyleValue *)iconOpacity { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconOpacity() ?: _rawLayer->getDefaultIconOpacity(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setIconTranslate:(MGLStyleValue *)iconTranslate { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer, NSValue *>().toPropertyValue(iconTranslate); _rawLayer->setIconTranslate(mbglValue); } - (MGLStyleValue *)iconTranslate { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconTranslate() ?: _rawLayer->getDefaultIconTranslate(); return MGLStyleValueTransformer, NSValue *>().toStyleValue(propertyValue); } - (void)setIconTranslateAnchor:(MGLStyleValue *)iconTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(iconTranslateAnchor); _rawLayer->setIconTranslateAnchor(mbglValue); } - (MGLStyleValue *)iconTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getIconTranslateAnchor() ?: _rawLayer->getDefaultIconTranslateAnchor(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); } - (void)setTextColor:(MGLStyleValue *)textColor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textColor); _rawLayer->setTextColor(mbglValue); } - (MGLStyleValue *)textColor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextColor() ?: _rawLayer->getDefaultTextColor(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextHaloBlur:(MGLStyleValue *)textHaloBlur { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textHaloBlur); _rawLayer->setTextHaloBlur(mbglValue); } - (MGLStyleValue *)textHaloBlur { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextHaloBlur() ?: _rawLayer->getDefaultTextHaloBlur(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextHaloColor:(MGLStyleValue *)textHaloColor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textHaloColor); _rawLayer->setTextHaloColor(mbglValue); } - (MGLStyleValue *)textHaloColor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextHaloColor() ?: _rawLayer->getDefaultTextHaloColor(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextHaloWidth:(MGLStyleValue *)textHaloWidth { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textHaloWidth); _rawLayer->setTextHaloWidth(mbglValue); } - (MGLStyleValue *)textHaloWidth { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextHaloWidth() ?: _rawLayer->getDefaultTextHaloWidth(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextOpacity:(MGLStyleValue *)textOpacity { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toPropertyValue(textOpacity); _rawLayer->setTextOpacity(mbglValue); } - (MGLStyleValue *)textOpacity { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextOpacity() ?: _rawLayer->getDefaultTextOpacity(); return MGLStyleValueTransformer().toStyleValue(propertyValue); } - (void)setTextTranslate:(MGLStyleValue *)textTranslate { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer, NSValue *>().toPropertyValue(textTranslate); _rawLayer->setTextTranslate(mbglValue); } - (MGLStyleValue *)textTranslate { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextTranslate() ?: _rawLayer->getDefaultTextTranslate(); return MGLStyleValueTransformer, NSValue *>().toStyleValue(propertyValue); } - (void)setTextTranslateAnchor:(MGLStyleValue *)textTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer().toEnumPropertyValue(textTranslateAnchor); _rawLayer->setTextTranslateAnchor(mbglValue); } - (MGLStyleValue *)textTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getTextTranslateAnchor() ?: _rawLayer->getDefaultTextTranslateAnchor(); return MGLStyleValueTransformer().toEnumStyleValue(propertyValue); }