From 85742b908da65f224b11404436b5ce552bc67780 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 25 Jul 2018 17:03:20 -0700 Subject: [core] Avoid unnecessary template instantiations --- src/mbgl/style/expression/compound_expression.cpp | 55 ++++++++++------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/src/mbgl/style/expression/compound_expression.cpp b/src/mbgl/style/expression/compound_expression.cpp index 46b0c7fe18..4a74fa4f76 100644 --- a/src/mbgl/style/expression/compound_expression.cpp +++ b/src/mbgl/style/expression/compound_expression.cpp @@ -188,36 +188,27 @@ struct Signature&)> : SignatureBas R (*evaluate)(const EvaluationContext&, const Varargs&); }; -// Machinery to pull out function types from class methods, lambdas, etc. -template -struct Signature - : Signature -{ using Signature::Signature; }; - -template -struct Signature - : Signature -{ using Signature::Signature; }; +// Machinery to pull out function type from lambdas. +template +struct SignatureType; template -struct Signature - : Signature -{ using Signature::Signature; }; +struct SignatureType +{ using Type = R (Params...); }; template -struct Signature::value>> - : Signature -{ using Signature::Signature; }; - -} // namespace detail - -using Definition = CompoundExpressionRegistry::Definition; +struct SignatureType::value>> +{ using Type = typename SignatureType::Type; }; template static std::unique_ptr makeSignature(Fn evaluateFunction, std::string name) { - return std::make_unique>(evaluateFunction, std::move(name)); + return std::make_unique::Type>>(evaluateFunction, std::move(name)); } +} // namespace detail + +using Definition = CompoundExpressionRegistry::Definition; + Value featureIdAsExpressionValue(EvaluationContext params) { assert(params.feature); auto id = params.feature->getID(); @@ -295,7 +286,7 @@ optional featureIdAsString(EvaluationContext params) { std::unordered_map initializeDefinitions() { std::unordered_map definitions; auto define = [&](std::string name, auto fn) { - definitions[name].push_back(makeSignature(fn, name)); + definitions[name].push_back(detail::makeSignature(fn, name)); }; define("e", []() -> Result { return 2.718281828459045; }); @@ -326,7 +317,7 @@ std::unordered_map initiali return color.toArray(); }); - define("rgba", rgba); + define("rgba", [](double r, double g, double b, double a) { return rgba(r, g, b, a); }); define("rgb", [](double r, double g, double b) { return rgba(r, g, b, 1.0f); }); define("zoom", [](const EvaluationContext& params) -> Result { @@ -346,7 +337,7 @@ std::unordered_map initiali } return *(params.heatmapDensity); }); - + define("has", [](const EvaluationContext& params, const std::string& key) -> Result { if (!params.feature) { return EvaluationError { @@ -359,7 +350,7 @@ std::unordered_map initiali define("has", [](const std::string& key, const std::unordered_map& object) -> Result { return object.find(key) != object.end(); }); - + define("get", [](const EvaluationContext& params, const std::string& key) -> Result { if (!params.feature) { return EvaluationError { @@ -475,7 +466,7 @@ std::unordered_map initiali } return result; }); - + define("round", [](double x) -> Result { return ::round(x); }); define("floor", [](double x) -> Result { return std::floor(x); }); define("ceil", [](double x) -> Result { return std::ceil(x); }); @@ -495,7 +486,7 @@ std::unordered_map initiali define("<=", [](const std::string& lhs, const std::string& rhs, const Collator& c) -> Result { return c.compare(lhs, rhs) <= 0; }); define("!", [](bool e) -> Result { return !e; }); - + define("is-supported-script", [](const std::string& x) -> Result { return util::i18n::isStringInSupportedScript(x); }); @@ -516,7 +507,7 @@ std::unordered_map initiali define("resolved-locale", [](const Collator& collator) -> Result { return collator.resolvedLocale(); }); - + define("error", [](const std::string& input) -> Result { return EvaluationError { input }; }); @@ -580,7 +571,7 @@ std::unordered_map initiali auto rhs = featurePropertyAsDouble(params, key); return rhs ? rhs <= lhs : false; }); - + define("filter-<=", [](const EvaluationContext& params, const std::string& key, std::string lhs) -> Result { auto rhs = featurePropertyAsString(params, key); return rhs ? rhs <= lhs : false; @@ -590,7 +581,7 @@ std::unordered_map initiali auto rhs = featureIdAsDouble(params); return rhs ? rhs <= lhs : false; }); - + define("filter-id-<=", [](const EvaluationContext& params, std::string lhs) -> Result { auto rhs = featureIdAsString(params); return rhs ? rhs <= lhs : false; @@ -600,7 +591,7 @@ std::unordered_map initiali auto rhs = featurePropertyAsDouble(params, key); return rhs ? rhs >= lhs : false; }); - + define("filter->=", [](const EvaluationContext& params, const std::string& key, std::string lhs) -> Result { auto rhs = featurePropertyAsString(params, key); return rhs ? rhs >= lhs : false; @@ -631,7 +622,7 @@ std::unordered_map initiali optional type = featureTypeAsString(params.feature->getType()); return std::find(types.begin(), types.end(), type) != types.end(); }); - + define("filter-id-in", [](const EvaluationContext& params, const Varargs& ids) -> Result { auto id = featureIdAsExpressionValue(params); return std::find(ids.begin(), ids.end(), id) != ids.end(); -- cgit v1.2.1