summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLStyleLayer.mm
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2017-04-03 14:25:37 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2017-04-13 10:33:18 -0700
commitde6c9b35f35f6ec0950529261b207d716c046beb (patch)
treef022e3e09b4f8ffbe7646341f91aa76b8081ff62 /platform/darwin/src/MGLStyleLayer.mm
parent98d005792b68d0b299f123c1d31e50c72ba91ba8 (diff)
downloadqtlocation-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/MGLStyleLayer.mm')
-rw-r--r--platform/darwin/src/MGLStyleLayer.mm43
1 files changed, 38 insertions, 5 deletions
diff --git a/platform/darwin/src/MGLStyleLayer.mm b/platform/darwin/src/MGLStyleLayer.mm
index 47f41e0388..c72541526f 100644
--- a/platform/darwin/src/MGLStyleLayer.mm
+++ b/platform/darwin/src/MGLStyleLayer.mm
@@ -1,24 +1,57 @@
#import "MGLStyleLayer_Private.h"
#import "MGLMapView_Private.h"
+#include <mbgl/map/map.hpp>
#include <mbgl/style/layer.hpp>
@interface MGLStyleLayer ()
-@property (nonatomic) mbgl::style::Layer *rawLayer;
+@property (nonatomic, readonly) mbgl::style::Layer *rawLayer;
@end
-@implementation MGLStyleLayer
+@implementation MGLStyleLayer {
+ std::unique_ptr<mbgl::style::Layer> _pendingLayer;
+}
-- (instancetype)initWithIdentifier:(NSString *)identifier
-{
+- (instancetype)initWithRawLayer:(mbgl::style::Layer *)rawLayer {
if (self = [super init]) {
- _identifier = identifier;
+ _identifier = @(rawLayer->getID().c_str());
+ _rawLayer = rawLayer;
+ }
+ return self;
+}
+
+- (instancetype)initWithPendingLayer:(std::unique_ptr<mbgl::style::Layer>)pendingLayer {
+ if (self = [self initWithRawLayer:pendingLayer.get()]) {
+ _pendingLayer = std::move(pendingLayer);
}
return self;
}
+- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
+{
+ if (_pendingLayer == nullptr) {
+ [NSException raise:@"MGLRedundantLayerException"
+ format:@"This instance %@ was already added to %@. Adding the same layer instance " \
+ "to the style more than once is invalid.", self, mapView.style];
+ }
+
+ 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
+{
+ if (self.rawLayer == mapView.mbglMap->getLayer(self.identifier.UTF8String)) {
+ _pendingLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
+ }
+}
+
- (void)setVisible:(BOOL)visible
{
MGLAssertStyleLayerIsValid();