1 | #include <mbgl/style/conversion/constant.hpp> |
---|---|
2 | |
3 | namespace mbgl { |
4 | namespace style { |
5 | namespace conversion { |
6 | |
7 | optional<bool> Converter<bool>::operator()(const Convertible& value, Error& error) const { |
8 | optional<bool> converted = toBool(v: value); |
9 | if (!converted) { |
10 | error = { .message: "value must be a boolean"}; |
11 | return {}; |
12 | } |
13 | return *converted; |
14 | } |
15 | |
16 | optional<float> Converter<float>::operator()(const Convertible& value, Error& error) const { |
17 | optional<float> converted = toNumber(v: value); |
18 | if (!converted) { |
19 | error = { .message: "value must be a number"}; |
20 | return {}; |
21 | } |
22 | return *converted; |
23 | } |
24 | |
25 | optional<std::string> Converter<std::string>::operator()(const Convertible& value, Error& error) const { |
26 | optional<std::string> converted = toString(v: value); |
27 | if (!converted) { |
28 | error = { .message: "value must be a string"}; |
29 | return {}; |
30 | } |
31 | return *converted; |
32 | } |
33 | |
34 | optional<Color> Converter<Color>::operator()(const Convertible& value, Error& error) const { |
35 | optional<std::string> string = toString(v: value); |
36 | if (!string) { |
37 | error = { .message: "value must be a string"}; |
38 | return {}; |
39 | } |
40 | |
41 | optional<Color> color = Color::parse(*string); |
42 | if (!color) { |
43 | error = { .message: "value must be a valid color"}; |
44 | return {}; |
45 | } |
46 | |
47 | return *color; |
48 | } |
49 | |
50 | optional<std::vector<float>> Converter<std::vector<float>>::operator()(const Convertible& value, Error& error) const { |
51 | if (!isArray(v: value)) { |
52 | error = { .message: "value must be an array"}; |
53 | return {}; |
54 | } |
55 | |
56 | std::vector<float> result; |
57 | result.reserve(n: arrayLength(v: value)); |
58 | |
59 | for (std::size_t i = 0; i < arrayLength(v: value); ++i) { |
60 | optional<float> number = toNumber(v: arrayMember(v: value, i)); |
61 | if (!number) { |
62 | error = { .message: "value must be an array of numbers"}; |
63 | return {}; |
64 | } |
65 | result.push_back(x: *number); |
66 | } |
67 | |
68 | return result; |
69 | } |
70 | |
71 | optional<std::vector<std::string>> Converter<std::vector<std::string>>::operator()(const Convertible& value, Error& error) const { |
72 | if (!isArray(v: value)) { |
73 | error = { .message: "value must be an array"}; |
74 | return {}; |
75 | } |
76 | |
77 | std::vector<std::string> result; |
78 | result.reserve(n: arrayLength(v: value)); |
79 | |
80 | for (std::size_t i = 0; i < arrayLength(v: value); ++i) { |
81 | optional<std::string> string = toString(v: arrayMember(v: value, i)); |
82 | if (!string) { |
83 | error = { .message: "value must be an array of strings"}; |
84 | return {}; |
85 | } |
86 | result.push_back(x: *string); |
87 | } |
88 | |
89 | return result; |
90 | } |
91 | |
92 | } // namespace conversion |
93 | } // namespace style |
94 | } // namespace mbgl |
95 |