summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLStyleValue_Private.h
blob: 5914e0a2aaff3bc46654e21c8cd6b5aad9c2280c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#import <Foundation/Foundation.h>

#import "MGLStyleValue.h"

#import "NSValue+MGLStyleAttributeAdditions.h"
#import "NSValue+MGLAdditions.h"
#import "MGLTypes.h"

#import "MGLConversion.h"
#include <mbgl/style/conversion/data_driven_property_value.hpp>
#include <mbgl/style/conversion.hpp>
#import <mbgl/style/types.hpp>

#import <mbgl/util/enum.hpp>

#include <mbgl/style/data_driven_property_value.hpp>

#include <mbgl/util/interpolate.hpp>

#if TARGET_OS_IPHONE
    #import "UIColor+MGLAdditions.h"
#else
    #import "NSColor+MGLAdditions.h"
#endif

template <typename MBGLType, typename ObjCType, typename MBGLElement = MBGLType, typename ObjCEnum = ObjCType>
class MGLStyleValueTransformer {
public:

    // Convert an mbgl property value into an mgl style value
    MGLStyleValue<ObjCType> *toStyleValue(const mbgl::style::PropertyValue<MBGLType> &mbglValue) {
        PropertyValueEvaluator evaluator;
        return mbglValue.evaluate(evaluator);
    }

    // Convert an mbgl data driven property value into an mgl style value
    MGLStyleValue<ObjCType> *toDataDrivenStyleValue(const mbgl::style::DataDrivenPropertyValue<MBGLType> &mbglValue) {
        PropertyValueEvaluator evaluator;
        return mbglValue.evaluate(evaluator);
    }

    // Convert an mbgl property value containing an enum into an mgl style value
    template <typename MBGLEnum = MBGLType,
              class = typename std::enable_if<std::is_enum<MBGLEnum>::value>::type,
              typename MGLEnum = ObjCEnum,
              class = typename std::enable_if<std::is_enum<MGLEnum>::value>::type>
    MGLStyleValue<ObjCType> *toEnumStyleValue(const mbgl::style::PropertyValue<MBGLEnum> &mbglValue) {
        EnumPropertyValueEvaluator<MBGLEnum, ObjCEnum> evaluator;
        return mbglValue.evaluate(evaluator);
    }

    // Convert an mgl style value into a non interpolatable (camera with interval stops) mbgl property value
    mbgl::style::PropertyValue<MBGLType> toPropertyValue(MGLStyleValue<ObjCType> *value) {
        if ([value isKindOfClass:[MGLSourceStyleFunction class]] || [value isKindOfClass:[MGLCompositeStyleFunction class]]) {
            [NSException raise:NSInvalidArgumentException
                        format:@"This property can only be set to camera functions. Use +[MGLStyleValue valueWithInterpolationMode:cameraStops:options:] instead."];
            return {};
        }

        if ([value isKindOfClass:[MGLConstantStyleValue class]]) {
            return toMBGLConstantValue((MGLConstantStyleValue<ObjCType> *)value);
        } else if ([value isKindOfClass:[MGLCameraStyleFunction class]]) {
            MGLCameraStyleFunction<ObjCType> *cameraStyleFunction = (MGLCameraStyleFunction<ObjCType> *)value;
            // Intentionally ignore the stop type set by the developer becuase non interpolatable property values
            // can only have interval stops. This also allows for backwards compatiblity when the developer uses
            // a deprecated MGLStyleValue method (that used to create an MGLStyleFunction) to create a function
            // for properties that are piecewise-constant (i.e. enum, bool, string)
            return toMBGLIntervalCameraFunction(cameraStyleFunction);
        } else if ([value isMemberOfClass:[MGLStyleFunction class]]) {
            MGLStyleFunction<ObjCType> *styleFunction = (MGLStyleFunction<ObjCType> *)value;
            return toMBGLIntervalCameraFunction(styleFunction);
        } else if (value) {
            [NSException raise:@"MGLAbstractClassException" format:
             @"The style value %@ cannot be applied to the style. "
             @"Make sure the style value was created as a member of a concrete subclass of MGLStyleValue.",
             NSStringFromClass([value class])];
            return {};
        } else {
            return {};
        }
    }

