1 | #pragma once |
---|---|
2 | |
3 | #include <mbgl/style/data_driven_property_value.hpp> |
4 | #include <mbgl/style/conversion.hpp> |
5 | #include <mbgl/style/conversion/constant.hpp> |
6 | #include <mbgl/style/conversion/function.hpp> |
7 | #include <mbgl/style/expression/is_expression.hpp> |
8 | #include <mbgl/style/expression/is_constant.hpp> |
9 | #include <mbgl/style/expression/literal.hpp> |
10 | #include <mbgl/style/expression/value.hpp> |
11 | #include <mbgl/style/expression/parsing_context.hpp> |
12 | |
13 | namespace mbgl { |
14 | namespace style { |
15 | namespace conversion { |
16 | |
17 | template <class T> |
18 | struct Converter<DataDrivenPropertyValue<T>> { |
19 | optional<DataDrivenPropertyValue<T>> operator()(const Convertible& value, Error& error, bool convertTokens) const { |
20 | using namespace mbgl::style::expression; |
21 | |
22 | if (isUndefined(v: value)) { |
23 | return DataDrivenPropertyValue<T>(); |
24 | } |
25 | |
26 | optional<PropertyExpression<T>> expression; |
27 | |
28 | if (isExpression(value)) { |
29 | ParsingContext ctx(valueTypeToExpressionType<T>()); |
30 | ParseResult parsed = ctx.parseLayerPropertyExpression(value); |
31 | if (!parsed) { |
32 | error = { .message: ctx.getCombinedErrors() }; |
33 | return {}; |
34 | } |
35 | expression = PropertyExpression<T>(std::move(*parsed)); |
36 | } else if (isObject(v: value)) { |
37 | expression = convertFunctionToExpression<T>(value, error, convertTokens); |
38 | } else { |
39 | optional<T> constant = convert<T>(value, error); |
40 | if (!constant) { |
41 | return {}; |
42 | } |
43 | return convertTokens ? maybeConvertTokens(*constant) : DataDrivenPropertyValue<T>(*constant); |
44 | } |
45 | |
46 | if (!expression) { |
47 | return {}; |
48 | } else if (!(*expression).isFeatureConstant() || !(*expression).isZoomConstant()) { |
49 | return { std::move(*expression) }; |
50 | } else if ((*expression).getExpression().getKind() == Kind::Literal) { |
51 | optional<T> constant = fromExpressionValue<T>( |
52 | static_cast<const Literal&>((*expression).getExpression()).getValue()); |
53 | if (!constant) { |
54 | return {}; |
55 | } |
56 | return DataDrivenPropertyValue<T>(*constant); |
57 | } else { |
58 | assert(false); |
59 | error = { .message: "expected a literal expression"}; |
60 | return {}; |
61 | } |
62 | } |
63 | |
64 | template <class S> |
65 | DataDrivenPropertyValue<T> maybeConvertTokens(const S& t) const { |
66 | return DataDrivenPropertyValue<T>(t); |
67 | }; |
68 | |
69 | DataDrivenPropertyValue<T> maybeConvertTokens(const std::string& t) const { |
70 | return hasTokens(t) |
71 | ? DataDrivenPropertyValue<T>(PropertyExpression<T>(convertTokenStringToExpression(t))) |
72 | : DataDrivenPropertyValue<T>(t); |
73 | } |
74 | }; |
75 | |
76 | } // namespace conversion |
77 | } // namespace style |
78 | } // namespace mbgl |
79 |