summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLConversion.h
blob: d6363b28eb140a920d27ebfbdb3dcf9955073cda (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
#import <Foundation/Foundation.h>

#include <mbgl/util/logging.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/optional.hpp>

NS_ASSUME_NONNULL_BEGIN

namespace mbgl {
namespace style {
namespace conversion {

/**
 A minimal wrapper class conforming to the requirements for `objectMember(v, name)` (see mbgl/style/conversion.hpp)
 This is necessary because using `NSObject*` as the value type in `optional<NSObject*>` causes problems for the ARC,
 due to things like `optional(const value_type& __v)`
 */
class OptionalNSObjectValue {
public:
    OptionalNSObjectValue(NSObject * _Nullable _value) : value(_value) {}
    
    explicit operator bool() const {
        return value;
    }
    
    NSObject * _Nullable operator*() {
        NSCAssert(this, @"Expected non-null value.");
        return value;
    }
private:
    NSObject * _Nullable value;
};

inline bool isUndefined(const id value) {
    return !value || value == [NSNull null];
}

inline bool isArray(const id value) {
    return [value isKindOfClass:[NSArray class]];
}

inline bool isObject(const id value) {
    return [value isKindOfClass:[NSDictionary class]];
}

inline std::size_t arrayLength(const id value) {
    NSCAssert([value isKindOfClass:[NSArray class]], @"Value must be an NSArray for getLength().");
    NSArray *array = value;
    auto length = [array count];
    NSCAssert(length <= std::numeric_limits<size_t>::max(), @"Array length out of bounds.");
    return length;
}

inline NSObject *arrayMember(const id value, std::size_t i) {
    NSCAssert([value isKindOfClass:[NSArray class]], @"Value must be an NSArray for get(int).");
    NSCAssert(i < NSUIntegerMax, @"Index must be less than NSUIntegerMax");
    return [value objectAtIndex: i];
}

inline OptionalNSObjectValue objectMember(const id value, const char *key) {
    NSCAssert([value isKindOfClass:[NSDictionary class]], @"Value must be an NSDictionary for get(string).");
    NSObject *member = [value objectForKey: @(key)];
    if (member && member != [NSNull null]) {
        return { member };
    } else {
        return { nullptr };
    }
}

// Not implemented (unneeded for MGLStyleFunction conversion):
// optional<Error> eachMember(const NSObject*, Fn&&)

inline bool _isBool(const id value) {
    if (![value isKindOfClass:[NSNumber class]]) return false;
    // char: 32-bit boolean
    // BOOL: 64-bit boolean
    NSNumber *number = value;
    return ((strcmp([number objCType], @encode(char)) == 0) ||
            (strcmp([number objCType], @encode(BOOL)) == 0));
}
    
inline bool _isNumber(const id value) {
    return [value isKindOfClass:[NSNumber class]] && !_isBool(value);
}
    
inline bool _isString(const id value) {
    return [value isKindOfClass:[NSString class]];
}

inline optional<bool> toBool(const id value) {
    if (_isBool(value)) {
        return ((NSNumber *)value).boolValue;
    } else {
        return {};
    }
}

inline optional<float> toNumber(const id value) {
    if (_isNumber(value)) {
        return ((NSNumber *)value).floatValue;
    } else {
        return {};
    }
}

inline optional<double> toDouble(const id value) {
    if (_isNumber(value)) {
        return ((NSNumber *)value).doubleValue;
    } else {
        return {};
    }
}

inline optional<std::string> toString(const id value) {
    if (_isString(value)) {
        return std::string(static_cast<const char *>([value UTF8String]));
    } else {
        return {};
    }
}

inline optional<mbgl::Value> toValue(const id value) {
    if (isUndefined(value)) {
        return {};
    } else if (_isBool(value)) {
        return { *toBool(value) };
    } else if ( _isString(value)) {
        return { *toString(value) };
    } else if (_isNumber(value)) {
        // Need to cast to a double here as the float is otherwise considered a bool...
       return { static_cast<double>(*toNumber(value)) };
    } else {
        return {};
    }
}

} // namespace conversion
} // namespace style
} // namespace mbgl

NS_ASSUME_NONNULL_END