diff options
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/MGLComputedShapeSource.mm | 3 | ||||
-rw-r--r-- | platform/darwin/src/MGLForegroundStyleLayer.mm | 16 | ||||
-rw-r--r-- | platform/darwin/src/MGLImageSource.mm | 19 | ||||
-rw-r--r-- | platform/darwin/src/MGLRasterTileSource.mm | 2 | ||||
-rw-r--r-- | platform/darwin/src/MGLRasterTileSource_Private.h | 2 | ||||
-rw-r--r-- | platform/darwin/src/MGLShapeSource.mm | 16 | ||||
-rw-r--r-- | platform/darwin/src/MGLSource.h | 3 | ||||
-rw-r--r-- | platform/darwin/src/MGLSource.mm | 15 | ||||
-rw-r--r-- | platform/darwin/src/MGLSource_Private.h | 15 | ||||
-rw-r--r-- | platform/darwin/src/MGLStyle.mm | 21 | ||||
-rw-r--r-- | platform/darwin/src/MGLStyleLayer.mm | 22 | ||||
-rw-r--r-- | platform/darwin/src/MGLStyleLayer_Private.h | 1 | ||||
-rw-r--r-- | platform/darwin/src/MGLTypes.h | 4 | ||||
-rw-r--r-- | platform/darwin/src/MGLVectorStyleLayer.m | 25 | ||||
-rw-r--r-- | platform/darwin/src/MGLVectorStyleLayer.mm | 34 | ||||
-rw-r--r-- | platform/darwin/src/MGLVectorTileSource.mm | 4 |
16 files changed, 152 insertions, 50 deletions
diff --git a/platform/darwin/src/MGLComputedShapeSource.mm b/platform/darwin/src/MGLComputedShapeSource.mm index 5110435b03..a04181af2f 100644 --- a/platform/darwin/src/MGLComputedShapeSource.mm +++ b/platform/darwin/src/MGLComputedShapeSource.mm @@ -199,6 +199,7 @@ mbgl::style::CustomGeometrySource::Options MBGLCustomGeometrySourceOptionsFromDi } - (void)setFeatures:(NSArray<MGLShape <MGLFeature> *>*)features inTileAtX:(NSUInteger)x y:(NSUInteger)y zoomLevel:(NSUInteger)zoomLevel { + MGLAssertStyleSourceIsValid(); mbgl::CanonicalTileID tileID = mbgl::CanonicalTileID((uint8_t)zoomLevel, (uint32_t)x, (uint32_t)y); mbgl::FeatureCollection featureCollection; featureCollection.reserve(features.count); @@ -236,10 +237,12 @@ mbgl::style::CustomGeometrySource::Options MBGLCustomGeometrySourceOptionsFromDi } - (void) invalidateBounds:(MGLCoordinateBounds)bounds { + MGLAssertStyleSourceIsValid(); ((mbgl::style::CustomGeometrySource *)self.rawSource)->invalidateRegion(MGLLatLngBoundsFromCoordinateBounds(bounds)); } - (void) invalidateTileAtX:(NSUInteger)x y:(NSUInteger)y zoomLevel:(NSUInteger)z { + MGLAssertStyleSourceIsValid(); ((mbgl::style::CustomGeometrySource *)self.rawSource)->invalidateTile(mbgl::CanonicalTileID(z, (unsigned int)x, (unsigned int)y)); } diff --git a/platform/darwin/src/MGLForegroundStyleLayer.mm b/platform/darwin/src/MGLForegroundStyleLayer.mm index 76700d6f77..eaa5e83e59 100644 --- a/platform/darwin/src/MGLForegroundStyleLayer.mm +++ b/platform/darwin/src/MGLForegroundStyleLayer.mm @@ -1,4 +1,5 @@ #import "MGLForegroundStyleLayer.h" +#import "MGLStyleLayer_Private.h" @implementation MGLForegroundStyleLayer @@ -9,10 +10,17 @@ } - (NSString *)description { - return [NSString stringWithFormat: - @"<%@: %p; identifier = %@; sourceIdentifier = %@; visible = %@>", - NSStringFromClass([self class]), (void *)self, self.identifier, - self.sourceIdentifier, self.visible ? @"YES" : @"NO"]; + if (self.rawLayer) { + return [NSString stringWithFormat: + @"<%@: %p; identifier = %@; sourceIdentifier = %@; visible = %@>", + NSStringFromClass([self class]), (void *)self, self.identifier, + self.sourceIdentifier, self.visible ? @"YES" : @"NO"]; + } + else { + return [NSString stringWithFormat: + @"<%@: %p; identifier = %@; sourceIdentifier = <unknown>; visible = NO>", + NSStringFromClass([self class]), (void *)self, self.identifier]; + } } @end diff --git a/platform/darwin/src/MGLImageSource.mm b/platform/darwin/src/MGLImageSource.mm index 351247e901..421cc7a155 100644 --- a/platform/darwin/src/MGLImageSource.mm +++ b/platform/darwin/src/MGLImageSource.mm @@ -45,11 +45,13 @@ } - (NSURL *)URL { + MGLAssertStyleSourceIsValid(); auto url = self.rawSource->getURL(); return url ? [NSURL URLWithString:@(url->c_str())] : nil; } - (void)setURL:(NSURL *)url { + MGLAssertStyleSourceIsValid(); if (url) { self.rawSource->setURL(url.mgl_URLByStandardizingScheme.absoluteString.UTF8String); _image = nil; @@ -59,6 +61,7 @@ } - (void)setImage:(MGLImage *)image { + MGLAssertStyleSourceIsValid(); if (image != nullptr) { self.rawSource->setImage(image.mgl_premultipliedImage); } else { @@ -68,16 +71,27 @@ } - (MGLCoordinateQuad)coordinates { + MGLAssertStyleSourceIsValid(); return MGLCoordinateQuadFromLatLngArray(self.rawSource->getCoordinates()); } - (void)setCoordinates: (MGLCoordinateQuad)coordinateQuad { + MGLAssertStyleSourceIsValid(); self.rawSource->setCoordinates(MGLLatLngArrayFromCoordinateQuad(coordinateQuad)); } - (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p; identifier = %@; coordinates = %@; URL = %@; image = %@>", - NSStringFromClass([self class]), (void *)self, self.identifier, MGLStringFromCoordinateQuad(self.coordinates), self.URL, self.image]; + if (self.rawSource) { + return [NSString stringWithFormat:@"<%@: %p; identifier = %@; coordinates = %@; URL = %@; image = %@>", + NSStringFromClass([self class]), (void *)self, self.identifier, + MGLStringFromCoordinateQuad(self.coordinates), + self.URL, + self.image]; + } + else { + return [NSString stringWithFormat:@"<%@: %p; identifier = %@; coordinates = <unknown>; URL = <unknown>; image = %@>", + NSStringFromClass([self class]), (void *)self, self.identifier, self.image]; + } } - (mbgl::style::ImageSource *)rawSource { @@ -85,6 +99,7 @@ } - (NSString *)attributionHTMLString { + MGLAssertStyleSourceIsValid(); auto attribution = self.rawSource->getAttribution(); return attribution ? @(attribution->c_str()) : nil; } diff --git a/platform/darwin/src/MGLRasterTileSource.mm b/platform/darwin/src/MGLRasterTileSource.mm index 61e9ef97fd..e89367711e 100644 --- a/platform/darwin/src/MGLRasterTileSource.mm +++ b/platform/darwin/src/MGLRasterTileSource.mm @@ -65,11 +65,13 @@ static const CGFloat MGLRasterTileSourceRetinaTileSize = 512; } - (NSURL *)configurationURL { + MGLAssertStyleSourceIsValid(); auto url = self.rawSource->getURL(); return url ? [NSURL URLWithString:@(url->c_str())] : nil; } - (NSString *)attributionHTMLString { + MGLAssertStyleSourceIsValid(); auto attribution = self.rawSource->getAttribution(); return attribution ? @(attribution->c_str()) : nil; } diff --git a/platform/darwin/src/MGLRasterTileSource_Private.h b/platform/darwin/src/MGLRasterTileSource_Private.h index 128dcb447d..8502b811e2 100644 --- a/platform/darwin/src/MGLRasterTileSource_Private.h +++ b/platform/darwin/src/MGLRasterTileSource_Private.h @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN @interface MGLRasterTileSource (Private) -@property (nonatomic, readonly) mbgl::style::RasterSource *rawSource; +@property (nonatomic, readonly, nullable) mbgl::style::RasterSource *rawSource; - (std::unique_ptr<mbgl::style::RasterSource>)pendingSourceWithIdentifier:(NSString *)identifier configurationURL:(NSURL *)configurationURL tileSize:(CGFloat)tileSize; diff --git a/platform/darwin/src/MGLShapeSource.mm b/platform/darwin/src/MGLShapeSource.mm index fc526f9850..3628a0eb74 100644 --- a/platform/darwin/src/MGLShapeSource.mm +++ b/platform/darwin/src/MGLShapeSource.mm @@ -150,11 +150,13 @@ mbgl::style::GeoJSONOptions MGLGeoJSONOptionsFromDictionary(NSDictionary<MGLShap } - (NSURL *)URL { + MGLAssertStyleSourceIsValid(); auto url = self.rawSource->getURL(); return url ? [NSURL URLWithString:@(url->c_str())] : nil; } - (void)setURL:(NSURL *)url { + MGLAssertStyleSourceIsValid(); if (url) { self.rawSource->setURL(url.mgl_URLByStandardizingScheme.absoluteString.UTF8String); _shape = nil; @@ -164,17 +166,24 @@ mbgl::style::GeoJSONOptions MGLGeoJSONOptionsFromDictionary(NSDictionary<MGLShap } - (void)setShape:(MGLShape *)shape { + MGLAssertStyleSourceIsValid(); self.rawSource->setGeoJSON({ shape.geoJSONObject }); _shape = shape; } - (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p; identifier = %@; URL = %@; shape = %@>", - NSStringFromClass([self class]), (void *)self, self.identifier, self.URL, self.shape]; + if (self.rawSource) { + return [NSString stringWithFormat:@"<%@: %p; identifier = %@; URL = %@; shape = %@>", + NSStringFromClass([self class]), (void *)self, self.identifier, self.URL, self.shape]; + } + else { + return [NSString stringWithFormat:@"<%@: %p; identifier = %@; URL = <unknown>; shape = %@>", + NSStringFromClass([self class]), (void *)self, self.identifier, self.shape]; + } } - (NSArray<id <MGLFeature>> *)featuresMatchingPredicate:(nullable NSPredicate *)predicate { - + MGLAssertStyleSourceIsValid(); mbgl::optional<mbgl::style::Filter> optionalFilter; if (predicate) { optionalFilter = predicate.mgl_filter; @@ -190,6 +199,7 @@ mbgl::style::GeoJSONOptions MGLGeoJSONOptionsFromDictionary(NSDictionary<MGLShap #pragma mark - MGLCluster management - (mbgl::optional<mbgl::FeatureExtensionValue>)featureExtensionValueOfCluster:(MGLShape<MGLCluster> *)cluster extension:(std::string)extension options:(const std::map<std::string, mbgl::Value>)options { + MGLAssertStyleSourceIsValid(); mbgl::optional<mbgl::FeatureExtensionValue> extensionValue; // Check parameters diff --git a/platform/darwin/src/MGLSource.h b/platform/darwin/src/MGLSource.h index 7bbf02fa7c..dc92e652e8 100644 --- a/platform/darwin/src/MGLSource.h +++ b/platform/darwin/src/MGLSource.h @@ -1,9 +1,12 @@ #import <Foundation/Foundation.h> #import "MGLFoundation.h" +#import "MGLTypes.h" NS_ASSUME_NONNULL_BEGIN +FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLInvalidStyleSourceException; + /** `MGLSource` is an abstract base class for map content sources. A map content source supplies content to be shown on the map. A source is added to an diff --git a/platform/darwin/src/MGLSource.mm b/platform/darwin/src/MGLSource.mm index fd54627471..ef23fe30e7 100644 --- a/platform/darwin/src/MGLSource.mm +++ b/platform/darwin/src/MGLSource.mm @@ -7,6 +7,8 @@ #include <mbgl/map/map.hpp> #include <mbgl/style/source.hpp> +const MGLExceptionName MGLInvalidStyleSourceException = @"MGLInvalidStyleSourceException"; + @interface MGLSource () // Even though this class is abstract, MGLStyle uses it to represent some @@ -19,13 +21,14 @@ @implementation MGLSource { std::unique_ptr<mbgl::style::Source> _pendingSource; + mapbox::base::WeakPtr<mbgl::style::Source> _weakSource; } - (instancetype)initWithIdentifier:(NSString *)identifier { if (self = [super init]) { - _identifier = identifier; + _identifier = [identifier copy]; } return self; } @@ -33,13 +36,18 @@ - (instancetype)initWithRawSource:(mbgl::style::Source *)rawSource mapView:(MGLMapView *)mapView { NSString *identifier = @(rawSource->getID().c_str()); if (self = [self initWithIdentifier:identifier]) { - _rawSource = rawSource; - _rawSource->peer = SourceWrapper { self }; + _weakSource = rawSource->makeWeakPtr(); + rawSource->peer = SourceWrapper { self }; _mapView = mapView; } return self; } +- (mbgl::style::Source *)rawSource +{ + return _weakSource.get(); +} + - (instancetype)initWithPendingSource:(std::unique_ptr<mbgl::style::Source>)pendingSource { if (self = [self initWithRawSource:pendingSource.get() mapView:nil]) { _pendingSource = std::move(pendingSource); @@ -59,6 +67,7 @@ } - (BOOL)removeFromMapView:(MGLMapView *)mapView error:(NSError * __nullable * __nullable)outError { + MGLAssertStyleSourceIsValid(); BOOL removed = NO; if (self.rawSource == mapView.style.rawStyle->getSource(self.identifier.UTF8String)) { diff --git a/platform/darwin/src/MGLSource_Private.h b/platform/darwin/src/MGLSource_Private.h index af14c11b90..4a7c31694d 100644 --- a/platform/darwin/src/MGLSource_Private.h +++ b/platform/darwin/src/MGLSource_Private.h @@ -2,6 +2,7 @@ #include <memory> + NS_ASSUME_NONNULL_BEGIN namespace mbgl { @@ -18,6 +19,20 @@ struct SourceWrapper { __weak MGLSource *source; }; +/** + Assert that the style source is valid. + + This macro should be used at the beginning of any public-facing instance method + of `MGLSource` and its subclasses. For private methods, an assertion is more appropriate. + */ +#define MGLAssertStyleSourceIsValid() \ +do { \ + if (!self.rawSource) { \ + [NSException raise:MGLInvalidStyleSourceException \ + format:@"This source got invalidated after the style change"]; \ + } \ +} while (NO); + @class MGLMapView; @interface MGLSource (Private) diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index 5768a7aaa7..cab0eaae28 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -226,12 +226,23 @@ static_assert(6 == mbgl::util::default_styles::numOrderedStyles, MGLLogDebug(@"Removing source: %@", source); if (!source.rawSource) { - [NSException raise:NSInvalidArgumentException format: - @"The source %@ cannot be removed from the style. " - @"Make sure the source was created as a member of a concrete subclass of MGLSource.", - source]; + NSString *errorMessage = [NSString stringWithFormat: + @"The source %@ cannot be removed from the style. " + @"Make sure the source was created as a member of a concrete subclass of MGLSource." + @"Automatic re-addition of sources after style changes is not currently supported.", + source]; + + if (outError) { + *outError = [NSError errorWithDomain:MGLErrorDomain + code:MGLErrorCodeSourceCannotBeRemovedFromStyle + userInfo:@{ NSLocalizedDescriptionKey : errorMessage }]; + return NO; + } + else { + [NSException raise:NSInvalidArgumentException format:@"%@", errorMessage]; + } } - + return [source removeFromMapView:self.mapView error:outError]; } diff --git a/platform/darwin/src/MGLStyleLayer.mm b/platform/darwin/src/MGLStyleLayer.mm index 05885bc63e..7847cbb319 100644 --- a/platform/darwin/src/MGLStyleLayer.mm +++ b/platform/darwin/src/MGLStyleLayer.mm @@ -14,13 +14,14 @@ const MGLExceptionName MGLInvalidStyleLayerException = @"MGLInvalidStyleLayerExc @implementation MGLStyleLayer { std::unique_ptr<mbgl::style::Layer> _pendingLayer; + mapbox::base::WeakPtr<mbgl::style::Layer> _weakLayer; } - (instancetype)initWithRawLayer:(mbgl::style::Layer *)rawLayer { if (self = [super init]) { _identifier = @(rawLayer->getID().c_str()); - _rawLayer = rawLayer; - _rawLayer->peer = LayerWrapper { self }; + _weakLayer = rawLayer->makeWeakPtr(); + rawLayer->peer = LayerWrapper { self }; } return self; } @@ -32,6 +33,11 @@ const MGLExceptionName MGLInvalidStyleLayerException = @"MGLInvalidStyleLayerExc return self; } +- (mbgl::style::Layer *)rawLayer +{ + return _weakLayer.get(); +} + - (void)addToStyle:(MGLStyle *)style belowLayer:(MGLStyleLayer *)otherLayer { if (_pendingLayer == nullptr) { @@ -103,9 +109,15 @@ const MGLExceptionName MGLInvalidStyleLayerException = @"MGLInvalidStyleLayerExc - (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p; identifier = %@; visible = %@>", - NSStringFromClass([self class]), (void *)self, self.identifier, - self.visible ? @"YES" : @"NO"]; + if (self.rawLayer) { + return [NSString stringWithFormat:@"<%@: %p; identifier = %@; visible = %@>", + NSStringFromClass([self class]), (void *)self, self.identifier, + self.visible ? @"YES" : @"NO"]; + } + else { + return [NSString stringWithFormat:@"<%@: %p; identifier = %@; visible = NO>", + NSStringFromClass([self class]), (void *)self, self.identifier]; + } } @end diff --git a/platform/darwin/src/MGLStyleLayer_Private.h b/platform/darwin/src/MGLStyleLayer_Private.h index 314f61f680..52254f78c6 100644 --- a/platform/darwin/src/MGLStyleLayer_Private.h +++ b/platform/darwin/src/MGLStyleLayer_Private.h @@ -27,6 +27,7 @@ struct LayerWrapper { if (!self.rawLayer) { \ [NSException raise:MGLInvalidStyleLayerException \ format: \ + @"Either this layer got invalidated after the style change or " \ @"-[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 " \ diff --git a/platform/darwin/src/MGLTypes.h b/platform/darwin/src/MGLTypes.h index 7e0dd27141..c0c93002fb 100644 --- a/platform/darwin/src/MGLTypes.h +++ b/platform/darwin/src/MGLTypes.h @@ -55,7 +55,9 @@ typedef NS_ENUM(NSInteger, MGLErrorCode) { /** Source is in use and cannot be removed */ MGLErrorCodeSourceIdentifierMismatch = 8, /** An error occurred while modifying the offline storage database */ - MGLErrorCodeModifyingOfflineStorageFailed = 9 + MGLErrorCodeModifyingOfflineStorageFailed = 9, + /** Source is invalid and cannot be removed from the style (e.g. after a style change) */ + MGLErrorCodeSourceCannotBeRemovedFromStyle = 10 }; /** Options for enabling debugging features in an `MGLMapView` instance. */ diff --git a/platform/darwin/src/MGLVectorStyleLayer.m b/platform/darwin/src/MGLVectorStyleLayer.m deleted file mode 100644 index 23f3556e0b..0000000000 --- a/platform/darwin/src/MGLVectorStyleLayer.m +++ /dev/null @@ -1,25 +0,0 @@ -#import "MGLVectorStyleLayer.h" - -@implementation MGLVectorStyleLayer - -- (void)setPredicate:(NSPredicate *)predicate { - [NSException raise:MGLAbstractClassException - format:@"MGLVectorStyleLayer is an abstract class"]; -} - -- (NSPredicate *)predicate { - [NSException raise:MGLAbstractClassException - format:@"MGLVectorStyleLayer is an abstract class"]; - return nil; -} - -- (NSString *)description { - return [NSString stringWithFormat: - @"<%@: %p; identifier = %@; sourceIdentifier = %@; " - @"sourceLayerIdentifier = %@; predicate = %@; visible = %@>", - NSStringFromClass([self class]), (void *)self, self.identifier, - self.sourceIdentifier, self.sourceLayerIdentifier, self.predicate, - self.visible ? @"YES" : @"NO"]; -} - -@end diff --git a/platform/darwin/src/MGLVectorStyleLayer.mm b/platform/darwin/src/MGLVectorStyleLayer.mm new file mode 100644 index 0000000000..691668629a --- /dev/null +++ b/platform/darwin/src/MGLVectorStyleLayer.mm @@ -0,0 +1,34 @@ +#import "MGLVectorStyleLayer.h" +#import "MGLStyleLayer_Private.h" + +@implementation MGLVectorStyleLayer + +- (void)setPredicate:(NSPredicate *)predicate { + [NSException raise:MGLAbstractClassException + format:@"MGLVectorStyleLayer is an abstract class"]; +} + +- (NSPredicate *)predicate { + [NSException raise:MGLAbstractClassException + format:@"MGLVectorStyleLayer is an abstract class"]; + return nil; +} + +- (NSString *)description { + if (self.rawLayer) { + return [NSString stringWithFormat: + @"<%@: %p; identifier = %@; sourceIdentifier = %@; " + @"sourceLayerIdentifier = %@; predicate = %@; visible = %@>", + NSStringFromClass([self class]), (void *)self, self.identifier, + self.sourceIdentifier, self.sourceLayerIdentifier, self.predicate, + self.visible ? @"YES" : @"NO"]; + } + else { + return [NSString stringWithFormat: + @"<%@: %p; identifier = %@; sourceIdentifier = <unknown>; " + @"sourceLayerIdentifier = <unknown>; predicate = <unknown>; visible = <unknown>>", + NSStringFromClass([self class]), (void *)self, self.identifier]; + } +} + +@end diff --git a/platform/darwin/src/MGLVectorTileSource.mm b/platform/darwin/src/MGLVectorTileSource.mm index f7a6869ade..85270c4a49 100644 --- a/platform/darwin/src/MGLVectorTileSource.mm +++ b/platform/darwin/src/MGLVectorTileSource.mm @@ -38,17 +38,19 @@ } - (NSURL *)configurationURL { + MGLAssertStyleSourceIsValid(); auto url = self.rawSource->getURL(); return url ? [NSURL URLWithString:@(url->c_str())] : nil; } - (NSString *)attributionHTMLString { + MGLAssertStyleSourceIsValid(); auto attribution = self.rawSource->getAttribution(); return attribution ? @(attribution->c_str()) : nil; } - (NSArray<id <MGLFeature>> *)featuresInSourceLayersWithIdentifiers:(NSSet<NSString *> *)sourceLayerIdentifiers predicate:(nullable NSPredicate *)predicate { - + MGLAssertStyleSourceIsValid(); mbgl::optional<std::vector<std::string>> optionalSourceLayerIDs; if (sourceLayerIdentifiers) { __block std::vector<std::string> layerIDs; |