summaryrefslogtreecommitdiff
path: root/include/mbgl/style/property_value.hpp
blob: 86812e23013bc0a6f77ef6d638708b54c6bc6563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once

#include <mbgl/util/variant.hpp>
#include <mbgl/style/undefined.hpp>
#include <mbgl/style/property_expression.hpp>

namespace mbgl {
namespace style {

template <class T>
class PropertyValue {
private:
    using Value = variant<
        Undefined,
        T,
        PropertyExpression<T>>;

    Value value;

    friend bool operator==(const PropertyValue& lhs,
                           const PropertyValue& rhs) {
        return lhs.value == rhs.value;
    }

    friend bool operator!=(const PropertyValue& lhs,
                           const PropertyValue& rhs) {
        return !(lhs == rhs);
    }

public:
    PropertyValue() = default;

    PropertyValue(T constant)
        : value(std::move(constant)) {}

    PropertyValue(PropertyExpression<T> expression)
        : value(std::move(expression)) {}

    bool isUndefined() const {
        return value.template is<Undefined>();
    }

    bool isConstant() const {
        return value.template is<T>();
    }

    bool isExpression() const {
        return value.template is<PropertyExpression<T>>();
    }

    bool isDataDriven() const {
        return value.match(
            [] (const Undefined&)                { return false; },
            [] (const T&)                        { return false; },
            [] (const PropertyExpression<T>& fn) { return !fn.isFeatureConstant(); }
        );
    }

    bool isZoomConstant() const {
        return value.match(
            [] (const Undefined&)                { return true; },
            [] (const T&)                        { return true; },
            [] (const PropertyExpression<T>& fn) { return fn.isZoomConstant(); }
        );
    }

    const T& asConstant() const {
        return value.template get<T>();
    }

    const PropertyExpression<T>& asExpression() const {
        return value.template get<PropertyExpression<T>>();
    }

    template <class... Ts>
    auto match(Ts&&... ts) const {
        return value.match(std::forward<Ts>(ts)...);
    }

    template <typename Evaluator>
    auto evaluate(const Evaluator& evaluator, TimePoint = {}) const {
        return Value::visit(value, evaluator);
    }

    bool hasDataDrivenPropertyDifference(const PropertyValue<T>& other) const {
        return *this != other && (isDataDriven() || other.isDataDriven());
    }
};

} // namespace style
} // namespace mbgl