MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
property_expression.hpp
Go to the documentation of this file.
1 #pragma once
2 
8 #include <mbgl/util/range.hpp>
9 
10 #include <optional>
11 
12 namespace mbgl {
13 namespace style {
14 
16 public:
17  explicit PropertyExpressionBase(std::unique_ptr<expression::Expression>);
18 
19  bool isZoomConstant() const noexcept;
20  bool isFeatureConstant() const noexcept;
21  bool isRuntimeConstant() const noexcept;
22  float interpolationFactor(const Range<float>&, float) const noexcept;
23  Range<float> getCoveringStops(float, float) const noexcept;
24  const expression::Expression& getExpression() const noexcept;
25 
29  std::shared_ptr<const expression::Expression> getSharedExpression() const noexcept;
30 
31  bool useIntegerZoom = false;
32 
33 protected:
34  std::shared_ptr<const expression::Expression> expression;
35  variant<std::nullptr_t, const expression::Interpolate*, const expression::Step*> zoomCurve;
39 };
40 
41 template <class T>
43 public:
44  // Second parameter to be used only for conversions from legacy functions.
45  PropertyExpression(std::unique_ptr<expression::Expression> expression_, std::optional<T> defaultValue_ = std::nullopt)
46  : PropertyExpressionBase(std::move(expression_)),
47  defaultValue(std::move(defaultValue_)) {
48  }
49 
50  T evaluate(const expression::EvaluationContext& context, T finalDefaultValue = T()) const {
51  const expression::EvaluationResult result = expression->evaluate(context);
52  if (result) {
53  const std::optional<T> typed = expression::fromExpressionValue<T>(*result);
54  return typed ? *typed : defaultValue ? *defaultValue : finalDefaultValue;
55  }
56  return defaultValue ? *defaultValue : finalDefaultValue;
57  }
58 
59  T evaluate(float zoom) const {
60  assert(!isZoomConstant());
61  assert(isFeatureConstant());
62  return evaluate(expression::EvaluationContext(zoom));
63  }
64 
65  T evaluate(const GeometryTileFeature& feature, T finalDefaultValue) const {
66  assert(isZoomConstant());
67  assert(!isFeatureConstant());
68  return evaluate(expression::EvaluationContext(&feature), finalDefaultValue);
69  }
70 
71  T evaluate(const GeometryTileFeature& feature,
72  const std::set<std::string>& availableImages,
73  T finalDefaultValue) const {
74  return evaluate(expression::EvaluationContext(&feature).withAvailableImages(&availableImages),
75  finalDefaultValue);
76  }
77 
78  T evaluate(const GeometryTileFeature& feature, const CanonicalTileID& canonical, T finalDefaultValue) const {
79  return evaluate(expression::EvaluationContext(&feature).withCanonicalTileID(&canonical), finalDefaultValue);
80  }
81 
82  T evaluate(const GeometryTileFeature& feature,
83  const std::set<std::string>& availableImages,
84  const CanonicalTileID& canonical,
85  T finalDefaultValue) const {
86  return evaluate(expression::EvaluationContext(&feature)
87  .withAvailableImages(&availableImages)
88  .withCanonicalTileID(&canonical),
89  finalDefaultValue);
90  }
91 
92  T evaluate(float zoom, const GeometryTileFeature& feature, T finalDefaultValue) const {
93  return evaluate(expression::EvaluationContext(zoom, &feature), finalDefaultValue);
94  }
95 
96  T evaluate(float zoom,
97  const GeometryTileFeature& feature,
98  const std::set<std::string>& availableImages,
99  T finalDefaultValue) const {
100  return evaluate(expression::EvaluationContext(zoom, &feature).withAvailableImages(&availableImages),
101  finalDefaultValue);
102  }
103 
104  T evaluate(float zoom,
105  const GeometryTileFeature& feature,
106  const std::set<std::string>& availableImages,
107  const CanonicalTileID& canonical,
108  T finalDefaultValue) const {
109  return evaluate(expression::EvaluationContext(zoom, &feature)
110  .withAvailableImages(&availableImages)
111  .withCanonicalTileID(&canonical),
112  finalDefaultValue);
113  }
114 
115  T evaluate(float zoom,
116  const GeometryTileFeature& feature,
117  const CanonicalTileID& canonical,
118  T finalDefaultValue) const {
119  return evaluate(expression::EvaluationContext(zoom, &feature).withCanonicalTileID(&canonical),
120  finalDefaultValue);
121  }
122 
123  T evaluate(float zoom, const GeometryTileFeature& feature, const FeatureState& state, T finalDefaultValue) const {
124  assert(!isFeatureConstant());
125  return evaluate(expression::EvaluationContext(zoom, &feature, &state), finalDefaultValue);
126  }
127 
128  std::vector<std::optional<T>> possibleOutputs() const {
129  return expression::fromExpressionValues<T>(expression->possibleOutputs());
130  }
131 
132  friend bool operator==(const PropertyExpression& lhs,
133  const PropertyExpression& rhs) {
134  return *lhs.expression == *rhs.expression;
135  }
136 
137 private:
138  std::optional<T> defaultValue;
139 };
140 
141 } // namespace style
142 } // namespace mbgl
variant< std::nullptr_t, const expression::Interpolate *, const expression::Step * > zoomCurve
float interpolationFactor(const Range< float > &, float) const noexcept
Range< float > getCoveringStops(float, float) const noexcept
std::shared_ptr< const expression::Expression > getSharedExpression() const noexcept
const expression::Expression & getExpression() const noexcept
bool isFeatureConstant() const noexcept
bool isZoomConstant() const noexcept
bool isRuntimeConstant() const noexcept
PropertyExpressionBase(std::unique_ptr< expression::Expression >)
std::shared_ptr< const expression::Expression > expression
T evaluate(const GeometryTileFeature &feature, T finalDefaultValue) const
T evaluate(float zoom, const GeometryTileFeature &feature, T finalDefaultValue) const
std::vector< std::optional< T > > possibleOutputs() const
PropertyExpression(std::unique_ptr< expression::Expression > expression_, std::optional< T > defaultValue_=std::nullopt)
T evaluate(const GeometryTileFeature &feature, const std::set< std::string > &availableImages, const CanonicalTileID &canonical, T finalDefaultValue) const
T evaluate(float zoom, const GeometryTileFeature &feature, const std::set< std::string > &availableImages, const CanonicalTileID &canonical, T finalDefaultValue) const
T evaluate(const GeometryTileFeature &feature, const std::set< std::string > &availableImages, T finalDefaultValue) const
T evaluate(const GeometryTileFeature &feature, const CanonicalTileID &canonical, T finalDefaultValue) const
friend bool operator==(const PropertyExpression &lhs, const PropertyExpression &rhs)
T evaluate(const expression::EvaluationContext &context, T finalDefaultValue=T()) const
T evaluate(float zoom, const GeometryTileFeature &feature, const std::set< std::string > &availableImages, T finalDefaultValue) const
T evaluate(float zoom, const GeometryTileFeature &feature, const CanonicalTileID &canonical, T finalDefaultValue) const
T evaluate(float zoom, const GeometryTileFeature &feature, const FeatureState &state, T finalDefaultValue) const
std::unique_ptr< Expression > zoom()
Definition: actor.hpp:15
mapbox::base::ValueObject FeatureState
Definition: feature.hpp:16
mapbox::util::variant< T... > variant
Definition: variant.hpp:17
Definition: tile_id.hpp:256