summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorBruno de Oliveira Abinader <bruno@mapbox.com>2018-07-20 15:40:47 +0300
committerBruno de Oliveira Abinader <bruno@mapbox.com>2018-07-24 10:06:18 +0300
commitcb714d57c5c5ad181aaf5e1690221da8d965682b (patch)
tree21fa8cc6caf1e66de40f9715fa98426e86cfd964 /include
parent1683da3225d0cbed3bb6238fd292fa288f6a32d6 (diff)
downloadqtlocation-mapboxgl-cb714d57c5c5ad181aaf5e1690221da8d965682b.tar.gz
[core] Replace expressions RTTI with enums + static cast
Diffstat (limited to 'include')
-rw-r--r--include/mbgl/style/conversion/data_driven_property_value.hpp8
-rw-r--r--include/mbgl/style/conversion/property_value.hpp8
-rw-r--r--include/mbgl/style/expression/array_assertion.hpp5
-rw-r--r--include/mbgl/style/expression/at.hpp5
-rw-r--r--include/mbgl/style/expression/boolean_operator.hpp4
-rw-r--r--include/mbgl/style/expression/case.hpp2
-rw-r--r--include/mbgl/style/expression/coalesce.hpp2
-rw-r--r--include/mbgl/style/expression/compound_expression.hpp5
-rw-r--r--include/mbgl/style/expression/error.hpp4
-rw-r--r--include/mbgl/style/expression/expression.hpp27
-rw-r--r--include/mbgl/style/expression/interpolate.hpp3
-rw-r--r--include/mbgl/style/expression/is_constant.hpp3
-rw-r--r--include/mbgl/style/expression/let.hpp10
-rw-r--r--include/mbgl/style/expression/literal.hpp7
-rw-r--r--include/mbgl/style/expression/match.hpp2
15 files changed, 68 insertions, 27 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<DataDrivenPropertyValue<T>> {
return {};
} else if (!(*expression).isFeatureConstant() || !(*expression).isZoomConstant()) {
return { std::move(*expression) };
- } else {
+ } else if ((*expression).getExpression().getKind() == Kind::Literal) {
optional<T> constant = fromExpressionValue<T>(
- dynamic_cast<const Literal&>((*expression).getExpression()).getValue());
+ static_cast<const Literal&>((*expression).getExpression()).getValue());
if (!constant) {
return {};
}
return DataDrivenPropertyValue<T>(*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<PropertyValue<T>> {
return {};
} else if (!(*expression).isZoomConstant()) {
return { std::move(*expression) };
- } else {
+ } else if ((*expression).getExpression().getKind() == Kind::Literal) {
optional<T> constant = fromExpressionValue<T>(
- dynamic_cast<const Literal&>((*expression).getExpression()).getValue());
+ static_cast<const Literal&>((*expression).getExpression()).getValue());
if (!constant) {
return {};
}
return PropertyValue<T>(*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<Expression> input_) :
- Expression(type_),
+ Expression(Kind::ArrayAssertion, type_),
input(std::move(input_))
{}
@@ -24,7 +24,8 @@ public:
void eachChild(const std::function<void(const Expression&)>& visit) const override;
bool operator==(const Expression& e) const override {
- if (auto rhs = dynamic_cast<const ArrayAssertion*>(&e)) {
+ if (e.getKind() == Kind::ArrayAssertion) {
+ auto rhs = static_cast<const ArrayAssertion*>(&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<Expression> index_, std::unique_ptr<Expression> input_) :
- Expression(input_->getType().get<type::Array>().itemType),
+ Expression(Kind::At, input_->getType().get<type::Array>().itemType),
index(std::move(index_)),
input(std::move(input_))
{}
@@ -22,7 +22,8 @@ public:
void eachChild(const std::function<void(const Expression&)>&) const override;
bool operator==(const Expression& e) const override {
- if (auto rhs = dynamic_cast<const At*>(&e)) {
+ if (e.getKind() == Kind::At) {
+ auto rhs = static_cast<const At*>(&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<std::unique_ptr<Expression>> 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<std::unique_ptr<Expression>> 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<Expression>, std::unique_ptr<Expression>>;
Case(type::Type type_, std::vector<Branch> branches_, std::unique_ptr<Expression> 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<std::unique_ptr<Expression>>;
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<const CompoundExpression*>(&e)) {
+ if (e.getKind() == Kind::CompoundExpression) {
+ auto rhs = static_cast<const CompoundExpression*>(&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<void(const Expression&)>&) const override {}
bool operator==(const Expression& e) const override {
- return dynamic_cast<const Error*>(&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<float> zoom, const Feature& feature, optional<double> 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<const Interpolate*>(&e)) {
+ if (e.getKind() == Kind::Interpolate) {
+ auto rhs = static_cast<const Interpolate*>(&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 <typename T>
bool isGlobalPropertyConstant(const Expression& expression, const T& properties) {
- if (auto e = dynamic_cast<const CompoundExpressionBase*>(&expression)) {
+ if (expression.getKind() == Kind::CompoundExpression) {
+ auto e = static_cast<const CompoundExpressionBase*>(&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<std::string, std::shared_ptr<Expression>>;
Let(Bindings bindings_, std::unique_ptr<Expression> 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<void(const Expression&)>&) const override;
bool operator==(const Expression& e) const override {
- if (auto rhs = dynamic_cast<const Let*>(&e)) {
+ if (e.getKind() == Kind::Let) {
+ auto rhs = static_cast<const Let*>(&e);
return *result == *(rhs->result);
}
return false;
@@ -49,7 +50,7 @@ private:
class Var : public Expression {
public:
Var(std::string name_, std::shared_ptr<Expression> 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<void(const Expression&)>&) const override;
bool operator==(const Expression& e) const override {
- if (auto rhs = dynamic_cast<const Var*>(&e)) {
+ if (e.getKind() == Kind::Var) {
+ auto rhs = static_cast<const Var*>(&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> value_)
- : Expression(type_)
+ : Expression(Kind::Literal, type_)
, value(value_)
{}
@@ -31,7 +31,8 @@ public:
void eachChild(const std::function<void(const Expression&)>&) const override {}
bool operator==(const Expression& e) const override {
- if (auto rhs = dynamic_cast<const Literal*>(&e)) {
+ if (e.getKind() == Kind::Literal) {
+ auto rhs = static_cast<const Literal*>(&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<Expression> input_,
Branches branches_,
std::unique_ptr<Expression> otherwise_
- ) : Expression(type_),
+ ) : Expression(Kind::Match, type_),
input(std::move(input_)),
branches(std::move(branches_)),
otherwise(std::move(otherwise_))