summaryrefslogtreecommitdiff
path: root/include/mbgl/style/function/source_function.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/mbgl/style/function/source_function.hpp')
-rw-r--r--include/mbgl/style/function/source_function.hpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/include/mbgl/style/function/source_function.hpp b/include/mbgl/style/function/source_function.hpp
new file mode 100644
index 0000000000..29b1067a19
--- /dev/null
+++ b/include/mbgl/style/function/source_function.hpp
@@ -0,0 +1,59 @@
+#pragma once
+
+#include <mbgl/style/function/exponential_stops.hpp>
+#include <mbgl/style/function/interval_stops.hpp>
+#include <mbgl/style/function/categorical_stops.hpp>
+#include <mbgl/style/function/identity_stops.hpp>
+#include <mbgl/tile/geometry_tile_data.hpp>
+#include <mbgl/util/interpolate.hpp>
+#include <mbgl/util/variant.hpp>
+
+#include <string>
+
+namespace mbgl {
+namespace style {
+
+template <class T>
+class SourceFunction {
+public:
+ using Stops = std::conditional_t<
+ util::Interpolatable<T>,
+ variant<
+ ExponentialStops<T>,
+ IntervalStops<T>,
+ CategoricalStops<T>,
+ IdentityStops<T>>,
+ variant<
+ IntervalStops<T>,
+ CategoricalStops<T>,
+ IdentityStops<T>>>;
+
+ SourceFunction(std::string property_, Stops stops_, optional<T> defaultValue_ = {})
+ : property(std::move(property_)),
+ stops(std::move(stops_)),
+ defaultValue(std::move(defaultValue_)) {
+ }
+
+ T evaluate(const GeometryTileFeature& feature, T finalDefaultValue) const {
+ optional<Value> v = feature.getValue(property);
+ if (!v) {
+ return defaultValue.value_or(finalDefaultValue);
+ }
+ return stops.match([&] (const auto& s) -> T {
+ return s.evaluate(*v).value_or(defaultValue.value_or(finalDefaultValue));
+ });
+ }
+
+ friend bool operator==(const SourceFunction& lhs,
+ const SourceFunction& rhs) {
+ return std::tie(lhs.property, lhs.stops, lhs.defaultValue)
+ == std::tie(rhs.property, rhs.stops, rhs.defaultValue);
+ }
+
+ std::string property;
+ Stops stops;
+ optional<T> defaultValue;
+};
+
+} // namespace style
+} // namespace mbgl