diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2019-09-04 16:57:16 +0300 |
---|---|---|
committer | Julian Rex <julian.rex@mapbox.com> | 2019-09-07 00:15:38 -0400 |
commit | db339af506fd9cb90e91c7576b56ec04462c0ae7 (patch) | |
tree | 2f52d902cc946cc912db69406b8c079745b5b89e | |
parent | 78ae1917ba448b4cbb8f9ae5cb4da63a81836354 (diff) | |
download | qtlocation-mapboxgl-db339af506fd9cb90e91c7576b56ec04462c0ae7.tar.gz |
[darwin] Introduce MGLAssertStyleSourceIsValid() macro and use it
-rw-r--r-- | platform/darwin/src/MGLComputedShapeSource.mm | 3 | ||||
-rw-r--r-- | platform/darwin/src/MGLImageSource.mm | 6 | ||||
-rw-r--r-- | platform/darwin/src/MGLRasterTileSource.mm | 2 | ||||
-rw-r--r-- | platform/darwin/src/MGLShapeSource.mm | 6 | ||||
-rw-r--r-- | platform/darwin/src/MGLSource.h | 3 | ||||
-rw-r--r-- | platform/darwin/src/MGLSource.mm | 3 | ||||
-rw-r--r-- | platform/darwin/src/MGLSource_Private.h | 16 | ||||
-rw-r--r-- | platform/darwin/src/MGLVectorTileSource.mm | 4 |
8 files changed, 41 insertions, 2 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/MGLImageSource.mm b/platform/darwin/src/MGLImageSource.mm index 351247e901..50422ccedc 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,10 +71,12 @@ } - (MGLCoordinateQuad)coordinates { + MGLAssertStyleSourceIsValid(); return MGLCoordinateQuadFromLatLngArray(self.rawSource->getCoordinates()); } - (void)setCoordinates: (MGLCoordinateQuad)coordinateQuad { + MGLAssertStyleSourceIsValid(); self.rawSource->setCoordinates(MGLLatLngArrayFromCoordinateQuad(coordinateQuad)); } @@ -85,6 +90,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/MGLShapeSource.mm b/platform/darwin/src/MGLShapeSource.mm index fc526f9850..19aad56182 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,6 +166,7 @@ mbgl::style::GeoJSONOptions MGLGeoJSONOptionsFromDictionary(NSDictionary<MGLShap } - (void)setShape:(MGLShape *)shape { + MGLAssertStyleSourceIsValid(); self.rawSource->setGeoJSON({ shape.geoJSONObject }); _shape = shape; } @@ -174,7 +177,7 @@ mbgl::style::GeoJSONOptions MGLGeoJSONOptionsFromDictionary(NSDictionary<MGLShap } - (NSArray<id <MGLFeature>> *)featuresMatchingPredicate:(nullable NSPredicate *)predicate { - + MGLAssertStyleSourceIsValid(); mbgl::optional<mbgl::style::Filter> optionalFilter; if (predicate) { optionalFilter = predicate.mgl_filter; @@ -190,6 +193,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 2c673fbb30..01bcd94fec 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 @@ -65,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..2d4b2a3c41 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,21 @@ 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/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; |