summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLSource.mm
diff options
context:
space:
mode:
Diffstat (limited to 'platform/darwin/src/MGLSource.mm')
-rw-r--r--platform/darwin/src/MGLSource.mm34
1 files changed, 24 insertions, 10 deletions
diff --git a/platform/darwin/src/MGLSource.mm b/platform/darwin/src/MGLSource.mm
index c96b6c41c6..1940db688c 100644
--- a/platform/darwin/src/MGLSource.mm
+++ b/platform/darwin/src/MGLSource.mm
@@ -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 {