    // Convert an mgl style value into a non interpolatable (camera with exponential or interval stops) mbgl property value
    mbgl::style::PropertyValue<MBGLType> toInterpolatablePropertyValue(MGLStyleValue<ObjCType> *value) {
        if ([value isKindOfClass:[MGLSourceStyleFunction class]] || [value isKindOfClass:[MGLCompositeStyleFunction class]]) {
            [NSException raise:NSInvalidArgumentException
                        format:@"This property can only be set to camera functions. Use +[MGLStyleValue valueWithInterpolationMode:cameraStops:options:] instead."];
            return {};
        }

        if ([value isKindOfClass:[MGLConstantStyleValue class]]) {
            return toMBGLConstantValue((MGLConstantStyleValue<ObjCType> *)value);
        } else if ([value isMemberOfClass:[MGLStyleFunction class]]) {
            MGLStyleFunction<ObjCType> *styleFunction = (MGLStyleFunction<ObjCType> *)value;
            return toMBGLExponentialCameraFunction(styleFunction);
        } else if ([value isKindOfClass:[MGLCameraStyleFunction class]]) {
            MGLCameraStyleFunction<ObjCType> *cameraStyleFunction = (MGLCameraStyleFunction<ObjCType> *)value;
            switch (cameraStyleFunction.interpolationMode) {
                case MGLInterpolationModeExponential:
                    return toMBGLExponentialCameraFunction(cameraStyleFunction);
                    break;
                case MGLInterpolationModeInterval:
                    return toMBGLIntervalCameraFunction(cameraStyleFunction);
                    break;
                default:
                    [NSException raise:NSInvalidArgumentException
                                format:@"A camera function must use either exponential or interval stops."];
                    break;
            }
            return {};
        } else if (value) {
            [NSException raise:@"MGLAbstractClassException" format:
             @"The style value %@ cannot be applied to the style. "
             @"Make sure the style value was created as a member of a concrete subclass of MGLStyleValue.",
             NSStringFromClass([value class])];
            return {};
        } else {
            return {};
        }
    }

    // Convert an mgl style value into a mbgl data driven property value
    mbgl::style::DataDrivenPropertyValue<MBGLType> toDataDrivenPropertyValue(MGLStyleValue<ObjCType> *value) {
        if ([value isKindOfClass:[MGLConstantStyleValue class]]) {
            return toMBGLConstantValue((MGLConstantStyleValue<ObjCType> *)value);
        } else if ([value isKindOfClass:[MGLStyleFunction class]]) {
            mbgl::style::conversion::Error error;
            auto result = mbgl::style::conversion::convert<mbgl::style::DataDrivenPropertyValue<MBGLType>>(
                mbgl::style::conversion::makeConvertible(toRawStyleSpecValue((MGLStyleFunction<ObjCType> *) value)), error);
            NSCAssert(result, @(error.message.c_str()));
            return *result;
        } else {
            return {};
        }
    }

    // Convert an mgl style value containing an enum into a mbgl property value containing an enum
    template <typename MBGLEnum = MBGLType,
              class = typename std::enable_if<std::is_enum<MBGLEnum>::value>::type,
              typename MGLEnum = ObjCEnum,
              class = typename std::enable_if<std::is_enum<MGLEnum>::value>::type>
    mbgl::style::PropertyValue<MBGLEnum> toEnumPropertyValue(MGLStyleValue<ObjCType> *value) {
        if ([value isKindOfClass:[MGLSourceStyleFunction class]] || [value isKindOfClass:[MGLCompositeStyleFunction class]]) {
            [NSException raise:NSInvalidArgumentException
                        format:@"This property can only be set to camera functions. Use +[MGLStyleValue valueWithInterpolationMode:cameraStops:options:] instead."];
            return {};
        }

        if ([value isKindOfClass:[MGLConstantStyleValue class]]) {
            MBGLEnum mbglValue;
            getMBGLValue([(MGLConstantStyleValue<ObjCType> *)value rawValue], mbglValue);
            return mbglValue;
        } else if ([value isKindOfClass:[MGLCameraStyleFunction class]]) {
            MGLCameraStyleFunction<NSValue *> *cameraStyleFunction = (MGLCameraStyleFunction<NSValue *> *)value;
            __block std::map<float, MBGLType> stops = {};
            [cameraStyleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, MGLStyleValue<NSValue *> * _Nonnull stopValue, BOOL * _Nonnull stop) {
                NSCAssert([stopValue isKindOfClass:[MGLStyleValue class]], @"Stops should be MGLStyleValues");
                auto mbglStopValue = toEnumPropertyValue(stopValue);
                NSCAssert(mbglStopValue.isConstant(), @"Stops must be constant");
                stops[zoomKey.floatValue] = mbglStopValue.asConstant();
            }];

            // Enumerations can only ever use interval stops.
            mbgl::style::IntervalStops<MBGLType> intervalStops = {stops};

            mbgl::style::CameraFunction<MBGLType> cameraFunction = {intervalStops};
            return cameraFunction;
        } else if (value) {
            [NSException raise:@"MGLAbstractClassException" format:
             @"The style value %@ cannot be applied to the style. "
             @"Make sure the style value was created as a member of a concrete subclass of MGLStyleValue.",
             NSStringFromClass([value class])];
            return {};
        } else {
            return {};
        }
    }

