summaryrefslogtreecommitdiff
path: root/src/mbgl/style/expression/length.cpp
blob: 258353ae4efb164ba28b8541d4f782518f56c259 (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
#include <mbgl/style/expression/length.hpp>
#include <mbgl/util/string.hpp>

namespace mbgl {
namespace style {
namespace expression {

Length::Length(std::unique_ptr<Expression> input_)
    : Expression(type::Number),
      input(std::move(input_)) {
}

EvaluationResult Length::evaluate(const EvaluationContext& params) const {
    const EvaluationResult value = input->evaluate(params);
    if (!value) return value;
    return value->match(
        [] (const std::string& s) {
            return EvaluationResult { double(s.size()) };
        },
        [] (const std::vector<Value>& v) {
            return EvaluationResult { double(v.size()) };
        },
        [&] (const auto&) -> EvaluationResult {
            return EvaluationError { "Expected value to be of type string or array, but found " + toString(typeOf(*value)) + " instead." };
        });
}

void Length::eachChild(const std::function<void(const Expression&)>& visit) const {
    visit(*input);
}

bool Length::operator==(const Expression& e) const {
    if (auto eq = dynamic_cast<const Length*>(&e)) {
        return *eq->input == *input;
    }
    return false;
}

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

using namespace mbgl::style::conversion;
ParseResult Length::parse(const Convertible& value, ParsingContext& ctx) {
    std::size_t length = arrayLength(value);

    if (length != 2) {
        ctx.error("Expected one argument, but found " + util::toString(length) + " instead.");
        return ParseResult();
    }

    ParseResult input = ctx.parse(arrayMember(value, 1), 1);
    if (!input) return ParseResult();

    type::Type type = (*input)->getType();
    if (!type.is<type::Array>() && !type.is<type::StringType>() && !type.is<type::ValueType>()) {
        ctx.error("Expected argument of type string or array, but found " + toString(type) + " instead.");
        return ParseResult();
    }

    return ParseResult(std::make_unique<Length>(std::move(*input)));
}

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