| 1 | #pragma once |
| 2 | |
| 3 | #include <mbgl/style/expression/expression.hpp> |
| 4 | #include <mbgl/style/expression/parsing_context.hpp> |
| 5 | #include <mbgl/style/conversion.hpp> |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <map> |
| 9 | |
| 10 | namespace mbgl { |
| 11 | namespace style { |
| 12 | namespace expression { |
| 13 | |
| 14 | class Let : public Expression { |
| 15 | public: |
| 16 | using Bindings = std::map<std::string, std::shared_ptr<Expression>>; |
| 17 | |
| 18 | Let(Bindings bindings_, std::unique_ptr<Expression> result_) : |
| 19 | Expression(Kind::Let, result_->getType()), |
| 20 | bindings(std::move(bindings_)), |
| 21 | result(std::move(result_)) |
| 22 | {} |
| 23 | |
| 24 | static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&); |
| 25 | |
| 26 | EvaluationResult evaluate(const EvaluationContext& params) const override; |
| 27 | void eachChild(const std::function<void(const Expression&)>&) const override; |
| 28 | |
| 29 | bool operator==(const Expression& e) const override { |
| 30 | if (e.getKind() == Kind::Let) { |
| 31 | auto rhs = static_cast<const Let*>(&e); |
| 32 | return *result == *(rhs->result); |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | std::vector<optional<Value>> possibleOutputs() const override; |
| 38 | |
| 39 | Expression* getResult() const { |
| 40 | return result.get(); |
| 41 | } |
| 42 | |
| 43 | mbgl::Value serialize() const override; |
| 44 | std::string getOperator() const override { return "let" ; } |
| 45 | private: |
| 46 | Bindings bindings; |
| 47 | std::unique_ptr<Expression> result; |
| 48 | }; |
| 49 | |
| 50 | class Var : public Expression { |
| 51 | public: |
| 52 | Var(std::string name_, std::shared_ptr<Expression> value_) : |
| 53 | Expression(Kind::Var, value_->getType()), |
| 54 | name(std::move(name_)), |
| 55 | value(value_) |
| 56 | {} |
| 57 | |
| 58 | static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&); |
| 59 | |
| 60 | EvaluationResult evaluate(const EvaluationContext& params) const override; |
| 61 | void eachChild(const std::function<void(const Expression&)>&) const override; |
| 62 | |
| 63 | bool operator==(const Expression& e) const override { |
| 64 | if (e.getKind() == Kind::Var) { |
| 65 | auto rhs = static_cast<const Var*>(&e); |
| 66 | return *value == *(rhs->value); |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | std::vector<optional<Value>> possibleOutputs() const override; |
| 72 | |
| 73 | mbgl::Value serialize() const override; |
| 74 | std::string getOperator() const override { return "var" ; } |
| 75 | |
| 76 | const std::shared_ptr<Expression>& getBoundExpression() const { return value; } |
| 77 | |
| 78 | private: |
| 79 | std::string name; |
| 80 | std::shared_ptr<Expression> value; |
| 81 | }; |
| 82 | |
| 83 | } // namespace expression |
| 84 | } // namespace style |
| 85 | } // namespace mbgl |
| 86 | |