#pragma once #include #include #include #include #include #include #include #include #include #include namespace mbgl { class GeometryTileFeature; namespace style { namespace expression { class EvaluationError { public: std::string message; }; class EvaluationContext { public: EvaluationContext() = default; explicit EvaluationContext(float zoom_) : zoom(zoom_) {} explicit EvaluationContext(GeometryTileFeature const * feature_) : feature(feature_) {} EvaluationContext(float zoom_, GeometryTileFeature const* feature_) : zoom(zoom_), feature(feature_) {} EvaluationContext(optional accumulated_, GeometryTileFeature const * feature_) : accumulated(std::move(accumulated_)), feature(feature_) {} EvaluationContext(float zoom_, GeometryTileFeature const* feature_, const FeatureState* state_) : zoom(zoom_), feature(feature_), featureState(state_) {} EvaluationContext(optional zoom_, GeometryTileFeature const * feature_, optional colorRampParameter_) : zoom(std::move(zoom_)), feature(feature_), colorRampParameter(std::move(colorRampParameter_)) {} EvaluationContext& withFormattedSection(const Value* formattedSection_) noexcept { formattedSection = formattedSection_; return *this; }; EvaluationContext& withFeatureState(const FeatureState* featureState_) noexcept { featureState = featureState_; return *this; }; EvaluationContext& withAvailableImages(const std::set* availableImages_) noexcept { availableImages = availableImages_; return *this; }; EvaluationContext& withCanonicalTileID(const mbgl::CanonicalTileID* canonical_) noexcept { canonical = canonical_; return *this; }; optional zoom; optional accumulated; GeometryTileFeature const* feature = nullptr; optional colorRampParameter; // Contains formatted section object, std::unordered_map. const Value* formattedSection = nullptr; const FeatureState* featureState = nullptr; const std::set* availableImages = nullptr; const mbgl::CanonicalTileID* canonical = nullptr; }; template class Result : private variant { public: using variant::variant; using Value = T; Result() = default; explicit operator bool () const { return this->template is(); } // optional does some type trait magic for this one, so this might // be problematic as is. const T* operator->() const { assert(this->template is()); return std::addressof(this->template get()); } T* operator->() { assert(this->template is()); return std::addressof(this->template get()); } T& operator*() { assert(this->template is()); return this->template get(); } const T& operator*() const { assert(this->template is()); return this->template get(); } const EvaluationError& error() const { assert(this->template is()); return this->template get(); } }; class EvaluationResult : public Result { public: using Result::Result; // NOLINT EvaluationResult() = default; EvaluationResult(const std::array& arr) : Result(toExpressionValue(arr)) {} // used only for the special (private) "error" expression EvaluationResult(const type::ErrorType&) { assert(false); } }; /* Expression is an abstract class that serves as an interface and base class for particular expression implementations. CompoundExpression implements the majority of expressions in the spec by inferring the argument and output from a simple function (const T0& arg0, const T1& arg1, ...) -> Result where T0, T1, ..., U are member types of mbgl::style::expression::Value. The other Expression subclasses (Let, Curve, Match, etc.) exist in order to implement expressions that need specialized parsing, type checking, or evaluation logic that can't be handled by CompoundExpression's inference mechanism. Each Expression subclass also provides a static ParseResult ExpressionClass::parse(const V&, ParsingContext), which handles parsing a style-spec JSON representation of the expression. */ enum class Kind : int32_t { Coalesce, CompoundExpression, Literal, At, Interpolate, Assertion, Length, Step, Let, Var, CollatorExpression, Coercion, Match, Error, Case, Any, All, Comparison, FormatExpression, FormatSectionOverride, NumberFormat, ImageExpression, In, Within, Distance }; class Expression : public Serializable { public: Expression(Kind kind_, type::Type type_) : kind(kind_), type(std::move(type_)) {} ~Expression() override = default; virtual EvaluationResult evaluate(const EvaluationContext& params) const = 0; virtual void eachChild(const std::function&) const = 0; virtual bool operator==(const Expression&) const = 0; bool operator!=(const Expression& rhs) const { return !operator==(rhs); } Kind getKind() const { return kind; }; type::Type getType() const { return type; }; EvaluationResult evaluate(optional zoom, const Feature& feature, optional colorRampParameter) const; EvaluationResult evaluate(optional zoom, const Feature& feature, optional colorRampParameter, const std::set& availableImages) const; EvaluationResult evaluate(optional zoom, const Feature& feature, optional colorRampParameter, const std::set& availableImages, const CanonicalTileID& canonical) const; EvaluationResult evaluate(optional accumulated, const Feature& feature) const; /** * Statically analyze the expression, attempting to enumerate possible outputs. Returns * an array of values plus the sentinel null optional value, used to indicate that the * complete set of outputs is statically undecidable. */ virtual std::vector> possibleOutputs() const = 0; mbgl::Value serialize() const override { std::vector serialized; serialized.emplace_back(getOperator()); eachChild([&](const Expression &child) { serialized.emplace_back(child.serialize()); }); return serialized; }; virtual std::string getOperator() const = 0; protected: template static bool childrenEqual(const T& lhs, const T& rhs) { if (lhs.size() != rhs.size()) return false; for (auto leftChild = lhs.begin(), rightChild = rhs.begin(); leftChild != lhs.end(); leftChild++, rightChild++) { if (!Expression::childEqual(*leftChild, *rightChild)) return false; } return true; } static bool childEqual(const std::unique_ptr& lhs, const std::unique_ptr& rhs) { return *lhs == *rhs; } template static bool childEqual(const std::pair>& lhs, const std::pair>& rhs) { return lhs.first == rhs.first && *(lhs.second) == *(rhs.second); } template static bool childEqual(const std::pair>& lhs, const std::pair>& rhs) { return lhs.first == rhs.first && *(lhs.second) == *(rhs.second); } static bool childEqual(const std::pair, std::unique_ptr>& lhs, const std::pair, std::unique_ptr>& rhs) { return *(lhs.first) == *(rhs.first) && *(lhs.second) == *(rhs.second); } private: Kind kind; type::Type type; }; } // namespace expression } // namespace style } // namespace mbgl