summaryrefslogtreecommitdiff
path: root/src/mbgl/style/expression/find_zoom_curve.cpp
blob: 1e0a936605062203d68d272a44097a89c704ca82 (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
#include <mbgl/style/expression/find_zoom_curve.hpp>
#include <mbgl/style/expression/compound_expression.hpp>
#include <mbgl/style/expression/let.hpp>
#include <mbgl/style/expression/coalesce.hpp>
#include <mbgl/style/expression/is_constant.hpp>

#include <mbgl/util/variant.hpp>
#include <mbgl/util/optional.hpp>

namespace mbgl {
namespace style {
namespace expression {

optional<variant<const Interpolate*, const Step*, ParsingError>> findZoomCurve(const expression::Expression* e) {
    optional<variant<const Interpolate*, const Step*, ParsingError>> result;
    
    if (auto let = dynamic_cast<const Let*>(e)) {
        result = findZoomCurve(let->getResult());
    } else if (auto coalesce = dynamic_cast<const Coalesce*>(e)) {
        std::size_t length = coalesce->getLength();
        for (std::size_t i = 0; i < length; i++) {
            result = findZoomCurve(coalesce->getChild(i));
            if (result) {
                break;
            }
        }
    } else if (auto curve = dynamic_cast<const Interpolate*>(e)) {
        auto z = dynamic_cast<CompoundExpressionBase*>(curve->getInput().get());
        if (z && z->getName() == "zoom") {
            result = {curve};
        }
    } else if (auto step = dynamic_cast<const Step*>(e)) {
        auto z = dynamic_cast<CompoundExpressionBase*>(step->getInput().get());
        if (z && z->getName() == "zoom") {
            result = {step};
        }
    }
    
    if (result && result->is<ParsingError>()) {
        return result;
    }
    
    e->eachChild([&](const Expression& child) {
        optional<variant<const Interpolate*, const Step*, ParsingError>> childResult(findZoomCurve(&child));
        if (childResult) {
            if (childResult->is<ParsingError>()) {
                result = childResult;
            } else if (!result && childResult) {
                result = {ParsingError {
                    R"("zoom" expression may only be used as input to a top-level "step" or "interpolate" expression.)", ""
                }};
            } else if (result && childResult && result != childResult) {
                result = {ParsingError {
                    R"(Only one zoom-based "step" or "interpolate" subexpression may be used in an expression.)", ""
                }};
            }
        }
    });

    return result;
}

variant<std::nullptr_t, const Interpolate*, const Step*> findZoomCurveChecked(const expression::Expression* e) {
    if (isZoomConstant(*e)) {
        return nullptr;
    }
    return findZoomCurve(e)->match(
        [](const ParsingError&) -> variant<std::nullptr_t, const Interpolate*, const Step*> {
            assert(false);
            return nullptr;
        },
        [](auto zoomCurve) -> variant<std::nullptr_t, const Interpolate*, const Step*> {
            return zoomCurve;
        }
    );
}

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