[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.
This commit is contained in:
John Firebaugh
2017-04-04 15:01:39 -07:00
parent 3a342c709c
commit 98d005792b
8 changed files with 66 additions and 212 deletions

View File

@@ -15,15 +15,11 @@ static const CGFloat MGLRasterSourceRetinaTileSize = 512;
@interface MGLRasterSource ()
- (instancetype)initWithRawSource:(mbgl::style::RasterSource *)rawSource NS_DESIGNATED_INITIALIZER;
@property (nonatomic) mbgl::style::RasterSource *rawSource;
@property (nonatomic, readonly) mbgl::style::RasterSource *rawSource;
@end
@implementation MGLRasterSource {
std::unique_ptr<mbgl::style::RasterSource> _pendingSource;
}
@implementation MGLRasterSource
- (instancetype)initWithIdentifier:(NSString *)identifier configurationURL:(NSURL *)configurationURL {
// The style specification default is 512, but 256 is the expected value for
@@ -37,76 +33,32 @@ static const CGFloat MGLRasterSourceRetinaTileSize = 512;
}
- (instancetype)initWithIdentifier:(NSString *)identifier configurationURL:(NSURL *)configurationURL tileSize:(CGFloat)tileSize {
if (self = [super initWithIdentifier:identifier]) {
auto source = std::make_unique<mbgl::style::RasterSource>(identifier.UTF8String,
configurationURL.mgl_URLByStandardizingScheme.absoluteString.UTF8String,
uint16_t(round(tileSize)));
_pendingSource = std::move(source);
self.rawSource = _pendingSource.get();
}
return self;
auto source = std::make_unique<mbgl::style::RasterSource>(identifier.UTF8String,
configurationURL.mgl_URLByStandardizingScheme.absoluteString.UTF8String,
uint16_t(round(tileSize)));
return self = [super initWithPendingSource:std::move(source)];
}
- (instancetype)initWithIdentifier:(NSString *)identifier tileURLTemplates:(NS_ARRAY_OF(NSString *) *)tileURLTemplates options:(nullable NS_DICTIONARY_OF(MGLTileSourceOption, id) *)options {
if (self = [super initWithIdentifier:identifier]) {
mbgl::Tileset tileSet = MGLTileSetFromTileURLTemplates(tileURLTemplates, options);
mbgl::Tileset tileSet = MGLTileSetFromTileURLTemplates(tileURLTemplates, options);
uint16_t tileSize = MGLRasterSourceRetinaTileSize;
if (NSNumber *tileSizeNumber = options[MGLTileSourceOptionTileSize]) {
if (![tileSizeNumber isKindOfClass:[NSNumber class]]) {
[NSException raise:NSInvalidArgumentException
format:@"MGLTileSourceOptionTileSize must be set to an NSNumber."];
}
tileSize = static_cast<uint16_t>(round(tileSizeNumber.doubleValue));
uint16_t tileSize = MGLRasterSourceRetinaTileSize;
if (NSNumber *tileSizeNumber = options[MGLTileSourceOptionTileSize]) {
if (![tileSizeNumber isKindOfClass:[NSNumber class]]) {
[NSException raise:NSInvalidArgumentException
format:@"MGLTileSourceOptionTileSize must be set to an NSNumber."];
}
auto source = std::make_unique<mbgl::style::RasterSource>(identifier.UTF8String, tileSet, tileSize);
_pendingSource = std::move(source);
self.rawSource = _pendingSource.get();
}
return self;
}
- (instancetype)initWithRawSource:(mbgl::style::RasterSource *)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];
tileSize = static_cast<uint16_t>(round(tileSizeNumber.doubleValue));
}
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::RasterSource *source = dynamic_cast<mbgl::style::RasterSource *>(removedSource.get());
if (!source) {
return;
}
removedSource.release();
_pendingSource = std::unique_ptr<mbgl::style::RasterSource>(source);
self.rawSource = _pendingSource.get();
auto source = std::make_unique<mbgl::style::RasterSource>(identifier.UTF8String, tileSet, tileSize);
return self = [super initWithPendingSource:std::move(source)];
}
- (mbgl::style::RasterSource *)rawSource {
return (mbgl::style::RasterSource *)super.rawSource;
}
- (void)setRawSource:(mbgl::style::RasterSource *)rawSource {
super.rawSource = rawSource;
}
- (NSURL *)configurationURL {
auto url = self.rawSource->getURL();
return url ? [NSURL URLWithString:@(url->c_str())] : nil;

View File

@@ -2,16 +2,7 @@
NS_ASSUME_NONNULL_BEGIN
namespace mbgl {
namespace style {
class RasterSource;
}
}
@interface MGLRasterSource (Private)
- (instancetype)initWithRawSource:(mbgl::style::RasterSource *)rawSource;
@end
NS_ASSUME_NONNULL_END

View File

@@ -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;

View File

@@ -6,14 +6,10 @@ NS_ASSUME_NONNULL_BEGIN
namespace mbgl {
namespace style {
class GeoJSONOptions;
struct GeoJSONSource;
}
}
@interface MGLShapeSource (Private)
- (instancetype)initWithRawSource:(mbgl::style::GeoJSONSource *)rawSource;
@end
MGL_EXPORT

View File

@@ -1,16 +1,21 @@
#import "MGLSource_Private.h"
#import "MGLMapView_Private.h"
#include <mbgl/map/map.hpp>
#include <mbgl/style/source.hpp>
@interface MGLSource ()
// Even though this class is abstract, MGLStyle uses it to represent some
// special internal source types like mbgl::AnnotationSource.
@property (nonatomic) mbgl::style::Source *rawSource;
@property (nonatomic, readonly) mbgl::style::Source *rawSource;
@end
@implementation MGLSource
@implementation MGLSource {
std::unique_ptr<mbgl::style::Source> _pendingSource;
}
- (instancetype)initWithIdentifier:(NSString *)identifier
{
@@ -28,18 +33,27 @@
return self;
}
- (instancetype)initWithPendingSource:(std::unique_ptr<mbgl::style::Source>)pendingSource {
if (self = [self initWithRawSource:pendingSource.get()]) {
_pendingSource = std::move(pendingSource);
}
return self;
}
- (void)addToMapView:(MGLMapView *)mapView {
[NSException raise:NSInvalidArgumentException format:
@"The source %@ cannot be added to the style. "
@"Make sure the source was created as a member of a concrete subclass of MGLSource.",
self];
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 {
[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.",
self];
if (self.rawSource == mapView.mbglMap->getSource(self.identifier.UTF8String)) {
_pendingSource = mapView.mbglMap->removeSource(self.identifier.UTF8String);
}
}
- (NSString *)description {

View File

@@ -1,5 +1,7 @@
#import "MGLSource.h"
#include <memory>
NS_ASSUME_NONNULL_BEGIN
namespace mbgl {
@@ -13,10 +15,17 @@ namespace mbgl {
@interface MGLSource (Private)
/**
Initializes and returns a source with a raw pointer to the backing store.
Initializes and returns a source with a raw pointer to the backing store,
associated with a style.
*/
- (instancetype)initWithRawSource:(mbgl::style::Source *)rawSource;
/**
Initializes and returns a source with an owning pointer to the backing store,
unassociated from a style.
*/
- (instancetype)initWithPendingSource:(std::unique_ptr<mbgl::style::Source>)pendingSource;
/**
A raw pointer to the mbgl object, which is always initialized, either to the
value returned by `mbgl::Map getSource`, or for independently created objects,
@@ -24,7 +33,7 @@ namespace mbgl {
pointer value stays even after ownership of the object is transferred via
`mbgl::Map addSource`.
*/
@property (nonatomic) mbgl::style::Source *rawSource;
@property (nonatomic, readonly) mbgl::style::Source *rawSource;
/**
Adds the mbgl source that this object represents to the mbgl map.

View File

@@ -1,88 +1,37 @@
#import "MGLVectorSource_Private.h"
#import "MGLMapView_Private.h"
#import "MGLFeature_Private.h"
#import "MGLSource_Private.h"
#import "MGLTileSource_Private.h"
#import "NSPredicate+MGLAdditions.h"
#import "NSURL+MGLAdditions.h"
#include <mbgl/map/map.hpp>
#include <mbgl/style/sources/vector_source.hpp>
@interface MGLVectorSource ()
- (instancetype)initWithRawSource:(mbgl::style::VectorSource *)rawSource NS_DESIGNATED_INITIALIZER;
@property (nonatomic) mbgl::style::VectorSource *rawSource;
@property (nonatomic, readonly) mbgl::style::VectorSource *rawSource;
@end
@implementation MGLVectorSource {
std::unique_ptr<mbgl::style::VectorSource> _pendingSource;
}
@implementation MGLVectorSource
- (instancetype)initWithIdentifier:(NSString *)identifier configurationURL:(NSURL *)configurationURL {
if (self = [super initWithIdentifier:identifier]) {
auto source = std::make_unique<mbgl::style::VectorSource>(identifier.UTF8String,
configurationURL.mgl_URLByStandardizingScheme.absoluteString.UTF8String);
_pendingSource = std::move(source);
self.rawSource = _pendingSource.get();
}
return self;
auto source = std::make_unique<mbgl::style::VectorSource>(identifier.UTF8String,
configurationURL.mgl_URLByStandardizingScheme.absoluteString.UTF8String);
return self = [super initWithPendingSource:std::move(source)];
}
- (instancetype)initWithIdentifier:(NSString *)identifier tileURLTemplates:(NS_ARRAY_OF(NSString *) *)tileURLTemplates options:(nullable NS_DICTIONARY_OF(MGLTileSourceOption, id) *)options {
if (self = [super initWithIdentifier:identifier]) {
mbgl::Tileset tileSet = MGLTileSetFromTileURLTemplates(tileURLTemplates, options);
auto source = std::make_unique<mbgl::style::VectorSource>(identifier.UTF8String, tileSet);
_pendingSource = std::move(source);
self.rawSource = _pendingSource.get();
}
return self;
}
- (instancetype)initWithRawSource:(mbgl::style::VectorSource *)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::VectorSource *source = dynamic_cast<mbgl::style::VectorSource *>(removedSource.get());
if (!source) {
return;
}
removedSource.release();
_pendingSource = std::unique_ptr<mbgl::style::VectorSource>(source);
self.rawSource = _pendingSource.get();
mbgl::Tileset tileSet = MGLTileSetFromTileURLTemplates(tileURLTemplates, options);
auto source = std::make_unique<mbgl::style::VectorSource>(identifier.UTF8String, tileSet);
return self = [super initWithPendingSource:std::move(source)];
}
- (mbgl::style::VectorSource *)rawSource {
return (mbgl::style::VectorSource *)super.rawSource;
}
- (void)setRawSource:(mbgl::style::VectorSource *)rawSource {
super.rawSource = rawSource;
}
- (NSURL *)configurationURL {
auto url = self.rawSource->getURL();
return url ? [NSURL URLWithString:@(url->c_str())] : nil;

View File

@@ -2,16 +2,7 @@
NS_ASSUME_NONNULL_BEGIN
namespace mbgl {
namespace style {
class VectorSource;
}
}
@interface MGLVectorSource (Private)
- (instancetype)initWithRawSource:(mbgl::style::VectorSource *)rawSource;
@end
NS_ASSUME_NONNULL_END