[core] use constexpr map using eternal for expression lookups

This commit is contained in:
Konstantin Käfer
2018-12-03 16:31:09 +02:00
parent c89daf6bf1
commit 2b9f3aad9a
8 changed files with 56 additions and 41 deletions

3
.gitmodules vendored
View File

@@ -76,3 +76,6 @@
[submodule "vendor/glfw"]
path = vendor/glfw
url = https://github.com/glfw/glfw.git
[submodule "vendor/eternal"]
path = vendor/eternal
url = https://github.com/mapbox/eternal.git

View File

@@ -9,6 +9,7 @@ target_include_directories(mbgl-core
target_link_libraries(mbgl-core PRIVATE
earcut.hpp
eternal
expected
geojson-vt-cpp
kdbush.hpp

View File

@@ -7,6 +7,7 @@ include(${CMAKE_SOURCE_DIR}/vendor/benchmark.cmake)
include(${CMAKE_SOURCE_DIR}/vendor/boost.cmake)
include(${CMAKE_SOURCE_DIR}/vendor/cheap-ruler-cpp.cmake)
include(${CMAKE_SOURCE_DIR}/vendor/earcut.hpp.cmake)
include(${CMAKE_SOURCE_DIR}/vendor/eternal.cmake)
include(${CMAKE_SOURCE_DIR}/vendor/expected.cmake)
include(${CMAKE_SOURCE_DIR}/vendor/geojson-vt-cpp.cmake)
include(${CMAKE_SOURCE_DIR}/vendor/geojson.hpp.cmake)

View File

@@ -178,8 +178,7 @@ private:
};
using ParseFunction = ParseResult (*)(const conversion::Convertible&, ParsingContext&);
using ExpressionRegistry = std::unordered_map<std::string, ParseFunction>;
const ExpressionRegistry& getExpressionRegistry();
ParseFunction getExpression(const std::string&);
} // namespace expression
} // namespace style

View File

@@ -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());
}

View File

@@ -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);

1
vendor/eternal vendored Submodule

Submodule vendor/eternal added at 960f4c8526

5
vendor/eternal.cmake vendored Normal file
View File

@@ -0,0 +1,5 @@
add_library(eternal INTERFACE)
target_include_directories(eternal SYSTEM INTERFACE
${CMAKE_SOURCE_DIR}/vendor/eternal/include
)