private: // Private utilities for converting from mgl to mbgl values

    MBGLType toMBGLConstantValue(MGLConstantStyleValue<ObjCType> *value) {
        MBGLType mbglValue;
        getMBGLValue(value.rawValue, mbglValue);
        return mbglValue;
    }
    
    /**
     As hack to allow converting enum => string values, we accept a second, dummy parameter in
     the toRawStyleSpecValue() methods for converting 'atomic' (non-style-function) values.
     This allows us to use `std::enable_if` to test (at compile time) whether or not MBGLType is an Enum.
     */
    template <typename MBGLEnum = MBGLType,
    class = typename std::enable_if<!std::is_enum<MBGLEnum>::value>::type,
    typename MGLEnum = ObjCEnum,
    class = typename std::enable_if<!std::is_enum<MGLEnum>::value>::type>
    NSObject* toRawStyleSpecValue(NSObject *rawMGLValue, MBGLEnum &mbglValue) {
        if ([rawMGLValue isKindOfClass:[NSValue class]]) {
            const auto rawNSValue = (NSValue *)rawMGLValue;
            if (strcmp([rawNSValue objCType], @encode(CGVector)) == 0) {
                // offset [x, y]
                std::array<float, 2> mglValue = rawNSValue.mgl_offsetArrayValue;
                return [NSArray arrayWithObjects:@(mglValue[0]), @(mglValue[1]), nil];
            }
        }
        // noop pass-through plain NSObject-based items
        return rawMGLValue;
    }
    
    template <typename MBGLEnum = MBGLType,
    class = typename std::enable_if<std::is_enum<MBGLEnum>::value>::type,
    typename MGLEnum = ObjCEnum,
    class = typename std::enable_if<std::is_enum<MGLEnum>::value>::type>
    NSString* toRawStyleSpecValue(ObjCType rawValue, MBGLEnum &mbglValue) {
        MGLEnum mglEnum;
        [rawValue getValue:&mglEnum];
        return @(mbgl::Enum<MGLEnum>::toString(mglEnum));
    }
    
    NSObject* toRawStyleSpecValue(MGLColor *color, MBGLType &mbglValue) {
        return @(color.mgl_color.stringify().c_str());
    }

    
    NSObject* toRawStyleSpecValue(MGLStyleFunction<ObjCType>* styleFunction) {
        NSMutableDictionary * rawFunction = [NSMutableDictionary new];
        // interpolationMode => type
        switch (styleFunction.interpolationMode) {
            case MGLInterpolationModeExponential:
                rawFunction[@"type"] = @"exponential";
                break;
            case MGLInterpolationModeInterval:
                rawFunction[@"type"] = @"interval";
                break;
            case MGLInterpolationModeCategorical:
                rawFunction[@"type"] = @"categorical";
                break;
            case MGLInterpolationModeIdentity:
                rawFunction[@"type"] = @"identity";
                break;
        }
        
        // interpolationBase => base
        if (styleFunction.interpolationBase) {
            rawFunction[@"base"] = @(styleFunction.interpolationBase);
        }
        
        // stops and default value
        if ([styleFunction isKindOfClass:[MGLCameraStyleFunction class]]) {
            // zoom-only function (no default value)
            __block NSMutableArray *stops = [[NSMutableArray alloc] init];
            [styleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, MGLConstantStyleValue<ObjCType> * _Nonnull outputValue, BOOL * _Nonnull stop) {
                MBGLType dummyMbglValue;
                NSArray *rawStop = @[zoomKey, toRawStyleSpecValue([outputValue rawValue], dummyMbglValue)];
                [stops addObject:rawStop];
            }];
            rawFunction[@"stops"] = stops;

        } else if ([styleFunction isKindOfClass:[MGLSourceStyleFunction class]]) {
            auto sourceStyleFunction = (MGLSourceStyleFunction<ObjCType> *)styleFunction;
            rawFunction[@"property"] = sourceStyleFunction.attributeName;
            // property-only function
            __block NSMutableArray *stops = [[NSMutableArray alloc] init];
            [styleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSObject * _Nonnull propertyKey, MGLConstantStyleValue<ObjCType> * _Nonnull outputValue, BOOL * _Nonnull stop) {
                MBGLType dummyMbglValue;
                NSArray *rawStop = @[propertyKey, toRawStyleSpecValue([outputValue rawValue], dummyMbglValue)];
                [stops addObject:rawStop];
            }];
            rawFunction[@"stops"] = stops;
            
            // defaultValue => default
            if (sourceStyleFunction.defaultValue) {
                NSCAssert([sourceStyleFunction.defaultValue isKindOfClass:[MGLConstantStyleValue class]], @"Default value must be constant");
                MBGLType dummyMbglValue;
                rawFunction[@"default"] = toRawStyleSpecValue([(MGLConstantStyleValue<ObjCType> *)sourceStyleFunction.defaultValue rawValue], dummyMbglValue);
            }
        } else if ([styleFunction isKindOfClass:[MGLCompositeStyleFunction class]]) {
            // zoom-and-property function
            auto compositeStyleFunction = (MGLCompositeStyleFunction<ObjCType> *)styleFunction;
            rawFunction[@"property"] = compositeStyleFunction.attributeName;
            
            __block NSMutableArray *stops = [[NSMutableArray alloc] init];
            [compositeStyleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSDictionary * _Nonnull stopValue, BOOL * _Nonnull stop) {
                for (NSObject *valueKey in stopValue.allKeys) {
                    NSDictionary *stopKey = @{
                         @"zoom": zoomKey,
                         @"value": valueKey
                     };
                    MGLConstantStyleValue<ObjCType> *outputValue = stopValue[valueKey];
                    NSCAssert([outputValue isKindOfClass:[MGLConstantStyleValue<ObjCType> class]], @"Stop outputs should be MGLConstantStyleValues");
                    MBGLType dummyMbglValue;
                    NSArray *rawStop = @[stopKey, toRawStyleSpecValue([outputValue rawValue], dummyMbglValue)];
                    [stops addObject:rawStop];
                }
            }];
            rawFunction[@"stops"] = stops;
            
            // defaultValue => default
            if (compositeStyleFunction.defaultValue) {
                NSCAssert([compositeStyleFunction.defaultValue isKindOfClass:[MGLConstantStyleValue class]], @"Default value must be constant");
                MBGLType dummyMbglValue;
                rawFunction[@"default"] = toRawStyleSpecValue([(MGLConstantStyleValue<ObjCType> *)compositeStyleFunction.defaultValue rawValue], dummyMbglValue);
            }
        }
        
        return rawFunction;
    }

    mbgl::style::CameraFunction<MBGLType> toMBGLExponentialCameraFunction(MGLStyleFunction<ObjCType> *styleFunction) {
        __block std::map<float, MBGLType> stops = {};
        [styleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, MGLStyleValue<ObjCType> * _Nonnull stopValue, BOOL * _Nonnull stop) {
            NSCAssert([stopValue isKindOfClass:[MGLStyleValue class]], @"Stops should be MGLStyleValues");
            auto mbglStopValue = toPropertyValue(stopValue);
            NSCAssert(mbglStopValue.isConstant(), @"Stops must be constant");
            stops[zoomKey.floatValue] = mbglStopValue.asConstant();
        }];

        // Camera function with Exponential stops
        mbgl::style::ExponentialStops<MBGLType> exponentialStops = {stops, (float)styleFunction.interpolationBase};
        mbgl::style::CameraFunction<MBGLType> cameraFunction = {exponentialStops};

        return cameraFunction;
    }

    mbgl::style::CameraFunction<MBGLType> toMBGLIntervalCameraFunction(MGLStyleFunction<ObjCType> *styleFunction) {
        __block std::map<float, MBGLType> stops = {};
        [styleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, MGLStyleValue<ObjCType> * _Nonnull stopValue, BOOL * _Nonnull stop) {
            NSCAssert([stopValue isKindOfClass:[MGLStyleValue class]], @"Stops should be MGLStyleValues");
            auto mbglStopValue = toPropertyValue(stopValue);
            NSCAssert(mbglStopValue.isConstant(), @"Stops must be constant");
            stops[zoomKey.floatValue] = mbglStopValue.asConstant();
        }];

        // Camera function with Interval stops
        mbgl::style::IntervalStops<MBGLType> intervalStops = {stops};
        mbgl::style::CameraFunction<MBGLType> cameraFunction = {intervalStops};

        return cameraFunction;
    }

    mbgl::style::SourceFunction<MBGLType> toMBGLCategoricalSourceFunction(MGLSourceStyleFunction<ObjCType> *sourceStyleFunction) {
        __block std::map<mbgl::style::CategoricalValue, MBGLType> stops = {};
        [sourceStyleFunction.stops enumerateKeysAndObjectsUsingBlock:^(id categoryKey, MGLStyleValue<ObjCType> *stopValue, BOOL *stop) {
            NSCAssert([stopValue isKindOfClass:[MGLStyleValue class]], @"Stops should be MGLStyleValues");
            auto mbglStopValue = toPropertyValue(stopValue);
            NSCAssert(mbglStopValue.isConstant(), @"Stops must be constant");

            if ([categoryKey isKindOfClass:[NSString class]]) {
                const std::string& convertedValueKey = [((NSString *)categoryKey) UTF8String];
                stops[mbgl::style::CategoricalValue(convertedValueKey)] = mbglStopValue.asConstant();
            } else if ([categoryKey isKindOfClass:[NSNumber class]]) {
                NSNumber *key = (NSNumber *)categoryKey;
                if ((strcmp([key objCType], @encode(char)) == 0) ||
                    (strcmp([key objCType], @encode(BOOL)) == 0)) {
                    stops[mbgl::style::CategoricalValue((bool)[key boolValue])] = mbglStopValue.asConstant();
                } else if (strcmp([key objCType], @encode(double)) == 0 ||
                           strcmp([key objCType], @encode(float)) == 0) {
                    NSCAssert(mbglStopValue.isConstant(), @"Categorical stop keys must be strings, booleans, or integers");
                } else if ([key compare:@(0)] == NSOrderedDescending ||
                           [key compare:@(0)] == NSOrderedSame ||
                           [key compare:@(0)] == NSOrderedAscending) {
                    stops[mbgl::style::CategoricalValue((int64_t)[key integerValue])] = mbglStopValue.asConstant();
                }
            }
        }];
        mbgl::style::CategoricalStops<MBGLType> categoricalStops = {stops};
        mbgl::style::SourceFunction<MBGLType> sourceFunction = {sourceStyleFunction.attributeName.UTF8String, categoricalStops};
        setDefaultMBGLValue(sourceStyleFunction, sourceFunction);
        return sourceFunction;
    }

    void setDefaultMBGLValue(MGLSourceStyleFunction<ObjCType> *sourceStyleFunction, mbgl::style::SourceFunction<MBGLType> &sourceFunction) {
        if (sourceStyleFunction.defaultValue) {
            NSCAssert([sourceStyleFunction.defaultValue isKindOfClass:[MGLConstantStyleValue class]], @"Default value must be constant");
            MBGLType mbglValue;
            id mglValue = [(MGLConstantStyleValue<ObjCType> *)sourceStyleFunction.defaultValue rawValue];
            getMBGLValue(mglValue, mbglValue);
            sourceFunction.defaultValue = mbglValue;
        }
    }

    // Bool
    void getMBGLValue(NSNumber *rawValue, bool &mbglValue) {
        mbglValue = !!rawValue.boolValue;
    }

    // Float
    void getMBGLValue(NSNumber *rawValue, float &mbglValue) {
        mbglValue = rawValue.floatValue;
    }

    // String
    void getMBGLValue(NSString *rawValue, std::string &mbglValue) {
        mbglValue = rawValue.UTF8String;
    }

    // Offsets
    void getMBGLValue(NSValue *rawValue, std::array<float, 2> &mbglValue) {
        mbglValue = rawValue.mgl_offsetArrayValue;
    }

    // Padding
    void getMBGLValue(NSValue *rawValue, std::array<float, 4> &mbglValue) {
        mbglValue = rawValue.mgl_paddingArrayValue;
    }

    // Color
    void getMBGLValue(MGLColor *rawValue, mbgl::Color &mbglValue) {
        mbglValue = rawValue.mgl_color;
    }

    // Array
    void getMBGLValue(ObjCType rawValue, std::vector<MBGLElement> &mbglValue) {
        mbglValue.reserve(rawValue.count);
        for (id obj in rawValue) {
            MBGLElement mbglElement;
            getMBGLValue(obj, mbglElement);
            mbglValue.push_back(mbglElement);
        }
    }
    
    void getMBGLValue(NSValue *rawValue, mbgl::style::Position &mbglValue) {
        auto spherical = rawValue.mgl_lightPositionArrayValue;
        mbgl::style::Position position(spherical);
        mbglValue = position;
    }

    // Enumerations
    template <typename MBGLEnum = MBGLType,
    class = typename std::enable_if<std::is_enum<MBGLEnum>::value>::type,
    typename MGLEnum = ObjCEnum,
    class = typename std::enable_if<std::is_enum<MGLEnum>::value>::type>
    void getMBGLValue(ObjCType rawValue, MBGLEnum &mbglValue) {
        MGLEnum mglEnum;
        [rawValue getValue:&mglEnum];
        auto str = mbgl::Enum<MGLEnum>::toString(mglEnum);
        mbglValue = *mbgl::Enum<MBGLEnum>::toEnum(str);
    }

