Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[core] Tweak conversions to reduce binary size #12511

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,10 @@ set(MBGL_CORE_FILES
include/mbgl/style/conversion/source.hpp
include/mbgl/style/conversion/tileset.hpp
include/mbgl/style/conversion/transition_options.hpp
src/mbgl/style/conversion/color_ramp_property_value.cpp
src/mbgl/style/conversion/constant.cpp
src/mbgl/style/conversion/coordinate.cpp
src/mbgl/style/conversion/custom_geometry_source_options.cpp
src/mbgl/style/conversion/filter.cpp
src/mbgl/style/conversion/function.cpp
src/mbgl/style/conversion/geojson.cpp
Expand Down
32 changes: 1 addition & 31 deletions include/mbgl/style/conversion/color_ramp_property_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,14 @@

#include <mbgl/style/color_ramp_property_value.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/is_constant.hpp>
#include <mbgl/style/expression/is_expression.hpp>
#include <mbgl/style/expression/parsing_context.hpp>

namespace mbgl {
namespace style {
namespace conversion {

template <>
struct Converter<ColorRampPropertyValue> {
optional<ColorRampPropertyValue> 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<ColorRampPropertyValue> operator()(const Convertible& value, Error& error, bool /* convertTokens */ = false) const;
};

} // namespace conversion
Expand Down
16 changes: 8 additions & 8 deletions include/mbgl/style/conversion/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ struct Converter<T, typename std::enable_if_t<std::is_enum<T>::value>> {
optional<T> operator()(const Convertible& value, Error& error) const {
optional<std::string> 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<T>::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;
Expand All @@ -56,16 +56,16 @@ template <size_t N>
struct Converter<std::array<float, N>> {
optional<std::array<float, N>> 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<float, N> result;
for (size_t i = 0; i < N; i++) {
optional<float> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,74 +9,7 @@ namespace conversion {

template <>
struct Converter<CustomGeometrySource::Options> {

template <class V>
optional<CustomGeometrySource::Options> 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<uint8_t>(*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<uint8_t>(*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<uint16_t>(*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<double>(*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<bool>(*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<double>(*toBool(*clipValue));
} else {
error = { "CustomGeometrySource TileOptiosn clip value must be a boolean" };
return {};
}
}

return { options };
}

optional<CustomGeometrySource::Options> operator()(const Convertible& value, Error& error) const;
};

} // namespace conversion
Expand Down
14 changes: 7 additions & 7 deletions include/mbgl/style/conversion/data_driven_property_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,35 @@ struct Converter<DataDrivenPropertyValue<T>> {
ParsingContext ctx(valueTypeToExpressionType<T>());
ParseResult parsed = ctx.parseLayerPropertyExpression(value);
if (!parsed) {
error = { ctx.getCombinedErrors() };
return {};
error.message = ctx.getCombinedErrors();
return nullopt;
}
expression = PropertyExpression<T>(std::move(*parsed));
} else if (isObject(value)) {
expression = convertFunctionToExpression<T>(value, error, convertTokens);
} else {
optional<T> constant = convert<T>(value, error);
if (!constant) {
return {};
return nullopt;
}
return convertTokens ? maybeConvertTokens(*constant) : DataDrivenPropertyValue<T>(*constant);
}

if (!expression) {
return {};
return nullopt;
} else if (!(*expression).isFeatureConstant() || !(*expression).isZoomConstant()) {
return { std::move(*expression) };
} else if ((*expression).getExpression().getKind() == Kind::Literal) {
optional<T> constant = fromExpressionValue<T>(
static_cast<const Literal&>((*expression).getExpression()).getValue());
if (!constant) {
return {};
return nullopt;
}
return DataDrivenPropertyValue<T>(*constant);
} else {
assert(false);
error = { "expected a literal expression" };
return {};
error.message = "expected a literal expression";
return nullopt;
}
}

Expand Down
6 changes: 3 additions & 3 deletions include/mbgl/style/conversion/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ template <class T>
optional<PropertyExpression<T>> convertFunctionToExpression(const Convertible& value, Error& error, bool convertTokens) {
auto expression = convertFunctionToExpression(expression::valueTypeToExpressionType<T>(), value, error, convertTokens);
if (!expression) {
return {};
return nullopt;
}

optional<T> defaultValue;
Expand All @@ -28,8 +28,8 @@ optional<PropertyExpression<T>> convertFunctionToExpression(const Convertible& v
if (defaultValueValue) {
defaultValue = convert<T>(*defaultValueValue, error);
if (!defaultValue) {
error = { R"(wrong type for "default": )" + error.message };
return {};
error.message = R"(wrong type for "default": )" + error.message;
return nullopt;
}
}

Expand Down
18 changes: 9 additions & 9 deletions include/mbgl/style/conversion/property_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,38 @@ struct Converter<PropertyValue<T>> {
ParsingContext ctx(valueTypeToExpressionType<T>());
ParseResult parsed = ctx.parseLayerPropertyExpression(value);
if (!parsed) {
error = { ctx.getCombinedErrors() };
return {};
error.message = ctx.getCombinedErrors();
return nullopt;
}
expression = PropertyExpression<T>(std::move(*parsed));
} else if (isObject(value)) {
expression = convertFunctionToExpression<T>(value, error, false);
} else {
optional<T> constant = convert<T>(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<T> constant = fromExpressionValue<T>(
static_cast<const Literal&>((*expression).getExpression()).getValue());
if (!constant) {
return {};
return nullopt;
}
return PropertyValue<T>(*constant);
} else {
assert(false);
error = { "expected a literal expression" };
return {};
error.message = "expected a literal expression";
return nullopt;
}
}
};
Expand Down
42 changes: 42 additions & 0 deletions src/mbgl/style/conversion/color_ramp_property_value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <mbgl/style/conversion/color_ramp_property_value.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/is_constant.hpp>
#include <mbgl/style/expression/is_expression.hpp>
#include <mbgl/style/expression/parsing_context.hpp>

namespace mbgl {
namespace style {
namespace conversion {

optional<ColorRampPropertyValue> Converter<ColorRampPropertyValue>::operator()(const Convertible& value, Error& error, bool) 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.message = ctx.getCombinedErrors();
return nullopt;
}
assert(*expression);
if (!isFeatureConstant(**expression)) {
error.message = "data expressions not supported";
return nullopt;
}
if (!isZoomConstant(**expression)) {
error.message = "zoom expressions not supported";
return nullopt;
}
return ColorRampPropertyValue(std::move(*expression));
} else {
error.message = "color ramp must be an expression";
return nullopt;
}
}

} // namespace conversion
} // namespace style
} // namespace mbgl
Loading