summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2018-12-03 16:31:09 +0200
committerKonstantin Käfer <mail@kkaefer.com>2018-12-04 16:20:51 +0200
commit2b9f3aad9a69aba0035442364320c372c49053c0 (patch)
tree4a639ce5693845b0027f05cf283617029bb3f1ee /src
parentc89daf6bf190223b6da7867973d3f42f785eee01 (diff)
downloadqtlocation-mapboxgl-2b9f3aad9a69aba0035442364320c372c49053c0.tar.gz
[core] use constexpr map using eternal for expression lookups
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/style/expression/is_expression.cpp4
-rw-r--r--src/mbgl/style/expression/parsing_context.cpp79
2 files changed, 44 insertions, 39 deletions
diff --git a/src/mbgl/style/expression/is_expression.cpp b/src/mbgl/style/expression/is_expression.cpp
index acf074c25b..34f0fec7a3 100644
--- a/src/mbgl/style/expression/is_expression.cpp
+++ b/src/mbgl/style/expression/is_expression.cpp
@@ -12,13 +12,11 @@ namespace expression {
using namespace mbgl::style::conversion;
bool isExpression(const Convertible& value) {
- const ExpressionRegistry& registry = getExpressionRegistry();
-
if (!isArray(value) || arrayLength(value) == 0) return false;
optional<std::string> name = toString(arrayMember(value, 0));
if (!name) return false;
- return (registry.find(*name) != registry.end()) ||
+ return getExpression(*name) ||
(CompoundExpressionRegistry::definitions.find(*name) != CompoundExpressionRegistry::definitions.end());
}
diff --git a/src/mbgl/style/expression/parsing_context.cpp b/src/mbgl/style/expression/parsing_context.cpp
index 34fbc5c380..79a2f251e2 100644
--- a/src/mbgl/style/expression/parsing_context.cpp
+++ b/src/mbgl/style/expression/parsing_context.cpp
@@ -29,6 +29,8 @@
#include <mbgl/util/string.hpp>
+#include <mapbox/eternal.hpp>
+
namespace mbgl {
namespace style {
namespace expression {
@@ -95,39 +97,45 @@ ParseResult ParsingContext::parse(const Convertible& value, std::size_t index_,
return child.parse(value);
}
-const ExpressionRegistry& getExpressionRegistry() {
- static ExpressionRegistry registry {{
- {"==", parseComparison},
- {"!=", parseComparison},
- {">", parseComparison},
- {"<", parseComparison},
- {">=", parseComparison},
- {"<=", parseComparison},
- {"all", All::parse},
- {"any", Any::parse},
- {"array", Assertion::parse},
- {"at", At::parse},
- {"boolean", Assertion::parse},
- {"case", Case::parse},
- {"coalesce", Coalesce::parse},
- {"collator", CollatorExpression::parse},
- {"format", FormatExpression::parse},
- {"interpolate", parseInterpolate},
- {"length", Length::parse},
- {"let", Let::parse},
- {"literal", Literal::parse},
- {"match", parseMatch},
- {"number", Assertion::parse},
- {"object", Assertion::parse},
- {"step", Step::parse},
- {"string", Assertion::parse},
- {"to-boolean", Coercion::parse},
- {"to-color", Coercion::parse},
- {"to-number", Coercion::parse},
- {"to-string", Coercion::parse},
- {"var", Var::parse}
- }};
- return registry;
+MAPBOX_ETERNAL_CONSTEXPR const auto expressionRegistry = mapbox::eternal::hash_map<mapbox::eternal::string, ParseFunction>({
+ {"==", parseComparison},
+ {"!=", parseComparison},
+ {">", parseComparison},
+ {"<", parseComparison},
+ {">=", parseComparison},
+ {"<=", parseComparison},
+ {"all", All::parse},
+ {"any", Any::parse},
+ {"array", Assertion::parse},
+ {"at", At::parse},
+ {"boolean", Assertion::parse},
+ {"case", Case::parse},
+ {"coalesce", Coalesce::parse},
+ {"collator", CollatorExpression::parse},
+ {"format", FormatExpression::parse},
+ {"interpolate", parseInterpolate},
+ {"length", Length::parse},
+ {"let", Let::parse},
+ {"literal", Literal::parse},
+ {"match", parseMatch},
+ {"number", Assertion::parse},
+ {"object", Assertion::parse},
+ {"step", Step::parse},
+ {"string", Assertion::parse},
+ {"to-boolean", Coercion::parse},
+ {"to-color", Coercion::parse},
+ {"to-number", Coercion::parse},
+ {"to-string", Coercion::parse},
+ {"var", Var::parse}
+});
+
+ParseFunction getExpression(const std::string& name) {
+ auto parseFunction = expressionRegistry.find(name.c_str());
+ if (parseFunction != expressionRegistry.end()) {
+ return parseFunction->second;
+ } else {
+ return nullptr;
+ }
}
ParseResult ParsingContext::parse(const Convertible& value, optional<TypeAnnotationOption> typeAnnotationOption) {
@@ -150,9 +158,8 @@ ParseResult ParsingContext::parse(const Convertible& value, optional<TypeAnnotat
return ParseResult();
}
- const ExpressionRegistry& registry = getExpressionRegistry();
- auto parseFunction = registry.find(*op);
- if (parseFunction != registry.end()) {
+ auto parseFunction = expressionRegistry.find(op->c_str());
+ if (parseFunction != expressionRegistry.end()) {
parsed = parseFunction->second(value, *this);
} else {
parsed = parseCompoundExpression(*op, value, *this);