summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2018-07-13 17:02:30 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2018-07-20 12:35:00 -0700
commitaf89318b1d3bef15e92e591887c9d65b10be54ce (patch)
treed3ccb07da91bb56197607f5319100e64f7493211 /include
parenta3d988ab8520ea12272bb80a746a2d91cbc332f5 (diff)
downloadqtlocation-mapboxgl-af89318b1d3bef15e92e591887c9d65b10be54ce.tar.gz
[core] Convert token strings to expressions
Diffstat (limited to 'include')
-rw-r--r--include/mbgl/style/conversion/color_ramp_property_value.hpp2
-rw-r--r--include/mbgl/style/conversion/data_driven_property_value.hpp17
-rw-r--r--include/mbgl/style/conversion/function.hpp9
-rw-r--r--include/mbgl/style/conversion/property_value.hpp8
-rw-r--r--include/mbgl/style/data_driven_property_value.hpp8
-rw-r--r--include/mbgl/style/expression/dsl.hpp12
-rw-r--r--include/mbgl/style/property_expression.hpp14
7 files changed, 41 insertions, 29 deletions
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<ColorRampPropertyValue> {
- optional<ColorRampPropertyValue> operator()(const Convertible& value, Error& error) const {
+ optional<ColorRampPropertyValue> 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 <class T>
struct Converter<DataDrivenPropertyValue<T>> {
- optional<DataDrivenPropertyValue<T>> operator()(const Convertible& value, Error& error) const {
+ optional<DataDrivenPropertyValue<T>> operator()(const Convertible& value, Error& error, bool convertTokens) const {
using namespace mbgl::style::expression;
if (isUndefined(value)) {
@@ -34,13 +34,13 @@ struct Converter<DataDrivenPropertyValue<T>> {
}
expression = PropertyExpression<T>(std::move(*parsed));
} else if (isObject(value)) {
- expression = convertFunctionToExpression<T>(value, error);
+ expression = convertFunctionToExpression<T>(value, error, convertTokens);
} else {
optional<T> constant = convert<T>(value, error);
if (!constant) {
return {};
}
- return DataDrivenPropertyValue<T>(*constant);
+ return convertTokens ? maybeConvertTokens(*constant) : DataDrivenPropertyValue<T>(*constant);
}
if (!expression) {
@@ -56,6 +56,17 @@ struct Converter<DataDrivenPropertyValue<T>> {
return DataDrivenPropertyValue<T>(*constant);
}
}
+
+ template <class S>
+ DataDrivenPropertyValue<T> maybeConvertTokens(const S& t) const {
+ return DataDrivenPropertyValue<T>(t);
+ };
+
+ DataDrivenPropertyValue<T> maybeConvertTokens(const std::string& t) const {
+ return hasTokens(t)
+ ? DataDrivenPropertyValue<T>(PropertyExpression<T>(convertTokenStringToExpression(t)))
+ : DataDrivenPropertyValue<T>(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<std::unique_ptr<expression::Expression>> convertFunctionToExpression(expression::type::Type, const Convertible&, Error&);
+bool hasTokens(const std::string&);
+std::unique_ptr<expression::Expression> convertTokenStringToExpression(const std::string&);
+
+optional<std::unique_ptr<expression::Expression>> convertFunctionToExpression(expression::type::Type, const Convertible&, Error&, bool convertTokens);
template <class T>
-optional<PropertyExpression<T>> convertFunctionToExpression(const Convertible& value, Error& error) {
- auto expression = convertFunctionToExpression(expression::valueTypeToExpressionType<T>(), value, error);
+optional<PropertyExpression<T>> convertFunctionToExpression(const Convertible& value, Error& error, bool convertTokens) {
+ auto expression = convertFunctionToExpression(expression::valueTypeToExpressionType<T>(), 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 <class T>
struct Converter<PropertyValue<T>> {
- optional<PropertyValue<T>> operator()(const Convertible& value, Error& error) const {
+ optional<PropertyValue<T>> 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<T>();
}
@@ -34,7 +38,7 @@ struct Converter<PropertyValue<T>> {
}
expression = PropertyExpression<T>(std::move(*parsed));
} else if (isObject(value)) {
- expression = convertFunctionToExpression<T>(value, error);
+ expression = convertFunctionToExpression<T>(value, error, false);
} else {
optional<T> constant = convert<T>(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<T>& fn) { return fn.isExpression; }
- );
- }
-
const T & asConstant() const { return value.template get< T >(); }
const PropertyExpression<T>& asExpression() const { return value.template get<PropertyExpression<T>>(); }
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 <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/interpolator.hpp>
+#include <mbgl/util/ignore.hpp>
#include <memory>
#include <string>
@@ -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 <class... Args>
+std::vector<std::unique_ptr<Expression>> vec(Args... args) {
+ std::vector<std::unique_ptr<Expression>> result;
+ util::ignore({ (result.push_back(std::move(args)), 0)... });
+ return result;
+}
+
std::unique_ptr<Expression> error(std::string);
std::unique_ptr<Expression> literal(const char* value);
@@ -27,8 +35,8 @@ std::unique_ptr<Expression> number(std::unique_ptr<Expression>);
std::unique_ptr<Expression> string(std::unique_ptr<Expression>);
std::unique_ptr<Expression> boolean(std::unique_ptr<Expression>);
-std::unique_ptr<Expression> toColor(const char* value);
std::unique_ptr<Expression> toColor(std::unique_ptr<Expression>);
+std::unique_ptr<Expression> toString(std::unique_ptr<Expression>);
std::unique_ptr<Expression> get(const char* value);
std::unique_ptr<Expression> get(std::unique_ptr<Expression>);
@@ -68,6 +76,8 @@ std::unique_ptr<Expression> interpolate(Interpolator interpolator,
double input2, std::unique_ptr<Expression> output2,
double input3, std::unique_ptr<Expression> output3);
+std::unique_ptr<Expression> concat(std::vector<std::unique_ptr<Expression>> 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 T>
class PropertyExpression {
public:
- PropertyExpression(std::unique_ptr<expression::Expression> 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::Expression> expression_, optional<T> defaultValue_)
- : isExpression(false),
- expression(std::move(expression_)),
+ // Second parameter to be used only for conversions from legacy functions.
+ PropertyExpression(std::unique_ptr<expression::Expression> expression_, optional<T> 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) {