diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2018-01-08 13:18:40 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2018-01-09 16:10:51 -0800 |
commit | a13027c1b6e2aa9a213628acf9124cd6c872f204 (patch) | |
tree | c0c86f052025c3637c9af0922551492f4398860b | |
parent | 43a9bd3fbab666f21583ff264d6b6ba1d7069374 (diff) | |
download | qtlocation-mapboxgl-a13027c1b6e2aa9a213628acf9124cd6c872f204.tar.gz |
[core] Improve typing for !=, == expressions
-rw-r--r-- | cmake/core-files.cmake | 2 | ||||
-rw-r--r-- | include/mbgl/style/expression/equals.hpp | 31 | ||||
-rw-r--r-- | platform/node/test/ignores.json | 14 | ||||
-rw-r--r-- | src/mbgl/style/expression/compound_expression.cpp | 16 | ||||
-rw-r--r-- | src/mbgl/style/expression/equals.cpp | 83 | ||||
-rw-r--r-- | src/mbgl/style/expression/parsing_context.cpp | 3 |
6 files changed, 119 insertions, 30 deletions
diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index cfe543e102..8866932fc4 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -419,6 +419,7 @@ set(MBGL_CORE_FILES include/mbgl/style/expression/coalesce.hpp include/mbgl/style/expression/coercion.hpp include/mbgl/style/expression/compound_expression.hpp + include/mbgl/style/expression/equals.hpp include/mbgl/style/expression/expression.hpp include/mbgl/style/expression/find_zoom_curve.hpp include/mbgl/style/expression/get_covering_stops.hpp @@ -441,6 +442,7 @@ set(MBGL_CORE_FILES src/mbgl/style/expression/coalesce.cpp src/mbgl/style/expression/coercion.cpp src/mbgl/style/expression/compound_expression.cpp + src/mbgl/style/expression/equals.cpp src/mbgl/style/expression/find_zoom_curve.cpp src/mbgl/style/expression/get_covering_stops.cpp src/mbgl/style/expression/interpolate.cpp diff --git a/include/mbgl/style/expression/equals.hpp b/include/mbgl/style/expression/equals.hpp new file mode 100644 index 0000000000..3c0c024294 --- /dev/null +++ b/include/mbgl/style/expression/equals.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include <mbgl/style/expression/expression.hpp> +#include <mbgl/style/expression/parsing_context.hpp> +#include <mbgl/style/conversion.hpp> + +#include <memory> + +namespace mbgl { +namespace style { +namespace expression { + +class Equals : public Expression { +public: + Equals(std::unique_ptr<Expression> lhs, std::unique_ptr<Expression> rhs, bool negate); + + static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&); + + void eachChild(const std::function<void(const Expression&)>& visit) const override; + bool operator==(const Expression&) const override; + EvaluationResult evaluate(const EvaluationContext&) const override; + +private: + std::unique_ptr<Expression> lhs; + std::unique_ptr<Expression> rhs; + bool negate; +}; + +} // namespace expression +} // namespace style +} // namespace mbgl diff --git a/platform/node/test/ignores.json b/platform/node/test/ignores.json index 95335e530a..6e551f9daa 100644 --- a/platform/node/test/ignores.json +++ b/platform/node/test/ignores.json @@ -3,19 +3,6 @@ "expression-tests/array/implicit-2": "https://github.com/mapbox/mapbox-gl-native/issues/10533", "expression-tests/array/implicit-3": "https://github.com/mapbox/mapbox-gl-native/issues/10533", "expression-tests/coalesce/inference": "https://github.com/mapbox/mapbox-gl-native/issues/10588", - "expression-tests/equal/array": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/equal/color": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/equal/mismatch": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/equal/null-lhs": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/equal/null-rhs": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/equal/number": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/equal/object": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/equal/string": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/equal/value": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/not_equal/mismatch": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/not_equal/number": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/not_equal/string": "https://github.com/mapbox/mapbox-gl-native/issues/10678", - "expression-tests/not_equal/value": "https://github.com/mapbox/mapbox-gl-native/issues/10678", "expression-tests/interpolate/linear-color": "https://github.com/mapbox/mapbox-gl-native/issues/10604", "expression-tests/to-rgba/alpha": "https://github.com/mapbox/mapbox-gl-native/issues/10604", "expression-tests/to-rgba/basic": "https://github.com/mapbox/mapbox-gl-native/issues/10604", @@ -74,7 +61,6 @@ "render-tests/regressions/mapbox-gl-js#5370": "skip - https://github.com/mapbox/mapbox-gl-native/pull/9439", "render-tests/regressions/mapbox-gl-js#5599": "https://github.com/mapbox/mapbox-gl-native/issues/10399", "render-tests/regressions/mapbox-gl-js#5740": "https://github.com/mapbox/mapbox-gl-native/issues/10619", - "render-tests/regressions/mapbox-gl-js#5947": "https://github.com/mapbox/mapbox-gl-native/issues/10678", "render-tests/regressions/mapbox-gl-native#7357": "https://github.com/mapbox/mapbox-gl-native/issues/7357", "render-tests/runtime-styling/image-add-sdf": "https://github.com/mapbox/mapbox-gl-native/issues/9847", "render-tests/runtime-styling/paint-property-fill-flat-to-extrude": "https://github.com/mapbox/mapbox-gl-native/issues/6745", diff --git a/src/mbgl/style/expression/compound_expression.cpp b/src/mbgl/style/expression/compound_expression.cpp index 70bc6dfd95..ff21157faa 100644 --- a/src/mbgl/style/expression/compound_expression.cpp +++ b/src/mbgl/style/expression/compound_expression.cpp @@ -175,12 +175,6 @@ struct Signature<Lambda, std::enable_if_t<std::is_class<Lambda>::value>> using Definition = CompoundExpressionRegistry::Definition; -template <typename T> -Result<bool> equal(const T& lhs, const T& rhs) { return lhs == rhs; } - -template <typename T> -Result<bool> notEqual(const T& lhs, const T& rhs) { return lhs != rhs; } - template <typename Fn> static std::unique_ptr<detail::SignatureBase> makeSignature(Fn evaluateFunction) { return std::make_unique<detail::Signature<Fn>>(evaluateFunction); @@ -376,16 +370,6 @@ std::unordered_map<std::string, CompoundExpressionRegistry::Definition> initiali return result; }); - define("==", equal<double>); - define("==", equal<const std::string&>); - define("==", equal<bool>); - define("==", equal<NullValue>); - - define("!=", notEqual<double>); - define("!=", notEqual<const std::string&>); - define("!=", notEqual<bool>); - define("!=", notEqual<NullValue>); - define(">", [](double lhs, double rhs) -> Result<bool> { return lhs > rhs; }); define(">", [](const std::string& lhs, const std::string& rhs) -> Result<bool> { return lhs > rhs; }); define(">=", [](double lhs, double rhs) -> Result<bool> { return lhs >= rhs; }); diff --git a/src/mbgl/style/expression/equals.cpp b/src/mbgl/style/expression/equals.cpp new file mode 100644 index 0000000000..08ef85e92b --- /dev/null +++ b/src/mbgl/style/expression/equals.cpp @@ -0,0 +1,83 @@ +#include <mbgl/style/expression/equals.hpp> + +namespace mbgl { +namespace style { +namespace expression { + +Equals::Equals(std::unique_ptr<Expression> lhs_, std::unique_ptr<Expression> rhs_, bool negate_) + : Expression(type::Boolean), + lhs(std::move(lhs_)), + rhs(std::move(rhs_)), + negate(negate_) { +} + +EvaluationResult Equals::evaluate(const EvaluationContext& params) const { + EvaluationResult lhsResult = lhs->evaluate(params); + if (!lhsResult) return lhsResult; + + EvaluationResult rhsResult = rhs->evaluate(params); + if (!rhsResult) return lhsResult; + + bool result = *lhsResult == *rhsResult; + if (negate) { + result = !result; + } + return result; +} + +void Equals::eachChild(const std::function<void(const Expression&)>& visit) const { + visit(*lhs); + visit(*rhs); +} + +bool Equals::operator==(const Expression& e) const { + if (auto eq = dynamic_cast<const Equals*>(&e)) { + return eq->negate == negate && *eq->lhs == *lhs && *eq->rhs == *rhs; + } + return false; +} + +static bool isComparableType(const type::Type& type) { + return type == type::String || + type == type::Number || + type == type::Boolean || + type == type::Null; +} + +using namespace mbgl::style::conversion; +ParseResult Equals::parse(const Convertible& value, ParsingContext& ctx) { + std::size_t length = arrayLength(value); + + if (length != 3) { + ctx.error("Expected two arguments."); + return ParseResult(); + } + + bool negate = toString(arrayMember(value, 0)) == std::string("!="); + + ParseResult lhs = ctx.parse(arrayMember(value, 1), 1, {type::Value}); + if (!lhs) return ParseResult(); + + ParseResult rhs = ctx.parse(arrayMember(value, 2), 2, {type::Value}); + if (!rhs) return ParseResult(); + + type::Type lhsType = (*lhs)->getType(); + type::Type rhsType = (*rhs)->getType(); + + if (!isComparableType(lhsType) && !isComparableType(rhsType)) { + ctx.error("Expected at least one argument to be a string, number, boolean, or null, but found (" + + toString(lhsType) + ", " + toString(rhsType) + ") instead."); + return ParseResult(); + } + + if (lhsType != rhsType && lhsType != type::Value && rhsType != type::Value) { + ctx.error("Cannot compare " + toString(lhsType) + " and " + toString(rhsType) + "."); + return ParseResult(); + } + + return ParseResult(std::make_unique<Equals>(std::move(*lhs), std::move(*rhs), negate)); +} + +} // namespace expression +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/expression/parsing_context.cpp b/src/mbgl/style/expression/parsing_context.cpp index 501ba2749f..8dd99dd8d0 100644 --- a/src/mbgl/style/expression/parsing_context.cpp +++ b/src/mbgl/style/expression/parsing_context.cpp @@ -13,6 +13,7 @@ #include <mbgl/style/expression/coalesce.hpp> #include <mbgl/style/expression/coercion.hpp> #include <mbgl/style/expression/compound_expression.hpp> +#include <mbgl/style/expression/equals.hpp> #include <mbgl/style/expression/interpolate.hpp> #include <mbgl/style/expression/let.hpp> #include <mbgl/style/expression/literal.hpp> @@ -75,6 +76,8 @@ ParseResult ParsingContext::parse(const Convertible& value, std::size_t index_, const ExpressionRegistry& getExpressionRegistry() { static ExpressionRegistry registry {{ + {"==", Equals::parse}, + {"!=", Equals::parse}, {"all", All::parse}, {"any", Any::parse}, {"array", ArrayAssertion::parse}, |