1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#include <mbgl/style/expression/boolean_operator.hpp>
namespace mbgl {
namespace style {
namespace expression {
EvaluationResult Any::evaluate(const EvaluationContext& params) const {
for (auto it = inputs.begin(); it != inputs.end(); it++) {
const EvaluationResult result = (*it)->evaluate(params);
if (!result) return result;
if (result->get<bool>()) return EvaluationResult(true);
}
return EvaluationResult(false);
}
void Any::eachChild(const std::function<void(const Expression&)>& visit) const {
for (const std::unique_ptr<Expression>& input : inputs) {
visit(*input);
}
}
bool Any::operator==(const Expression& e) const {
if (e.getKind() == Kind::Any) {
auto rhs = static_cast<const Any*>(&e);
return Expression::childrenEqual(inputs, rhs->inputs);
}
return false;
}
std::vector<optional<Value>> Any::possibleOutputs() const {
return {{ true }, { false }};
}
EvaluationResult All::evaluate(const EvaluationContext& params) const {
for (auto it = inputs.begin(); it != inputs.end(); it++) {
const EvaluationResult result = (*it)->evaluate(params);
if (!result) return result;
if (!result->get<bool>()) return EvaluationResult(false);
}
return EvaluationResult(true);
}
void All::eachChild(const std::function<void(const Expression&)>& visit) const {
for (const std::unique_ptr<Expression>& input : inputs) {
visit(*input);
}
}
bool All::operator==(const Expression& e) const {
if (e.getKind() == Kind::All) {
auto rhs = static_cast<const All*>(&e);
return Expression::childrenEqual(inputs, rhs->inputs);
}
return false;
}
std::vector<optional<Value>> All::possibleOutputs() const {
return {{ true }, { false }};
}
using namespace mbgl::style::conversion;
template <class T>
ParseResult parseBooleanOp(const Convertible& value, ParsingContext& ctx) {
assert(isArray(value));
auto length = arrayLength(value);
std::vector<std::unique_ptr<Expression>> parsedInputs;
parsedInputs.reserve(length - 1);
for (std::size_t i = 1; i < length; i++) {
auto parsed = ctx.parse(arrayMember(value, i), i, {type::Boolean});
if (!parsed) {
return parsed;
}
parsedInputs.push_back(std::move(*parsed));
}
return ParseResult(std::make_unique<T>(std::move(parsedInputs)));
}
ParseResult Any::parse(const Convertible& value, ParsingContext& ctx) {
return parseBooleanOp<Any>(value, ctx);
}
ParseResult All::parse(const Convertible& value, ParsingContext& ctx) {
return parseBooleanOp<All>(value, ctx);
}
} // namespace expression
} // namespace style
} // namespace mbgl
|