[ios, macos] Raise NSException if layer or source added more than once

When MGLSource and MGLLayer instances are added to the style, they lose
ownership of their std::unique_ptr<T> after it is moved to the mbgl
level. Subsequent attempts to add such instances result in a C++
exception.

This adds logic in the Darwin platform to raise a NSException if the
source and layer addToMapView methods are called more than once and the
pointer is invalid. In addition, the documentation in MGLStyle for
addSource: and addLayer: has been update to warn developers to avoid
adding the same instance twice and, for that matter, instances with the
same identifier.
This commit is contained in:
Jesse Bounds
2016-12-01 21:20:53 -08:00
parent 2db31be8bc
commit 1db0a96927
12 changed files with 135 additions and 1 deletions

View File

@@ -61,6 +61,12 @@ const MGLGeoJSONSourceOption MGLGeoJSONSourceOptionSimplificationTolerance = @"M
- (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));
}