Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 6ffb8b5

Browse files
committed
[ios, macos] Fixed covariant rawLayer property
Properly implement rawLayer as a covariant property so that it doesn’t end up getting autosynthesized as a shadow ivar. This prevents concrete subclasses of MGLStyleLayer from setting _rawLayer directly but getting a different value out of self.rawLayer.
1 parent 1cd2f83 commit 6ffb8b5

8 files changed

+313
-223
lines changed

platform/darwin/src/MGLBackgroundStyleLayer.mm

+19-9
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@ - (instancetype)initWithIdentifier:(NSString *)identifier
2626
if (self = [super initWithIdentifier:identifier]) {
2727
auto layer = std::make_unique<mbgl::style::BackgroundLayer>(identifier.UTF8String);
2828
_pendingLayer = std::move(layer);
29-
_rawLayer = _pendingLayer.get();
29+
self.rawLayer = _pendingLayer.get();
3030
}
3131
return self;
3232
}
3333

34+
- (mbgl::style::BackgroundLayer *)rawLayer
35+
{
36+
return (mbgl::style::BackgroundLayer *)super.rawLayer;
37+
}
38+
39+
- (void)setRawLayer:(mbgl::style::BackgroundLayer *)rawLayer
40+
{
41+
super.rawLayer = rawLayer;
42+
}
43+
3444
#pragma mark - Adding to and removing from a map view
3545

3646
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
@@ -52,7 +62,7 @@ - (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLaye
5262
- (void)removeFromMapView:(MGLMapView *)mapView
5363
{
5464
_pendingLayer = nullptr;
55-
_rawLayer = nullptr;
65+
self.rawLayer = nullptr;
5666

5767
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
5868
if (!removedLayer) {
@@ -67,7 +77,7 @@ - (void)removeFromMapView:(MGLMapView *)mapView
6777
removedLayer.release();
6878

6979
_pendingLayer = std::unique_ptr<mbgl::style::BackgroundLayer>(layer);
70-
_rawLayer = _pendingLayer.get();
80+
self.rawLayer = _pendingLayer.get();
7181
}
7282

7383
#pragma mark - Accessing the Paint Attributes
@@ -76,41 +86,41 @@ - (void)setBackgroundColor:(MGLStyleValue<MGLColor *> *)backgroundColor {
7686
MGLAssertStyleLayerIsValid();
7787

7888
auto mbglValue = MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toPropertyValue(backgroundColor);
79-
_rawLayer->setBackgroundColor(mbglValue);
89+
self.rawLayer->setBackgroundColor(mbglValue);
8090
}
8191

8292
- (MGLStyleValue<MGLColor *> *)backgroundColor {
8393
MGLAssertStyleLayerIsValid();
8494

85-
auto propertyValue = _rawLayer->getBackgroundColor() ?: _rawLayer->getDefaultBackgroundColor();
95+
auto propertyValue = self.rawLayer->getBackgroundColor() ?: self.rawLayer->getDefaultBackgroundColor();
8696
return MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toStyleValue(propertyValue);
8797
}
8898

8999
- (void)setBackgroundOpacity:(MGLStyleValue<NSNumber *> *)backgroundOpacity {
90100
MGLAssertStyleLayerIsValid();
91101

92102
auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(backgroundOpacity);
93-
_rawLayer->setBackgroundOpacity(mbglValue);
103+
self.rawLayer->setBackgroundOpacity(mbglValue);
94104
}
95105

96106
- (MGLStyleValue<NSNumber *> *)backgroundOpacity {
97107
MGLAssertStyleLayerIsValid();
98108

99-
auto propertyValue = _rawLayer->getBackgroundOpacity() ?: _rawLayer->getDefaultBackgroundOpacity();
109+
auto propertyValue = self.rawLayer->getBackgroundOpacity() ?: self.rawLayer->getDefaultBackgroundOpacity();
100110
return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue);
101111
}
102112

103113
- (void)setBackgroundPattern:(MGLStyleValue<NSString *> *)backgroundPattern {
104114
MGLAssertStyleLayerIsValid();
105115

106116
auto mbglValue = MGLStyleValueTransformer<std::string, NSString *>().toPropertyValue(backgroundPattern);
107-
_rawLayer->setBackgroundPattern(mbglValue);
117+
self.rawLayer->setBackgroundPattern(mbglValue);
108118
}
109119

110120
- (MGLStyleValue<NSString *> *)backgroundPattern {
111121
MGLAssertStyleLayerIsValid();
112122

113-
auto propertyValue = _rawLayer->getBackgroundPattern() ?: _rawLayer->getDefaultBackgroundPattern();
123+
auto propertyValue = self.rawLayer->getBackgroundPattern() ?: self.rawLayer->getDefaultBackgroundPattern();
114124
return MGLStyleValueTransformer<std::string, NSString *>().toStyleValue(propertyValue);
115125
}
116126

platform/darwin/src/MGLCircleStyleLayer.mm

+33-21
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,50 @@ - (instancetype)initWithIdentifier:(NSString *)identifier source:(MGLSource *)so
3939
if (self = [super initWithIdentifier:identifier source:source]) {
4040
auto layer = std::make_unique<mbgl::style::CircleLayer>(identifier.UTF8String, source.identifier.UTF8String);
4141
_pendingLayer = std::move(layer);
42-
_rawLayer = _pendingLayer.get();
42+
self.rawLayer = _pendingLayer.get();
4343
}
4444
return self;
4545
}
46+
47+
- (mbgl::style::CircleLayer *)rawLayer
48+
{
49+
return (mbgl::style::CircleLayer *)super.rawLayer;
50+
}
51+
52+
- (void)setRawLayer:(mbgl::style::CircleLayer *)rawLayer
53+
{
54+
super.rawLayer = rawLayer;
55+
}
56+
4657
- (NSString *)sourceLayerIdentifier
4758
{
4859
MGLAssertStyleLayerIsValid();
4960

50-
auto layerID = _rawLayer->getSourceLayer();
61+
auto layerID = self.rawLayer->getSourceLayer();
5162
return layerID.empty() ? nil : @(layerID.c_str());
5263
}
5364