private: // Private utilities for converting from mbgl to mgl values

    // Bool
    static NSNumber *toMGLRawStyleValue(const bool mbglStopValue) {
        return @(mbglStopValue);
    }

    // Float
    static NSNumber *toMGLRawStyleValue(const float mbglStopValue) {
        return @(mbglStopValue);
    }

    // Integer
    static NSNumber *toMGLRawStyleValue(const int64_t mbglStopValue) {
        return @(mbglStopValue);
    }

    // String
    static NSString *toMGLRawStyleValue(const std::string &mbglStopValue) {
        return @(mbglStopValue.c_str());
    }

    // Offsets
    static NSValue *toMGLRawStyleValue(const std::array<float, 2> &mbglStopValue) {
        return [NSValue mgl_valueWithOffsetArray:mbglStopValue];
    }

    // Padding
    static NSValue *toMGLRawStyleValue(const std::array<float, 4> &mbglStopValue) {
        return [NSValue mgl_valueWithPaddingArray:mbglStopValue];
    }

    // Color
    static MGLColor *toMGLRawStyleValue(const mbgl::Color mbglStopValue) {
        return [MGLColor mgl_colorWithColor:mbglStopValue];
    }

    // Array
    static ObjCType toMGLRawStyleValue(const std::vector<MBGLElement> &mbglStopValue) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:mbglStopValue.size()];
        for (const auto &mbglElement: mbglStopValue) {
            [array addObject:toMGLRawStyleValue(mbglElement)];
        }
        return array;
    }
    
    static NSValue *toMGLRawStyleValue(const mbgl::style::Position &mbglStopValue) {
        std::array<float, 3> spherical = mbglStopValue.getSpherical();
        MGLSphericalPosition position = MGLSphericalPositionMake(spherical[0], spherical[1], spherical[2]);
        return [NSValue valueWithMGLSphericalPosition:position];
    }

    // Enumerations
    template <typename MBGLEnum = MBGLType, typename MGLEnum = ObjCEnum>
    static NSValue *toMGLRawStyleValue(const MBGLEnum &value) {
        auto str = mbgl::Enum<MBGLEnum>::toString(value);
        MGLEnum mglType = *mbgl::Enum<MGLEnum>::toEnum(str);
        return [NSValue value:&mglType withObjCType:@encode(MGLEnum)];
    }

    // Converts mbgl stops to an equivilent NSDictionary for mgl
    static NSMutableDictionary *toConvertedStops(const std::map<float, MBGLType> &mbglStops) {
        NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.size()];
        for (const auto &mbglStop : mbglStops) {
            auto rawValue = toMGLRawStyleValue(mbglStop.second);
            stops[@(mbglStop.first)] = [MGLStyleValue valueWithRawValue:rawValue];
        }
        return stops;
    }

    // Converts mbgl interval stop categorical values to an equivilant object for mgl
    class CategoricalValueVisitor {
    public:
        id operator()(const bool value) {
            return toMGLRawStyleValue(value);
        }

        id operator()(const int64_t value) {
            return toMGLRawStyleValue(value);
        }

        id operator()(const std::string value) {
            return toMGLRawStyleValue(value);
        }
    };

    // Converts all types of mbgl property values containing enumerations into an equivilant mgl style value
    template <typename MBGLEnum = MBGLType, typename MGLEnum = ObjCEnum>
    class EnumPropertyValueEvaluator {
    public:
        id operator()(const mbgl::style::Undefined) const {
            return nil;
        }

        id operator()(const MBGLEnum &value) const {
            auto str = mbgl::Enum<MBGLEnum>::toString(value);
            MGLEnum mglType = *mbgl::Enum<MGLEnum>::toEnum(str);
            return [MGLConstantStyleValue<ObjCType> valueWithRawValue:[NSValue value:&mglType withObjCType:@encode(MGLEnum)]];
        }

        id operator()(const mbgl::style::CameraFunction<MBGLEnum> &mbglValue) const {
            CameraFunctionStopsVisitor visitor;
            return apply_visitor(visitor, mbglValue.stops);
        }
    };

    // Converts all possible mbgl camera function stops into an equivilant mgl style value
    class CameraFunctionStopsVisitor {
    public:
        id operator()(const mbgl::style::ExponentialStops<MBGLType> &mbglStops) {
            return [MGLCameraStyleFunction functionWithInterpolationMode:MGLInterpolationModeExponential
                                                          stops:toConvertedStops(mbglStops.stops)
                                                        options:@{MGLStyleFunctionOptionInterpolationBase: @(mbglStops.base)}];
        }

        id operator()(const mbgl::style::IntervalStops<MBGLType> &mbglStops) {
            return [MGLCameraStyleFunction functionWithInterpolationMode:MGLInterpolationModeInterval
                                                          stops:toConvertedStops(mbglStops.stops)
                                                        options:nil];
        }
    };

    // Converts a source function and all possible mbgl source function stops into an equivilant mgl style value
    class SourceFunctionStopsVisitor {
    public:
        id operator()(const mbgl::style::ExponentialStops<MBGLType> &mbglStops) {
            MGLSourceStyleFunction *sourceFunction = [MGLSourceStyleFunction functionWithInterpolationMode:MGLInterpolationModeExponential
                                                                                            stops:toConvertedStops(mbglStops.stops)
                                                                                    attributeName:@(mbglFunction.property.c_str())
                                                                                          options:@{MGLStyleFunctionOptionInterpolationBase: @(mbglStops.base)}];
            if (mbglFunction.defaultValue) {
                sourceFunction.defaultValue = [MGLStyleValue valueWithRawValue:toMGLRawStyleValue(*mbglFunction.defaultValue)];
            }
            return sourceFunction;
        }

        id operator()(const mbgl::style::IntervalStops<MBGLType> &mbglStops) {
            MGLSourceStyleFunction *sourceFunction = [MGLSourceStyleFunction functionWithInterpolationMode:MGLInterpolationModeInterval
                                                                                            stops:toConvertedStops(mbglStops.stops)
                                                                                    attributeName:@(mbglFunction.property.c_str())
                                                                                          options:nil];
            if (mbglFunction.defaultValue) {
                sourceFunction.defaultValue = [MGLStyleValue valueWithRawValue:toMGLRawStyleValue(*mbglFunction.defaultValue)];
            }
            return sourceFunction;
        }

        id operator()(const mbgl::style::CategoricalStops<MBGLType> &mbglStops) {
            NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.stops.size()];
            for (const auto &mbglStop : mbglStops.stops) {
                auto categoricalValue = mbglStop.first;
                auto rawValue = toMGLRawStyleValue(mbglStop.second);
                CategoricalValueVisitor categoricalValueVisitor;
                id stopKey = apply_visitor(categoricalValueVisitor, categoricalValue);
                stops[stopKey] = [MGLStyleValue valueWithRawValue:rawValue];
            }

            MGLSourceStyleFunction *sourceFunction = [MGLSourceStyleFunction functionWithInterpolationMode:MGLInterpolationModeCategorical
                                                                                            stops:stops
                                                                                    attributeName:@(mbglFunction.property.c_str())
                                                                                          options:nil];
            if (mbglFunction.defaultValue) {
                sourceFunction.defaultValue = [MGLStyleValue valueWithRawValue:toMGLRawStyleValue(*mbglFunction.defaultValue)];
            }
            return sourceFunction;

        }

        id operator()(const mbgl::style::IdentityStops<MBGLType> &mbglStops) {
            MGLSourceStyleFunction *sourceFunction = [MGLSourceStyleFunction functionWithInterpolationMode:MGLInterpolationModeIdentity
                                                                                                     stops:nil
                                                                                             attributeName:@(mbglFunction.property.c_str()) options:nil];
            if (mbglFunction.defaultValue) {
                sourceFunction.defaultValue = [MGLStyleValue valueWithRawValue:toMGLRawStyleValue(*mbglFunction.defaultValue)];
            }
            return sourceFunction;
        }

        const mbgl::style::SourceFunction<MBGLType> &mbglFunction;
    };

    // Converts a composite function and all possible mbgl stops into an equivilant mgl style value
    class CompositeFunctionStopsVisitor {
    public:
        id operator()(const mbgl::style::CompositeExponentialStops<MBGLType> &mbglStops) {
            NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.stops.size()];
            for (auto const& outerStop: mbglStops.stops) {
                stops[@(outerStop.first)] = toConvertedStops(outerStop.second);
            }
            MGLCompositeStyleFunction *compositeFunction = [MGLCompositeStyleFunction functionWithInterpolationMode:MGLInterpolationModeExponential
                                                                                                     stops:stops
                                                                                             attributeName:@(mbglFunction.property.c_str())
                                                                                                   options:@{MGLStyleFunctionOptionInterpolationBase: @(mbglStops.base)}];
            if (mbglFunction.defaultValue) {
                compositeFunction.defaultValue = [MGLStyleValue valueWithRawValue:toMGLRawStyleValue(*mbglFunction.defaultValue)];
            }
            return compositeFunction;
        }

        id operator()(const mbgl::style::CompositeIntervalStops<MBGLType> &mbglStops) {
            NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.stops.size()];
            for (auto const& outerStop: mbglStops.stops) {
                stops[@(outerStop.first)] = toConvertedStops(outerStop.second);
            }
            MGLCompositeStyleFunction *compositeFunction = [MGLCompositeStyleFunction functionWithInterpolationMode:MGLInterpolationModeInterval
                                                                                                     stops:stops
                                                                                             attributeName:@(mbglFunction.property.c_str())
                                                                                                   options:nil];
            if (mbglFunction.defaultValue) {
                compositeFunction.defaultValue = [MGLStyleValue valueWithRawValue:toMGLRawStyleValue(*mbglFunction.defaultValue)];
            }
            return compositeFunction;
        }

        id operator()(const mbgl::style::CompositeCategoricalStops<MBGLType> &mbglStops) {
            NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.stops.size()];
            for (auto const& outerStop: mbglStops.stops) {
                    NSMutableDictionary *innerStops = [NSMutableDictionary dictionaryWithCapacity:outerStop.second.size()];
                for (const auto &mbglStop : outerStop.second) {
                    auto categoricalValue = mbglStop.first;
                    auto rawValue = toMGLRawStyleValue(mbglStop.second);
                    CategoricalValueVisitor categoricalValueVisitor;
                    id stopKey = apply_visitor(categoricalValueVisitor, categoricalValue);
                    innerStops[stopKey] = [MGLStyleValue valueWithRawValue:rawValue];
                }
                stops[@(outerStop.first)] = innerStops;
            }

            MGLCompositeStyleFunction *compositeFunction = [MGLCompositeStyleFunction functionWithInterpolationMode:MGLInterpolationModeCategorical
                                                                                                     stops:stops attributeName:@(mbglFunction.property.c_str())
                                                                                                   options:nil];
            if (mbglFunction.defaultValue) {
                compositeFunction.defaultValue = [MGLStyleValue valueWithRawValue:toMGLRawStyleValue(*mbglFunction.defaultValue)];
            }
            return compositeFunction;
        }

        const mbgl::style::CompositeFunction<MBGLType> &mbglFunction;
    };


    // Converts all types of mbgl property values that don't contain enumerations into an equivilant mgl style value
    class PropertyValueEvaluator {
    public:
        id operator()(const mbgl::style::Undefined) const {
            return nil;
        }

        id operator()(const MBGLType &value) const {
            auto rawValue = toMGLRawStyleValue(value);
            return [MGLConstantStyleValue<ObjCType> valueWithRawValue:rawValue];
        }

        id operator()(const mbgl::style::CameraFunction<MBGLType> &mbglValue) const {
            CameraFunctionStopsVisitor visitor;
            return apply_visitor(visitor, mbglValue.stops);
        }

        id operator()(const mbgl::style::SourceFunction<MBGLType> &mbglValue) const {
            SourceFunctionStopsVisitor visitor { mbglValue };
            return apply_visitor(visitor, mbglValue.stops);
        }

        MGLCompositeStyleFunction<ObjCType> * operator()(const mbgl::style::CompositeFunction<MBGLType> &mbglValue) const {
            CompositeFunctionStopsVisitor visitor { mbglValue };
            return apply_visitor(visitor, mbglValue.stops);
        }
    };
};