From f35ca0d9dfc8a6ba88273edbeda43e633ae0adce Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 7 Feb 2017 12:35:27 -0800 Subject: [core, ios, macos] Refactor composite stop types and conversions --- include/mbgl/style/conversion/function.hpp | 127 ++++++++++++++++------------- 1 file changed, 72 insertions(+), 55 deletions(-) (limited to 'include/mbgl/style/conversion') 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 @@ -282,6 +282,72 @@ struct Converter> { } }; +template +struct Converter> { + static constexpr const char * type = "exponential"; + + template + Result> operator()(const V& value) const { + auto stops = convertStops, 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> convertedStops; + for (const auto& stop : *stops) { + convertedStops[stop.first.first].emplace(stop.first.second, stop.second); + } + + return CompositeExponentialStops(convertedStops, base); + } +}; + +template +struct Converter> { + static constexpr const char * type = "interval"; + + template + Result> operator()(const V& value) const { + auto stops = convertStops, T>(value); + if (!stops) { + return stops.error(); + } + + std::map> convertedStops; + for (const auto& stop : *stops) { + convertedStops[stop.first.first].emplace(stop.first.second, stop.second); + } + + return CompositeIntervalStops(convertedStops); + } +}; + +template +struct Converter> { + static constexpr const char * type = "categorical"; + + template + Result> operator()(const V& value) const { + auto stops = convertStops, T>(value); + if (!stops) { + return stops.error(); + } + + std::map> convertedStops; + for (const auto& stop : *stops) { + convertedStops[stop.first.first].emplace(stop.first.second, stop.second); + } + + return CompositeCategoricalStops(convertedStops); + } +}; + template struct Converter> { template @@ -300,66 +366,17 @@ struct Converter> { return Error { "function property must be a string" }; } + auto stops = StopsConverter::Stops>()(value); + if (!stops) { + return stops.error(); + } + auto defaultValue = convertDefaultValue(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, 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> 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(*propertyString, convertedStops, *defaultValue); - } else if (type == "interval") { - auto stops = convertStops, T>(value); - if (!stops) { - return stops.error(); - } - - std::map> convertedStops; - for (const auto& stop : *stops) { - auto& inner = convertedStops[stop.first.first]; - inner.stops.emplace(stop.first.second, stop.second); - } - - return CompositeFunction(*propertyString, convertedStops, *defaultValue); - } else if (type == "categorical") { - auto stops = convertStops, T>(value); - if (!stops) { - return stops.error(); - } - - std::map> convertedStops; - for (const auto& stop : *stops) { - auto& inner = convertedStops[stop.first.first]; - inner.stops.emplace(stop.first.second, stop.second); - } - - return CompositeFunction(*propertyString, convertedStops, *defaultValue); - } else { - return Error { "unsupported function type" }; - } + return CompositeFunction(*propertyString, *stops, *defaultValue); } }; -- cgit v1.2.1