[core] use constexpr map using eternal for expression lookups
This commit is contained in:
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -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
|
||||
|
||||
@@ -9,6 +9,7 @@ target_include_directories(mbgl-core
|
||||
|
||||
target_link_libraries(mbgl-core PRIVATE
|
||||
earcut.hpp
|
||||
eternal
|
||||
expected
|
||||
geojson-vt-cpp
|
||||
kdbush.hpp
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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
1
vendor/eternal
vendored
Submodule
Submodule vendor/eternal added at 960f4c8526
5
vendor/eternal.cmake
vendored
Normal file
5
vendor/eternal.cmake
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
add_library(eternal INTERFACE)
|
||||
|
||||
target_include_directories(eternal SYSTEM INTERFACE
|
||||
${CMAKE_SOURCE_DIR}/vendor/eternal/include
|
||||
)
|
||||
Reference in New Issue
Block a user