MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
format_section_override.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include <mbgl/renderer/possibly_evaluated_property_value.hpp>
5 
6 namespace mbgl {
7 namespace style {
8 namespace expression {
9 
10 template<class T>
11 class FormatSectionOverride final : public Expression {
12 public:
14  PossiblyEvaluatedPropertyValue<T> defaultValue_,
15  std::string propertyName_) :
17  defaultValue(std::move(defaultValue_)),
18  propertyName(std::move(propertyName_))
19  {}
20 
21  EvaluationResult evaluate(const EvaluationContext& context) const final {
22  using Object = std::unordered_map<std::string, expression::Value>;
23  if (context.formattedSection &&
24  context.formattedSection->is<Object>()) {
25  const auto& section = context.formattedSection->get<Object>();
26  if (section.find(propertyName) != section.end()) {
27  return section.at(propertyName);
28  }
29  }
30 
31  return defaultValue.match(
32  [&context] (const style::PropertyExpression<T>& e) { return e.getExpression().evaluate(context); },
33  [] (const T& t) -> EvaluationResult { return t; }
34  );
35  }
36 
37  void eachChild(const std::function<void(const Expression&)>& fn) const final {
38  defaultValue.match([&fn] (const style::PropertyExpression<T>& e) { fn(e.getExpression()); },
39  [] (const T&) {});
40  }
41 
42  bool operator==(const Expression& e) const final {
43  if (e.getKind() == Kind::FormatSectionOverride) {
44  const auto* other = static_cast<const FormatSectionOverride*>(&e);
45 
46  if (getType() != other->getType() || propertyName != other->propertyName) {
47  return false;
48  }
49 
50  // Check that default values or property expressions are equal.
51  return defaultValue.match(
52  [other] (const style::PropertyExpression<T>& thisExpr) {
53  return other->defaultValue.match([&thisExpr] (const style::PropertyExpression<T>& otherExpr) {
54  return thisExpr == otherExpr;
55  },
56  [] (const T&) {
57  return false;
58  });
59  },
60  [other] (const T& thisValue) {
61  return other->defaultValue.match([&thisValue] (const T& otherValue) {
62  return thisValue == otherValue;
63  },
64  [] (const style::PropertyExpression<T>&) {
65  return false;
66  });
67  });
68  }
69 
70  return false;
71  }
72 
73  std::vector<std::optional<Value>> possibleOutputs() const final {
74  return {std::nullopt};
75  }
76 
77  std::string getOperator() const final { return "format-section-override"; }
78 
79 private:
80  PossiblyEvaluatedPropertyValue<T> defaultValue;
81  std::string propertyName;
82 };
83 
84 } // namespace expression
85 } // namespace style
86 } // namespace mbgl
const expression::Expression & getExpression() const noexcept
virtual EvaluationResult evaluate(const EvaluationContext &params) const =0
std::vector< std::optional< Value > > possibleOutputs() const final
bool operator==(const Expression &e) const final
EvaluationResult evaluate(const EvaluationContext &context) const final
void eachChild(const std::function< void(const Expression &)> &fn) const final
FormatSectionOverride(const type::Type &type_, PossiblyEvaluatedPropertyValue< T > defaultValue_, std::string propertyName_)
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
constexpr ObjectType Object
Definition: type.hpp:89
variant< NullType, NumberType, BooleanType, StringType, ColorType, ObjectType, ValueType, mapbox::util::recursive_wrapper< Array >, CollatorType, FormattedType, ErrorType, ImageType > Type
Definition: type.hpp:108
Definition: actor.hpp:15
Definition: tile_id.hpp:256