summaryrefslogtreecommitdiff
path: root/include/mbgl/style/conversion
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2017-02-07 12:35:27 -0800
committerJohn Firebaugh <john.firebaugh@gmail.com>2017-02-09 11:16:26 -0600
commitf35ca0d9dfc8a6ba88273edbeda43e633ae0adce (patch)
tree74c11b9c7125c22516a5cbc46322763a72964c43 /include/mbgl/style/conversion
parent9df24addfbfd922baada7d9778f402b91e69ed33 (diff)
downloadqtlocation-mapboxgl-f35ca0d9dfc8a6ba88273edbeda43e633ae0adce.tar.gz
[core, ios, macos] Refactor composite stop types and conversions
Diffstat (limited to 'include/mbgl/style/conversion')
-rw-r--r--include/mbgl/style/conversion/function.hpp127
1 files changed, 72 insertions, 55 deletions
diff --git a/include/mbgl/style/conversion/function.hpp b/include/mbgl/style/conversion/function.hpp
index 7d69fa55c5..1f7a3fe778 100644
--- a/include/mbgl/style/conversion/function.hpp
+++ b/include/mbgl/style/conversion/function.hpp
@@ -283,6 +283,72 @@ struct Converter<CompositeValue<S>> {
};
template <class T>
+struct Converter<CompositeExponentialStops<T>> {
+ static constexpr const char * type = "exponential";
+
+ template <class V>
+ Result<CompositeExponentialStops<T>> operator()(const V& value) const {
+ auto stops = convertStops<CompositeValue<float>, T>(value);
+ if (!stops) {
+ return stops.error();
+ }
+
+ auto base = 1.0f;
+ auto baseValue = objectMember(value, "base");
+ if (baseValue && toNumber(*baseValue)) {
+ base = *toNumber(*baseValue);
+ }
+
+ std::map<float, std::map<float, T>> convertedStops;
+ for (const auto& stop : *stops) {
+ convertedStops[stop.first.first].emplace(stop.first.second, stop.second);
+ }
+
+ return CompositeExponentialStops<T>(convertedStops, base);
+ }
+};
+
+template <class T>
+struct Converter<CompositeIntervalStops<T>> {
+ static constexpr const char * type = "interval";
+
+ template <class V>
+ Result<CompositeIntervalStops<T>> operator()(const V& value) const {
+ auto stops = convertStops<CompositeValue<float>, T>(value);
+ if (!stops) {
+ return stops.error();
+ }
+
+ std::map<float, std::map<float, T>> convertedStops;
+ for (const auto& stop : *stops) {
+ convertedStops[stop.first.first].emplace(stop.first.second, stop.second);
+ }
+
+ return CompositeIntervalStops<T>(convertedStops);
+ }
+};
+
+template <class T>
+struct Converter<CompositeCategoricalStops<T>> {
+ static constexpr const char * type = "categorical";
+
+ template <class V>
+ Result<CompositeCategoricalStops<T>> operator()(const V& value) const {
+ auto stops = convertStops<CompositeValue<CategoricalValue>, T>(value);
+ if (!stops) {
+ return stops.error();
+ }
+
+ std::map<float, std::map<CategoricalValue, T>> convertedStops;
+ for (const auto& stop : *stops) {
+ convertedStops[stop.first.first].emplace(stop.first.second, stop.second);
+ }
+
+ return CompositeCategoricalStops<T>(convertedStops);
+ }
+};
+
+template <class T>
struct Converter<CompositeFunction<T>> {
template <class V>
Result<CompositeFunction<T>> operator()(const V& value) const {
@@ -300,66 +366,17 @@ struct Converter<CompositeFunction<T>> {
return Error { "function property must be a string" };
}
+ auto stops = StopsConverter<T, typename CompositeFunction<T>::Stops>()(value);
+ if (!stops) {
+ return stops.error();
+ }
+
auto defaultValue = convertDefaultValue<T>(value);
if (!defaultValue) {
return defaultValue.error();
}
- std::string type = "exponential";
- auto typeValue = objectMember(value, "type");
- if (typeValue && toString(*typeValue)) {
- type = *toString(*typeValue);
- }
-
- if (type == "exponential") {
- auto stops = convertStops<CompositeValue<float>, T>(value);
- if (!stops) {
- return stops.error();
- }
-
- auto base = 1.0f;
- auto baseValue = objectMember(value, "base");
- if (baseValue && toNumber(*baseValue)) {
- base = *toNumber(*baseValue);
- }
-
- std::map<float, ExponentialStops<T>> convertedStops;
- for (const auto& stop : *stops) {
- auto& inner = convertedStops[stop.first.first];
- inner.base = base;
- inner.stops.emplace(stop.first.second, stop.second);
- }
-
- return CompositeFunction<T>(*propertyString, convertedStops, *defaultValue);
- } else if (type == "interval") {
- auto stops = convertStops<CompositeValue<float>, T>(value);
- if (!stops) {
- return stops.error();
- }
-
- std::map<float, IntervalStops<T>> convertedStops;
- for (const auto& stop : *stops) {
- auto& inner = convertedStops[stop.first.first];
- inner.stops.emplace(stop.first.second, stop.second);
- }
-
- return CompositeFunction<T>(*propertyString, convertedStops, *defaultValue);
- } else if (type == "categorical") {
- auto stops = convertStops<CompositeValue<CategoricalValue>, T>(value);
- if (!stops) {
- return stops.error();
- }
-
- std::map<float, CategoricalStops<T>> convertedStops;
- for (const auto& stop : *stops) {
- auto& inner = convertedStops[stop.first.first];
- inner.stops.emplace(stop.first.second, stop.second);
- }
-
- return CompositeFunction<T>(*propertyString, convertedStops, *defaultValue);
- } else {
- return Error { "unsupported function type" };
- }
+ return CompositeFunction<T>(*propertyString, *stops, *defaultValue);
}
};