summaryrefslogtreecommitdiff
path: root/include/mbgl/style/expression/format_section_override.hpp
blob: 63382fda5142aad57f5530c177df9ad4e2089565 (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
#pragma once

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/renderer/possibly_evaluated_property_value.hpp>

namespace mbgl {
namespace style {
namespace expression {

template<class T>
class FormatSectionOverride final : public Expression {
public:
    FormatSectionOverride(const type::Type& type_,
                         PossiblyEvaluatedPropertyValue<T> defaultValue_,
                         std::string propertyName_) :
        Expression(Kind::FormatSectionOverride, type_),
        defaultValue(std::move(defaultValue_)),
        propertyName(std::move(propertyName_))
    {}

    EvaluationResult evaluate(const EvaluationContext& context) const final {
        using Object = std::unordered_map<std::string, expression::Value>;
        if (context.formattedSection &&
            context.formattedSection->is<Object>()) {
            const auto& section = context.formattedSection->get<Object>();
            if (section.find(propertyName) != section.end()) {
                return section.at(propertyName);
            }
        }

        return defaultValue.match(
                [&context] (const style::PropertyExpression<T>& e) { return e.getExpression().evaluate(context); },
                [] (const T& t) -> EvaluationResult { return t; }
        );
    }

    void eachChild(const std::function<void(const Expression&)>& fn) const final {
        defaultValue.match([&fn] (const style::PropertyExpression<T>& e) { fn(e.getExpression()); },
                           [] (const T&) {});
    }

    bool operator==(const Expression& e) const final {
        if (e.getKind() == Kind::FormatSectionOverride) {
            const auto* other = static_cast<const FormatSectionOverride*>(&e);

            if (getType() != other->getType() || propertyName != other->propertyName) {
                return false;
            }

            // Check that default values or property expressions are equal.
            return defaultValue.match(
                    [other] (const style::PropertyExpression<T>& thisExpr) {
                        return other->defaultValue.match([&thisExpr] (const style::PropertyExpression<T>& otherExpr) {
                                                             return thisExpr == otherExpr;
                                                         },
                                                         [] (const T&) {
                                                             return false;
                                                         });
                    },
                    [other] (const T& thisValue) {
                        return other->defaultValue.match([&thisValue] (const T& otherValue) {
                                                            return thisValue == otherValue;
                                                         },
                                                         [] (const style::PropertyExpression<T>&) {
                                                            return false;
                                                        });
                    });
        }

        return false;
    }

    std::vector<optional<Value>> possibleOutputs() const final {
        return {nullopt};
    }

    std::string getOperator() const final { return "format-section-override"; }

private:
    PossiblyEvaluatedPropertyValue<T> defaultValue;
    std::string propertyName;
};

} // namespace expression
} // namespace style
} // namespace mbgl