5465
- (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier
5566
{
5667
MGLAssertStyleLayerIsValid();
5768

58-
_rawLayer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: "");
69+
self.rawLayer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: "");
5970
}
6071

6172
- (void)setPredicate:(NSPredicate *)predicate
6273
{
6374
MGLAssertStyleLayerIsValid();
6475

65-
_rawLayer->setFilter(predicate.mgl_filter);
76+
self.rawLayer->setFilter(predicate.mgl_filter);
6677
}
6778

6879
- (NSPredicate *)predicate
6980
{
7081
MGLAssertStyleLayerIsValid();
7182

72-
return [NSPredicate mgl_predicateWithFilter:_rawLayer->getFilter()];
83+
return [NSPredicate mgl_predicateWithFilter:self.rawLayer->getFilter()];
7384
}
85+
7486
#pragma mark - Adding to and removing from a map view
7587

7688
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer
@@ -92,7 +104,7 @@ - (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLaye
92104
- (void)removeFromMapView:(MGLMapView *)mapView
93105
{
94106
_pendingLayer = nullptr;
95-
_rawLayer = nullptr;
107+
self.rawLayer = nullptr;
96108

97109
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String);
98110
if (!removedLayer) {
@@ -107,7 +119,7 @@ - (void)removeFromMapView:(MGLMapView *)mapView
107119
removedLayer.release();
108120

109121
_pendingLayer = std::unique_ptr<mbgl::style::CircleLayer>(layer);
110-
_rawLayer = _pendingLayer.get();
122+
self.rawLayer = _pendingLayer.get();
111123
}
112124

113125
#pragma mark - Accessing the Paint Attributes
@@ -116,97 +128,97 @@ - (void)setCircleBlur:(MGLStyleValue<NSNumber *> *)circleBlur {
116128
MGLAssertStyleLayerIsValid();
117129

118130
auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(circleBlur);
119-
_rawLayer->setCircleBlur(mbglValue);
131+
self.rawLayer->setCircleBlur(mbglValue);
120132
}
121133

122134
- (MGLStyleValue<NSNumber *> *)circleBlur {
123135
MGLAssertStyleLayerIsValid();
124136

125-
auto propertyValue = _rawLayer->getCircleBlur() ?: _rawLayer->getDefaultCircleBlur();
137+
auto propertyValue = self.rawLayer->getCircleBlur() ?: self.rawLayer->getDefaultCircleBlur();
126138
return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue);
127139
}
128140

129141
- (void)setCircleColor:(MGLStyleValue<MGLColor *> *)circleColor {
130142
MGLAssertStyleLayerIsValid();
131143

132144
auto mbglValue = MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toPropertyValue(circleColor);
133-
_rawLayer->setCircleColor(mbglValue);
145+
self.rawLayer->setCircleColor(mbglValue);
134146
}
135147

136148
- (MGLStyleValue<MGLColor *> *)circleColor {
137149
MGLAssertStyleLayerIsValid();
138150

139-
auto propertyValue = _rawLayer->getCircleColor() ?: _rawLayer->getDefaultCircleColor();
151+
auto propertyValue = self.rawLayer->getCircleColor() ?: self.rawLayer->getDefaultCircleColor();
140152
return MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toStyleValue(propertyValue);
141153
}
142154

143155
- (void)setCircleOpacity:(MGLStyleValue<NSNumber *> *)circleOpacity {
144156
MGLAssertStyleLayerIsValid();
145157

146158
auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(circleOpacity);
147-
_rawLayer->setCircleOpacity(mbglValue);
159+
self.rawLayer->setCircleOpacity(mbglValue);
148160
}
149161

