From 8a5bff8ee630673c6ebc496322eab94a41ae9353 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Tue, 31 Jan 2017 15:44:18 +0200 Subject: [core] default value support in categorical function conversion --- include/mbgl/style/function/source_function.hpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'include/mbgl/style/function/source_function.hpp') diff --git a/include/mbgl/style/function/source_function.hpp b/include/mbgl/style/function/source_function.hpp index e998be082a..29b1067a19 100644 --- a/include/mbgl/style/function/source_function.hpp +++ b/include/mbgl/style/function/source_function.hpp @@ -28,28 +28,31 @@ public: CategoricalStops, IdentityStops>>; - SourceFunction(std::string property_, Stops stops_) + SourceFunction(std::string property_, Stops stops_, optional defaultValue_ = {}) : property(std::move(property_)), - stops(std::move(stops_)) { + stops(std::move(stops_)), + defaultValue(std::move(defaultValue_)) { } - T evaluate(const GeometryTileFeature& feature) const { + T evaluate(const GeometryTileFeature& feature, T finalDefaultValue) const { optional v = feature.getValue(property); if (!v) { - return T(); + return defaultValue.value_or(finalDefaultValue); } - return stops.match([&] (const auto& s) { - return s.evaluate(*v); + 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 lhs.property == rhs.property && lhs.stops == rhs.stops; + return std::tie(lhs.property, lhs.stops, lhs.defaultValue) + == std::tie(rhs.property, rhs.stops, rhs.defaultValue); } std::string property; Stops stops; + optional defaultValue; }; } // namespace style -- cgit v1.2.1