summaryrefslogtreecommitdiff
path: root/include/mbgl/style/conversion/function.hpp
blob: 90b4b95063a838dcebee67372e560c1e364b47dc (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
#pragma once

#include <mbgl/style/function/camera_function.hpp>
#include <mbgl/style/function/source_function.hpp>
#include <mbgl/style/function/composite_function.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/util/ignore.hpp>

namespace mbgl {
namespace style {
namespace conversion {

template <class D, class R, class V>
Result<std::map<D, R>> convertStops(const V& value) {
    auto stopsValue = objectMember(value, "stops");
    if (!stopsValue) {
        return Error { "function value must specify stops" };
    }

    if (!isArray(*stopsValue)) {
        return Error { "function stops must be an array" };
    }

    if (arrayLength(*stopsValue) == 0) {
        return Error { "function must have at least one stop" };
    }

    std::map<D, R> stops;
    for (std::size_t i = 0; i < arrayLength(*stopsValue); ++i) {
        const auto& stopValue = arrayMember(*stopsValue, i);

        if (!isArray(stopValue)) {
            return Error { "function stop must be an array" };
        }

        if (arrayLength(stopValue) != 2) {
            return Error { "function stop must have two elements" };
        }

        Result<D> d = convert<D>(arrayMember(stopValue, 0));
        if (!d) {
            return d.error();
        }

        Result<R> r = convert<R>(arrayMember(stopValue, 1));
        if (!r) {
            return r.error();
        }

        stops.emplace(*d, *r);
    }

    return stops;
}

template <class T>
struct Converter<ExponentialStops<T>> {
    static constexpr const char * type = "exponential";

    template <class V>
    Result<ExponentialStops<T>> operator()(const V& value) const {
        auto stops = convertStops<float, T>(value);
        if (!stops) {
            return stops.error();
        }

        auto baseValue = objectMember(value, "base");
        if (!baseValue) {
            return ExponentialStops<T>(*stops);
        }

        optional<float> base = toNumber(*baseValue);
        if (!base) {
            return Error { "function base must be a number"};
        }

        return ExponentialStops<T>(*stops, *base);
    }
};

template <class T>
struct Converter<IntervalStops<T>> {
    static constexpr const char * type = "interval";

    template <class V>
    Result<IntervalStops<T>> operator()(const V& value) const {
        auto stops = convertStops<float, T>(value);
        if (!stops) {
            return stops.error();
        }
        return IntervalStops<T>(*stops);
    }
};

template <>
struct Converter<CategoricalValue> {
    template <class V>
    Result<CategoricalValue> operator()(const V& value) const {
        auto b = toBool(value);
        if (b) {
            return *b;
        }

        auto n = toNumber(value);
        if (n) {
            return int64_t(*n);
        }

        auto s = toString(value);
        if (s) {
            return *s;
        }

        return Error { "stop domain value must be a number, string, or boolean" };
    }
};

template <class T>
struct Converter<CategoricalStops<T>> {
    static constexpr const char * type = "categorical";

    template <class V>
    Result<CategoricalStops<T>> operator()(const V& value) const {
        auto stops = convertStops<CategoricalValue, T>(value);
        if (!stops) {
            return stops.error();
        }
        return CategoricalStops<T>(
            std::map<CategoricalValue, T>((*stops).begin(), (*stops).end()));
    }
};

template <class T>
struct Converter<IdentityStops<T>> {
    static constexpr const char * type = "identity";

    template <class V>
    Result<IdentityStops<T>> operator()(const V&) const {
        return IdentityStops<T>();
    }
};

template <class, class>
struct StopsConverter;

template <class T, class... Ts>
struct StopsConverter<T, variant<Ts...>> {
public:
    template <class V>
    Result<variant<Ts...>> operator()(const V& value) const {
        std::string type = util::Interpolatable<T> ? "exponential" : "interval";

        auto typeValue = objectMember(value, "type");
        if (typeValue && toString(*typeValue)) {
            type = *toString(*typeValue);
        }

        optional<Result<variant<Ts...>>> result;

        // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47226
        auto tryConvert = [&] (auto* tp) {
            using Stops = std::decay_t<decltype(*tp)>;
            if (type == Converter<Stops>::type) {
                auto stops = convert<Stops>(value);
                result = stops
                    ? Result<variant<Ts...>>(*stops)
                    : Result<variant<Ts...>>(stops.error());
            }
        };

        util::ignore({
            (tryConvert((Ts*)nullptr), 0)...
        });

        if (!result) {
            return Error { "unsupported function type" };
        }

        return *result;
    }
};

template <class T>
struct Converter<CameraFunction<T>> {
    template <class V>
    Result<CameraFunction<T>> operator()(const V& value) const {
        if (!isObject(value)) {
            return Error { "function must be an object" };
        }

        auto stops = StopsConverter<T, typename CameraFunction<T>::Stops>()(value);
        if (!stops) {
            return stops.error();
        }

        return CameraFunction<T>(*stops);
    }
};

template <class T>
struct Converter<SourceFunction<T>> {
    template <class V>
    Result<SourceFunction<T>> operator()(const V& value) const {
        if (!isObject(value)) {
            return Error { "function must be an object" };
        }

        auto propertyValue = objectMember(value, "property");
        if (!propertyValue) {
            return Error { "function must specify property" };
        }

        auto propertyString = toString(*propertyValue);
        if (!propertyString) {
            return Error { "function property must be a string" };
        }

        auto stops = StopsConverter<T, typename SourceFunction<T>::Stops>()(value);
        if (!stops) {
            return stops.error();
        }

        return SourceFunction<T>(*propertyString, *stops);
    }
};

template <class S>
struct CompositeValue : std::pair<float, S> {
    using std::pair<float, S>::pair;
};

template <class S>
struct Converter<CompositeValue<S>> {
    template <class V>
    Result<CompositeValue<S>> operator()(const V& value) const {
        if (!isObject(value)) {
            return Error { "stop must be an object" };
        }

        auto zoomValue = objectMember(value, "zoom");
        if (!zoomValue) {
            return Error { "stop must specify zoom" };
        }

        auto propertyValue = objectMember(value, "value");
        if (!propertyValue) {
            return Error { "stop must specify value" };
        }

        Result<float> z = convert<float>(*zoomValue);
        if (!z) {
            return z.error();
        }

        Result<S> s = convert<S>(*propertyValue);
        if (!s) {
            return s.error();
        }

        return CompositeValue<S> { *z, *s };
    }
};

template <class T>
struct Converter<CompositeFunction<T>> {
    template <class V>
    Result<CompositeFunction<T>> operator()(const V& value) const {
        if (!isObject(value)) {
            return Error { "function must be an object" };
        }

        auto propertyValue = objectMember(value, "property");
        if (!propertyValue) {
            return Error { "function must specify property" };
        }

        auto propertyString = toString(*propertyValue);
        if (!propertyString) {
            return Error { "function property must be a string" };
        }

        std::string type = "exponential";
        auto typeValue = objectMember(value, "type");
        if (typeValue && toString(*typeValue)) {
            type = *toString(*typeValue);
        }

        if (type == "exponential") {
            auto stops = convertStops<CompositeValue<float>, T>(value);
            if (!stops) {
                return stops.error();
            }

            auto base = 1.0f;
            auto baseValue = objectMember(value, "base");
            if (baseValue && toNumber(*baseValue)) {
                base = *toNumber(*baseValue);
            }

            std::map<float, ExponentialStops<T>> convertedStops;
            for (const auto& stop : *stops) {
                auto& inner = convertedStops[stop.first.first];
                inner.base = base;
                inner.stops.emplace(stop.first.second, stop.second);
            }

            return CompositeFunction<T>(*propertyString, convertedStops);
        } else if (type == "interval") {
            auto stops = convertStops<CompositeValue<float>, T>(value);
            if (!stops) {
                return stops.error();
            }

            std::map<float, IntervalStops<T>> convertedStops;
            for (const auto& stop : *stops) {
                auto& inner = convertedStops[stop.first.first];
                inner.stops.emplace(stop.first.second, stop.second);
            }

            return CompositeFunction<T>(*propertyString, convertedStops);
        } else if (type == "categorical") {
            auto stops = convertStops<CompositeValue<CategoricalValue>, T>(value);
            if (!stops) {
                return stops.error();
            }

            std::map<float, CategoricalStops<T>> convertedStops;
            for (const auto& stop : *stops) {
                auto& inner = convertedStops[stop.first.first];
                inner.stops.emplace(stop.first.second, stop.second);
            }

            return CompositeFunction<T>(*propertyString, convertedStops);
        } else {
            return Error { "unsupported function type" };
        }
    }
};

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