150162
- (MGLStyleValue<NSNumber *> *)circleOpacity {
151163
MGLAssertStyleLayerIsValid();
152164

153-
auto propertyValue = _rawLayer->getCircleOpacity() ?: _rawLayer->getDefaultCircleOpacity();
165+
auto propertyValue = self.rawLayer->getCircleOpacity() ?: self.rawLayer->getDefaultCircleOpacity();
154166
return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue);
155167
}
156168

157169
- (void)setCirclePitchScale:(MGLStyleValue<NSValue *> *)circlePitchScale {
158170
MGLAssertStyleLayerIsValid();
159171

160172
auto mbglValue = MGLStyleValueTransformer<mbgl::style::CirclePitchScaleType, NSValue *, mbgl::style::CirclePitchScaleType, MGLCirclePitchScale>().toEnumPropertyValue(circlePitchScale);
161-
_rawLayer->setCirclePitchScale(mbglValue);
173+
self.rawLayer->setCirclePitchScale(mbglValue);
162174
}
163175

164176
- (MGLStyleValue<NSValue *> *)circlePitchScale {
165177
MGLAssertStyleLayerIsValid();
166178

167-
auto propertyValue = _rawLayer->getCirclePitchScale() ?: _rawLayer->getDefaultCirclePitchScale();
179+
auto propertyValue = self.rawLayer->getCirclePitchScale() ?: self.rawLayer->getDefaultCirclePitchScale();
168180
return MGLStyleValueTransformer<mbgl::style::CirclePitchScaleType, NSValue *, mbgl::style::CirclePitchScaleType, MGLCirclePitchScale>().toEnumStyleValue(propertyValue);
169181
}
170182

171183
- (void)setCircleRadius:(MGLStyleValue<NSNumber *> *)circleRadius {
172184
MGLAssertStyleLayerIsValid();
173185

174186
auto mbglValue = MGLStyleValueTransformer<float, NSNumber *>().toPropertyValue(circleRadius);
175-
_rawLayer->setCircleRadius(mbglValue);
187+
self.rawLayer->setCircleRadius(mbglValue);
176188
}
177189

178190
- (MGLStyleValue<NSNumber *> *)circleRadius {
179191
MGLAssertStyleLayerIsValid();
180192

181-
auto propertyValue = _rawLayer->getCircleRadius() ?: _rawLayer->getDefaultCircleRadius();
193+
auto propertyValue = self.rawLayer->getCircleRadius() ?: self.rawLayer->getDefaultCircleRadius();
182194
return MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(propertyValue);
183195
}
184196

185197
- (void)setCircleTranslate:(MGLStyleValue<NSValue *> *)circleTranslate {
186198
MGLAssertStyleLayerIsValid();
187199

188200
auto mbglValue = MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toPropertyValue(circleTranslate);
189-
_rawLayer->setCircleTranslate(mbglValue);
201+
self.rawLayer->setCircleTranslate(mbglValue);
190202
}
191203

192204
- (MGLStyleValue<NSValue *> *)circleTranslate {
193205
MGLAssertStyleLayerIsValid();
194206

195-
auto propertyValue = _rawLayer->getCircleTranslate() ?: _rawLayer->getDefaultCircleTranslate();
207+
auto propertyValue = self.rawLayer->getCircleTranslate() ?: self.rawLayer->getDefaultCircleTranslate();
196208
return MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toStyleValue(propertyValue);
197209
}
198210

199211
- (void)setCircleTranslateAnchor:(MGLStyleValue<NSValue *> *)circleTranslateAnchor {
200212
MGLAssertStyleLayerIsValid();
201213

202214
auto mbglValue = MGLStyleValueTransformer<mbgl::style::TranslateAnchorType, NSValue *, mbgl::style::TranslateAnchorType, MGLCircleTranslateAnchor>().toEnumPropertyValue(circleTranslateAnchor);
203-
_rawLayer->setCircleTranslateAnchor(mbglValue);
215+
self.rawLayer->setCircleTranslateAnchor(mbglValue);
204216
}
205217

206218
- (MGLStyleValue<NSValue *> *)circleTranslateAnchor {
207219
MGLAssertStyleLayerIsValid();
208220

209-
auto propertyValue = _rawLayer->getCircleTranslateAnchor() ?: _rawLayer->getDefaultCircleTranslateAnchor();
221+
auto propertyValue = self.rawLayer->getCircleTranslateAnchor() ?: self.rawLayer->getDefaultCircleTranslateAnchor();
210222
return MGLStyleValueTransformer<mbgl::style::TranslateAnchorType, NSValue *, mbgl::style::TranslateAnchorType, MGLCircleTranslateAnchor>().toEnumStyleValue(propertyValue);
211223
}
212224

0 commit comments

Comments
 (0)