1 | #pragma once |
2 | |
3 | #include <mbgl/util/variant.hpp> |
4 | #include <mbgl/style/undefined.hpp> |
5 | #include <mbgl/style/property_expression.hpp> |
6 | |
7 | namespace mbgl { |
8 | namespace style { |
9 | |
10 | template <class T> |
11 | class DataDrivenPropertyValue { |
12 | private: |
13 | using Value = variant< |
14 | Undefined, |
15 | T, |
16 | PropertyExpression<T>>; |
17 | |
18 | Value value; |
19 | |
20 | friend bool operator==(const DataDrivenPropertyValue& lhs, |
21 | const DataDrivenPropertyValue& rhs) { |
22 | return lhs.value == rhs.value; |
23 | } |
24 | |
25 | friend bool operator!=(const DataDrivenPropertyValue& lhs, |
26 | const DataDrivenPropertyValue& rhs) { |
27 | return !(lhs == rhs); |
28 | } |
29 | |
30 | public: |
31 | DataDrivenPropertyValue() = default; |
32 | DataDrivenPropertyValue( T v) : value(std::move(v)) {} |
33 | DataDrivenPropertyValue(PropertyExpression<T> v) : value(std::move(v)) {} |
34 | |
35 | bool isUndefined() const { |
36 | return value.template is<Undefined>(); |
37 | } |
38 | |
39 | bool isDataDriven() const { |
40 | return value.match( |
41 | [] (const Undefined&) { return false; }, |
42 | [] (const T&) { return false; }, |
43 | [] (const PropertyExpression<T>& fn) { return !fn.isFeatureConstant(); } |
44 | ); |
45 | } |
46 | |
47 | bool isZoomConstant() const { |
48 | return value.match( |
49 | [] (const Undefined&) { return true; }, |
50 | [] (const T&) { return true; }, |
51 | [] (const PropertyExpression<T>& fn) { return fn.isZoomConstant(); } |
52 | ); |
53 | } |
54 | |
55 | const T & asConstant() const { return value.template get< T >(); } |
56 | const PropertyExpression<T>& asExpression() const { return value.template get<PropertyExpression<T>>(); } |
57 | |
58 | template <class... Ts> |
59 | auto match(Ts&&... ts) const { |
60 | return value.match(std::forward<Ts>(ts)...); |
61 | } |
62 | |
63 | template <typename Evaluator> |
64 | auto evaluate(const Evaluator& evaluator, TimePoint = {}) const { |
65 | return Value::visit(value, evaluator); |
66 | } |
67 | |
68 | bool hasDataDrivenPropertyDifference(const DataDrivenPropertyValue<T>& other) const { |
69 | return *this != other && (isDataDriven() || other.isDataDriven()); |
70 | } |
71 | }; |
72 | |
73 | } // namespace style |
74 | } // namespace mbgl |
75 | |