summaryrefslogtreecommitdiff
path: root/src/mbgl/style/conversion/stringify.hpp
blob: d06b2f2814ddff25499d218f8c1b32e73b88a6a8 (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
#pragma once

#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/layout_property.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>
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(f, "has");
    }

    void operator()(const NotHasFilter& f) {
        stringifyUnaryFilter(f, "!has");
    }

private:
    template <class F>
    void stringifyBinaryFilter(const F& f, const char * op) {
        writer.StartArray();
        writer.String(op);
        writer.String(f.key);
        stringify(writer, f.value);
        writer.EndArray();
    }

    template <class F>
    void stringifySetFilter(const F& f, const char * op) {
        writer.StartArray();
        writer.String(op);
        writer.String(f.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();
    }

    template <class F>
    void stringifyUnaryFilter(const F& f, const char * op) {
        writer.StartArray();
        writer.String(op);
        writer.String(f.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, class T>
void stringify(Writer& writer, const Function<T>& f) {
    writer.StartObject();
    writer.Key("base");
    writer.Double(f.getBase());
    writer.Key("stops");
    writer.StartArray();
    for (const auto& stop : f.getStops()) {
        writer.StartArray();
        writer.Double(stop.first);
        stringify(writer, stop.second);
        writer.EndArray();
    }
    writer.EndArray();
    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) {
        writer.Key(Property::key);
        stringify(writer, value);
    }
}

template <class Writer, class... Ps>
void stringify(Writer& writer, const LayoutProperties<Ps...>& ps) {
    writer.StartObject();
    util::ignore({ (stringify<Ps>(writer, ps.unevaluated.template get<Ps>()), 0)... });
    writer.EndObject();
}

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