diff options
author | Anand Thakker <github@anandthakker.net> | 2017-09-14 12:13:10 -0400 |
---|---|---|
committer | Anand Thakker <github@anandthakker.net> | 2017-10-13 12:50:51 -0400 |
commit | 0aef3df72427a9b0290e659b9a8172ee36829bb2 (patch) | |
tree | e74c8ebc9a730e77f743050d1d57dd2869720aac | |
parent | 9a102c71da1366dd117e309096abfcf98f984f90 (diff) | |
download | qtlocation-mapboxgl-0aef3df72427a9b0290e659b9a8172ee36829bb2.tar.gz |
CompoundExpressionRegistry::create => standalone function
-rw-r--r-- | include/mbgl/style/expression/compound_expression.hpp | 22 | ||||
-rw-r--r-- | include/mbgl/style/expression/parse.hpp | 2 | ||||
-rw-r--r-- | include/mbgl/style/expression/parse/compound_expression.hpp | 2 | ||||
-rw-r--r-- | include/mbgl/style/function/convert.hpp | 10 | ||||
-rw-r--r-- | src/mbgl/style/expression/compound_expression.cpp | 163 |
5 files changed, 100 insertions, 99 deletions
diff --git a/include/mbgl/style/expression/compound_expression.hpp b/include/mbgl/style/expression/compound_expression.hpp index 2fc43b4ba8..f9c9a6839b 100644 --- a/include/mbgl/style/expression/compound_expression.hpp +++ b/include/mbgl/style/expression/compound_expression.hpp @@ -115,22 +115,16 @@ private: struct CompoundExpressionRegistry { using Definition = std::vector<std::unique_ptr<detail::SignatureBase>>; static std::unordered_map<std::string, Definition> definitions; - - static ParseResult create(const std::string& name, - const Definition& definition, - std::vector<std::unique_ptr<Expression>> args, - ParsingContext ctx); - - - static ParseResult create(const std::string& name, - std::vector<std::unique_ptr<Expression>> args, - ParsingContext ctx) - { - return create(name, definitions.at(name), std::move(args), ctx); - } - }; +ParseResult createCompoundExpression(const std::string& name, + const CompoundExpressionRegistry::Definition& definition, + std::vector<std::unique_ptr<Expression>> args, + ParsingContext ctx); + +ParseResult createCompoundExpression(const std::string& name, + std::vector<std::unique_ptr<Expression>> args, + ParsingContext ctx); } // namespace expression } // namespace style diff --git a/include/mbgl/style/expression/parse.hpp b/include/mbgl/style/expression/parse.hpp index 27c7160698..2b5bab7750 100644 --- a/include/mbgl/style/expression/parse.hpp +++ b/include/mbgl/style/expression/parse.hpp @@ -101,7 +101,7 @@ ParseResult parseExpression(const V& value, ParsingContext context) auto wrapForType = [&](const std::string& wrapper, std::unique_ptr<Expression> expression) { std::vector<std::unique_ptr<Expression>> args; args.push_back(std::move(expression)); - return CompoundExpressionRegistry::create(wrapper, std::move(args), context); + return createCompoundExpression(wrapper, std::move(args), context); }; const type::Type actual = (*parsed)->getType(); diff --git a/include/mbgl/style/expression/parse/compound_expression.hpp b/include/mbgl/style/expression/parse/compound_expression.hpp index 3c5f89c1ab..e9cc577642 100644 --- a/include/mbgl/style/expression/parse/compound_expression.hpp +++ b/include/mbgl/style/expression/parse/compound_expression.hpp @@ -47,7 +47,7 @@ struct ParseCompoundExpression { } args.push_back(std::move(*parsed)); } - return CompoundExpressionRegistry::create(name, definition, std::move(args), ctx); + return createCompoundExpression(name, definition, std::move(args), ctx); } }; diff --git a/include/mbgl/style/function/convert.hpp b/include/mbgl/style/function/convert.hpp index ac5ee382fb..1082b06971 100644 --- a/include/mbgl/style/function/convert.hpp +++ b/include/mbgl/style/function/convert.hpp @@ -57,16 +57,16 @@ struct Convert { static std::unique_ptr<Expression> makeGet(const std::string& type, const std::string& property, ParsingContext ctx) { std::vector<std::unique_ptr<Expression>> getArgs; getArgs.push_back(makeLiteral(property)); - ParseResult get = CompoundExpressionRegistry::create("get", std::move(getArgs), ctx); + ParseResult get = createCompoundExpression("get", std::move(getArgs), ctx); std::vector<std::unique_ptr<Expression>> assertionArgs; assertionArgs.push_back(std::move(*get)); - return std::move(*(CompoundExpressionRegistry::create(type, std::move(assertionArgs), ctx))); + return std::move(*(createCompoundExpression(type, std::move(assertionArgs), ctx))); } static std::unique_ptr<Expression> makeZoom(ParsingContext ctx) { - return std::move(*(CompoundExpressionRegistry::create("zoom", std::vector<std::unique_ptr<Expression>>(), ctx))); + return std::move(*(createCompoundExpression("zoom", std::vector<std::unique_ptr<Expression>>(), ctx))); } static std::unique_ptr<Expression> makeError(std::string message) { @@ -337,14 +337,14 @@ struct Convert { [&] (const type::ColorType&) { std::vector<std::unique_ptr<Expression>> args; args.push_back(makeGet("string", property, ParsingContext(errors))); - ParseResult color = CompoundExpressionRegistry::create("to-color", std::move(args), ParsingContext(errors)); + ParseResult color = createCompoundExpression("to-color", std::move(args), ParsingContext(errors)); assert(color); return std::move(*color); }, [&] (const type::Array& arr) { std::vector<std::unique_ptr<Expression>> getArgs; getArgs.push_back(makeLiteral(property)); - ParseResult get = CompoundExpressionRegistry::create("get", std::move(getArgs), ParsingContext(errors)); + ParseResult get = createCompoundExpression("get", std::move(getArgs), ParsingContext(errors)); return std::make_unique<ArrayAssertion>(arr, std::move(*get)); }, [&] (const auto&) -> std::unique_ptr<Expression> { diff --git a/src/mbgl/style/expression/compound_expression.cpp b/src/mbgl/style/expression/compound_expression.cpp index 3fdf3c8c17..295920909e 100644 --- a/src/mbgl/style/expression/compound_expression.cpp +++ b/src/mbgl/style/expression/compound_expression.cpp @@ -179,84 +179,6 @@ struct Signature<Lambda, std::enable_if_t<std::is_class<Lambda>::value>> } // namespace detail - -ParseResult CompoundExpressionRegistry::create(const std::string& name, - const Definition& definition, - std::vector<std::unique_ptr<Expression>> args, - ParsingContext ctx) -{ - std::vector<ParsingError> currentSignatureErrors; - - ParsingContext signatureContext(currentSignatureErrors); - signatureContext.key = ctx.key; - - for (const std::unique_ptr<detail::SignatureBase>& signature : definition) { - currentSignatureErrors.clear(); - - - if (signature->params.is<std::vector<type::Type>>()) { - const std::vector<type::Type>& params = signature->params.get<std::vector<type::Type>>(); - if (params.size() != args.size()) { - signatureContext.error( - "Expected " + std::to_string(params.size()) + - " arguments, but found " + std::to_string(args.size()) + " instead." - ); - continue; - } - - for (std::size_t i = 0; i < args.size(); i++) { - const std::unique_ptr<Expression>& arg = args[i]; - checkSubtype(params.at(i), arg->getType(), ParsingContext(signatureContext, i + 1)); - } - } else if (signature->params.is<VarargsType>()) { - const type::Type& paramType = signature->params.get<VarargsType>().type; - for (std::size_t i = 0; i < args.size(); i++) { - const std::unique_ptr<Expression>& arg = args[i]; - checkSubtype(paramType, arg->getType(), ParsingContext(signatureContext, i + 1)); - } - } - - if (currentSignatureErrors.size() == 0) { - return ParseResult(signature->makeExpression(name, std::move(args))); - } - } - - if (definition.size() == 1) { - ctx.errors.insert(ctx.errors.end(), currentSignatureErrors.begin(), currentSignatureErrors.end()); - } else { - std::string signatures; - for (const auto& signature : definition) { - signatures += (signatures.size() > 0 ? " | " : ""); - signature->params.match( - [&](const VarargsType& varargs) { - signatures += "(" + toString(varargs.type) + ")"; - }, - [&](const std::vector<type::Type>& params) { - signatures += "("; - bool first = true; - for (const type::Type& param : params) { - if (!first) signatures += ", "; - signatures += toString(param); - first = false; - } - signatures += ")"; - } - ); - - } - std::string actualTypes; - for (const auto& arg : args) { - if (actualTypes.size() > 0) { - actualTypes += ", "; - } - actualTypes += toString(arg->getType()); - } - ctx.error("Expected arguments of type " + signatures + ", but found (" + actualTypes + ") instead."); - } - - return ParseResult(); -} - using Definition = CompoundExpressionRegistry::Definition; // Helper for creating expression Definition from one or more lambdas @@ -618,6 +540,91 @@ std::unordered_map<std::string, Definition> CompoundExpressionRegistry::definiti }) ); + +ParseResult createCompoundExpression(const std::string& name, + std::vector<std::unique_ptr<Expression>> args, + ParsingContext ctx) +{ + return createCompoundExpression(name, CompoundExpressionRegistry::definitions.at(name), std::move(args), ctx); +} + +ParseResult createCompoundExpression(const std::string& name, + const Definition& definition, + std::vector<std::unique_ptr<Expression>> args, + ParsingContext ctx) +{ + std::vector<ParsingError> currentSignatureErrors; + + ParsingContext signatureContext(currentSignatureErrors); + signatureContext.key = ctx.key; + + for (const std::unique_ptr<detail::SignatureBase>& signature : definition) { + currentSignatureErrors.clear(); + + + if (signature->params.is<std::vector<type::Type>>()) { + const std::vector<type::Type>& params = signature->params.get<std::vector<type::Type>>(); + if (params.size() != args.size()) { + signatureContext.error( + "Expected " + std::to_string(params.size()) + + " arguments, but found " + std::to_string(args.size()) + " instead." + ); + continue; + } + + for (std::size_t i = 0; i < args.size(); i++) { + const std::unique_ptr<Expression>& arg = args[i]; + checkSubtype(params.at(i), arg->getType(), ParsingContext(signatureContext, i + 1)); + } + } else if (signature->params.is<VarargsType>()) { + const type::Type& paramType = signature->params.get<VarargsType>().type; + for (std::size_t i = 0; i < args.size(); i++) { + const std::unique_ptr<Expression>& arg = args[i]; + checkSubtype(paramType, arg->getType(), ParsingContext(signatureContext, i + 1)); + } + } + + if (currentSignatureErrors.size() == 0) { + return ParseResult(signature->makeExpression(name, std::move(args))); + } + } + + if (definition.size() == 1) { + ctx.errors.insert(ctx.errors.end(), currentSignatureErrors.begin(), currentSignatureErrors.end()); + } else { + std::string signatures; + for (const auto& signature : definition) { + signatures += (signatures.size() > 0 ? " | " : ""); + signature->params.match( + [&](const VarargsType& varargs) { + signatures += "(" + toString(varargs.type) + ")"; + }, + [&](const std::vector<type::Type>& params) { + signatures += "("; + bool first = true; + for (const type::Type& param : params) { + if (!first) signatures += ", "; + signatures += toString(param); + first = false; + } + signatures += ")"; + } + ); + + } + std::string actualTypes; + for (const auto& arg : args) { + if (actualTypes.size() > 0) { + actualTypes += ", "; + } + actualTypes += toString(arg->getType()); + } + ctx.error("Expected arguments of type " + signatures + ", but found (" + actualTypes + ") instead."); + } + + return ParseResult(); +} + } // namespace expression } // namespace style } // namespace mbgl |