summaryrefslogtreecommitdiff
path: root/src/mbgl/style/expression/value.cpp
blob: cbcf7e220691a116b6beb65748cf7525c939235e (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
#include  <mbgl/style/expression/value.hpp>
#include <sstream>

namespace mbgl {
namespace style {
namespace expression {

type::Type typeOf(const Value& value) {
    return value.match(
        [&](bool) -> type::Type { return type::Boolean; },
        [&](double) -> type::Type { return type::Number; },
        [&](const std::string&) -> type::Type { return type::String; },
        [&](const mbgl::Color&) -> type::Type { return type::Color; },
        [&](const NullValue&) -> type::Type { return type::Null; },
        [&](const std::unordered_map<std::string, Value>&) -> type::Type { return type::Object; },
        [&](const std::vector<Value>& arr) -> type::Type {
            optional<type::Type> itemType;
            for (const auto& item : arr) {
                const auto& t = typeOf(item);
                const auto& tname = type::toString(t);
                if (!itemType) {
                    itemType = {t};
                } else if (type::toString(*itemType) == tname) {
                    continue;
                } else {
                    itemType = {type::Value};
                    break;
                }
            }
            
            if (!itemType) { itemType = {type::Value}; }

            return type::Array(*itemType, arr.size());
        }
    );
}

std::string stringify(const Value& value) {
    return value.match(
        [&] (const NullValue&) { return std::string("null"); },
        [&] (bool b) { return std::string(b ? "true" : "false"); },
        [&] (double f) {
            std::stringstream ss;
            ss << f;
            return ss.str();
        },
        [&] (const std::string& s) { return "\"" + s + "\""; },
        [&] (const mbgl::Color& c) { return c.stringify(); },
        [&] (const std::vector<Value>& arr) {
            std::string result = "[";
            for(const auto& item : arr) {
                if (result.size() > 1) result += ",";
                result += stringify(item);
            }
            return result + "]";
        },
        [&] (const std::unordered_map<std::string, Value>& obj) {
            std::string result = "{";
            for(const auto& entry : obj) {
                if (result.size() > 1) result += ",";
                result += stringify(entry.first) + ":" + stringify(entry.second);
            }
            return result + "}";
        }
    );
}


template <class T, class Enable = void>
struct Converter {
    static Value toExpressionValue(const T& value) {
        return Value(value);
    }
    static optional<T> fromExpressionValue(const Value& value) {
        return value.template is<T>() ? value.template get<T>() : optional<T>();
    }
};

template <>
struct Converter<float> {
    static Value toExpressionValue(const float& value) {
        return static_cast<double>(value);
    }
    
    static optional<float> fromExpressionValue(const Value& value) {
        if (value.template is<double>()) {
            double v = value.template get<double>();
            if (v <= Value::max()) {
                return static_cast<float>(v);
            }
        }
        return optional<float>();
    }
    
    static type::Type expressionType() {
        return type::Number;
    }
};

template<>
struct Converter<mbgl::Value> {
    static Value toExpressionValue(const mbgl::Value& value) {
        return mbgl::Value::visit(value, Converter<mbgl::Value>());
    }


    // Double duty as a variant visitor for mbgl::Value:
    Value operator()(const std::vector<mbgl::Value>& v) {
        std::vector<Value> result;
        for(const auto& item : v) {
            result.emplace_back(toExpressionValue(item));
        }
        return result;
    }
    
    Value operator()(const std::unordered_map<std::string, mbgl::Value>& v) {
        std::unordered_map<std::string, Value> result;
        for(const auto& entry : v) {
            result.emplace(entry.first, toExpressionValue(entry.second));
        }
        return result;
    }
    
    Value operator()(const std::string& s) { return s; }
    Value operator()(const bool& b) { return b; }
    Value operator()(const mbgl::NullValue) { return Null; }
    Value operator()(const double& v) { return v; }
    Value operator()(const uint64_t& v) {
        return v > static_cast<uint64_t>(Value::max()) ? static_cast<double>(Value::max()) : v;
    }
    Value operator()(const int64_t& v) {
        return v > std::numeric_limits<double>::max() ? std::numeric_limits<double>::max() : v;
    }
};

template <typename T, typename Container>
std::vector<Value> toArrayValue(const Container& value) {
    std::vector<Value> result;
    for (const T& item : value) {
        result.push_back(Converter<T>::toExpressionValue(item));
    }
    return result;
}

template <typename T, typename Container>
optional<Container> fromArrayValue(const std::vector<Value>& value) {
    Container result;
    auto it = result.begin();
    for(const Value& item : value) {
        optional<T> convertedItem = Converter<T>::fromExpressionValue(item);
        if (!convertedItem) {
            return optional<Container>();
        }
        *it = *convertedItem;
        it = std::next(it);
    }
    return result;
}

template <typename T, std::size_t N>
struct Converter<std::array<T, N>> {
    static Value toExpressionValue(const std::array<T, N>& value) {
        return toArrayValue<T>(value);
    }
    
    static optional<std::array<T, N>> fromExpressionValue(const Value& value) {
        return value.match(
            [&] (const std::vector<Value>& v) -> optional<std::array<T, N>> {
                if (v.size() != N) return optional<std::array<T, N>>();
                return fromArrayValue<T, std::array<T, N>>(v);
            },
            [&] (const auto&) { return optional<std::array<T, N>>(); }
        );
    }
    
    static type::Type expressionType() {
        return type::Array(valueTypeToExpressionType<T>(), N);
    }
};

template <typename T>
struct Converter<std::vector<T>> {
    static Value toExpressionValue(const std::vector<T>& value) {
        return toArrayValue<T>(value);
    }
    
    static optional<std::vector<T>> fromExpressionValue(const Value& value) {
        return value.match(
            [&] (const std::vector<Value>& v) -> optional<std::vector<T>> {
                return fromArrayValue<T, std::vector<T>>(v);
            },
            [&] (const auto&) { return optional<std::vector<T>>(); }
        );
    }
    
    static type::Type expressionType() {
        return type::Array(valueTypeToExpressionType<T>());
    }
};

template <>
struct Converter<Position> {
    static Value toExpressionValue(const mbgl::style::Position& value) {
        return Converter<std::array<float, 3>>::toExpressionValue(value.getSpherical());
    }
    
    static optional<Position> fromExpressionValue(const Value& v) {
        auto pos = Converter<std::array<float, 3>>::fromExpressionValue(v);
        return pos ? optional<Position>(Position(*pos)) : optional<Position>();
    }
    
    static type::Type expressionType() {
        return type::Array(type::Number, 3);
    }
};

template <typename T>
struct Converter<T, std::enable_if_t< std::is_enum<T>::value >> {
    static Value toExpressionValue(const T& value) {
        return std::string(Enum<T>::toString(value));
    }
    
    static optional<T> fromExpressionValue(const Value& value) {
        return value.match(
            [&] (const std::string& v) { return Enum<T>::toEnum(v); },
            [&] (const auto&) { return optional<T>(); }
        );
    }
    
    static type::Type expressionType() {
        return type::String;
    }
};

Value toExpressionValue(const Value& v) {
    return v;
}

template <typename T, typename Enable>
Value toExpressionValue(const T& value) {
    return Converter<T>::toExpressionValue(value);
}

template <typename T>
std::enable_if_t< !std::is_convertible<T, Value>::value,
optional<T>> fromExpressionValue(const Value& v)
{
    return Converter<T>::fromExpressionValue(v);
}

template <typename T>
type::Type valueTypeToExpressionType() {
    return Converter<T>::expressionType();
}

template <> type::Type valueTypeToExpressionType<Value>() { return type::Value; }
template <> type::Type valueTypeToExpressionType<NullValue>() { return type::Null; }
template <> type::Type valueTypeToExpressionType<bool>() { return type::Boolean; }
template <> type::Type valueTypeToExpressionType<double>() { return type::Number; }
template <> type::Type valueTypeToExpressionType<std::string>() { return type::String; }
template <> type::Type valueTypeToExpressionType<mbgl::Color>() { return type::Color; }
template <> type::Type valueTypeToExpressionType<std::unordered_map<std::string, Value>>() { return type::Object; }
template <> type::Type valueTypeToExpressionType<std::vector<Value>>() { return type::Array(type::Value); }


template Value toExpressionValue(const mbgl::Value&);

// instantiate templates fromExpressionValue<T>, toExpressionValue<T>, and valueTypeToExpressionType<T>
template <typename T>
struct instantiate {
    void noop(const T& t) {
        fromExpressionValue<T>(toExpressionValue(t));
        valueTypeToExpressionType<T>();
    }
};

// for to_rgba expression
template struct instantiate<std::array<double, 4>>;

// layout/paint property types
template struct instantiate<float>;
template struct instantiate<std::array<float, 2>>;
template struct instantiate<std::array<float, 4>>;
template struct instantiate<std::vector<float>>;
template struct instantiate<std::vector<std::string>>;
template struct instantiate<AlignmentType>;
template struct instantiate<CirclePitchScaleType>;
template struct instantiate<IconTextFitType>;
template struct instantiate<LineCapType>;
template struct instantiate<LineJoinType>;
template struct instantiate<SymbolPlacementType>;
template struct instantiate<TextAnchorType>;
template struct instantiate<TextJustifyType>;
template struct instantiate<TextTransformType>;
template struct instantiate<TranslateAnchorType>;
template struct instantiate<LightAnchorType>;
template struct instantiate<Position>;

} // namespace expression
} // namespace style
} // namespace mbgl