summaryrefslogtreecommitdiff
path: root/src/mbgl/style/conversion/constant.cpp
blob: 0ed5fad425636afafdf63dc1080a2a840a4e50e6 (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
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion_impl.hpp>

namespace mbgl {
namespace style {
namespace conversion {

optional<bool> Converter<bool>::operator()(const Convertible& value, Error& error) const {
    optional<bool> converted = toBool(value);
    if (!converted) {
        error.message = "value must be a boolean";
        return nullopt;
    }
    return *converted;
}

optional<float> Converter<float>::operator()(const Convertible& value, Error& error) const {
    optional<float> converted = toNumber(value);
    if (!converted) {
        error.message = "value must be a number";
        return nullopt;
    }
    return *converted;
}

optional<std::string> Converter<std::string>::operator()(const Convertible& value, Error& error) const {
    optional<std::string> converted = toString(value);
    if (!converted) {
        error.message = "value must be a string";
        return nullopt;
    }
    return *converted;
}

template <class T>
optional<T> Converter<T, typename std::enable_if_t<std::is_enum<T>::value>>::operator()(const Convertible& value, Error& error) const {
    optional<std::string> string = toString(value);
    if (!string) {
        error.message = "value must be a string";
        return nullopt;
    }

    const auto result = Enum<T>::toEnum(*string);
    if (!result) {
        error.message = "value must be a valid enumeration value";
        return nullopt;
    }

    return *result;
}

template <class T>
auto Converter<std::vector<T>, typename std::enable_if_t<std::is_enum<T>::value>>::operator()(const Convertible& value, Error& error) const -> optional<std::vector<T>> {
    if (!isArray(value)) {
        error.message = "value must be an array";
        return nullopt;
    }

    std::vector<T> result;
    result.reserve(arrayLength(value));

    for (std::size_t i = 0; i < arrayLength(value); ++i) {
        optional<T> enumItem = Converter<T>{}(arrayMember(value, i), error);
        if (!enumItem) {
            return nullopt;
        }
        result.push_back(*enumItem);
    }

    return result;
}

template optional<AlignmentType> Converter<AlignmentType>::operator()(const Convertible&, Error&) const;
template optional<CirclePitchScaleType> Converter<CirclePitchScaleType>::operator()(const Convertible&, Error&) const;
template optional<HillshadeIlluminationAnchorType> Converter<HillshadeIlluminationAnchorType>::operator()(const Convertible&, Error&) const;
template optional<IconTextFitType> Converter<IconTextFitType>::operator()(const Convertible&, Error&) const;
template optional<LightAnchorType> Converter<LightAnchorType>::operator()(const Convertible&, Error&) const;
template optional<LineCapType> Converter<LineCapType>::operator()(const Convertible&, Error&) const;
template optional<LineJoinType> Converter<LineJoinType>::operator()(const Convertible&, Error&) const;
template optional<RasterResamplingType> Converter<RasterResamplingType>::operator()(const Convertible&, Error&) const;
template optional<SymbolAnchorType> Converter<SymbolAnchorType>::operator()(const Convertible&, Error&) const;
template optional<SymbolPlacementType> Converter<SymbolPlacementType>::operator()(const Convertible&, Error&) const;
template optional<SymbolZOrderType> Converter<SymbolZOrderType>::operator()(const Convertible&, Error&) const;
template optional<TextJustifyType> Converter<TextJustifyType>::operator()(const Convertible&, Error&) const;
template optional<TextTransformType> Converter<TextTransformType>::operator()(const Convertible&, Error&) const;
template optional<TranslateAnchorType> Converter<TranslateAnchorType>::operator()(const Convertible&, Error&) const;
template optional<VisibilityType> Converter<VisibilityType>::operator()(const Convertible&, Error&) const;
template optional<std::vector<TextVariableAnchorType>> Converter<std::vector<TextVariableAnchorType>>::operator()(const Convertible&, Error&) const;

optional<Color> Converter<Color>::operator()(const Convertible& value, Error& error) const {
    optional<std::string> string = toString(value);
    if (!string) {
        error.message = "value must be a string";
        return nullopt;
    }

    optional<Color> color = Color::parse(*string);
    if (!color) {
        error.message = "value must be a valid color";
        return nullopt;
    }

    return *color;
}

template <size_t N>
optional<std::array<float, N>> Converter<std::array<float, N>>::operator()(const Convertible& value, Error& error) const {
    if (!isArray(value) || arrayLength(value) != N) {
        error.message = "value must be an array of " + util::toString(N) + " numbers";
        return nullopt;
    }

    std::array<float, N> result;
    for (size_t i = 0; i < N; i++) {
        optional<float> n = toNumber(arrayMember(value, i));
        if (!n) {
            error.message = "value must be an array of " + util::toString(N) + " numbers";
            return nullopt;
        }
        result[i] = *n;
    }
    return result;
}

template optional<std::array<float, 2>> Converter<std::array<float, 2>>::operator()(const Convertible&, Error&) const;
template optional<std::array<float, 3>> Converter<std::array<float, 3>>::operator()(const Convertible&, Error&) const;
template optional<std::array<float, 4>> Converter<std::array<float, 4>>::operator()(const Convertible&, Error&) const;

optional<std::vector<float>> Converter<std::vector<float>>::operator()(const Convertible& value, Error& error) const {
    if (!isArray(value)) {
        error.message = "value must be an array";
        return nullopt;
    }

    std::vector<float> result;
    result.reserve(arrayLength(value));

    for (std::size_t i = 0; i < arrayLength(value); ++i) {
        optional<float> number = toNumber(arrayMember(value, i));
        if (!number) {
            error.message = "value must be an array of numbers";
            return nullopt;
        }
        result.push_back(*number);
    }

    return result;
}

optional<std::vector<std::string>> Converter<std::vector<std::string>>::operator()(const Convertible& value, Error& error) const {
    if (!isArray(value)) {
        error.message = "value must be an array";
        return nullopt;
    }

    std::vector<std::string> result;
    result.reserve(arrayLength(value));

    for (std::size_t i = 0; i < arrayLength(value); ++i) {
        optional<std::string> string = toString(arrayMember(value, i));
        if (!string) {
            error.message = "value must be an array of strings";
            return nullopt;
        }
        result.push_back(*string);
    }

    return result;
}

optional<RadialOffsetType> Converter<RadialOffsetType>::operator()(const Convertible& value, Error& error) const {
    Converter<std::vector<float>> arrayConverter;
    if (auto val = arrayConverter(value, error)) {
        RadialOffsetType result = std::move(*val);
        return result;
    }

    Converter<float> numberConverter;
    if (auto val = numberConverter(value, error)) {
        RadialOffsetType result(2, *val);
        return result;
    }

    error.message = "value must be an array of numbers or a number";

    return nullopt;
}

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