From d13ebedadbd6ef9d6308c3399d0d88b140de421d Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Fri, 20 Jul 2018 15:40:47 +0300 Subject: [core] Replace expressions RTTI with enums + static cast --- .../conversion/data_driven_property_value.hpp | 8 +++-- include/mbgl/style/conversion/property_value.hpp | 8 +++-- include/mbgl/style/expression/array_assertion.hpp | 5 +-- include/mbgl/style/expression/at.hpp | 5 +-- include/mbgl/style/expression/boolean_operator.hpp | 4 +-- include/mbgl/style/expression/case.hpp | 2 +- include/mbgl/style/expression/coalesce.hpp | 2 +- .../mbgl/style/expression/compound_expression.hpp | 5 +-- include/mbgl/style/expression/error.hpp | 4 +-- include/mbgl/style/expression/expression.hpp | 27 ++++++++++++++- include/mbgl/style/expression/interpolate.hpp | 3 +- include/mbgl/style/expression/is_constant.hpp | 3 +- include/mbgl/style/expression/let.hpp | 10 +++--- include/mbgl/style/expression/literal.hpp | 7 ++-- include/mbgl/style/expression/match.hpp | 2 +- src/mbgl/style/expression/assertion.cpp | 5 +-- src/mbgl/style/expression/boolean_operator.cpp | 6 ++-- src/mbgl/style/expression/case.cpp | 3 +- src/mbgl/style/expression/coalesce.cpp | 3 +- src/mbgl/style/expression/coercion.cpp | 5 +-- src/mbgl/style/expression/collator_expression.cpp | 5 +-- src/mbgl/style/expression/equals.cpp | 5 +-- src/mbgl/style/expression/find_zoom_curve.cpp | 39 ++++++++++++++++------ src/mbgl/style/expression/interpolate.cpp | 2 +- src/mbgl/style/expression/is_constant.cpp | 5 +-- src/mbgl/style/expression/length.cpp | 5 +-- src/mbgl/style/expression/match.cpp | 3 +- src/mbgl/style/expression/parsing_context.cpp | 16 +++++---- src/mbgl/style/expression/step.cpp | 5 +-- 29 files changed, 138 insertions(+), 64 deletions(-) diff --git a/include/mbgl/style/conversion/data_driven_property_value.hpp b/include/mbgl/style/conversion/data_driven_property_value.hpp index f1bd1bdbb7..59d197b216 100644 --- a/include/mbgl/style/conversion/data_driven_property_value.hpp +++ b/include/mbgl/style/conversion/data_driven_property_value.hpp @@ -47,13 +47,17 @@ struct Converter> { return {}; } else if (!(*expression).isFeatureConstant() || !(*expression).isZoomConstant()) { return { std::move(*expression) }; - } else { + } else if ((*expression).getExpression().getKind() == Kind::Literal) { optional constant = fromExpressionValue( - dynamic_cast((*expression).getExpression()).getValue()); + static_cast((*expression).getExpression()).getValue()); if (!constant) { return {}; } return DataDrivenPropertyValue(*constant); + } else { + assert(false); + error = { "expected a literal expression" }; + return {}; } } diff --git a/include/mbgl/style/conversion/property_value.hpp b/include/mbgl/style/conversion/property_value.hpp index db23074c5e..b03655a848 100644 --- a/include/mbgl/style/conversion/property_value.hpp +++ b/include/mbgl/style/conversion/property_value.hpp @@ -54,13 +54,17 @@ struct Converter> { return {}; } else if (!(*expression).isZoomConstant()) { return { std::move(*expression) }; - } else { + } else if ((*expression).getExpression().getKind() == Kind::Literal) { optional constant = fromExpressionValue( - dynamic_cast((*expression).getExpression()).getValue()); + static_cast((*expression).getExpression()).getValue()); if (!constant) { return {}; } return PropertyValue(*constant); + } else { + assert(false); + error = { "expected a literal expression" }; + return {}; } } }; diff --git a/include/mbgl/style/expression/array_assertion.hpp b/include/mbgl/style/expression/array_assertion.hpp index af153611ff..0c0912b73e 100644 --- a/include/mbgl/style/expression/array_assertion.hpp +++ b/include/mbgl/style/expression/array_assertion.hpp @@ -14,7 +14,7 @@ namespace expression { class ArrayAssertion : public Expression { public: ArrayAssertion(type::Array type_, std::unique_ptr input_) : - Expression(type_), + Expression(Kind::ArrayAssertion, type_), input(std::move(input_)) {} @@ -24,7 +24,8 @@ public: void eachChild(const std::function& visit) const override; bool operator==(const Expression& e) const override { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::ArrayAssertion) { + auto rhs = static_cast(&e); return getType() == rhs->getType() && *input == *(rhs->input); } return false; diff --git a/include/mbgl/style/expression/at.hpp b/include/mbgl/style/expression/at.hpp index 1e6f1c7dd2..f0dbccb794 100644 --- a/include/mbgl/style/expression/at.hpp +++ b/include/mbgl/style/expression/at.hpp @@ -11,7 +11,7 @@ namespace expression { class At : public Expression { public: At(std::unique_ptr index_, std::unique_ptr input_) : - Expression(input_->getType().get().itemType), + Expression(Kind::At, input_->getType().get().itemType), index(std::move(index_)), input(std::move(input_)) {} @@ -22,7 +22,8 @@ public: void eachChild(const std::function&) const override; bool operator==(const Expression& e) const override { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::At) { + auto rhs = static_cast(&e); return *index == *(rhs->index) && *input == *(rhs->input); } return false; diff --git a/include/mbgl/style/expression/boolean_operator.hpp b/include/mbgl/style/expression/boolean_operator.hpp index 6d0f85756a..d254747513 100644 --- a/include/mbgl/style/expression/boolean_operator.hpp +++ b/include/mbgl/style/expression/boolean_operator.hpp @@ -12,7 +12,7 @@ namespace expression { class Any : public Expression { public: Any(std::vector> inputs_) : - Expression(type::Boolean), + Expression(Kind::Any, type::Boolean), inputs(std::move(inputs_)) {} @@ -31,7 +31,7 @@ private: class All : public Expression { public: All(std::vector> inputs_) : - Expression(type::Boolean), + Expression(Kind::All, type::Boolean), inputs(std::move(inputs_)) {} diff --git a/include/mbgl/style/expression/case.hpp b/include/mbgl/style/expression/case.hpp index 667ca53712..02dc3bc4c2 100644 --- a/include/mbgl/style/expression/case.hpp +++ b/include/mbgl/style/expression/case.hpp @@ -16,7 +16,7 @@ public: using Branch = std::pair, std::unique_ptr>; Case(type::Type type_, std::vector branches_, std::unique_ptr otherwise_) - : Expression(type_), branches(std::move(branches_)), otherwise(std::move(otherwise_)) { + : Expression(Kind::Case, type_), branches(std::move(branches_)), otherwise(std::move(otherwise_)) { } static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx); diff --git a/include/mbgl/style/expression/coalesce.hpp b/include/mbgl/style/expression/coalesce.hpp index a858bef695..cd60cee02e 100644 --- a/include/mbgl/style/expression/coalesce.hpp +++ b/include/mbgl/style/expression/coalesce.hpp @@ -15,7 +15,7 @@ class Coalesce : public Expression { public: using Args = std::vector>; Coalesce(const type::Type& type_, Args args_) : - Expression(type_), + Expression(Kind::Coalesce, type_), args(std::move(args_)) {} diff --git a/include/mbgl/style/expression/compound_expression.hpp b/include/mbgl/style/expression/compound_expression.hpp index 9d39194563..04562752a6 100644 --- a/include/mbgl/style/expression/compound_expression.hpp +++ b/include/mbgl/style/expression/compound_expression.hpp @@ -62,7 +62,7 @@ struct SignatureBase { class CompoundExpressionBase : public Expression { public: CompoundExpressionBase(std::string name_, const detail::SignatureBase& signature) : - Expression(signature.result), + Expression(Kind::CompoundExpression, signature.result), name(std::move(name_)), params(signature.params) {} @@ -108,7 +108,8 @@ public: } bool operator==(const Expression& e) const override { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::CompoundExpression) { + auto rhs = static_cast(&e); return getName() == rhs->getName() && Expression::childrenEqual(args, rhs->args); } return false; diff --git a/include/mbgl/style/expression/error.hpp b/include/mbgl/style/expression/error.hpp index bfb9247d11..9e1da2f0f0 100644 --- a/include/mbgl/style/expression/error.hpp +++ b/include/mbgl/style/expression/error.hpp @@ -11,13 +11,13 @@ namespace expression { class Error : public Expression { public: Error(std::string message_) - : Expression(type::Error), + : Expression(Kind::Error, type::Error), message(std::move(message_)) {} void eachChild(const std::function&) const override {} bool operator==(const Expression& e) const override { - return dynamic_cast(&e); + return e.getKind() == Kind::Error; } EvaluationResult evaluate(const EvaluationContext&) const override { diff --git a/include/mbgl/style/expression/expression.hpp b/include/mbgl/style/expression/expression.hpp index c41ac0b5f1..8301f1572c 100644 --- a/include/mbgl/style/expression/expression.hpp +++ b/include/mbgl/style/expression/expression.hpp @@ -113,9 +113,32 @@ public: ParseResult ExpressionClass::parse(const V&, ParsingContext), which handles parsing a style-spec JSON representation of the expression. */ + +enum class Kind : int32_t { + Coalesce, + CompoundExpression, + Literal, + ArrayAssertion, + At, + Interpolate, + Assertion, + Length, + Step, + Let, + Var, + CollatorExpression, + Coercion, + Match, + Error, + Case, + Any, + All, + Equals, +}; + class Expression { public: - Expression(type::Type type_) : type(std::move(type_)) {} + Expression(Kind kind_, type::Type type_) : kind(kind_), type(std::move(type_)) {} virtual ~Expression() = default; virtual EvaluationResult evaluate(const EvaluationContext& params) const = 0; @@ -125,6 +148,7 @@ public: return !operator==(rhs); } + Kind getKind() const { return kind; }; type::Type getType() const { return type; }; EvaluationResult evaluate(optional zoom, const Feature& feature, optional heatmapDensity) const; @@ -182,6 +206,7 @@ protected: } private: + Kind kind; type::Type type; }; diff --git a/include/mbgl/style/expression/interpolate.hpp b/include/mbgl/style/expression/interpolate.hpp index 0aca6909a3..0e78504719 100644 --- a/include/mbgl/style/expression/interpolate.hpp +++ b/include/mbgl/style/expression/interpolate.hpp @@ -51,7 +51,8 @@ public: } bool operator==(const Expression& e) const override { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Interpolate) { + auto rhs = static_cast(&e); if (interpolator != rhs->interpolator || *input != *(rhs->input) || stops.size() != rhs->stops.size()) diff --git a/include/mbgl/style/expression/is_constant.hpp b/include/mbgl/style/expression/is_constant.hpp index 29e03ccbc0..065fa30db1 100644 --- a/include/mbgl/style/expression/is_constant.hpp +++ b/include/mbgl/style/expression/is_constant.hpp @@ -9,7 +9,8 @@ namespace expression { template bool isGlobalPropertyConstant(const Expression& expression, const T& properties) { - if (auto e = dynamic_cast(&expression)) { + if (expression.getKind() == Kind::CompoundExpression) { + auto e = static_cast(&expression); for (const std::string& property : properties) { if (e->getName() == property) { return false; diff --git a/include/mbgl/style/expression/let.hpp b/include/mbgl/style/expression/let.hpp index d0210d8bba..d11ba1b976 100644 --- a/include/mbgl/style/expression/let.hpp +++ b/include/mbgl/style/expression/let.hpp @@ -16,7 +16,7 @@ public: using Bindings = std::map>; Let(Bindings bindings_, std::unique_ptr result_) : - Expression(result_->getType()), + Expression(Kind::Let, result_->getType()), bindings(std::move(bindings_)), result(std::move(result_)) {} @@ -27,7 +27,8 @@ public: void eachChild(const std::function&) const override; bool operator==(const Expression& e) const override { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Let) { + auto rhs = static_cast(&e); return *result == *(rhs->result); } return false; @@ -49,7 +50,7 @@ private: class Var : public Expression { public: Var(std::string name_, std::shared_ptr value_) : - Expression(value_->getType()), + Expression(Kind::Var, value_->getType()), name(std::move(name_)), value(value_) {} @@ -60,7 +61,8 @@ public: void eachChild(const std::function&) const override; bool operator==(const Expression& e) const override { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Var) { + auto rhs = static_cast(&e); return *value == *(rhs->value); } return false; diff --git a/include/mbgl/style/expression/literal.hpp b/include/mbgl/style/expression/literal.hpp index 44c095bf28..bcae23b1fa 100644 --- a/include/mbgl/style/expression/literal.hpp +++ b/include/mbgl/style/expression/literal.hpp @@ -13,12 +13,12 @@ namespace expression { class Literal : public Expression { public: Literal(Value value_) - : Expression(typeOf(value_)) + : Expression(Kind::Literal, typeOf(value_)) , value(value_) {} Literal(type::Array type_, std::vector value_) - : Expression(type_) + : Expression(Kind::Literal, type_) , value(value_) {} @@ -31,7 +31,8 @@ public: void eachChild(const std::function&) const override {} bool operator==(const Expression& e) const override { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Literal) { + auto rhs = static_cast(&e); return value == rhs->value; } return false; diff --git a/include/mbgl/style/expression/match.hpp b/include/mbgl/style/expression/match.hpp index 3775e38067..2ce4b7a533 100644 --- a/include/mbgl/style/expression/match.hpp +++ b/include/mbgl/style/expression/match.hpp @@ -19,7 +19,7 @@ public: std::unique_ptr input_, Branches branches_, std::unique_ptr otherwise_ - ) : Expression(type_), + ) : Expression(Kind::Match, type_), input(std::move(input_)), branches(std::move(branches_)), otherwise(std::move(otherwise_)) diff --git a/src/mbgl/style/expression/assertion.cpp b/src/mbgl/style/expression/assertion.cpp index eefc9e24f8..2434d7a2f8 100644 --- a/src/mbgl/style/expression/assertion.cpp +++ b/src/mbgl/style/expression/assertion.cpp @@ -8,7 +8,7 @@ namespace expression { using namespace mbgl::style::conversion; Assertion::Assertion(type::Type type_, std::vector> inputs_) : - Expression(type_), + Expression(Kind::Assertion, type_), inputs(std::move(inputs_)) { assert(!inputs.empty()); @@ -72,7 +72,8 @@ void Assertion::eachChild(const std::function& visit) c }; bool Assertion::operator==(const Expression& e) const { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Assertion) { + auto rhs = static_cast(&e); return getType() == rhs->getType() && Expression::childrenEqual(inputs, rhs->inputs); } return false; diff --git a/src/mbgl/style/expression/boolean_operator.cpp b/src/mbgl/style/expression/boolean_operator.cpp index 8d277450ba..68e96129aa 100644 --- a/src/mbgl/style/expression/boolean_operator.cpp +++ b/src/mbgl/style/expression/boolean_operator.cpp @@ -20,7 +20,8 @@ void Any::eachChild(const std::function& visit) const { } bool Any::operator==(const Expression& e) const { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Any) { + auto rhs = static_cast(&e); return Expression::childrenEqual(inputs, rhs->inputs); } return false; @@ -47,7 +48,8 @@ void All::eachChild(const std::function& visit) const { } bool All::operator==(const Expression& e) const { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::All) { + auto rhs = static_cast(&e); return Expression::childrenEqual(inputs, rhs->inputs); } return false; diff --git a/src/mbgl/style/expression/case.cpp b/src/mbgl/style/expression/case.cpp index 295e694189..e885c0ce6b 100644 --- a/src/mbgl/style/expression/case.cpp +++ b/src/mbgl/style/expression/case.cpp @@ -28,7 +28,8 @@ void Case::eachChild(const std::function& visit) const } bool Case::operator==(const Expression& e) const { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Case) { + auto rhs = static_cast(&e); return *otherwise == *(rhs->otherwise) && Expression::childrenEqual(branches, rhs->branches); } return false; diff --git a/src/mbgl/style/expression/coalesce.cpp b/src/mbgl/style/expression/coalesce.cpp index 872a9abbef..0090f16009 100644 --- a/src/mbgl/style/expression/coalesce.cpp +++ b/src/mbgl/style/expression/coalesce.cpp @@ -21,7 +21,8 @@ void Coalesce::eachChild(const std::function& visit) co } bool Coalesce::operator==(const Expression& e) const { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Coalesce) { + auto rhs = static_cast(&e); return Expression::childrenEqual(args, rhs->args); } return false; diff --git a/src/mbgl/style/expression/coercion.cpp b/src/mbgl/style/expression/coercion.cpp index 11172a3668..f5a4d70f66 100644 --- a/src/mbgl/style/expression/coercion.cpp +++ b/src/mbgl/style/expression/coercion.cpp @@ -68,7 +68,7 @@ EvaluationResult toColor(const Value& colorValue) { } Coercion::Coercion(type::Type type_, std::vector> inputs_) : - Expression(std::move(type_)), + Expression(Kind::Coercion, std::move(type_)), inputs(std::move(inputs_)) { assert(!inputs.empty()); @@ -138,7 +138,8 @@ void Coercion::eachChild(const std::function& visit) co }; bool Coercion::operator==(const Expression& e) const { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Coercion) { + auto rhs = static_cast(&e); return getType() == rhs->getType() && Expression::childrenEqual(inputs, rhs->inputs); } return false; diff --git a/src/mbgl/style/expression/collator_expression.cpp b/src/mbgl/style/expression/collator_expression.cpp index f5e4e3fdff..b27eedbc76 100644 --- a/src/mbgl/style/expression/collator_expression.cpp +++ b/src/mbgl/style/expression/collator_expression.cpp @@ -10,7 +10,7 @@ namespace expression { CollatorExpression::CollatorExpression(std::unique_ptr caseSensitive_, std::unique_ptr diacriticSensitive_, optional> locale_) - : Expression(type::Collator) + : Expression(Kind::CollatorExpression, type::Collator) , caseSensitive(std::move(caseSensitive_)) , diacriticSensitive(std::move(diacriticSensitive_)) , locale(std::move(locale_)) @@ -73,7 +73,8 @@ void CollatorExpression::eachChild(const std::function& } bool CollatorExpression::operator==(const Expression& e) const { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::CollatorExpression) { + auto rhs = static_cast(&e); if ((locale && (!rhs->locale || **locale != **(rhs->locale))) || (!locale && rhs->locale)) { return false; diff --git a/src/mbgl/style/expression/equals.cpp b/src/mbgl/style/expression/equals.cpp index 245899f975..73e2baf71b 100644 --- a/src/mbgl/style/expression/equals.cpp +++ b/src/mbgl/style/expression/equals.cpp @@ -13,7 +13,7 @@ static bool isComparableType(const type::Type& type) { } Equals::Equals(std::unique_ptr lhs_, std::unique_ptr rhs_, optional> collator_, bool negate_) - : Expression(type::Boolean), + : Expression(Kind::Equals, type::Boolean), lhs(std::move(lhs_)), rhs(std::move(rhs_)), collator(std::move(collator_)), @@ -53,7 +53,8 @@ void Equals::eachChild(const std::function& visit) cons } bool Equals::operator==(const Expression& e) const { - if (auto eq = dynamic_cast(&e)) { + if (e.getKind() == Kind::Equals) { + auto eq = static_cast(&e); return eq->negate == negate && *eq->lhs == *lhs && *eq->rhs == *rhs; } return false; diff --git a/src/mbgl/style/expression/find_zoom_curve.cpp b/src/mbgl/style/expression/find_zoom_curve.cpp index 1e0a936605..a27f8560ef 100644 --- a/src/mbgl/style/expression/find_zoom_curve.cpp +++ b/src/mbgl/style/expression/find_zoom_curve.cpp @@ -14,9 +14,14 @@ namespace expression { optional> findZoomCurve(const expression::Expression* e) { optional> result; - if (auto let = dynamic_cast(e)) { + switch (e->getKind()) { + case Kind::Let: { + auto let = static_cast(e); result = findZoomCurve(let->getResult()); - } else if (auto coalesce = dynamic_cast(e)) { + break; + } + case Kind::Coalesce: { + auto coalesce = static_cast(e); std::size_t length = coalesce->getLength(); for (std::size_t i = 0; i < length; i++) { result = findZoomCurve(coalesce->getChild(i)); @@ -24,16 +29,30 @@ optional> findZoomCurve(c break; } } - } else if (auto curve = dynamic_cast(e)) { - auto z = dynamic_cast(curve->getInput().get()); - if (z && z->getName() == "zoom") { - result = {curve}; + break; + } + case Kind::Interpolate: { + auto curve = static_cast(e); + if (curve->getInput()->getKind() == Kind::CompoundExpression) { + auto z = static_cast(curve->getInput().get()); + if (z && z->getName() == "zoom") { + result = {curve}; + } } - } else if (auto step = dynamic_cast(e)) { - auto z = dynamic_cast(step->getInput().get()); - if (z && z->getName() == "zoom") { - result = {step}; + break; + } + case Kind::Step: { + auto step = static_cast(e); + if (step->getInput()->getKind() == Kind::CompoundExpression) { + auto z = static_cast(step->getInput().get()); + if (z && z->getName() == "zoom") { + result = {step}; + } } + break; + } + default: + break; } if (result && result->is()) { diff --git a/src/mbgl/style/expression/interpolate.cpp b/src/mbgl/style/expression/interpolate.cpp index a9bb3bf05e..852042382c 100644 --- a/src/mbgl/style/expression/interpolate.cpp +++ b/src/mbgl/style/expression/interpolate.cpp @@ -264,7 +264,7 @@ Interpolate::Interpolate(const type::Type& type_, Interpolator interpolator_, std::unique_ptr input_, std::map> stops_) - : Expression(type_), + : Expression(Kind::Interpolate, type_), interpolator(std::move(interpolator_)), input(std::move(input_)), stops(std::move(stops_)) { diff --git a/src/mbgl/style/expression/is_constant.cpp b/src/mbgl/style/expression/is_constant.cpp index b877ed550a..3b1f1aba8c 100644 --- a/src/mbgl/style/expression/is_constant.cpp +++ b/src/mbgl/style/expression/is_constant.cpp @@ -9,7 +9,8 @@ namespace expression { constexpr static const char filter[] = "filter-"; bool isFeatureConstant(const Expression& expression) { - if (auto e = dynamic_cast(&expression)) { + if (expression.getKind() == Kind::CompoundExpression) { + auto e = static_cast(&expression); const std::string name = e->getName(); optional parameterCount = e->getParameterCount(); if (name == "get" && parameterCount && *parameterCount == 1) { @@ -28,7 +29,7 @@ bool isFeatureConstant(const Expression& expression) { } } - if (dynamic_cast(&expression)) { + if (expression.getKind() == Kind::CollatorExpression) { // Although the results of a Collator expression with fixed arguments // generally shouldn't change between executions, we can't serialize them // as constant expressions because results change based on environment. diff --git a/src/mbgl/style/expression/length.cpp b/src/mbgl/style/expression/length.cpp index 258353ae4e..ad7a15675a 100644 --- a/src/mbgl/style/expression/length.cpp +++ b/src/mbgl/style/expression/length.cpp @@ -6,7 +6,7 @@ namespace style { namespace expression { Length::Length(std::unique_ptr input_) - : Expression(type::Number), + : Expression(Kind::Length, type::Number), input(std::move(input_)) { } @@ -30,7 +30,8 @@ void Length::eachChild(const std::function& visit) cons } bool Length::operator==(const Expression& e) const { - if (auto eq = dynamic_cast(&e)) { + if (e.getKind() == Kind::Length) { + auto eq = static_cast(&e); return *eq->input == *input; } return false; diff --git a/src/mbgl/style/expression/match.cpp b/src/mbgl/style/expression/match.cpp index 59123c9812..340f1dab4d 100644 --- a/src/mbgl/style/expression/match.cpp +++ b/src/mbgl/style/expression/match.cpp @@ -18,7 +18,8 @@ void Match::eachChild(const std::function& visit) co template bool Match::operator==(const Expression& e) const { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Match) { + auto rhs = static_cast(&e); return (*input == *(rhs->input) && *otherwise == *(rhs->otherwise) && Expression::childrenEqual(branches, rhs->branches)); diff --git a/src/mbgl/style/expression/parsing_context.cpp b/src/mbgl/style/expression/parsing_context.cpp index fe3102f158..a4c04b03b1 100644 --- a/src/mbgl/style/expression/parsing_context.cpp +++ b/src/mbgl/style/expression/parsing_context.cpp @@ -32,19 +32,21 @@ namespace style { namespace expression { bool isConstant(const Expression& expression) { - if (auto varExpression = dynamic_cast(&expression)) { + if (expression.getKind() == Kind::Var) { + auto varExpression = static_cast(&expression); return isConstant(*varExpression->getBoundExpression()); } - if (auto compound = dynamic_cast(&expression)) { + if (expression.getKind() == Kind::CompoundExpression) { + auto compound = static_cast(&expression); if (compound->getName() == "error") { return false; } } - bool isTypeAnnotation = dynamic_cast(&expression) || - dynamic_cast(&expression) || - dynamic_cast(&expression); + bool isTypeAnnotation = expression.getKind() == Kind::Coercion || + expression.getKind() == Kind::Assertion || + expression.getKind() == Kind::ArrayAssertion; bool childrenConstant = true; expression.eachChild([&](const Expression& child) { @@ -58,7 +60,7 @@ bool isConstant(const Expression& expression) { if (isTypeAnnotation) { childrenConstant = childrenConstant && isConstant(child); } else { - childrenConstant = childrenConstant && dynamic_cast(&child); + childrenConstant = childrenConstant && child.getKind() == Kind::Literal; } }); if (!childrenConstant) { @@ -186,7 +188,7 @@ ParseResult ParsingContext::parse(const Convertible& value, TypeAnnotationOption // If an expression's arguments are all constant, we can evaluate // it immediately and replace it with a literal value in the // parsed result. - if (!dynamic_cast(parsed->get()) && isConstant(**parsed)) { + if ((*parsed)->getKind() != Kind::Literal && isConstant(**parsed)) { EvaluationContext params(nullptr); EvaluationResult evaluated((*parsed)->evaluate(params)); if (!evaluated) { diff --git a/src/mbgl/style/expression/step.cpp b/src/mbgl/style/expression/step.cpp index f42a2721a9..ee6055091e 100644 --- a/src/mbgl/style/expression/step.cpp +++ b/src/mbgl/style/expression/step.cpp @@ -11,7 +11,7 @@ namespace expression { Step::Step(const type::Type& type_, std::unique_ptr input_, std::map> stops_) - : Expression(type_), + : Expression(Kind::Step, type_), input(std::move(input_)), stops(std::move(stops_)) { @@ -57,7 +57,8 @@ void Step::eachStop(const std::function& visit) } bool Step::operator==(const Expression& e) const { - if (auto rhs = dynamic_cast(&e)) { + if (e.getKind() == Kind::Step) { + auto rhs = static_cast(&e); return *input == *(rhs->input) && Expression::childrenEqual(stops, rhs->stops); } return false; -- cgit v1.2.1