diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2017-04-04 15:01:39 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2017-04-13 10:33:18 -0700 |
commit | 98d005792b68d0b299f123c1d31e50c72ba91ba8 (patch) | |
tree | b1ac869a0e8beb82f0f148f23679b0a1137739ed /platform/darwin/src/MGLShapeSource.mm | |
parent | 3a342c709c1784d23c66c0e1b7f9963c588d7653 (diff) | |
download | qtlocation-mapboxgl-98d005792b68d0b299f123c1d31e50c72ba91ba8.tar.gz |
[darwin] Simplify MGLSource initialization and pointer management
Introduce `-[MGLSource initWithPendingSource:]`, allowing the base class to track the owned `_pendingSource` pointer and implement `-addToMapView:` and `-removeFromMapView:` without any casts.
Diffstat (limited to 'platform/darwin/src/MGLShapeSource.mm')
-rw-r--r-- | platform/darwin/src/MGLShapeSource.mm | 64 |
1 files changed, 8 insertions, 56 deletions
diff --git a/platform/darwin/src/MGLShapeSource.mm b/platform/darwin/src/MGLShapeSource.mm index 7de2d69af3..15437ebedb 100644 --- a/platform/darwin/src/MGLShapeSource.mm +++ b/platform/darwin/src/MGLShapeSource.mm @@ -20,38 +20,26 @@ const MGLShapeSourceOption MGLShapeSourceOptionSimplificationTolerance = @"MGLSh @interface MGLShapeSource () -- (instancetype)initWithRawSource:(mbgl::style::GeoJSONSource *)rawSource NS_DESIGNATED_INITIALIZER; - @property (nonatomic, readwrite) NSDictionary *options; -@property (nonatomic) mbgl::style::GeoJSONSource *rawSource; +@property (nonatomic, readonly) mbgl::style::GeoJSONSource *rawSource; @end -@implementation MGLShapeSource { - std::unique_ptr<mbgl::style::GeoJSONSource> _pendingSource; -} +@implementation MGLShapeSource - (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url options:(NS_DICTIONARY_OF(NSString *, id) *)options { - if (self = [super initWithIdentifier:identifier]) { - auto geoJSONOptions = MGLGeoJSONOptionsFromDictionary(options); - auto source = std::make_unique<mbgl::style::GeoJSONSource>(identifier.UTF8String, geoJSONOptions); - - _pendingSource = std::move(source); - self.rawSource = _pendingSource.get(); - + auto geoJSONOptions = MGLGeoJSONOptionsFromDictionary(options); + auto source = std::make_unique<mbgl::style::GeoJSONSource>(identifier.UTF8String, geoJSONOptions); + if (self = [super initWithPendingSource:std::move(source)]) { self.URL = url; } return self; } - (instancetype)initWithIdentifier:(NSString *)identifier shape:(nullable MGLShape *)shape options:(NS_DICTIONARY_OF(MGLShapeSourceOption, id) *)options { - if (self = [super initWithIdentifier:identifier]) { - auto geoJSONOptions = MGLGeoJSONOptionsFromDictionary(options); - auto source = std::make_unique<mbgl::style::GeoJSONSource>(identifier.UTF8String, geoJSONOptions); - - _pendingSource = std::move(source); - self.rawSource = _pendingSource.get(); - + auto geoJSONOptions = MGLGeoJSONOptionsFromDictionary(options); + auto source = std::make_unique<mbgl::style::GeoJSONSource>(identifier.UTF8String, geoJSONOptions); + if (self = [super initWithPendingSource:std::move(source)]) { self.shape = shape; } return self; @@ -72,46 +60,10 @@ const MGLShapeSourceOption MGLShapeSourceOptionSimplificationTolerance = @"MGLSh return [self initWithIdentifier:identifier shape:shapeCollection options:options]; } -- (instancetype)initWithRawSource:(mbgl::style::GeoJSONSource *)rawSource { - return [super initWithRawSource:rawSource]; -} - -- (void)addToMapView:(MGLMapView *)mapView { - if (_pendingSource == nullptr) { - [NSException raise:@"MGLRedundantSourceException" - format:@"This instance %@ was already added to %@. Adding the same source instance " \ - "to the style more than once is invalid.", self, mapView.style]; - } - - mapView.mbglMap->addSource(std::move(_pendingSource)); -} - -- (void)removeFromMapView:(MGLMapView *)mapView { - if (self.rawSource != mapView.mbglMap->getSource(self.identifier.UTF8String)) { - return; - } - - auto removedSource = mapView.mbglMap->removeSource(self.identifier.UTF8String); - - mbgl::style::GeoJSONSource *source = dynamic_cast<mbgl::style::GeoJSONSource *>(removedSource.get()); - if (!source) { - return; - } - - removedSource.release(); - - _pendingSource = std::unique_ptr<mbgl::style::GeoJSONSource>(source); - self.rawSource = _pendingSource.get(); -} - - (mbgl::style::GeoJSONSource *)rawSource { return (mbgl::style::GeoJSONSource *)super.rawSource; } -- (void)setRawSource:(mbgl::style::GeoJSONSource *)rawSource { - super.rawSource = rawSource; -} - - (NSURL *)URL { auto url = self.rawSource->getURL(); return url ? [NSURL URLWithString:@(url->c_str())] : nil; |