diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2017-04-03 14:25:37 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2017-04-13 10:33:18 -0700 |
commit | de6c9b35f35f6ec0950529261b207d716c046beb (patch) | |
tree | f022e3e09b4f8ffbe7646341f91aa76b8081ff62 /platform/darwin/src/MGLOpenGLStyleLayer.mm | |
parent | 98d005792b68d0b299f123c1d31e50c72ba91ba8 (diff) | |
download | qtlocation-mapboxgl-de6c9b35f35f6ec0950529261b207d716c046beb.tar.gz |
[darwin] Simplify MGLStyleLayer initialization and pointer management
Similarly to the previous commit, introduce `-[MGLStyleLayer initWithPendingLayer:]`, allowing the base class to track the owned `_pendingSource` pointer and implement `-addToMapView:` and `-removeFromMapView:` without any casts.
Fixes an issue where `-[MGLStyle layerFromMBGLLayer:]` would wind up creating layers whose `_rawLayer` and `_pendingLayer` held different values.
Diffstat (limited to 'platform/darwin/src/MGLOpenGLStyleLayer.mm')
-rw-r--r-- | platform/darwin/src/MGLOpenGLStyleLayer.mm | 40 |
1 files changed, 10 insertions, 30 deletions
diff --git a/platform/darwin/src/MGLOpenGLStyleLayer.mm b/platform/darwin/src/MGLOpenGLStyleLayer.mm index da131b6de8..39eda758eb 100644 --- a/platform/darwin/src/MGLOpenGLStyleLayer.mm +++ b/platform/darwin/src/MGLOpenGLStyleLayer.mm @@ -72,7 +72,7 @@ void MGLFinishCustomStyleLayer(void *context) { */ @interface MGLOpenGLStyleLayer () -@property (nonatomic) mbgl::style::CustomLayer *rawLayer; +@property (nonatomic, readonly) mbgl::style::CustomLayer *rawLayer; /** The map view whose style currently contains the layer. @@ -84,9 +84,7 @@ void MGLFinishCustomStyleLayer(void *context) { @end -@implementation MGLOpenGLStyleLayer { - std::unique_ptr<mbgl::style::CustomLayer> _pendingLayer; -} +@implementation MGLOpenGLStyleLayer /** Returns an OpenGL style layer object initialized with the given identifier. @@ -100,26 +98,18 @@ void MGLFinishCustomStyleLayer(void *context) { @return An initialized OpenGL style layer. */ - (instancetype)initWithIdentifier:(NSString *)identifier { - if (self = [super initWithIdentifier:identifier]) { - auto layer = std::make_unique<mbgl::style::CustomLayer>(identifier.UTF8String, - MGLPrepareCustomStyleLayer, - MGLDrawCustomStyleLayer, - MGLFinishCustomStyleLayer, - (__bridge void *)self); - _pendingLayer = std::move(layer); - self.rawLayer = _pendingLayer.get(); - } - return self; + auto layer = std::make_unique<mbgl::style::CustomLayer>(identifier.UTF8String, + MGLPrepareCustomStyleLayer, + MGLDrawCustomStyleLayer, + MGLFinishCustomStyleLayer, + (__bridge void *)self); + return self = [super initWithPendingLayer:std::move(layer)]; } - (mbgl::style::CustomLayer *)rawLayer { return (mbgl::style::CustomLayer *)super.rawLayer; } -- (void)setRawLayer:(mbgl::style::CustomLayer *)rawLayer { - super.rawLayer = rawLayer; -} - #pragma mark - Adding to and removing from a map view - (void)setMapView:(MGLMapView *)mapView { @@ -134,22 +124,12 @@ void MGLFinishCustomStyleLayer(void *context) { - (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer { self.mapView = mapView; - 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)); - } + [super addToMapView:mapView belowLayer:otherLayer]; } - (void)removeFromMapView:(MGLMapView *)mapView { - auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String); + [super removeFromMapView:mapView]; self.mapView = nil; - if (!removedLayer) { - return; - } - _pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::CustomLayer> &>(removedLayer)); - self.rawLayer = _pendingLayer.get(); } /** |