From 9a1bb1ad14e18433d4cc8e805af57a6014275561 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 30 Jul 2018 15:04:41 -0700 Subject: [core] Tweak conversions to reduce binary size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * return {} → return nullopt * error = { "..." } → error.message = "..." --- .../style/conversion/color_ramp_property_value.hpp | 32 +--------- include/mbgl/style/conversion/constant.hpp | 16 ++--- .../conversion/custom_geometry_source_options.hpp | 69 +--------------------- .../conversion/data_driven_property_value.hpp | 14 ++--- include/mbgl/style/conversion/function.hpp | 6 +- include/mbgl/style/conversion/property_value.hpp | 18 +++--- 6 files changed, 29 insertions(+), 126 deletions(-) (limited to 'include/mbgl') diff --git a/include/mbgl/style/conversion/color_ramp_property_value.hpp b/include/mbgl/style/conversion/color_ramp_property_value.hpp index 290ee6a56c..87049b043b 100644 --- a/include/mbgl/style/conversion/color_ramp_property_value.hpp +++ b/include/mbgl/style/conversion/color_ramp_property_value.hpp @@ -2,11 +2,6 @@ #include #include -#include -#include -#include -#include -#include namespace mbgl { namespace style { @@ -14,32 +9,7 @@ namespace conversion { template <> struct Converter { - optional operator()(const Convertible& value, Error& error, bool /* convertTokens */ = false) const { - using namespace mbgl::style::expression; - if (isUndefined(value)) { - return ColorRampPropertyValue(); - } else if (isExpression(value)) { - ParsingContext ctx(type::Color); - ParseResult expression = ctx.parseLayerPropertyExpression(value); - if (!expression) { - error = { ctx.getCombinedErrors() }; - return {}; - } - assert(*expression); - if (!isFeatureConstant(**expression)) { - error = { "property expressions not supported" }; - return {}; - } - if (!isZoomConstant(**expression)) { - error = { "zoom expressions not supported" }; - return {}; - } - return {ColorRampPropertyValue(std::move(*expression))}; - } else { - error = { "color ramp must be an expression" }; - return {}; - } - } + optional operator()(const Convertible& value, Error& error, bool /* convertTokens */ = false) const; }; } // namespace conversion diff --git a/include/mbgl/style/conversion/constant.hpp b/include/mbgl/style/conversion/constant.hpp index 7b3249da52..7d74ec42ce 100644 --- a/include/mbgl/style/conversion/constant.hpp +++ b/include/mbgl/style/conversion/constant.hpp @@ -33,14 +33,14 @@ struct Converter::value>> { optional operator()(const Convertible& value, Error& error) const { optional string = toString(value); if (!string) { - error = { "value must be a string" }; - return {}; + error.message = "value must be a string"; + return nullopt; } const auto result = Enum::toEnum(*string); if (!result) { - error = { "value must be a valid enumeration value" }; - return {}; + error.message = "value must be a valid enumeration value"; + return nullopt; } return *result; @@ -56,16 +56,16 @@ template struct Converter> { optional> operator()(const Convertible& value, Error& error) const { if (!isArray(value) || arrayLength(value) != N) { - error = { "value must be an array of " + util::toString(N) + " numbers" }; - return {}; + error.message = "value must be an array of " + util::toString(N) + " numbers"; + return nullopt; } std::array result; for (size_t i = 0; i < N; i++) { optional n = toNumber(arrayMember(value, i)); if (!n) { - error = { "value must be an array of " + util::toString(N) + " numbers" }; - return {}; + error.message = "value must be an array of " + util::toString(N) + " numbers"; + return nullopt; } result[i] = *n; } diff --git a/include/mbgl/style/conversion/custom_geometry_source_options.hpp b/include/mbgl/style/conversion/custom_geometry_source_options.hpp index dedecd1aa4..f0f505e54f 100644 --- a/include/mbgl/style/conversion/custom_geometry_source_options.hpp +++ b/include/mbgl/style/conversion/custom_geometry_source_options.hpp @@ -9,74 +9,7 @@ namespace conversion { template <> struct Converter { - - template - optional operator()(const V& value, Error& error) const { - CustomGeometrySource::Options options; - - const auto minzoomValue = objectMember(value, "minzoom"); - if (minzoomValue) { - if (toNumber(*minzoomValue)) { - options.zoomRange.min = static_cast(*toNumber(*minzoomValue)); - } else { - error = { "GeoJSON source minzoom value must be a number" }; - return {}; - } - } - - const auto maxzoomValue = objectMember(value, "maxzoom"); - if (maxzoomValue) { - if (toNumber(*maxzoomValue)) { - options.zoomRange.max = static_cast(*toNumber(*maxzoomValue)); - } else { - error = { "GeoJSON source maxzoom value must be a number" }; - return {}; - } - } - - const auto bufferValue = objectMember(value, "buffer"); - if (bufferValue) { - if (toNumber(*bufferValue)) { - options.tileOptions.buffer = static_cast(*toNumber(*bufferValue)); - } else { - error = { "GeoJSON source buffer value must be a number" }; - return {}; - } - } - - const auto toleranceValue = objectMember(value, "tolerance"); - if (toleranceValue) { - if (toNumber(*toleranceValue)) { - options.tileOptions.tolerance = static_cast(*toNumber(*toleranceValue)); - } else { - error = { "GeoJSON source tolerance value must be a number" }; - return {}; - } - } - - const auto wrapValue = objectMember(value, "wrap"); - if (wrapValue) { - if (toBool(*wrapValue)) { - options.tileOptions.wrap = static_cast(*toBool(*wrapValue)); - } else { - error = { "CustomGeometrySource TileOptions wrap value must be a boolean" }; - return {}; - } - } - - const auto clipValue = objectMember(value, "clip"); - if (clipValue) { - if (toBool(*clipValue)) { - options.tileOptions.clip = static_cast(*toBool(*clipValue)); - } else { - error = { "CustomGeometrySource TileOptiosn clip value must be a boolean" }; - return {}; - } - } - - return { options }; - } - + optional operator()(const Convertible& value, Error& error) const; }; } // namespace conversion diff --git a/include/mbgl/style/conversion/data_driven_property_value.hpp b/include/mbgl/style/conversion/data_driven_property_value.hpp index 59d197b216..2d8817ecf4 100644 --- a/include/mbgl/style/conversion/data_driven_property_value.hpp +++ b/include/mbgl/style/conversion/data_driven_property_value.hpp @@ -29,8 +29,8 @@ struct Converter> { ParsingContext ctx(valueTypeToExpressionType()); ParseResult parsed = ctx.parseLayerPropertyExpression(value); if (!parsed) { - error = { ctx.getCombinedErrors() }; - return {}; + error.message = ctx.getCombinedErrors(); + return nullopt; } expression = PropertyExpression(std::move(*parsed)); } else if (isObject(value)) { @@ -38,26 +38,26 @@ struct Converter> { } else { optional constant = convert(value, error); if (!constant) { - return {}; + return nullopt; } return convertTokens ? maybeConvertTokens(*constant) : DataDrivenPropertyValue(*constant); } if (!expression) { - return {}; + return nullopt; } else if (!(*expression).isFeatureConstant() || !(*expression).isZoomConstant()) { return { std::move(*expression) }; } else if ((*expression).getExpression().getKind() == Kind::Literal) { optional constant = fromExpressionValue( static_cast((*expression).getExpression()).getValue()); if (!constant) { - return {}; + return nullopt; } return DataDrivenPropertyValue(*constant); } else { assert(false); - error = { "expected a literal expression" }; - return {}; + error.message = "expected a literal expression"; + return nullopt; } } diff --git a/include/mbgl/style/conversion/function.hpp b/include/mbgl/style/conversion/function.hpp index 8799e9faa4..49825a3410 100644 --- a/include/mbgl/style/conversion/function.hpp +++ b/include/mbgl/style/conversion/function.hpp @@ -19,7 +19,7 @@ template optional> convertFunctionToExpression(const Convertible& value, Error& error, bool convertTokens) { auto expression = convertFunctionToExpression(expression::valueTypeToExpressionType(), value, error, convertTokens); if (!expression) { - return {}; + return nullopt; } optional defaultValue; @@ -28,8 +28,8 @@ optional> convertFunctionToExpression(const Convertible& v if (defaultValueValue) { defaultValue = convert(*defaultValueValue, error); if (!defaultValue) { - error = { R"(wrong type for "default": )" + error.message }; - return {}; + error.message = R"(wrong type for "default": )" + error.message; + return nullopt; } } diff --git a/include/mbgl/style/conversion/property_value.hpp b/include/mbgl/style/conversion/property_value.hpp index b03655a848..4d13144dd7 100644 --- a/include/mbgl/style/conversion/property_value.hpp +++ b/include/mbgl/style/conversion/property_value.hpp @@ -33,8 +33,8 @@ struct Converter> { ParsingContext ctx(valueTypeToExpressionType()); ParseResult parsed = ctx.parseLayerPropertyExpression(value); if (!parsed) { - error = { ctx.getCombinedErrors() }; - return {}; + error.message = ctx.getCombinedErrors(); + return nullopt; } expression = PropertyExpression(std::move(*parsed)); } else if (isObject(value)) { @@ -42,29 +42,29 @@ struct Converter> { } else { optional constant = convert(value, error); if (!constant) { - return {}; + return nullopt; } return { *constant }; } if (!expression) { - return {}; + return nullopt; } else if (!(*expression).isFeatureConstant()) { - error = { "data expressions not supported" }; - return {}; + error.message = "data expressions not supported"; + return nullopt; } else if (!(*expression).isZoomConstant()) { return { std::move(*expression) }; } else if ((*expression).getExpression().getKind() == Kind::Literal) { optional constant = fromExpressionValue( static_cast((*expression).getExpression()).getValue()); if (!constant) { - return {}; + return nullopt; } return PropertyValue(*constant); } else { assert(false); - error = { "expected a literal expression" }; - return {}; + error.message = "expected a literal expression"; + return nullopt; } } }; -- cgit v1.2.1