From af89318b1d3bef15e92e591887c9d65b10be54ce Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 13 Jul 2018 17:02:30 -0700 Subject: [core] Convert token strings to expressions --- .../mbgl/style/conversion/color_ramp_property_value.hpp | 2 +- .../style/conversion/data_driven_property_value.hpp | 17 ++++++++++++++--- include/mbgl/style/conversion/function.hpp | 9 ++++++--- include/mbgl/style/conversion/property_value.hpp | 8 ++++++-- include/mbgl/style/data_driven_property_value.hpp | 8 -------- include/mbgl/style/expression/dsl.hpp | 12 +++++++++++- include/mbgl/style/property_expression.hpp | 14 +++----------- 7 files changed, 41 insertions(+), 29 deletions(-) (limited to 'include') diff --git a/include/mbgl/style/conversion/color_ramp_property_value.hpp b/include/mbgl/style/conversion/color_ramp_property_value.hpp index 9394daa050..290ee6a56c 100644 --- a/include/mbgl/style/conversion/color_ramp_property_value.hpp +++ b/include/mbgl/style/conversion/color_ramp_property_value.hpp @@ -14,7 +14,7 @@ namespace conversion { template <> struct Converter { - optional operator()(const Convertible& value, Error& error) const { + optional operator()(const Convertible& value, Error& error, bool /* convertTokens */ = false) const { using namespace mbgl::style::expression; if (isUndefined(value)) { return ColorRampPropertyValue(); diff --git a/include/mbgl/style/conversion/data_driven_property_value.hpp b/include/mbgl/style/conversion/data_driven_property_value.hpp index 363134bd3e..f1bd1bdbb7 100644 --- a/include/mbgl/style/conversion/data_driven_property_value.hpp +++ b/include/mbgl/style/conversion/data_driven_property_value.hpp @@ -16,7 +16,7 @@ namespace conversion { template struct Converter> { - optional> operator()(const Convertible& value, Error& error) const { + optional> operator()(const Convertible& value, Error& error, bool convertTokens) const { using namespace mbgl::style::expression; if (isUndefined(value)) { @@ -34,13 +34,13 @@ struct Converter> { } expression = PropertyExpression(std::move(*parsed)); } else if (isObject(value)) { - expression = convertFunctionToExpression(value, error); + expression = convertFunctionToExpression(value, error, convertTokens); } else { optional constant = convert(value, error); if (!constant) { return {}; } - return DataDrivenPropertyValue(*constant); + return convertTokens ? maybeConvertTokens(*constant) : DataDrivenPropertyValue(*constant); } if (!expression) { @@ -56,6 +56,17 @@ struct Converter> { return DataDrivenPropertyValue(*constant); } } + + template + DataDrivenPropertyValue maybeConvertTokens(const S& t) const { + return DataDrivenPropertyValue(t); + }; + + DataDrivenPropertyValue maybeConvertTokens(const std::string& t) const { + return hasTokens(t) + ? DataDrivenPropertyValue(PropertyExpression(convertTokenStringToExpression(t))) + : DataDrivenPropertyValue(t); + } }; } // namespace conversion diff --git a/include/mbgl/style/conversion/function.hpp b/include/mbgl/style/conversion/function.hpp index 6bc75d7141..8799e9faa4 100644 --- a/include/mbgl/style/conversion/function.hpp +++ b/include/mbgl/style/conversion/function.hpp @@ -10,11 +10,14 @@ namespace mbgl { namespace style { namespace conversion { -optional> convertFunctionToExpression(expression::type::Type, const Convertible&, Error&); +bool hasTokens(const std::string&); +std::unique_ptr convertTokenStringToExpression(const std::string&); + +optional> convertFunctionToExpression(expression::type::Type, const Convertible&, Error&, bool convertTokens); template -optional> convertFunctionToExpression(const Convertible& value, Error& error) { - auto expression = convertFunctionToExpression(expression::valueTypeToExpressionType(), value, error); +optional> convertFunctionToExpression(const Convertible& value, Error& error, bool convertTokens) { + auto expression = convertFunctionToExpression(expression::valueTypeToExpressionType(), value, error, convertTokens); if (!expression) { return {}; } diff --git a/include/mbgl/style/conversion/property_value.hpp b/include/mbgl/style/conversion/property_value.hpp index 2256cfffb4..db23074c5e 100644 --- a/include/mbgl/style/conversion/property_value.hpp +++ b/include/mbgl/style/conversion/property_value.hpp @@ -16,9 +16,13 @@ namespace conversion { template struct Converter> { - optional> operator()(const Convertible& value, Error& error) const { + optional> operator()(const Convertible& value, Error& error, bool convertTokens = false) const { using namespace mbgl::style::expression; + // Only icon-image and text-field support tokens, and they are both data-driven. + assert(!convertTokens); + (void)convertTokens; + if (isUndefined(value)) { return PropertyValue(); } @@ -34,7 +38,7 @@ struct Converter> { } expression = PropertyExpression(std::move(*parsed)); } else if (isObject(value)) { - expression = convertFunctionToExpression(value, error); + expression = convertFunctionToExpression(value, error, false); } else { optional constant = convert(value, error); if (!constant) { diff --git a/include/mbgl/style/data_driven_property_value.hpp b/include/mbgl/style/data_driven_property_value.hpp index a95eaa4ea8..baea861f62 100644 --- a/include/mbgl/style/data_driven_property_value.hpp +++ b/include/mbgl/style/data_driven_property_value.hpp @@ -52,14 +52,6 @@ public: ); } - bool isExpression() const { - return value.match( - [] (const Undefined&) { return false; }, - [] (const T&) { return false; }, - [] (const PropertyExpression& fn) { return fn.isExpression; } - ); - } - const T & asConstant() const { return value.template get< T >(); } const PropertyExpression& asExpression() const { return value.template get>(); } diff --git a/include/mbgl/style/expression/dsl.hpp b/include/mbgl/style/expression/dsl.hpp index 22278b0975..e9de20de18 100644 --- a/include/mbgl/style/expression/dsl.hpp +++ b/include/mbgl/style/expression/dsl.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -16,6 +17,13 @@ namespace dsl { // This convenience API does little to no expression validation or type-checking, and is intended for // use only by test and other non-production code. +template +std::vector> vec(Args... args) { + std::vector> result; + util::ignore({ (result.push_back(std::move(args)), 0)... }); + return result; +} + std::unique_ptr error(std::string); std::unique_ptr literal(const char* value); @@ -27,8 +35,8 @@ std::unique_ptr number(std::unique_ptr); std::unique_ptr string(std::unique_ptr); std::unique_ptr boolean(std::unique_ptr); -std::unique_ptr toColor(const char* value); std::unique_ptr toColor(std::unique_ptr); +std::unique_ptr toString(std::unique_ptr); std::unique_ptr get(const char* value); std::unique_ptr get(std::unique_ptr); @@ -68,6 +76,8 @@ std::unique_ptr interpolate(Interpolator interpolator, double input2, std::unique_ptr output2, double input3, std::unique_ptr output3); +std::unique_ptr concat(std::vector> inputs); + } // namespace dsl } // namespace expression } // namespace style diff --git a/include/mbgl/style/property_expression.hpp b/include/mbgl/style/property_expression.hpp index 16f154cdf2..b198de02b2 100644 --- a/include/mbgl/style/property_expression.hpp +++ b/include/mbgl/style/property_expression.hpp @@ -14,16 +14,9 @@ namespace style { template class PropertyExpression { public: - PropertyExpression(std::unique_ptr expression_) - : isExpression(true), - expression(std::move(expression_)), - zoomCurve(expression::findZoomCurveChecked(expression.get())) { - } - - // To be used only for conversions from legacy functions. - PropertyExpression(std::unique_ptr expression_, optional defaultValue_) - : isExpression(false), - expression(std::move(expression_)), + // Second parameter to be used only for conversions from legacy functions. + PropertyExpression(std::unique_ptr expression_, optional defaultValue_ = {}) + : expression(std::move(expression_)), defaultValue(std::move(defaultValue_)), zoomCurve(expression::findZoomCurveChecked(expression.get())) { } @@ -108,7 +101,6 @@ public: const expression::Expression& getExpression() const { return *expression; } bool useIntegerZoom = false; - bool isExpression; friend bool operator==(const PropertyExpression& lhs, const PropertyExpression& rhs) { -- cgit v1.2.1