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

#include <vector>
#include <memory>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/variant.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/type.hpp>
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/parsing_context.hpp>
#include <mbgl/style/conversion.hpp>


namespace mbgl {
namespace style {
namespace expression {

class Literal : public Expression {
public:
    Literal(Value value_) : Expression(typeOf(value_)), value(value_) {}
    Literal(type::Array type, std::vector<Value> value_) : Expression(type), value(value_) {}
    EvaluationResult evaluate(const EvaluationParameters&) const override {
        return value;
    }
    bool isFeatureConstant() const override { return true; }
    bool isZoomConstant() const override { return true; }
    
    template <class V>
    static ParseResult parse(const V& value, ParsingContext ctx) {
        const Value& parsedValue = parseValue(value);
        
        // special case: infer the item type if possible for zero-length arrays
        if (
            ctx.expected &&
            ctx.expected->template is<type::Array>() &&
            parsedValue.template is<std::vector<Value>>()
        ) {
            auto type = typeOf(parsedValue).template get<type::Array>();
            auto expected = ctx.expected->template get<type::Array>();
            if (
                type.N && (*type.N == 0) &&
                (!expected.N || (*expected.N == 0))
            ) {
                return ParseResult(std::make_unique<Literal>(expected, parsedValue.template get<std::vector<Value>>()));
            }
        }
        return ParseResult(std::make_unique<Literal>(parsedValue));
    }
    
private:
    Value value;
};

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