diff options
author | Jesse Bounds <jesse@rebounds.net> | 2016-11-30 11:59:29 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-30 11:59:29 -0800 |
commit | 37a087c675b847f64dd73a570c11b4ddebac35fb (patch) | |
tree | 92a76a6dc4ad7aa4aecb0312de7183c012bd9793 /platform/darwin/src/MGLCircleStyleLayer.mm | |
parent | ba775404d88ddb61b7347eb14c5e39cf2df8e1dd (diff) | |
download | qtlocation-mapboxgl-37a087c675b847f64dd73a570c11b4ddebac35fb.tar.gz |
[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.
Diffstat (limited to 'platform/darwin/src/MGLCircleStyleLayer.mm')
-rw-r--r-- | platform/darwin/src/MGLCircleStyleLayer.mm | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/platform/darwin/src/MGLCircleStyleLayer.mm b/platform/darwin/src/MGLCircleStyleLayer.mm index 1c903ab008..7768ff66c9 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<std::unique_ptr<mbgl::style::CircleLayer> &>(removedLayer)); + + mbgl::style::CircleLayer *layer = dynamic_cast<mbgl::style::CircleLayer *>(removedLayer.get()); + if (!layer) { + return; + } + + removedLayer.release(); + + _pendingLayer = std::unique_ptr<mbgl::style::CircleLayer>(layer); _rawLayer = _pendingLayer.get(); } #pragma mark - Accessing the Paint Attributes - (void)setCircleBlur:(MGLStyleValue<NSNumber *> *)circleBlur { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(circleBlur); _rawLayer->setCircleBlur(mbglValue); } - (MGLStyleValue<NSNumber *> *)circleBlur { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleBlur() ?: _rawLayer->getDefaultCircleBlur(); return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue); } - (void)setCircleColor:(MGLStyleValue<MGLColor *> *)circleColor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toPropertyValue(circleColor); _rawLayer->setCircleColor(mbglValue); } - (MGLStyleValue<MGLColor *> *)circleColor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleColor() ?: _rawLayer->getDefaultCircleColor(); return MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toStyleValue(propertyValue); } - (void)setCircleOpacity:(MGLStyleValue<NSNumber *> *)circleOpacity { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(circleOpacity); _rawLayer->setCircleOpacity(mbglValue); } - (MGLStyleValue<NSNumber *> *)circleOpacity { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleOpacity() ?: _rawLayer->getDefaultCircleOpacity(); return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue); } - (void)setCirclePitchScale:(MGLStyleValue<NSValue *> *)circlePitchScale { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer<mbgl::style::CirclePitchScaleType, NSValue *, mbgl::style::CirclePitchScaleType, MGLCirclePitchScale>().toEnumPropertyValue(circlePitchScale); _rawLayer->setCirclePitchScale(mbglValue); } - (MGLStyleValue<NSValue *> *)circlePitchScale { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCirclePitchScale() ?: _rawLayer->getDefaultCirclePitchScale(); return MGLStyleValueTransformer<mbgl::style::CirclePitchScaleType, NSValue *, mbgl::style::CirclePitchScaleType, MGLCirclePitchScale>().toEnumStyleValue(propertyValue); } - (void)setCircleRadius:(MGLStyleValue<NSNumber *> *)circleRadius { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(circleRadius); _rawLayer->setCircleRadius(mbglValue); } - (MGLStyleValue<NSNumber *> *)circleRadius { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleRadius() ?: _rawLayer->getDefaultCircleRadius(); return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue); } - (void)setCircleTranslate:(MGLStyleValue<NSValue *> *)circleTranslate { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toPropertyValue(circleTranslate); _rawLayer->setCircleTranslate(mbglValue); } - (MGLStyleValue<NSValue *> *)circleTranslate { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleTranslate() ?: _rawLayer->getDefaultCircleTranslate(); return MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toStyleValue(propertyValue); } - (void)setCircleTranslateAnchor:(MGLStyleValue<NSValue *> *)circleTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto mbglValue = MGLStyleValueTransformer<mbgl::style::TranslateAnchorType, NSValue *, mbgl::style::TranslateAnchorType, MGLCircleTranslateAnchor>().toEnumPropertyValue(circleTranslateAnchor); _rawLayer->setCircleTranslateAnchor(mbglValue); } - (MGLStyleValue<NSValue *> *)circleTranslateAnchor { + MGLAssertStyleLayerIsValid(); + auto propertyValue = _rawLayer->getCircleTranslateAnchor() ?: _rawLayer->getDefaultCircleTranslateAnchor(); return MGLStyleValueTransformer<mbgl::style::TranslateAnchorType, NSValue *, mbgl::style::TranslateAnchorType, MGLCircleTranslateAnchor>().toEnumStyleValue(propertyValue); } |