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