This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
MGLConversion.h
159 lines (137 loc) · 4.79 KB
/
MGLConversion.h
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
#include <mbgl/style/conversion_impl.hpp>
NS_ASSUME_NONNULL_BEGIN
namespace mbgl {
namespace style {
namespace conversion {
// A wrapper class for `id`, so as not to confuse ARC.
class Holder {
public:
Holder(const id v) : value(v) {}
const id value;
};
template <>
class ConversionTraits<Holder> {
public:
static bool isUndefined(const Holder& holder) {
const id value = holder.value;
return !value || value == [NSNull null];
}
static bool isArray(const Holder& holder) {
const id value = holder.value;
return [value isKindOfClass:[NSArray class]];
}
static bool isObject(const Holder& holder) {
const id value = holder.value;
return [value isKindOfClass:[NSDictionary class]];
}
static std::size_t arrayLength(const Holder& holder) {
const id value = holder.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;
}
static Holder arrayMember(const Holder& holder, std::size_t i) {
const id value = holder.value;
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]};
}
static optional<Holder> objectMember(const Holder& holder, const char *key) {
const id value = holder.value;
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 {};
}
}
// Compiler is wrong about `Fn` parameter missing a nullability specifier.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullability-completeness"
template <class Fn>
static optional<Error> eachMember(const Holder& holder, Fn&& visit) {
#pragma clang diagnostic pop
[holder.value enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
auto result = visit(std::string(static_cast<const char *>([key UTF8String])), obj);
if (result) {
*stop = YES;
}
}];
return {};
}
static optional<bool> toBool(const Holder& holder) {
const id value = holder.value;
if (_isBool(value)) {
return ((NSNumber *)value).boolValue;
} else {
return {};
}
}
static optional<float> toNumber(const Holder& holder) {
const id value = holder.value;
if (_isNumber(value)) {
return ((NSNumber *)value).floatValue;
} else {
return {};
}
}
static optional<double> toDouble(const Holder& holder) {
const id value = holder.value;
if (_isNumber(value)) {
return ((NSNumber *)value).doubleValue;
} else {
return {};
}
}
static optional<std::string> toString(const Holder& holder) {
const id value = holder.value;
if (_isString(value)) {
return std::string(static_cast<const char *>([value UTF8String]));
} else {
return {};
}
}
static optional<mbgl::Value> toValue(const Holder& holder) {
const id value = holder.value;
if (isUndefined(value)) {
return {};
} else if (_isBool(value)) {
return { *toBool(holder) };
} else if ( _isString(value)) {
return { *toString(holder) };
} else if (_isNumber(value)) {
return { *toDouble(holder) };
} else {
return {};
}
}
static optional<GeoJSON> toGeoJSON(const Holder&, Error& error) {
error = { "toGeoJSON not implemented" };
return {};
}
private:
static 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));
}
static bool _isNumber(const id value) {
return [value isKindOfClass:[NSNumber class]] && !_isBool(value);
}
static bool _isString(const id value) {
return [value isKindOfClass:[NSString class]];
}
};
inline Convertible makeConvertible(const id value) {
return Convertible(Holder(value));
}
} // namespace conversion
} // namespace style
} // namespace mbgl
NS_ASSUME_NONNULL_END