summaryrefslogtreecommitdiff
path: root/include/mbgl/style/conversion/constant.hpp
blob: 05bf968f4dba08d1ad958261f86b4feb7ed24c93 (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
#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>
    Result<bool> operator()(const V& value) const {
        optional<bool> converted = toBool(value);
        if (!converted) {
            return Error { "value must be a boolean" };
        }
        return *converted;
    }
};

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

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

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

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

        return *result;
    }
};

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

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

        return *color;
    }
};

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

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

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

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

        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) {
            return Error { "value must be an array of four numbers" };
        }

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

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

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

        return result;
    }
};

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

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

        return result;
    }
};

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