summaryrefslogtreecommitdiff
path: root/include/mbgl/style
diff options
context:
space:
mode:
Diffstat (limited to 'include/mbgl/style')
-rw-r--r--include/mbgl/style/conversion/data_driven_property_value.hpp33
-rw-r--r--include/mbgl/style/conversion/expression.hpp39
-rw-r--r--include/mbgl/style/conversion/heatmap_color_property_value.hpp7
-rw-r--r--include/mbgl/style/conversion/property_value.hpp9
-rw-r--r--include/mbgl/style/data_driven_property_value.hpp9
-rw-r--r--include/mbgl/style/expression/length.hpp32
-rw-r--r--include/mbgl/style/expression/let.hpp3
-rw-r--r--include/mbgl/style/expression/literal.hpp4
-rw-r--r--include/mbgl/style/expression/parsing_context.hpp34
-rw-r--r--include/mbgl/style/function/camera_function.hpp11
-rw-r--r--include/mbgl/style/function/composite_function.hpp13
-rw-r--r--include/mbgl/style/function/source_function.hpp15
-rw-r--r--include/mbgl/style/layers/custom_layer.hpp89
13 files changed, 168 insertions, 130 deletions
diff --git a/include/mbgl/style/conversion/data_driven_property_value.hpp b/include/mbgl/style/conversion/data_driven_property_value.hpp
index 8880d28fb1..07ed201c99 100644
--- a/include/mbgl/style/conversion/data_driven_property_value.hpp
+++ b/include/mbgl/style/conversion/data_driven_property_value.hpp
@@ -4,10 +4,12 @@
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/function.hpp>
-#include <mbgl/style/conversion/expression.hpp>
#include <mbgl/style/expression/is_expression.hpp>
#include <mbgl/style/expression/is_constant.hpp>
#include <mbgl/style/expression/find_zoom_curve.hpp>
+#include <mbgl/style/expression/literal.hpp>
+#include <mbgl/style/expression/value.hpp>
+#include <mbgl/style/expression/parsing_context.hpp>
#include <unordered_set>
@@ -20,24 +22,35 @@ template <class T>
struct Converter<DataDrivenPropertyValue<T>> {
optional<DataDrivenPropertyValue<T>> operator()(const Convertible& value, Error& error) const {
+ using namespace mbgl::style::expression;
+
if (isUndefined(value)) {
return DataDrivenPropertyValue<T>();
- } else if (expression::isExpression(value)) {
- optional<std::unique_ptr<Expression>> expression = convert<std::unique_ptr<Expression>>(
- value,
- error,
- valueTypeToExpressionType<T>());
-
+ } else if (isExpression(value)) {
+ ParsingContext ctx(valueTypeToExpressionType<T>());
+ ParseResult expression = ctx.parseLayerPropertyExpression(value);
if (!expression) {
+ error = { ctx.getCombinedErrors() };
return {};
}
- if (isFeatureConstant(**expression)) {
+ bool featureConstant = isFeatureConstant(**expression);
+ bool zoomConstant = isZoomConstant(**expression);
+
+ if (featureConstant && !zoomConstant) {
return DataDrivenPropertyValue<T>(CameraFunction<T>(std::move(*expression)));
- } else if (isZoomConstant(**expression)) {
+ } else if (!featureConstant && zoomConstant) {
return DataDrivenPropertyValue<T>(SourceFunction<T>(std::move(*expression)));
- } else {
+ } else if (!featureConstant && !zoomConstant) {
return DataDrivenPropertyValue<T>(CompositeFunction<T>(std::move(*expression)));
+ } else {
+ auto literal = dynamic_cast<Literal*>(expression->get());
+ assert(literal);
+ optional<T> constant = fromExpressionValue<T>(literal->getValue());
+ if (!constant) {
+ return {};
+ }
+ return DataDrivenPropertyValue<T>(*constant);
}
} else if (!isObject(value)) {
optional<T> constant = convert<T>(value, error);
diff --git a/include/mbgl/style/conversion/expression.hpp b/include/mbgl/style/conversion/expression.hpp
deleted file mode 100644
index c5fcf906a7..0000000000
--- a/include/mbgl/style/conversion/expression.hpp
+++ /dev/null
@@ -1,39 +0,0 @@
-#pragma once
-
-#include <mbgl/style/expression/parsing_context.hpp>
-#include <mbgl/style/expression/type.hpp>
-#include <mbgl/style/conversion.hpp>
-
-#include <memory>
-
-namespace mbgl {
-namespace style {
-namespace conversion {
-
-using namespace mbgl::style::expression;
-
-template<> struct Converter<std::unique_ptr<Expression>> {
- optional<std::unique_ptr<Expression>> operator()(const Convertible& value, Error& error, type::Type expected) const {
- ParsingContext ctx(optional<type::Type> {expected});
- ParseResult parsed = ctx.parse(value);
- if (parsed) {
- return std::move(*parsed);
- }
- std::string combinedError;
- for (const ParsingError& parsingError : ctx.getErrors()) {
- if (combinedError.size() > 0) {
- combinedError += "\n";
- }
- if (parsingError.key.size() > 0) {
- combinedError += parsingError.key + ": ";
- }
- combinedError += parsingError.message;
- }
- error = { combinedError };
- return {};
- };
-};
-
-} // namespace conversion
-} // namespace style
-} // namespace mbgl
diff --git a/include/mbgl/style/conversion/heatmap_color_property_value.hpp b/include/mbgl/style/conversion/heatmap_color_property_value.hpp
index e3689c524c..a4710792d2 100644
--- a/include/mbgl/style/conversion/heatmap_color_property_value.hpp
+++ b/include/mbgl/style/conversion/heatmap_color_property_value.hpp
@@ -4,11 +4,11 @@
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/function.hpp>
-#include <mbgl/style/conversion/expression.hpp>
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/is_constant.hpp>
#include <mbgl/style/expression/is_expression.hpp>
#include <mbgl/style/expression/find_zoom_curve.hpp>
+#include <mbgl/style/expression/parsing_context.hpp>
namespace mbgl {
namespace style {
@@ -17,11 +17,14 @@ namespace conversion {
template <>
struct Converter<HeatmapColorPropertyValue> {
optional<HeatmapColorPropertyValue> operator()(const Convertible& value, Error& error) const {
+ using namespace mbgl::style::expression;
if (isUndefined(value)) {
return HeatmapColorPropertyValue();
} else if (isExpression(value)) {
- optional<std::unique_ptr<Expression>> expression = convert<std::unique_ptr<Expression>>(value, error, expression::type::Color);
+ ParsingContext ctx(type::Color);
+ ParseResult expression = ctx.parseLayerPropertyExpression(value);
if (!expression) {
+ error = { ctx.getCombinedErrors() };
return {};
}
assert(*expression);
diff --git a/include/mbgl/style/conversion/property_value.hpp b/include/mbgl/style/conversion/property_value.hpp
index 97117de2ec..3130661f61 100644
--- a/include/mbgl/style/conversion/property_value.hpp
+++ b/include/mbgl/style/conversion/property_value.hpp
@@ -4,11 +4,11 @@
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/function.hpp>
-#include <mbgl/style/conversion/expression.hpp>
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/is_constant.hpp>
#include <mbgl/style/expression/is_expression.hpp>
#include <mbgl/style/expression/find_zoom_curve.hpp>
+#include <mbgl/style/expression/parsing_context.hpp>
namespace mbgl {
namespace style {
@@ -17,13 +17,18 @@ namespace conversion {
template <class T>
struct Converter<PropertyValue<T>> {
optional<PropertyValue<T>> operator()(const Convertible& value, Error& error) const {
+ using namespace mbgl::style::expression;
+
if (isUndefined(value)) {
return PropertyValue<T>();
} else if (isExpression(value)) {
- optional<std::unique_ptr<Expression>> expression = convert<std::unique_ptr<Expression>>(value, error, valueTypeToExpressionType<T>());
+ ParsingContext ctx(valueTypeToExpressionType<T>());
+ ParseResult expression = ctx.parseLayerPropertyExpression(value);
if (!expression) {
+ error = { ctx.getCombinedErrors() };
return {};
}
+
if (isFeatureConstant(**expression)) {
return { CameraFunction<T>(std::move(*expression)) };
} else {
diff --git a/include/mbgl/style/data_driven_property_value.hpp b/include/mbgl/style/data_driven_property_value.hpp
index 5d7c596363..0a1fce29c7 100644
--- a/include/mbgl/style/data_driven_property_value.hpp
+++ b/include/mbgl/style/data_driven_property_value.hpp
@@ -50,6 +50,15 @@ public:
return !value.template is<CameraFunction<T>>() && !value.template is<CompositeFunction<T>>();
}
+ bool isExpression() const {
+ return value.match(
+ [] (const Undefined&) { return false; },
+ [] (const T&) { return false; },
+ [] (const CameraFunction<T>& fn) { return fn.isExpression; },
+ [] (const SourceFunction<T>& fn) { return fn.isExpression; },
+ [] (const CompositeFunction<T>& fn) { return fn.isExpression; });
+ }
+
template <class... Ts>
auto match(Ts&&... ts) const {
return value.match(std::forward<Ts>(ts)...);
diff --git a/include/mbgl/style/expression/length.hpp b/include/mbgl/style/expression/length.hpp
new file mode 100644
index 0000000000..1d754f1932
--- /dev/null
+++ b/include/mbgl/style/expression/length.hpp
@@ -0,0 +1,32 @@
+#pragma once
+
+#include <mbgl/style/expression/expression.hpp>
+#include <mbgl/style/conversion.hpp>
+#include <mbgl/style/expression/parsing_context.hpp>
+
+#include <memory>
+#include <vector>
+
+namespace mbgl {
+namespace style {
+namespace expression {
+
+class Length : public Expression {
+public:
+ Length(std::unique_ptr<Expression> input);
+
+ static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx);
+
+ EvaluationResult evaluate(const EvaluationContext& params) const override;
+ void eachChild(const std::function<void(const Expression&)>& visit) const override;
+ bool operator==(const Expression& e) const override;
+ std::vector<optional<Value>> possibleOutputs() const override;
+ std::string getOperator() const override { return "length"; }
+
+private:
+ std::unique_ptr<Expression> input;
+};
+
+} // namespace expression
+} // namespace style
+} // namespace mbgl
diff --git a/include/mbgl/style/expression/let.hpp b/include/mbgl/style/expression/let.hpp
index 75d2adda62..d0210d8bba 100644
--- a/include/mbgl/style/expression/let.hpp
+++ b/include/mbgl/style/expression/let.hpp
@@ -70,6 +70,9 @@ public:
mbgl::Value serialize() const override;
std::string getOperator() const override { return "var"; }
+
+ const std::shared_ptr<Expression>& getBoundExpression() const { return value; }
+
private:
std::string name;
std::shared_ptr<Expression> value;
diff --git a/include/mbgl/style/expression/literal.hpp b/include/mbgl/style/expression/literal.hpp
index d854b419f4..a00c468efc 100644
--- a/include/mbgl/style/expression/literal.hpp
+++ b/include/mbgl/style/expression/literal.hpp
@@ -40,6 +40,10 @@ public:
std::vector<optional<Value>> possibleOutputs() const override {
return {{ value }};
}
+
+ Value getValue() const {
+ return value;
+ }
mbgl::Value serialize() const override;
std::string getOperator() const override { return "literal"; }
diff --git a/include/mbgl/style/expression/parsing_context.hpp b/include/mbgl/style/expression/parsing_context.hpp
index f92a4c95ea..c19974a4f7 100644
--- a/include/mbgl/style/expression/parsing_context.hpp
+++ b/include/mbgl/style/expression/parsing_context.hpp
@@ -55,7 +55,7 @@ class ParsingContext {
public:
ParsingContext() : errors(std::make_shared<std::vector<ParsingError>>()) {}
ParsingContext(std::string key_) : key(std::move(key_)), errors(std::make_shared<std::vector<ParsingError>>()) {}
- explicit ParsingContext(optional<type::Type> expected_)
+ explicit ParsingContext(type::Type expected_)
: expected(std::move(expected_)),
errors(std::make_shared<std::vector<ParsingError>>())
{}
@@ -67,6 +67,7 @@ public:
std::string getKey() const { return key; }
optional<type::Type> getExpected() const { return expected; }
const std::vector<ParsingError>& getErrors() const { return *errors; }
+ const std::string getCombinedErrors() const;
enum TypeAnnotationOption {
includeTypeAnnotations,
@@ -74,16 +75,21 @@ public:
};
/*
- Parse the given style-spec JSON value into an Expression object.
- Specifically, this function is responsible for determining the expression
- type (either Literal, or the one named in value[0]) and dispatching to the
- appropriate ParseXxxx::parse(const V&, ParsingContext) method.
+ Parse the given style-spec JSON value as an expression.
*/
- ParseResult parse(const mbgl::style::conversion::Convertible& value,
- TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations);
+ ParseResult parseExpression(const mbgl::style::conversion::Convertible& value,
+ TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations);
/*
- Parse a child expression.
+ Parse the given style-spec JSON value as an expression intended to be used
+ in a layout or paint property. This entails checking additional constraints
+ that exist in that context but not, e.g., for filters.
+ */
+ ParseResult parseLayerPropertyExpression(const mbgl::style::conversion::Convertible& value,
+ TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations);
+
+ /*
+ Parse a child expression. For use by individual Expression::parse() methods.
*/
ParseResult parse(const mbgl::style::conversion::Convertible&,
std::size_t,
@@ -91,7 +97,7 @@ public:
TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations);
/*
- Parse a child expression.
+ Parse a child expression. For use by individual Expression::parse() methods.
*/
ParseResult parse(const mbgl::style::conversion::Convertible&,
std::size_t index,
@@ -141,6 +147,16 @@ private:
errors(std::move(errors_))
{}
+
+ /*
+ Parse the given style-spec JSON value into an Expression object.
+ Specifically, this function is responsible for determining the expression
+ type (either Literal, or the one named in value[0]) and dispatching to the
+ appropriate ParseXxxx::parse(const V&, ParsingContext) method.
+ */
+ ParseResult parse(const mbgl::style::conversion::Convertible& value,
+ TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations);
+
std::string key;
optional<type::Type> expected;
std::shared_ptr<detail::Scope> scope;
diff --git a/include/mbgl/style/function/camera_function.hpp b/include/mbgl/style/function/camera_function.hpp
index 1da5d2c601..97ba633e44 100644
--- a/include/mbgl/style/function/camera_function.hpp
+++ b/include/mbgl/style/function/camera_function.hpp
@@ -27,15 +27,16 @@ public:
IntervalStops<T>>>;
CameraFunction(std::unique_ptr<expression::Expression> expression_)
- : expression(std::move(expression_)),
+ : isExpression(true),
+ expression(std::move(expression_)),
zoomCurve(expression::findZoomCurveChecked(expression.get()))
{
assert(!expression::isZoomConstant(*expression));
assert(expression::isFeatureConstant(*expression));
}
- CameraFunction(Stops stops_)
- : stops(std::move(stops_)),
+ CameraFunction(const Stops& stops)
+ : isExpression(false),
expression(stops.match([&] (const auto& s) {
return expression::Convert::toExpression(s);
})),
@@ -76,12 +77,10 @@ public:
}
bool useIntegerZoom = false;
+ bool isExpression;
const expression::Expression& getExpression() const { return *expression; }
- // retained for compatibility with pre-expression function API
- Stops stops;
-
private:
std::shared_ptr<expression::Expression> expression;
const variant<const expression::InterpolateBase*, const expression::Step*> zoomCurve;
diff --git a/include/mbgl/style/function/composite_function.hpp b/include/mbgl/style/function/composite_function.hpp
index f391b101ae..614c345c25 100644
--- a/include/mbgl/style/function/composite_function.hpp
+++ b/include/mbgl/style/function/composite_function.hpp
@@ -51,16 +51,16 @@ public:
CompositeCategoricalStops<T>>>;
CompositeFunction(std::unique_ptr<expression::Expression> expression_)
- : expression(std::move(expression_)),
+ : isExpression(true),
+ expression(std::move(expression_)),
zoomCurve(expression::findZoomCurveChecked(expression.get()))
{
assert(!expression::isZoomConstant(*expression));
assert(!expression::isFeatureConstant(*expression));
}
- CompositeFunction(std::string property_, Stops stops_, optional<T> defaultValue_ = {})
- : property(std::move(property_)),
- stops(std::move(stops_)),
+ CompositeFunction(const std::string& property, const Stops& stops, optional<T> defaultValue_ = {})
+ : isExpression(false),
defaultValue(std::move(defaultValue_)),
expression(stops.match([&] (const auto& s) {
return expression::Convert::toExpression(property, s);
@@ -113,12 +113,11 @@ public:
const expression::Expression& getExpression() const { return *expression; }
- std::string property;
- Stops stops;
- optional<T> defaultValue;
bool useIntegerZoom = false;
+ bool isExpression;
private:
+ optional<T> defaultValue;
std::shared_ptr<expression::Expression> expression;
const variant<const expression::InterpolateBase*, const expression::Step*> zoomCurve;
};
diff --git a/include/mbgl/style/function/source_function.hpp b/include/mbgl/style/function/source_function.hpp
index d3caa90ee5..5b51d0bf81 100644
--- a/include/mbgl/style/function/source_function.hpp
+++ b/include/mbgl/style/function/source_function.hpp
@@ -30,15 +30,15 @@ public:
IdentityStops<T>>>;
SourceFunction(std::unique_ptr<expression::Expression> expression_)
- : expression(std::move(expression_))
+ : isExpression(true),
+ expression(std::move(expression_))
{
assert(expression::isZoomConstant(*expression));
assert(!expression::isFeatureConstant(*expression));
}
- SourceFunction(std::string property_, Stops stops_, optional<T> defaultValue_ = {})
- : property(std::move(property_)),
- stops(std::move(stops_)),
+ SourceFunction(const std::string& property, const Stops& stops, optional<T> defaultValue_ = {})
+ : isExpression(false),
defaultValue(std::move(defaultValue_)),
expression(stops.match([&] (const IdentityStops<T>&) {
return expression::Convert::fromIdentityFunction(expression::valueTypeToExpressionType<T>(), property);
@@ -67,15 +67,12 @@ public:
}
bool useIntegerZoom = false;
+ bool isExpression;
const expression::Expression& getExpression() const { return *expression; }
- // retained for compatibility with pre-expression function API
- std::string property;
- Stops stops;
- optional<T> defaultValue;
-
private:
+ optional<T> defaultValue;
std::shared_ptr<expression::Expression> expression;
};
diff --git a/include/mbgl/style/layers/custom_layer.hpp b/include/mbgl/style/layers/custom_layer.hpp
index bf3387f95b..fbe3a4a6c2 100644
--- a/include/mbgl/style/layers/custom_layer.hpp
+++ b/include/mbgl/style/layers/custom_layer.hpp
@@ -2,20 +2,13 @@
#include <mbgl/style/layer.hpp>
+#include <array>
+
namespace mbgl {
namespace style {
/**
- * Initialize any GL state needed by the custom layer. This method is called once, from the
- * main thread, at a point when the GL context is active but before rendering for the first
- * time.
- *
- * Resources that are acquired in this method must be released in the UninitializeFunction.
- */
-using CustomLayerInitializeFunction = void (*)(void* context);
-
-/**
- * Parameters that define the current camera position for a CustomLayerRenderFunction.
+ * Parameters that define the current camera position for a `CustomLayerHost::render()` function.
*/
struct CustomLayerRenderParameters {
double width;
@@ -26,48 +19,52 @@ struct CustomLayerRenderParameters {
double bearing;
double pitch;
double fieldOfView;
+ std::array<double, 16> projectionMatrix;
};
-/**
- * Render the layer. This method is called once per frame. The implementation should not make
- * any assumptions about the GL state (other than that the correct context is active). It may
- * make changes to the state, and is not required to reset values such as the depth mask, stencil
- * mask, and corresponding test flags to their original values.
- * Make sure that you are drawing your fragments with a z value of 1 to take advantage of the
- * opaque fragment culling in case there are opaque layers above your custom layer.
- */
-using CustomLayerRenderFunction = void (*)(void* context, const CustomLayerRenderParameters&);
-
-/**
- * Called when the system has destroyed the underlying GL context. The
- * `CustomLayerDeinitializeFunction` will not be called in this case, however
- * `CustomLayerInitializeFunction` will be called instead to prepare for a new render.
- *
- */
-using CustomLayerContextLostFunction = void (*)(void* context);
-
-/**
- * Destroy any GL state needed by the custom layer, and deallocate context, if necessary. This
- * method is called once, from the main thread, at a point when the GL context is active.
- *
- * Note that it may be called even when the InitializeFunction has not been called.
- */
-using CustomLayerDeinitializeFunction = void (*)(void* context);
+class CustomLayerHost {
+public:
+ virtual ~CustomLayerHost() = default;
+ /**
+ * Initialize any GL state needed by the custom layer. This method is called once, from the
+ * main thread, at a point when the GL context is active but before rendering for the first
+ * time.
+ *
+ * Resources that are acquired in this method must be released in the `deinitialize` function.
+ */
+ virtual void initialize() = 0;
+
+ /**
+ * Render the layer. This method is called once per frame. The implementation should not make
+ * any assumptions about the GL state (other than that the correct context is active). It may
+ * make changes to the state, and is not required to reset values such as the depth mask, stencil
+ * mask, and corresponding test flags to their original values.
+ * Make sure that you are drawing your fragments with a z value of 1 to take advantage of the
+ * opaque fragment culling in case there are opaque layers above your custom layer.
+ */
+ virtual void render(const CustomLayerRenderParameters&) = 0;
+
+ /**
+ * Called when the system has destroyed the underlying GL context. The
+ * `deinitialize` function will not be called in this case, however
+ * `initialize` will be called instead to prepare for a new render.
+ *
+ */
+ virtual void contextLost() = 0;
+
+ /**
+ * Destroy any GL state needed by the custom layer, and deallocate context, if necessary. This
+ * method is called once, from the main thread, at a point when the GL context is active.
+ *
+ * Note that it may be called even when the `initialize` function has not been called.
+ */
+ virtual void deinitialize() = 0;
+};
class CustomLayer : public Layer {
public:
CustomLayer(const std::string& id,
- CustomLayerInitializeFunction,
- CustomLayerRenderFunction,
- CustomLayerContextLostFunction,
- CustomLayerDeinitializeFunction,
- void* context);
-
- CustomLayer(const std::string& id,
- CustomLayerInitializeFunction,
- CustomLayerRenderFunction,
- CustomLayerDeinitializeFunction,
- void* context);
+ std::unique_ptr<CustomLayerHost> host);
~CustomLayer() final;