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