Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Replace expressions RTTI with enums + static cast
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Jul 24, 2018
1 parent 1683da3 commit cb714d5
Show file tree
Hide file tree
Showing 29 changed files with 138 additions and 64 deletions.
8 changes: 6 additions & 2 deletions include/mbgl/style/conversion/data_driven_property_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
}
}

Expand Down
8 changes: 6 additions & 2 deletions include/mbgl/style/conversion/property_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
}
}
};
Expand Down
5 changes: 3 additions & 2 deletions include/mbgl/style/expression/array_assertion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_))
{}

Expand All @@ -24,7 +24,8 @@ class ArrayAssertion : public Expression {
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;
Expand Down
5 changes: 3 additions & 2 deletions include/mbgl/style/expression/at.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_))
{}
Expand All @@ -22,7 +22,8 @@ class At : public Expression {
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;
Expand Down
4 changes: 2 additions & 2 deletions include/mbgl/style/expression/boolean_operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_))
{}

Expand All @@ -31,7 +31,7 @@ class Any : public Expression {
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_))
{}

Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/style/expression/case.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Case : public Expression {
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);
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/style/expression/coalesce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_))
{}

Expand Down
5 changes: 3 additions & 2 deletions include/mbgl/style/expression/compound_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{}
Expand Down Expand Up @@ -108,7 +108,8 @@ class CompoundExpression : public CompoundExpressionBase {
}

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;
Expand Down
4 changes: 2 additions & 2 deletions include/mbgl/style/expression/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 26 additions & 1 deletion include/mbgl/style/expression/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,32 @@ class EvaluationResult : public Result<Value> {
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;
Expand All @@ -125,6 +148,7 @@ class Expression {
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;
Expand Down Expand Up @@ -182,6 +206,7 @@ class Expression {
}

private:
Kind kind;
type::Type type;
};

Expand Down
3 changes: 2 additions & 1 deletion include/mbgl/style/expression/interpolate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class Interpolate : public Expression {
}

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())
Expand Down
3 changes: 2 additions & 1 deletion include/mbgl/style/expression/is_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions include/mbgl/style/expression/let.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Let : public Expression {
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_))
{}
Expand All @@ -27,7 +27,8 @@ class Let : public Expression {
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;
Expand All @@ -49,7 +50,7 @@ class Let : public Expression {
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_)
{}
Expand All @@ -60,7 +61,8 @@ class Var : public Expression {
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;
Expand Down
7 changes: 4 additions & 3 deletions include/mbgl/style/expression/literal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
{}

Expand All @@ -31,7 +31,8 @@ class Literal : public Expression {
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;
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/style/expression/match.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Match : public Expression {
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_))
Expand Down
5 changes: 3 additions & 2 deletions src/mbgl/style/expression/assertion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace expression {
using namespace mbgl::style::conversion;

Assertion::Assertion(type::Type type_, std::vector<std::unique_ptr<Expression>> inputs_) :
Expression(type_),
Expression(Kind::Assertion, type_),
inputs(std::move(inputs_))
{
assert(!inputs.empty());
Expand Down Expand Up @@ -72,7 +72,8 @@ void Assertion::eachChild(const std::function<void(const Expression&)>& visit) c
};

bool Assertion::operator==(const Expression& e) const {
if (auto rhs = dynamic_cast<const Assertion*>(&e)) {
if (e.getKind() == Kind::Assertion) {
auto rhs = static_cast<const Assertion*>(&e);
return getType() == rhs->getType() && Expression::childrenEqual(inputs, rhs->inputs);
}
return false;
Expand Down
6 changes: 4 additions & 2 deletions src/mbgl/style/expression/boolean_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ void Any::eachChild(const std::function<void(const Expression&)>& visit) const {
}

bool Any::operator==(const Expression& e) const {
if (auto rhs = dynamic_cast<const Any*>(&e)) {
if (e.getKind() == Kind::Any) {
auto rhs = static_cast<const Any*>(&e);
return Expression::childrenEqual(inputs, rhs->inputs);
}
return false;
Expand All @@ -47,7 +48,8 @@ void All::eachChild(const std::function<void(const Expression&)>& visit) const {
}

bool All::operator==(const Expression& e) const {
if (auto rhs = dynamic_cast<const All*>(&e)) {
if (e.getKind() == Kind::All) {
auto rhs = static_cast<const All*>(&e);
return Expression::childrenEqual(inputs, rhs->inputs);
}
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/style/expression/case.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ void Case::eachChild(const std::function<void(const Expression&)>& visit) const
}

bool Case::operator==(const Expression& e) const {
if (auto rhs = dynamic_cast<const Case*>(&e)) {
if (e.getKind() == Kind::Case) {
auto rhs = static_cast<const Case*>(&e);
return *otherwise == *(rhs->otherwise) && Expression::childrenEqual(branches, rhs->branches);
}
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/style/expression/coalesce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ void Coalesce::eachChild(const std::function<void(const Expression&)>& visit) co
}

bool Coalesce::operator==(const Expression& e) const {
if (auto rhs = dynamic_cast<const Coalesce*>(&e)) {
if (e.getKind() == Kind::Coalesce) {
auto rhs = static_cast<const Coalesce*>(&e);
return Expression::childrenEqual(args, rhs->args);
}
return false;
Expand Down
5 changes: 3 additions & 2 deletions src/mbgl/style/expression/coercion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ EvaluationResult toColor(const Value& colorValue) {
}

Coercion::Coercion(type::Type type_, std::vector<std::unique_ptr<Expression>> inputs_) :
Expression(std::move(type_)),
Expression(Kind::Coercion, std::move(type_)),
inputs(std::move(inputs_))
{
assert(!inputs.empty());
Expand Down Expand Up @@ -138,7 +138,8 @@ void Coercion::eachChild(const std::function<void(const Expression&)>& visit) co
};

bool Coercion::operator==(const Expression& e) const {
if (auto rhs = dynamic_cast<const Coercion*>(&e)) {
if (e.getKind() == Kind::Coercion) {
auto rhs = static_cast<const Coercion*>(&e);
return getType() == rhs->getType() && Expression::childrenEqual(inputs, rhs->inputs);
}
return false;
Expand Down
5 changes: 3 additions & 2 deletions src/mbgl/style/expression/collator_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace expression {
CollatorExpression::CollatorExpression(std::unique_ptr<Expression> caseSensitive_,
std::unique_ptr<Expression> diacriticSensitive_,
optional<std::unique_ptr<Expression>> locale_)
: Expression(type::Collator)
: Expression(Kind::CollatorExpression, type::Collator)
, caseSensitive(std::move(caseSensitive_))
, diacriticSensitive(std::move(diacriticSensitive_))
, locale(std::move(locale_))
Expand Down Expand Up @@ -73,7 +73,8 @@ void CollatorExpression::eachChild(const std::function<void(const Expression&)>&
}

bool CollatorExpression::operator==(const Expression& e) const {
if (auto rhs = dynamic_cast<const CollatorExpression*>(&e)) {
if (e.getKind() == Kind::CollatorExpression) {
auto rhs = static_cast<const CollatorExpression*>(&e);
if ((locale && (!rhs->locale || **locale != **(rhs->locale))) ||
(!locale && rhs->locale)) {
return false;
Expand Down
Loading

0 comments on commit cb714d5

Please sign in to comment.