From 25ac89e018a27fc689f742429c78ba492682b0a0 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 28 Jun 2018 13:59:55 -0700 Subject: [core] Remove stops-based *Function constructors --- include/mbgl/style/conversion/function.hpp | 57 +++++++++++++++++--- include/mbgl/style/function/camera_function.hpp | 36 +++---------- include/mbgl/style/function/composite_function.hpp | 61 +++------------------- include/mbgl/style/function/convert.hpp | 39 +++----------- include/mbgl/style/function/source_function.hpp | 43 +++------------ 5 files changed, 81 insertions(+), 155 deletions(-) (limited to 'include') diff --git a/include/mbgl/style/conversion/function.hpp b/include/mbgl/style/conversion/function.hpp index e230884944..932925c53c 100644 --- a/include/mbgl/style/conversion/function.hpp +++ b/include/mbgl/style/conversion/function.hpp @@ -3,9 +3,18 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include +#include namespace mbgl { namespace style { @@ -193,12 +202,22 @@ struct Converter> { return {}; } - auto stops = StopsConverter::Stops>()(value, error); + using Stops = std::conditional_t< + util::Interpolatable::value, + variant< + ExponentialStops, + IntervalStops>, + variant< + IntervalStops>>; + + auto stops = StopsConverter()(value, error); if (!stops) { return {}; } - return CameraFunction(*stops); + return CameraFunction((*stops).match([&] (const auto& s) { + return expression::Convert::toExpression(s); + }), false); } }; @@ -238,7 +257,19 @@ struct Converter> { return {}; } - auto stops = StopsConverter::Stops>()(value, error); + using Stops = std::conditional_t< + util::Interpolatable::value, + variant< + ExponentialStops, + IntervalStops, + CategoricalStops, + IdentityStops>, + variant< + IntervalStops, + CategoricalStops, + IdentityStops>>; + + auto stops = StopsConverter()(value, error); if (!stops) { return {}; } @@ -248,7 +279,9 @@ struct Converter> { return {}; } - return SourceFunction(*propertyString, *stops, *defaultValue); + return SourceFunction((*stops).match([&] (const auto& s) { + return expression::Convert::toExpression(*propertyString, s); + }), *defaultValue); } }; @@ -374,7 +407,17 @@ struct Converter> { return {}; } - auto stops = StopsConverter::Stops>()(value, error); + using Stops = std::conditional_t< + util::Interpolatable::value, + variant< + CompositeExponentialStops, + CompositeIntervalStops, + CompositeCategoricalStops>, + variant< + CompositeIntervalStops, + CompositeCategoricalStops>>; + + auto stops = StopsConverter()(value, error); if (!stops) { return {}; } @@ -384,7 +427,9 @@ struct Converter> { return {}; } - return CompositeFunction(*propertyString, *stops, *defaultValue); + return CompositeFunction((*stops).match([&] (const auto& s) { + return expression::Convert::toExpression(*propertyString, s); + }), *defaultValue); } }; diff --git a/include/mbgl/style/function/camera_function.hpp b/include/mbgl/style/function/camera_function.hpp index 479deb20df..65b7991849 100644 --- a/include/mbgl/style/function/camera_function.hpp +++ b/include/mbgl/style/function/camera_function.hpp @@ -1,16 +1,12 @@ #pragma once #include +#include +#include #include #include #include -#include -#include -#include -#include -#include -#include -#include +#include namespace mbgl { namespace style { @@ -18,31 +14,15 @@ namespace style { template class CameraFunction { public: - using Stops = std::conditional_t< - util::Interpolatable::value, - variant< - ExponentialStops, - IntervalStops>, - variant< - IntervalStops>>; - - CameraFunction(std::unique_ptr expression_) - : isExpression(true), + // The second parameter should be used only for conversions from legacy functions. + CameraFunction(std::unique_ptr expression_, bool isExpression_ = true) + : isExpression(isExpression_), expression(std::move(expression_)), - zoomCurve(expression::findZoomCurveChecked(expression.get())) - { + zoomCurve(expression::findZoomCurveChecked(expression.get())) { assert(!expression::isZoomConstant(*expression)); assert(expression::isFeatureConstant(*expression)); } - CameraFunction(const Stops& stops) - : isExpression(false), - expression(stops.match([&] (const auto& s) { - return expression::Convert::toExpression(s); - })), - zoomCurve(expression::findZoomCurveChecked(expression.get())) - {} - T evaluate(float zoom) const { const expression::EvaluationResult result = expression->evaluate(expression::EvaluationContext(zoom, nullptr)); if (result) { @@ -82,7 +62,7 @@ public: const expression::Expression& getExpression() const { return *expression; } private: - std::shared_ptr expression; + std::shared_ptr expression; const variant zoomCurve; }; diff --git a/include/mbgl/style/function/composite_function.hpp b/include/mbgl/style/function/composite_function.hpp index f99fc973ed..3b63bd005d 100644 --- a/include/mbgl/style/function/composite_function.hpp +++ b/include/mbgl/style/function/composite_function.hpp @@ -1,74 +1,29 @@ #pragma once #include +#include +#include #include #include #include -#include -#include -#include -#include -#include -#include -#include #include -#include - -#include -#include namespace mbgl { - -class GeometryTileFeature; - namespace style { -// A CompositeFunction consists of an outer zoom function whose stop range values are -// "inner" source functions. It provides the GL Native implementation of -// "zoom-and-property" functions from the style spec. - template class CompositeFunction { public: - using InnerStops = std::conditional_t< - util::Interpolatable::value, - variant< - ExponentialStops, - IntervalStops, - CategoricalStops>, - variant< - IntervalStops, - CategoricalStops>>; - - using Stops = std::conditional_t< - util::Interpolatable::value, - variant< - CompositeExponentialStops, - CompositeIntervalStops, - CompositeCategoricalStops>, - variant< - CompositeIntervalStops, - CompositeCategoricalStops>>; - + // The second parameter should be used only for conversions from legacy functions. CompositeFunction(std::unique_ptr expression_, optional defaultValue_ = {}) - : isExpression(true), - defaultValue(std::move(defaultValue_)), - expression(std::move(expression_)), - zoomCurve(expression::findZoomCurveChecked(expression.get())) - { + : isExpression(defaultValue_), + expression(std::move(expression_)), + defaultValue(std::move(defaultValue_)), + zoomCurve(expression::findZoomCurveChecked(expression.get())) { assert(!expression::isZoomConstant(*expression)); assert(!expression::isFeatureConstant(*expression)); } - CompositeFunction(const std::string& property, const Stops& stops, optional defaultValue_ = {}) - : isExpression(false), - defaultValue(std::move(defaultValue_)), - expression(stops.match([&] (const auto& s) { - return expression::Convert::toExpression(property, s); - })), - zoomCurve(expression::findZoomCurveChecked(expression.get())) - {} - // 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) { @@ -118,8 +73,8 @@ public: bool isExpression; private: + std::shared_ptr expression; optional defaultValue; - std::shared_ptr expression; const variant zoomCurve; }; diff --git a/include/mbgl/style/function/convert.hpp b/include/mbgl/style/function/convert.hpp index e5a5808e1d..a0dae992fa 100644 --- a/include/mbgl/style/function/convert.hpp +++ b/include/mbgl/style/function/convert.hpp @@ -306,40 +306,15 @@ struct Convert { return std::move(*zoomCurve); } - - static std::unique_ptr fromIdentityFunction(type::Type type, const std::string& property) + template + static std::unique_ptr toExpression(const std::string& property, + const IdentityStops&) { - std::unique_ptr input = type.match( - [&] (const type::StringType&) { - return makeGet(type::String, property); - }, - [&] (const type::NumberType&) { - return makeGet(type::Number, property); - }, - [&] (const type::BooleanType&) { - return makeGet(type::Boolean, property); - }, - [&] (const type::ColorType&) { - std::vector> args; - args.push_back(makeGet(type::String, property)); - return std::make_unique(type::Color, std::move(args)); - }, - [&] (const type::Array& arr) { - std::vector> getArgs; - getArgs.push_back(makeLiteral(property)); - ParsingContext ctx; - ParseResult get = createCompoundExpression("get", std::move(getArgs), ctx); - assert(get); - assert(ctx.getErrors().size() == 0); - return std::make_unique(arr, std::move(*get)); - }, - [&] (const auto&) -> std::unique_ptr { - return makeLiteral(Null); - } - ); - - return input; + return fromIdentityFunction(property, expression::valueTypeToExpressionType()); } + +private: + static std::unique_ptr fromIdentityFunction(const std::string& property, type::Type type); }; } // namespace expression diff --git a/include/mbgl/style/function/source_function.hpp b/include/mbgl/style/function/source_function.hpp index bc4e3b9c7d..a83e73a5d0 100644 --- a/include/mbgl/style/function/source_function.hpp +++ b/include/mbgl/style/function/source_function.hpp @@ -1,15 +1,8 @@ #pragma once +#include +#include #include -#include -#include -#include -#include -#include -#include -#include - -#include namespace mbgl { namespace style { @@ -17,37 +10,15 @@ namespace style { template class SourceFunction { public: - using Stops = std::conditional_t< - util::Interpolatable::value, - variant< - ExponentialStops, - IntervalStops, - CategoricalStops, - IdentityStops>, - variant< - IntervalStops, - CategoricalStops, - IdentityStops>>; - + // The second parameter should be used only for conversions from legacy functions. SourceFunction(std::unique_ptr expression_, optional defaultValue_ = {}) - : isExpression(true), - defaultValue(std::move(defaultValue_)), - expression(std::move(expression_)) - { + : isExpression(defaultValue_), + expression(std::move(expression_)), + defaultValue(std::move(defaultValue_)) { assert(expression::isZoomConstant(*expression)); assert(!expression::isFeatureConstant(*expression)); } - SourceFunction(const std::string& property, const Stops& stops, optional defaultValue_ = {}) - : isExpression(false), - defaultValue(std::move(defaultValue_)), - expression(stops.match([&] (const IdentityStops&) { - return expression::Convert::fromIdentityFunction(expression::valueTypeToExpressionType(), property); - }, [&] (const auto& s) { - return expression::Convert::toExpression(property, s); - })) - {} - template T evaluate(const Feature& feature, T finalDefaultValue) const { const expression::EvaluationResult result = expression->evaluate(expression::EvaluationContext(&feature)); @@ -73,8 +44,8 @@ public: const expression::Expression& getExpression() const { return *expression; } private: + std::shared_ptr expression; optional defaultValue; - std::shared_ptr expression; }; } // namespace style -- cgit v1.2.1