summaryrefslogtreecommitdiff
path: root/include/mbgl/style/conversion/constant.hpp
blob: 1e1fdc2ee8ce6b356a9bdea1e1c88756de2b872f (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
#pragma once

#include <mbgl/style/conversion.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>

#include <array>
#include <string>
#include <vector>

namespace mbgl {
namespace style {
namespace conversion {

template <>
struct Converter<bool> {
    template <class V>
    optional<bool> operator()(const V& value, Error& error) const {
        optional<bool> converted = toBool(value);
        if (!converted) {
            error = { "value must be a boolean" };
            return {};
        }
        return *converted;
    }
};

template <>
struct Converter<float> {
    template <class V>
    optional<float> operator()(const V& value, Error& error) const {
        optional<float> converted = toNumber(value);
        if (!converted) {
            error = { "value must be a number" };
            return {};
        }
        return *converted;
    }
};

template <>
struct Converter<std::string> {
    template <class V>
    optional<std::string> operator()(const V& value, Error& error) const {
        optional<std::string> converted = toString(value);
        if (!converted) {
            error = { "value must be a string" };
            return {};
        }
        return *converted;
    }
};

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

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

        return *result;
    }
};

template <>
struct Converter<Color> {
    template <class V>
    optional<Color> operator()(const V& value, Error& error) const {
        optional<std::string> string = toString(value);
        if (!string) {
            error = { "value must be a string" };
            return {};
        }

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

        return *color;
    }
};

template <>
struct Converter<std::array<float, 2>> {
    template <class V>
    optional<std::array<float, 2>> operator()(const V& value, Error& error) const {
        if (!isArray(value) || arrayLength(value) != 2) {
            error = { "value must be an array of two numbers" };
            return {};
        }

        optional<float> first = toNumber(arrayMember(value, 0));
        optional<float> second = toNumber(arrayMember(value, 1));
        if (!first || !second) {
            error = { "value must be an array of two numbers" };
            return {};
        }

        return std::array<float, 2> {{ *first, *second }};
    }
};

template <>
struct Converter<std::array<float, 4>> {
    template <class V>
    optional<std::array<float, 4>> operator()(const V& value, Error& error) const {
        if (!isArray(value) || arrayLength(value) != 4) {
            error = { "value must be an array of four numbers" };
            return {};
        }

        optional<float> first = toNumber(arrayMember(value, 0));
        optional<float> second = toNumber(arrayMember(value, 1));
        optional<float> third = toNumber(arrayMember(value, 2));
        optional<float> fourth = toNumber(arrayMember(value, 3));
        if (!first || !second) {
            error = { "value must be an array of four numbers" };
            return {};
        }

        return std::array<float, 4> {{ *first, *second, *third, *fourth }};
    }
};

template <>
struct Converter<std::vector<float>> {
    template <class V>
    optional<std::vector<float>> operator()(const V& value, Error& error) const {
        if (!isArray(value)) {
            error = { "value must be an array" };
            return {};
        }

        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 = { "value must be an array of numbers" };
                return {};
            }
            result.push_back(*number);
        }

        return result;
    }
};

template <>
struct Converter<std::vector<std::string>> {
    template <class V>
    optional<std::vector<std::string>> operator()(const V& value, Error& error) const {
        if (!isArray(value)) {
            error = { "value must be an array" };
            return {};
        }

        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 = { "value must be an array of strings" };
                return {};
            }
            result.push_back(*string);
        }

        return result;
    }
};

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