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
|
#pragma once
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/data_driven_property_value.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/ignore.hpp>
#include <mbgl/util/rapidjson.hpp>
#include <array>
#include <vector>
#include <unordered_map>
namespace mbgl {
namespace style {
namespace conversion {
template <class Writer>
void stringify(Writer& writer, NullValue) {
writer.Null();
}
template <class Writer>
void stringify(Writer& writer, bool v) {
writer.Bool(v);
}
template <class Writer>
void stringify(Writer& writer, uint64_t v) {
writer.Uint64(v);
}
template <class Writer>
void stringify(Writer& writer, int64_t v) {
writer.Int64(v);
}
template <class Writer>
void stringify(Writer& writer, double v) {
writer.Double(v);
}
template <class Writer>
void stringify(Writer& writer, const std::string& v) {
writer.String(v);
}
template <class Writer, class T, class Enable = std::enable_if_t<std::is_enum<T>::value>>
void stringify(Writer& writer, const T& v) {
writer.String(Enum<T>::toString(v));
}
template <class Writer>
void stringify(Writer& writer, const Color& v) {
writer.String(v.stringify());
}
template <class Writer>
void stringify(Writer& writer, const std::array<float, 2>& v) {
writer.StartArray();
writer.Double(v[0]);
writer.Double(v[1]);
writer.EndArray();
}
template <class Writer>
void stringify(Writer& writer, const std::array<float, 4>& v) {
writer.StartArray();
writer.Double(v[0]);
writer.Double(v[1]);
writer.Double(v[2]);
writer.Double(v[3]);
writer.EndArray();
}
template <class Writer>
void stringify(Writer&, const Value&);
template <class Writer, class T>
void stringify(Writer& writer, const std::vector<T>& v) {
writer.StartArray();
for (const auto& e : v) {
stringify(writer, e);
}
writer.EndArray();
}
template <class Writer, class T>
void stringify(Writer& writer, const std::unordered_map<std::string, T>& m) {
writer.StartObject();
for (const auto& p : m) {
writer.Key(p.first.data(), static_cast<unsigned>(p.first.size()));
stringify(writer, p.second);
}
writer.EndObject();
}
template <class Writer>
void stringify(Writer& writer, const Value& v) {
Value::visit(v, [&] (const auto& v_) { stringify(writer, v_); });
}
template <class Writer>
void stringify(Writer& writer, FeatureType type) {
switch (type) {
case FeatureType::Unknown:
writer.String("Unknown");
break;
case FeatureType::Point:
writer.String("Point");
break;
case FeatureType::LineString:
writer.String("LineString");
break;
case FeatureType::Polygon:
writer.String("Polygon");
break;
}
}
template <class Writer>
void stringify(Writer& writer, const FeatureIdentifier& id) {
FeatureIdentifier::visit(id, [&] (const auto& id_) { stringify(writer, id_); });
}
template <class Writer>
class StringifyFilter {
public:
Writer& writer;
void operator()(const NullFilter&) {
writer.Null();
}
void operator()(const EqualsFilter& f) {
stringifyBinaryFilter(f, "==");
}
void operator()(const NotEqualsFilter& f) {
stringifyBinaryFilter(f, "!=");
}
void operator()(const LessThanFilter& f) {
stringifyBinaryFilter(f, "<");
}
void operator()(const LessThanEqualsFilter& f) {
stringifyBinaryFilter(f, "<=");
}
void operator()(const GreaterThanFilter& f) {
stringifyBinaryFilter(f, ">");
}
void operator()(const GreaterThanEqualsFilter& f) {
stringifyBinaryFilter(f, ">=");
}
void operator()(const InFilter& f) {
stringifySetFilter(f, "in");
}
void operator()(const NotInFilter& f) {
stringifySetFilter(f, "!in");
}
void operator()(const AllFilter& f) {
stringifyCompoundFilter(f, "all");
}
void operator()(const AnyFilter& f) {
stringifyCompoundFilter(f, "any");
}
void operator()(const NoneFilter& f) {
stringifyCompoundFilter(f, "none");
}
void operator()(const HasFilter& f) {
stringifyUnaryFilter("has", f.key);
}
void operator()(const NotHasFilter& f) {
stringifyUnaryFilter("!has", f.key);
}
void operator()(const TypeEqualsFilter& f) {
stringifyBinaryFilter(f, "==", "$type");
}
void operator()(const TypeNotEqualsFilter& f) {
stringifyBinaryFilter(f, "!=", "$type");
}
void operator()(const TypeInFilter& f) {
stringifySetFilter(f, "in", "$type");
}
void operator()(const TypeNotInFilter& f) {
stringifySetFilter(f, "!in", "$type");
}
void operator()(const IdentifierEqualsFilter& f) {
stringifyBinaryFilter(f, "==", "$id");
}
void operator()(const IdentifierNotEqualsFilter& f) {
stringifyBinaryFilter(f, "!=", "$id");
}
void operator()(const IdentifierInFilter& f) {
stringifySetFilter(f, "in", "$id");
}
void operator()(const IdentifierNotInFilter& f) {
stringifySetFilter(f, "!in", "$id");
}
void operator()(const HasIdentifierFilter&) {
stringifyUnaryFilter("has", "$id");
}
void operator()(const NotHasIdentifierFilter&) {
stringifyUnaryFilter("!has", "$id");
}
void operator()(const ExpressionFilter& filter) {
stringify(writer, filter.expression->serialize());
}
private:
template <class F>
void stringifyBinaryFilter(const F& f, const char * op) {
stringifyBinaryFilter(f, op, f.key);
}
template <class F>
void stringifyBinaryFilter(const F& f, const char * op, const std::string& key) {
writer.StartArray();
writer.String(op);
writer.String(key);
stringify(writer, f.value);
writer.EndArray();
}
template <class F>
void stringifySetFilter(const F& f, const char * op) {
stringifySetFilter(f, op, f.key);
}
template <class F>
void stringifySetFilter(const F& f, const char * op, const std::string& key) {
writer.StartArray();
writer.String(op);
writer.String(key);
for (const auto& value : f.values) {
stringify(writer, value);
}
writer.EndArray();
}
template <class F>
void stringifyCompoundFilter(const F& f, const char * op) {
writer.StartArray();
writer.String(op);
for (const auto& filter : f.filters) {
Filter::visit(filter, *this);
}
writer.EndArray();
}
void stringifyUnaryFilter(const char * op, const std::string& key) {
writer.StartArray();
writer.String(op);
writer.String(key);
writer.EndArray();
}
};
template <class Writer>
void stringify(Writer& writer, const Filter& f) {
Filter::visit(f, StringifyFilter<Writer> { writer });
}
template <class Writer>
void stringify(Writer& writer, const Undefined&) {
assert(false); // Should be omitted entirely instead.
writer.Null();
}
template <class Writer>
void stringify(Writer& writer, const CategoricalValue& v) {
CategoricalValue::visit(v, [&] (const auto& v_) { stringify(writer, v_); });
}
template <class Writer>
class StringifyStops {
public:
Writer& writer;
template <class T>
void operator()(const ExponentialStops<T>& f) {
writer.Key("type");
writer.String("exponential");
writer.Key("base");
writer.Double(f.base);
writer.Key("stops");
stringifyStops(f.stops);
}
template <class T>
void operator()(const IntervalStops<T>& f) {
writer.Key("type");
writer.String("interval");
writer.Key("stops");
stringifyStops(f.stops);
}
template <class T>
void operator()(const CategoricalStops<T>& f) {
writer.Key("type");
writer.String("categorical");
writer.Key("stops");
stringifyStops(f.stops);
}
template <class T>
void operator()(const IdentityStops<T>&) {
writer.Key("type");
writer.String("identity");
}
template <class T>
void operator()(const CompositeExponentialStops<T>& f) {
writer.Key("type");
writer.String("exponential");
writer.Key("base");
writer.Double(f.base);
writer.Key("stops");
stringifyCompositeStops(f.stops);
}
template <class T>
void operator()(const CompositeIntervalStops<T>& f) {
writer.Key("type");
writer.String("interval");
writer.Key("stops");
stringifyCompositeStops(f.stops);
}
template <class T>
void operator()(const CompositeCategoricalStops<T>& f) {
writer.Key("type");
writer.String("categorical");
writer.Key("stops");
stringifyCompositeStops(f.stops);
}
private:
template <class K, class V>
void stringifyStops(const std::map<K, V>& stops) {
writer.StartArray();
for (const auto& stop : stops) {
writer.StartArray();
stringify(writer, stop.first);
stringify(writer, stop.second);
writer.EndArray();
}
writer.EndArray();
}
template <class InnerStops>
void stringifyCompositeStops(const std::map<float, InnerStops>& stops) {
writer.StartArray();
for (const auto& outer : stops) {
for (const auto& inner : outer.second) {
writer.StartArray();
writer.StartObject();
writer.Key("zoom");
writer.Double(outer.first);
writer.Key("value");
stringify(writer, inner.first);
writer.EndObject();
stringify(writer, inner.second);
writer.EndArray();
}
}
writer.EndArray();
}
};
template <class Writer, class T>
void stringify(Writer& writer, const CameraFunction<T>& f) {
writer.StartObject();
CameraFunction<T>::Stops::visit(f.stops, StringifyStops<Writer> { writer });
writer.EndObject();
}
template <class Writer, class T>
void stringify(Writer& writer, const SourceFunction<T>& f) {
writer.StartObject();
writer.Key("property");
writer.String(f.property);
SourceFunction<T>::Stops::visit(f.stops, StringifyStops<Writer> { writer });
if (f.defaultValue) {
writer.Key("default");
stringify(writer, *f.defaultValue);
}
writer.EndObject();
}
template <class Writer, class T>
void stringify(Writer& writer, const CompositeFunction<T>& f) {
writer.StartObject();
writer.Key("property");
writer.String(f.property);
CompositeFunction<T>::Stops::visit(f.stops, StringifyStops<Writer> { writer });
if (f.defaultValue) {
writer.Key("default");
stringify(writer, *f.defaultValue);
}
writer.EndObject();
}
template <class Writer, class T>
void stringify(Writer& writer, const PropertyValue<T>& v) {
v.evaluate([&] (const auto& v_) { stringify(writer, v_); });
}
template <class Property, class Writer, class T>
void stringify(Writer& writer, const PropertyValue<T>& value) {
if (!value.isUndefined()) {
writer.Key(Property::key);
stringify(writer, value);
}
}
template <class Writer, class T>
void stringify(Writer& writer, const DataDrivenPropertyValue<T>& v) {
v.evaluate([&] (const auto& v_) { stringify(writer, v_); });
}
template <class Property, class Writer, class T>
void stringify(Writer& writer, const DataDrivenPropertyValue<T>& value) {
if (!value.isUndefined()) {
writer.Key(Property::key);
stringify(writer, value);
}
}
} // namespace conversion
} // namespace style
} // namespace mbgl
|