summaryrefslogtreecommitdiff
path: root/include/mbgl/style
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-01-31 15:44:18 +0200
committerJohn Firebaugh <john.firebaugh@gmail.com>2017-02-02 09:44:42 -0800
commit8a5bff8ee630673c6ebc496322eab94a41ae9353 (patch)
tree8bb6428cd9c3d591c237d77f94d4b0e56efb0ee0 /include/mbgl/style
parent141e995806576364d185626176c1b993fc519291 (diff)
downloadqtlocation-mapboxgl-8a5bff8ee630673c6ebc496322eab94a41ae9353.tar.gz
[core] default value support in categorical function conversion
Diffstat (limited to 'include/mbgl/style')
-rw-r--r--include/mbgl/style/conversion/function.hpp33
-rw-r--r--include/mbgl/style/function/camera_function.hpp2
-rw-r--r--include/mbgl/style/function/categorical_stops.hpp10
-rw-r--r--include/mbgl/style/function/composite_function.hpp23
-rw-r--r--include/mbgl/style/function/exponential_stops.hpp2
-rw-r--r--include/mbgl/style/function/identity_stops.hpp2
-rw-r--r--include/mbgl/style/function/interval_stops.hpp6
-rw-r--r--include/mbgl/style/function/source_function.hpp17
8 files changed, 64 insertions, 31 deletions
diff --git a/include/mbgl/style/conversion/function.hpp b/include/mbgl/style/conversion/function.hpp
index 90b4b95063..7d69fa55c5 100644
--- a/include/mbgl/style/conversion/function.hpp
+++ b/include/mbgl/style/conversion/function.hpp
@@ -198,6 +198,21 @@ struct Converter<CameraFunction<T>> {
}
};
+template <class T, class V>
+Result<optional<T>> convertDefaultValue(const V& value) {
+ auto defaultValueValue = objectMember(value, "defaultValue");
+ if (!defaultValueValue) {
+ return {};
+ }
+
+ auto defaultValue = convert<T>(*defaultValueValue);
+ if (!defaultValue) {
+ return Error { "wrong type for \"default\": " + defaultValue.error().message };
+ }
+
+ return *defaultValue;
+}
+
template <class T>
struct Converter<SourceFunction<T>> {
template <class V>
@@ -221,7 +236,12 @@ struct Converter<SourceFunction<T>> {
return stops.error();
}
- return SourceFunction<T>(*propertyString, *stops);
+ auto defaultValue = convertDefaultValue<T>(value);
+ if (!defaultValue) {
+ return defaultValue.error();
+ }
+
+ return SourceFunction<T>(*propertyString, *stops, *defaultValue);
}
};
@@ -280,6 +300,11 @@ struct Converter<CompositeFunction<T>> {
return Error { "function property must be a string" };
}
+ auto defaultValue = convertDefaultValue<T>(value);
+ if (!defaultValue) {
+ return defaultValue.error();
+ }
+
std::string type = "exponential";
auto typeValue = objectMember(value, "type");
if (typeValue && toString(*typeValue)) {
@@ -305,7 +330,7 @@ struct Converter<CompositeFunction<T>> {
inner.stops.emplace(stop.first.second, stop.second);
}
- return CompositeFunction<T>(*propertyString, convertedStops);
+ return CompositeFunction<T>(*propertyString, convertedStops, *defaultValue);
} else if (type == "interval") {
auto stops = convertStops<CompositeValue<float>, T>(value);
if (!stops) {
@@ -318,7 +343,7 @@ struct Converter<CompositeFunction<T>> {
inner.stops.emplace(stop.first.second, stop.second);
}
- return CompositeFunction<T>(*propertyString, convertedStops);
+ return CompositeFunction<T>(*propertyString, convertedStops, *defaultValue);
} else if (type == "categorical") {
auto stops = convertStops<CompositeValue<CategoricalValue>, T>(value);
if (!stops) {
@@ -331,7 +356,7 @@ struct Converter<CompositeFunction<T>> {
inner.stops.emplace(stop.first.second, stop.second);
}
- return CompositeFunction<T>(*propertyString, convertedStops);
+ return CompositeFunction<T>(*propertyString, convertedStops, *defaultValue);
} else {
return Error { "unsupported function type" };
}
diff --git a/include/mbgl/style/function/camera_function.hpp b/include/mbgl/style/function/camera_function.hpp
index a96978939d..5636b1663c 100644
--- a/include/mbgl/style/function/camera_function.hpp
+++ b/include/mbgl/style/function/camera_function.hpp
@@ -25,7 +25,7 @@ public:
T evaluate(float zoom) const {
return stops.match([&] (const auto& s) {
- return s.evaluate(Value(double(zoom)));
+ return s.evaluate(Value(double(zoom))).value_or(T());
});
}
diff --git a/include/mbgl/style/function/categorical_stops.hpp b/include/mbgl/style/function/categorical_stops.hpp
index 11ec2a0e5a..c8505115ab 100644
--- a/include/mbgl/style/function/categorical_stops.hpp
+++ b/include/mbgl/style/function/categorical_stops.hpp
@@ -21,20 +21,18 @@ public:
using Stops = std::map<CategoricalValue, T>;
Stops stops;
- T defaultValue;
CategoricalStops() = default;
- CategoricalStops(Stops stops_, T defaultValue_ = T())
- : stops(std::move(stops_)),
- defaultValue(std::move(defaultValue_)) {
+ CategoricalStops(Stops stops_)
+ : stops(std::move(stops_)) {
assert(stops.size() > 0);
}
- T evaluate(const Value&) const;
+ optional<T> evaluate(const Value&) const;
friend bool operator==(const CategoricalStops& lhs,
const CategoricalStops& rhs) {
- return lhs.stops == rhs.stops && lhs.defaultValue == rhs.defaultValue;
+ return lhs.stops == rhs.stops;
}
};
diff --git a/include/mbgl/style/function/composite_function.hpp b/include/mbgl/style/function/composite_function.hpp
index 169a455435..f42a5f06f4 100644
--- a/include/mbgl/style/function/composite_function.hpp
+++ b/include/mbgl/style/function/composite_function.hpp
@@ -43,9 +43,10 @@ public:
std::map<float, IntervalStops<T>>,
std::map<float, CategoricalStops<T>>>>;
- CompositeFunction(std::string property_, Stops stops_)
+ CompositeFunction(std::string property_, Stops stops_, optional<T> defaultValue_ = {})
: property(std::move(property_)),
- stops(std::move(stops_)) {
+ stops(std::move(stops_)),
+ defaultValue(std::move(defaultValue_)) {
}
std::tuple<Range<float>, Range<InnerStops>>
@@ -73,13 +74,17 @@ public:
}
Range<T> evaluate(Range<InnerStops> coveringStops,
- const GeometryTileFeature& feature) const {
+ const GeometryTileFeature& feature,
+ T finalDefaultValue) const {
optional<Value> v = feature.getValue(property);
if (!v) {
- return { T(), T() };
+ return {
+ defaultValue.value_or(finalDefaultValue),
+ defaultValue.value_or(finalDefaultValue)
+ };
}
auto eval = [&] (const auto& s) {
- return s.evaluate(*v);
+ return s.evaluate(*v).value_or(defaultValue.value_or(finalDefaultValue));
};
return Range<T> {
coveringStops.min.match(eval),
@@ -87,9 +92,9 @@ public:
};
}
- T evaluate(float zoom, const GeometryTileFeature& feature) const {
+ T evaluate(float zoom, const GeometryTileFeature& feature, T finalDefaultValue) const {
std::tuple<Range<float>, Range<InnerStops>> ranges = coveringRanges(zoom);
- Range<T> resultRange = evaluate(std::get<1>(ranges), feature);
+ Range<T> resultRange = evaluate(std::get<1>(ranges), feature, finalDefaultValue);
return util::interpolate(
resultRange.min,
resultRange.max,
@@ -98,11 +103,13 @@ public:
friend bool operator==(const CompositeFunction& lhs,
const CompositeFunction& 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<T> defaultValue;
};
} // namespace style
diff --git a/include/mbgl/style/function/exponential_stops.hpp b/include/mbgl/style/function/exponential_stops.hpp
index 7bd8783614..051f5aa9aa 100644
--- a/include/mbgl/style/function/exponential_stops.hpp
+++ b/include/mbgl/style/function/exponential_stops.hpp
@@ -22,7 +22,7 @@ public:
base(base_) {
}
- T evaluate(const Value& value) const {
+ optional<T> evaluate(const Value& value) const {
if (stops.empty()) {
assert(false);
return T();
diff --git a/include/mbgl/style/function/identity_stops.hpp b/include/mbgl/style/function/identity_stops.hpp
index 4e199f2e15..741ebbbe0c 100644
--- a/include/mbgl/style/function/identity_stops.hpp
+++ b/include/mbgl/style/function/identity_stops.hpp
@@ -8,7 +8,7 @@ namespace style {
template <class T>
class IdentityStops {
public:
- T evaluate(const Value&) const;
+ optional<T> evaluate(const Value&) const;
friend bool operator==(const IdentityStops&,
const IdentityStops&) {
diff --git a/include/mbgl/style/function/interval_stops.hpp b/include/mbgl/style/function/interval_stops.hpp
index cf879d730b..50f2b48453 100644
--- a/include/mbgl/style/function/interval_stops.hpp
+++ b/include/mbgl/style/function/interval_stops.hpp
@@ -18,15 +18,15 @@ public:
: stops(std::move(stops_)) {
}
- T evaluate(const Value& value) const {
+ optional<T> evaluate(const Value& value) const {
if (stops.empty()) {
assert(false);
- return T();
+ return {};
}
optional<float> z = numericValue<float>(value);
if (!z) {
- return T();
+ return {};
}
auto it = stops.upper_bound(*z);
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<T>,
IdentityStops<T>>>;
- SourceFunction(std::string property_, Stops stops_)
+ SourceFunction(std::string property_, Stops stops_, optional<T> 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<Value> 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<T> defaultValue;
};
} // namespace style