summaryrefslogtreecommitdiff
path: root/include/mbgl/style/expression/value.hpp
blob: 6170ac46ecf0d970ed95a69be0c104e8d15bd4de (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
#pragma once

#include <mbgl/style/expression/collator.hpp>
#include <mbgl/style/expression/type.hpp>
#include <mbgl/style/position.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/variant.hpp>

#include <array>
#include <vector>

namespace mbgl {
namespace style {
namespace expression {

struct Value;

using ValueBase = variant<NullValue,
                          bool,
                          double,
                          std::string,
                          Color,
                          Collator,
                          mapbox::util::recursive_wrapper<std::vector<Value>>,
                          mapbox::util::recursive_wrapper<std::unordered_map<std::string, Value>>>;
struct Value : ValueBase {
    using ValueBase::ValueBase;

    template <typename T>
    bool is(T*) const {
        return ValueBase::is<T>();
    }

    template <>
    bool is(float*) const {
        return ValueBase::is<double>();
    }

    template <typename T>
    bool is() const {
        return ValueBase::is<T>();
    }
    template <typename T>
    T& get(T*) {
        return ValueBase::get<T>();
    }
    template <typename T>
    T const& get(T*) const {
        return ValueBase::get<T>();
    }
    template <>
    float const& get(float*) const {
        return ValueBase::get<double>();
    }

    template <typename T>
    T& get() {
        return ValueBase::get<T>();
    }
    template <typename T>
    T const& get() const {
        return ValueBase::get<T>();
    }

    Value(NullValue&& args) : ValueBase(std::move(args)) {
    }
    Value(bool&& args) : ValueBase(std::forward<bool>(args)) {
    }
    Value(double&& args) : ValueBase(std::forward<double>(args)) {
    }
    Value(std::string&& args) : ValueBase(std::move(args)) {
    }
    Value(Color&& args) : ValueBase(std::forward<Color>(args)) {
    }
    Value(std::vector<Value>&& args)
        : ValueBase(mapbox::util::recursive_wrapper<std::vector<Value>>(args)) {
    }
    typedef mapbox::util::recursive_wrapper<std::unordered_map<std::string, Value>> rec_map;
    Value(std::unordered_map<std::string, Value>&& args) : ValueBase(rec_map(args)) {
    }

    Value(const NullValue& args) : ValueBase(args) {
    }
    Value(const bool& args) : ValueBase(args) {
    }
    Value(const double& args) : ValueBase(args) {
    }
    Value(const std::string& args) : ValueBase(args) {
    }
    Value(const Color& args) : ValueBase(args) {
    }
    Value(const std::vector<Value>& args)
        : ValueBase(mapbox::util::recursive_wrapper<std::vector<Value>>(args)) {
    }
    Value(const std::unordered_map<std::string, Value>& args) : ValueBase(rec_map(args)) {
    }

    Value(const Value&) = default;

    // Javascript's Number.MAX_SAFE_INTEGER
    static uint64_t maxSafeInteger() {
        return 9007199254740991ULL;
    }

    static bool isSafeInteger(uint64_t x) {
        return x <= maxSafeInteger();
    };
    static bool isSafeInteger(int64_t x) {
        return static_cast<uint64_t>(x > 0 ? x : -x) <= maxSafeInteger();
    }
    static bool isSafeInteger(double x) {
        return static_cast<uint64_t>(x > 0 ? x : -x) <= maxSafeInteger();
    }
};

constexpr NullValue Null = NullValue();

type::Type typeOf(const Value& value);
std::string stringify(const Value& value);

/*
  Returns a Type object representing the expression type that corresponds to
  the value type T.  (Specialized for primitives and specific array types in
  the .cpp.)
*/
template <typename T>
type::Type valueTypeToExpressionType();

/*
  Conversions between style value types and expression::Value
*/

template <class T, class Enable = void>
struct ValueConverter {
    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 ValueConverter<Value> {
    static type::Type expressionType() {
        return type::Value;
    }
    static Value toExpressionValue(const Value& value) {
        return value;
    }
    static optional<Value> fromExpressionValue(const Value& value) {
        return value;
    }
};

template <>
struct ValueConverter<mbgl::Value> {
    static Value toExpressionValue(const mbgl::Value& value);
    static mbgl::Value fromExpressionValue(const Value& value);
};

template <>
struct ValueConverter<float> {
    static type::Type expressionType() {
        return type::Number;
    }
    static Value toExpressionValue(const float value);
    static optional<float> fromExpressionValue(const Value& value);
};

template <typename T, std::size_t N>
struct ValueConverter<std::array<T, N>> {
    static type::Type expressionType() {
        return type::Array(valueTypeToExpressionType<T>(), N);
    }
    static Value toExpressionValue(const std::array<T, N>& value);
    static optional<std::array<T, N>> fromExpressionValue(const Value& value);
};

template <typename T>
struct ValueConverter<std::vector<T>> {
    static type::Type expressionType() {
        return type::Array(valueTypeToExpressionType<T>());
    }
    static Value toExpressionValue(const std::vector<T>& value);
    static optional<std::vector<T>> fromExpressionValue(const Value& value);
};

template <>
struct ValueConverter<Position> {
    static type::Type expressionType() {
        return type::Array(type::Number, 3);
    }
    static Value toExpressionValue(const mbgl::style::Position& value);
    static optional<Position> fromExpressionValue(const Value& v);
};

template <typename T>
struct ValueConverter<T, std::enable_if_t<std::is_enum<T>::value>> {
    static type::Type expressionType() {
        return type::String;
    }
    static Value toExpressionValue(const T& value);
    static optional<T> fromExpressionValue(const Value& value);
};

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

template <typename T>
optional<T> fromExpressionValue(const Value& value) {
    return ValueConverter<T>::fromExpressionValue(value);
}

template <typename T>
std::vector<optional<T>> fromExpressionValues(const std::vector<optional<Value>>& values) {
    std::vector<optional<T>> result;
    for (const auto& value : values) {
        result.push_back(value ? fromExpressionValue<T>(*value) : nullopt);
    }
    return result;
}

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