From 598c7a18d342bc05149b58d53819fd0b28881938 Mon Sep 17 00:00:00 2001 From: Anand Thakker Date: Sat, 12 Aug 2017 22:44:27 -0400 Subject: wip - attempt to hook up expression parsing --- .../style/conversion/data_driven_property_value.hpp | 14 ++++++++++++++ include/mbgl/style/conversion/expression.hpp | 19 ++++++++++++++----- include/mbgl/style/function/camera_function.hpp | 7 +++++++ include/mbgl/style/function/composite_function.hpp | 12 ++++++++++++ include/mbgl/style/function/convert.hpp | 1 + include/mbgl/style/function/source_function.hpp | 7 +++++++ 6 files changed, 55 insertions(+), 5 deletions(-) diff --git a/include/mbgl/style/conversion/data_driven_property_value.hpp b/include/mbgl/style/conversion/data_driven_property_value.hpp index 79b15dcfb0..ed8478a108 100644 --- a/include/mbgl/style/conversion/data_driven_property_value.hpp +++ b/include/mbgl/style/conversion/data_driven_property_value.hpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include namespace mbgl { namespace style { @@ -21,6 +23,18 @@ struct Converter> { return {}; } return DataDrivenPropertyValue(*constant); + } else if (objectMember(value, "expression")) { + optional> expression = convert>(*objectMember(value, "expression"), error, valueTypeToExpressionType()); + if (!expression) { + return {}; + } + if ((*expression)->isFeatureConstant()) { + return DataDrivenPropertyValue(CameraFunction(std::move(*expression))); + } else if ((*expression)->isZoomConstant()) { + return DataDrivenPropertyValue(SourceFunction(std::move(*expression))); + } else { + return DataDrivenPropertyValue(CompositeFunction(std::move(*expression))); + } } else if (!objectMember(value, "property")) { optional> function = convert>(value, error); if (!function) { diff --git a/include/mbgl/style/conversion/expression.hpp b/include/mbgl/style/conversion/expression.hpp index bce7c91396..601916250d 100644 --- a/include/mbgl/style/conversion/expression.hpp +++ b/include/mbgl/style/conversion/expression.hpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace mbgl { @@ -12,12 +13,20 @@ using namespace mbgl::style::expression; template<> struct Converter> { template - optional> operator()(const V& value, Error& error) const { - auto parsed = parseExpression(value, ParsingContext()); - if (parsed.template is>()) { - return std::move(parsed.template get>()); + optional> operator()(const V& value, Error& error, type::Type expected) const { + std::vector errors; + auto parsed = parseExpression(value, ParsingContext(errors, expected)); + if (parsed) { + return std::move(*parsed); } - error = { parsed.template get().message }; + std::string combinedError; + for (const ParsingError& parsingError : errors) { + if (combinedError.size() > 0) { + combinedError += "\n"; + } + combinedError += parsingError.key + ": " + parsingError.message; + } + error = { combinedError }; return {}; }; }; diff --git a/include/mbgl/style/function/camera_function.hpp b/include/mbgl/style/function/camera_function.hpp index 5a0f5ffe4a..55fd5cd7cc 100644 --- a/include/mbgl/style/function/camera_function.hpp +++ b/include/mbgl/style/function/camera_function.hpp @@ -19,6 +19,13 @@ public: IntervalStops>, variant< IntervalStops>>; + + CameraFunction(std::unique_ptr expression_) + : expression(std::move(expression_)) + { + assert(!expression->isZoomConstant()); + assert(expression->isFeatureConstant()); + } CameraFunction(Stops stops_) : stops(std::move(stops_)), diff --git a/include/mbgl/style/function/composite_function.hpp b/include/mbgl/style/function/composite_function.hpp index f7cbab1b21..7d77d5e552 100644 --- a/include/mbgl/style/function/composite_function.hpp +++ b/include/mbgl/style/function/composite_function.hpp @@ -50,6 +50,18 @@ public: using Interpolator = expression::ExponentialInterpolator; using Curve = expression::Curve; + CompositeFunction(std::unique_ptr expression_) + : expression(std::move(expression_)), + interpolator([&]() -> Interpolator { + optional zoomCurve = findZoomCurve(expression.get()); + assert(zoomCurve); + return (*zoomCurve)->getInterpolator(); + }()) + { + assert(!expression->isZoomConstant()); + assert(!expression->isFeatureConstant()); + } + CompositeFunction(std::string property_, Stops stops_, optional defaultValue_ = {}) : property(std::move(property_)), stops(std::move(stops_)), diff --git a/include/mbgl/style/function/convert.hpp b/include/mbgl/style/function/convert.hpp index 67624625d4..ca021eadf6 100644 --- a/include/mbgl/style/function/convert.hpp +++ b/include/mbgl/style/function/convert.hpp @@ -1,5 +1,6 @@ #pragma once + #include #include #include diff --git a/include/mbgl/style/function/source_function.hpp b/include/mbgl/style/function/source_function.hpp index 0ae8357d7b..d65d03857a 100644 --- a/include/mbgl/style/function/source_function.hpp +++ b/include/mbgl/style/function/source_function.hpp @@ -29,6 +29,13 @@ public: CategoricalStops, IdentityStops>>; + SourceFunction(std::unique_ptr expression_) + : expression(std::move(expression_)) + { + assert(expression->isZoomConstant()); + assert(!expression->isFeatureConstant()); + } + SourceFunction(std::string property_, Stops stops_, optional defaultValue_ = {}) : property(std::move(property_)), stops(std::move(stops_)), -- cgit v1.2.1