#pragma once #include #include #include #include #include #include #include namespace mbgl { namespace style { template class CompositeFunction { public: // The second parameter should be used only for conversions from legacy functions. CompositeFunction(std::unique_ptr expression_, optional defaultValue_ = {}) : isExpression(defaultValue_), expression(std::move(expression_)), defaultValue(std::move(defaultValue_)), zoomCurve(expression::findZoomCurveChecked(expression.get())) { assert(!expression::isZoomConstant(*expression)); assert(!expression::isFeatureConstant(*expression)); } // Return the range obtained by evaluating the function at each of the zoom levels in zoomRange template Range evaluate(const Range& zoomRange, const Feature& feature, T finalDefaultValue) { return Range { evaluate(zoomRange.min, feature, finalDefaultValue), evaluate(zoomRange.max, feature, finalDefaultValue) }; } template T evaluate(float zoom, const Feature& feature, T finalDefaultValue) const { const expression::EvaluationResult result = expression->evaluate(expression::EvaluationContext({zoom}, &feature)); if (result) { const optional typed = expression::fromExpressionValue(*result); return typed ? *typed : defaultValue ? *defaultValue : finalDefaultValue; } return defaultValue ? *defaultValue : finalDefaultValue; } float interpolationFactor(const Range& inputLevels, const float inputValue) const { return zoomCurve.match( [&](const expression::Interpolate* z) { return z->interpolationFactor(Range { inputLevels.min, inputLevels.max }, inputValue); }, [&](const expression::Step*) { return 0.0f; } ); } Range getCoveringStops(const float lower, const float upper) const { return zoomCurve.match( [&](auto z) { return z->getCoveringStops(lower, upper); } ); } std::vector> possibleOutputs() const { return expression::fromExpressionValues(expression->possibleOutputs()); } friend bool operator==(const CompositeFunction& lhs, const CompositeFunction& rhs) { return *lhs.expression == *rhs.expression; } const expression::Expression& getExpression() const { return *expression; } bool useIntegerZoom = false; bool isExpression; private: std::shared_ptr expression; optional defaultValue; const variant zoomCurve; }; } // namespace style } // namespace mbgl