summaryrefslogtreecommitdiff
path: root/include/mbgl/style/function/expression.hpp
blob: 6a25f006df5a7e8125eb52699ccaba421b3bf572 (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
92
93
94
95
96
97
98
99
100
101
#pragma once

#include <array>
#include <vector>
#include <memory>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/style/function/type.hpp>
#include <mbgl/util/feature.hpp>


namespace mbgl {

class GeometryTileFeature;


namespace style {
namespace expression {

using OutputValue = variant<
    float,
    std::string,
    mbgl::Color,
    std::array<float, 2>,
    std::array<float, 4>>;

class Error {
public:
    Error() {}
    Error(std::string message_) : message(message_) {}
    std::string message;
};

class Expression {
public:
    Expression(std::string key_, type::Type type_) : key(key_), type(type_) {}
    virtual ~Expression() {}
    
    virtual optional<OutputValue> evaluate(float, const GeometryTileFeature&, Error& e) const = 0;
    
    // Exposed for use with pure Feature objects (e.g. beyond the context of tiled map data).
    optional<OutputValue> evaluate(float, const Feature&, Error&) const;
    
    type::Type getType() {
        return type;
    }
    
    bool isFeatureConstant() {
        return true;
    }
    
    bool isZoomConstant() {
        return true;
    }
    
private:
    std::string key;
    type::Type type;
};

class LiteralExpression : public Expression {
public:
    LiteralExpression(std::string key, OutputValue value_) :
        Expression(key, value_.match(
            [&] (const float&) -> type::Type { return type::Primitive::Number; },
            [&] (const std::string&) { return type::Primitive::String; },
            [&] (const mbgl::Color) { return type::Primitive::Color; },
            [&] (const auto&) { return type::Primitive::Null; } // TODO (remaining output types)
        )),
        value(value_)
    {}
    
    optional<OutputValue> evaluate(float, const GeometryTileFeature&, Error&) const override {
        return {value};
    }
    
private:
    OutputValue value;
};

class LambdaExpression : public Expression {
public:
    LambdaExpression(std::string key,
                    type::Lambda type,
                    std::string name_,
                    std::vector<std::unique_ptr<Expression>> args_) :
        Expression(key, type),
        name(name_),
        args(std::move(args_))
    {}
    
private:
    std::string name;
    std::vector<std::unique_ptr<Expression>> args;
};



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