@@ -25,7 +25,9 @@ namespace expression {
|
||||
Represents the parameter list for an expression that takes an arbitrary
|
||||
number of arguments (of a specific type).
|
||||
*/
|
||||
struct VarargsType { type::Type type; };
|
||||
struct VarargsType {
|
||||
type::Type type;
|
||||
};
|
||||
|
||||
bool operator==(const VarargsType& lhs, const VarargsType& rhs) {
|
||||
return lhs.type == rhs.type;
|
||||
@@ -34,7 +36,8 @@ bool operator==(const VarargsType& lhs, const VarargsType& rhs) {
|
||||
template <typename T>
|
||||
struct Varargs : std::vector<T> {
|
||||
template <class... Args>
|
||||
explicit Varargs(Args&&... args) : std::vector<T>(std::forward<Args>(args)...) {}
|
||||
explicit Varargs(Args&&... args)
|
||||
: std::vector<T>(std::forward<Args>(args)...) {}
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
@@ -44,11 +47,10 @@ namespace detail {
|
||||
struct SignatureBase {
|
||||
using Args = std::vector<std::unique_ptr<Expression>>;
|
||||
|
||||
SignatureBase(type::Type result_, variant<std::vector<type::Type>, VarargsType> params_, std::string name_) :
|
||||
result(std::move(result_)),
|
||||
params(std::move(params_)),
|
||||
name(std::move(name_))
|
||||
{}
|
||||
SignatureBase(type::Type result_, variant<std::vector<type::Type>, VarargsType> params_, std::string name_)
|
||||
: result(std::move(result_)),
|
||||
params(std::move(params_)),
|
||||
name(std::move(name_)) {}
|
||||
virtual ~SignatureBase() = default;
|
||||
|
||||
virtual EvaluationResult apply(const EvaluationContext&, const Args&) const = 0;
|
||||
@@ -62,20 +64,20 @@ struct SignatureBase {
|
||||
The Signature<Fn> structs are wrappers around an "evaluate()" function whose
|
||||
purpose is to extract the necessary Type data from the evaluate function's
|
||||
type. There are three key (partial) specializations:
|
||||
|
||||
|
||||
Signature<R (Params...)>:
|
||||
Wraps a simple evaluate function (const T0&, const T1&, ...) -> Result<U>
|
||||
|
||||
|
||||
Signature<R (const Varargs<T>&)>:
|
||||
Wraps an evaluate function that takes an arbitrary number of arguments (via
|
||||
a Varargs<T>, which is just an alias for std::vector).
|
||||
|
||||
|
||||
Signature<R (const EvaluationContext&, Params...)>:
|
||||
Wraps an evaluate function that needs to access the expression evaluation
|
||||
parameters in addition to its subexpressions, i.e.,
|
||||
(const EvaluationParams&, const T0&, const T1&, ...) -> Result<U>. Needed
|
||||
for expressions like ["zoom"], ["get", key], etc.
|
||||
|
||||
|
||||
In each of the above evaluate signatures, T0, T1, etc. are the types of
|
||||
the successfully evaluated subexpressions.
|
||||
*/
|
||||
@@ -84,7 +86,7 @@ struct Signature;
|
||||
|
||||
// Simple evaluate function (const T0&, const T1&, ...) -> Result<U>
|
||||
template <class R, class... Params>
|
||||
struct Signature<R (Params...)> : SignatureBase {
|
||||
struct Signature<R(Params...)> : SignatureBase {
|
||||
Signature(R (*evaluate_)(Params...), const std::string& name_)
|
||||
: SignatureBase(valueTypeToExpressionType<std::decay_t<typename R::Value>>(),
|
||||
std::vector<type::Type>{valueTypeToExpressionType<std::decay_t<Params>>()...},
|
||||
@@ -98,8 +100,10 @@ struct Signature<R (Params...)> : SignatureBase {
|
||||
R (*evaluate)(Params...);
|
||||
|
||||
private:
|
||||
template <std::size_t ...I>
|
||||
EvaluationResult applyImpl(const EvaluationContext& evaluationParameters, const Args& args, std::index_sequence<I...>) const {
|
||||
template <std::size_t... I>
|
||||
EvaluationResult applyImpl(const EvaluationContext& evaluationParameters,
|
||||
const Args& args,
|
||||
std::index_sequence<I...>) const {
|
||||
std::array<Value, sizeof...(Params)> evaluated;
|
||||
for (int i = 0; i < int(sizeof...(Params)); ++i) {
|
||||
const EvaluationResult evaluatedArg = args.at(i)->evaluate(evaluationParameters);
|
||||
@@ -114,7 +118,7 @@ private:
|
||||
|
||||
// Varargs evaluate function (const Varargs<T>&) -> Result<U>
|
||||
template <class R, typename T>
|
||||
struct Signature<R (const Varargs<T>&)> : SignatureBase {
|
||||
struct Signature<R(const Varargs<T>&)> : SignatureBase {
|
||||
Signature(R (*evaluate_)(const Varargs<T>&), const std::string& name_)
|
||||
: SignatureBase(valueTypeToExpressionType<std::decay_t<typename R::Value>>(),
|
||||
VarargsType{valueTypeToExpressionType<T>()},
|
||||
@@ -126,7 +130,7 @@ struct Signature<R (const Varargs<T>&)> : SignatureBase {
|
||||
evaluated.reserve(args.size());
|
||||
for (const auto& arg : args) {
|
||||
const EvaluationResult evaluatedArg = arg->evaluate(evaluationParameters);
|
||||
if(!evaluatedArg) return evaluatedArg.error();
|
||||
if (!evaluatedArg) return evaluatedArg.error();
|
||||
evaluated.push_back(*fromExpressionValue<std::decay_t<T>>(*evaluatedArg));
|
||||
}
|
||||
const R value = evaluate(evaluated);
|
||||
@@ -140,7 +144,7 @@ struct Signature<R (const Varargs<T>&)> : SignatureBase {
|
||||
// Evaluate function needing parameter access,
|
||||
// (const EvaluationParams&, const T0&, const T1&, ...) -> Result<U>
|
||||
template <class R, class... Params>
|
||||
struct Signature<R (const EvaluationContext&, Params...)> : SignatureBase {
|
||||
struct Signature<R(const EvaluationContext&, Params...)> : SignatureBase {
|
||||
Signature(R (*evaluate_)(const EvaluationContext&, Params...), const std::string& name_)
|
||||
: SignatureBase(valueTypeToExpressionType<std::decay_t<typename R::Value>>(),
|
||||
std::vector<type::Type>{valueTypeToExpressionType<std::decay_t<Params>>()...},
|
||||
@@ -152,8 +156,10 @@ struct Signature<R (const EvaluationContext&, Params...)> : SignatureBase {
|
||||
}
|
||||
|
||||
private:
|
||||
template <std::size_t ...I>
|
||||
EvaluationResult applyImpl(const EvaluationContext& evaluationParameters, const Args& args, std::index_sequence<I...>) const {
|
||||
template <std::size_t... I>
|
||||
EvaluationResult applyImpl(const EvaluationContext& evaluationParameters,
|
||||
const Args& args,
|
||||
std::index_sequence<I...>) const {
|
||||
std::array<Value, sizeof...(Params)> evaluated;
|
||||
for (int i = 0; i < int(sizeof...(Params)); ++i) {
|
||||
const EvaluationResult evaluatedArg = args.at(i)->evaluate(evaluationParameters);
|
||||
@@ -167,11 +173,11 @@ private:
|
||||
|
||||
R (*evaluate)(const EvaluationContext&, Params...);
|
||||
};
|
||||
|
||||
|
||||
// Evaluate function needing EvaluationContext and Varargs
|
||||
// (const EvaluationContext&, const Varargs<T>&) -> Result<U>
|
||||
template <class R, typename T>
|
||||
struct Signature<R (const EvaluationContext&, const Varargs<T>&)> : SignatureBase {
|
||||
struct Signature<R(const EvaluationContext&, const Varargs<T>&)> : SignatureBase {
|
||||
Signature(R (*evaluate_)(const EvaluationContext&, const Varargs<T>&), const std::string& name_)
|
||||
: SignatureBase(valueTypeToExpressionType<std::decay_t<typename R::Value>>(),
|
||||
VarargsType{valueTypeToExpressionType<T>()},
|
||||
@@ -183,7 +189,7 @@ struct Signature<R (const EvaluationContext&, const Varargs<T>&)> : SignatureBas
|
||||
evaluated.reserve(args.size());
|
||||
for (const auto& arg : args) {
|
||||
const EvaluationResult evaluatedArg = arg->evaluate(evaluationParameters);
|
||||
if(!evaluatedArg) return evaluatedArg.error();
|
||||
if (!evaluatedArg) return evaluatedArg.error();
|
||||
evaluated.push_back(*fromExpressionValue<std::decay_t<T>>(*evaluatedArg));
|
||||
}
|
||||
const R value = evaluate(evaluationParameters, evaluated);
|
||||
@@ -199,12 +205,14 @@ template <class, class = void>
|
||||
struct SignatureType;
|
||||
|
||||
template <class T, class R, class... Params>
|
||||
struct SignatureType<R (T::*)(Params...) const>
|
||||
{ using Type = R (Params...); };
|
||||
struct SignatureType<R (T::*)(Params...) const> {
|
||||
using Type = R(Params...);
|
||||
};
|
||||
|
||||
template <class Lambda>
|
||||
struct SignatureType<Lambda, std::enable_if_t<std::is_class_v<Lambda>>>
|
||||
{ using Type = typename SignatureType<decltype(&Lambda::operator())>::Type; };
|
||||
struct SignatureType<Lambda, std::enable_if_t<std::is_class_v<Lambda>>> {
|
||||
using Type = typename SignatureType<decltype(&Lambda::operator())>::Type;
|
||||
};
|
||||
|
||||
template <typename Fn>
|
||||
static std::unique_ptr<detail::SignatureBase> makeSignature(std::string name, Fn evaluateFunction) {
|
||||
@@ -217,9 +225,7 @@ Value featureIdAsExpressionValue(const EvaluationContext& params) {
|
||||
assert(params.feature);
|
||||
auto id = params.feature->getID();
|
||||
if (id.is<NullValue>()) return Null;
|
||||
return id.match([](const auto& idid) {
|
||||
return toExpressionValue(mbgl::Value(idid));
|
||||
});
|
||||
return id.match([](const auto& idid) { return toExpressionValue(mbgl::Value(idid)); });
|
||||
};
|
||||
|
||||
std::optional<Value> featurePropertyAsExpressionValue(const EvaluationContext& params, const std::string& key) {
|
||||
@@ -229,17 +235,17 @@ std::optional<Value> featurePropertyAsExpressionValue(const EvaluationContext& p
|
||||
};
|
||||
|
||||
std::optional<std::string> featureTypeAsString(FeatureType type) {
|
||||
switch(type) {
|
||||
case FeatureType::Point:
|
||||
return std::optional<std::string>("Point");
|
||||
case FeatureType::LineString:
|
||||
return std::optional<std::string>("LineString");
|
||||
case FeatureType::Polygon:
|
||||
return std::optional<std::string>("Polygon");
|
||||
case FeatureType::Unknown:
|
||||
return std::optional<std::string>("Unknown");
|
||||
default:
|
||||
return {};
|
||||
switch (type) {
|
||||
case FeatureType::Point:
|
||||
return std::optional<std::string>("Point");
|
||||
case FeatureType::LineString:
|
||||
return std::optional<std::string>("LineString");
|
||||
case FeatureType::Polygon:
|
||||
return std::optional<std::string>("Polygon");
|
||||
case FeatureType::Unknown:
|
||||
return std::optional<std::string>("Unknown");
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -292,33 +298,35 @@ const auto& ln2CompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& typeofCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("typeof", [](const Value& v) -> Result<std::string> { return toString(typeOf(v)); });
|
||||
static auto signature = detail::makeSignature(
|
||||
"typeof", [](const Value& v) -> Result<std::string> { return toString(typeOf(v)); });
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& toRgbaCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("to-rgba", [](const Color& color) -> Result<std::array<double, 4>> {
|
||||
return color.toArray();
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"to-rgba", [](const Color& color) -> Result<std::array<double, 4>> { return color.toArray(); });
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& rgbaCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("rgba", [](double r, double g, double b, double a) { return rgba(r, g, b, a); });
|
||||
static auto signature = detail::makeSignature(
|
||||
"rgba", [](double r, double g, double b, double a) { return rgba(r, g, b, a); });
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& rgbCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("rgb", [](double r, double g, double b) { return rgba(r, g, b, 1.0f); });
|
||||
static auto signature = detail::makeSignature("rgb",
|
||||
[](double r, double g, double b) { return rgba(r, g, b, 1.0f); });
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& zoomCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("zoom", [](const EvaluationContext& params) -> Result<double> {
|
||||
if (!params.zoom) {
|
||||
return EvaluationError {
|
||||
"The 'zoom' expression is unavailable in the current evaluation context."
|
||||
};
|
||||
return EvaluationError{
|
||||
"The 'zoom' expression is unavailable in the current "
|
||||
"evaluation context."};
|
||||
}
|
||||
return *(params.zoom);
|
||||
});
|
||||
@@ -326,142 +334,147 @@ const auto& zoomCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& heatmapDensityCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("heatmap-density", [](const EvaluationContext& params) -> Result<double> {
|
||||
if (!params.colorRampParameter) {
|
||||
return EvaluationError {
|
||||
"The 'heatmap-density' expression is unavailable in the current evaluation context."
|
||||
};
|
||||
}
|
||||
return *(params.colorRampParameter);
|
||||
});
|
||||
static auto signature = detail::makeSignature("heatmap-density",
|
||||
[](const EvaluationContext& params) -> Result<double> {
|
||||
if (!params.colorRampParameter) {
|
||||
return EvaluationError{
|
||||
"The 'heatmap-density' expression is unavailable in the "
|
||||
"current evaluation context."};
|
||||
}
|
||||
return *(params.colorRampParameter);
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& lineProgressCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("line-progress", [](const EvaluationContext& params) -> Result<double> {
|
||||
if (!params.colorRampParameter) {
|
||||
return EvaluationError {
|
||||
"The 'line-progress' expression is unavailable in the current evaluation context."
|
||||
};
|
||||
}
|
||||
return *(params.colorRampParameter);
|
||||
});
|
||||
static auto signature = detail::makeSignature("line-progress",
|
||||
[](const EvaluationContext& params) -> Result<double> {
|
||||
if (!params.colorRampParameter) {
|
||||
return EvaluationError{
|
||||
"The 'line-progress' expression is unavailable in the "
|
||||
"current evaluation context."};
|
||||
}
|
||||
return *(params.colorRampParameter);
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& accumulatedCompoundExpression() {
|
||||
const static auto signature = detail::makeSignature("accumulated", [](const EvaluationContext& params) -> Result<Value> {
|
||||
if (!params.accumulated) {
|
||||
return EvaluationError {
|
||||
"The 'accumulated' expression is unavailable in the current evaluation context."
|
||||
};
|
||||
}
|
||||
return Value(toExpressionValue(*params.accumulated));
|
||||
});
|
||||
const static auto signature = detail::makeSignature(
|
||||
"accumulated", [](const EvaluationContext& params) -> Result<Value> {
|
||||
if (!params.accumulated) {
|
||||
return EvaluationError{
|
||||
"The 'accumulated' expression is unavailable in the "
|
||||
"current evaluation context."};
|
||||
}
|
||||
return Value(toExpressionValue(*params.accumulated));
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& hasContextCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("has", [](const EvaluationContext& params, const std::string& key) -> Result<bool> {
|
||||
if (!params.feature) {
|
||||
return EvaluationError {
|
||||
"Feature data is unavailable in the current evaluation context."
|
||||
};
|
||||
}
|
||||
|
||||
return static_cast<bool>(params.feature->getValue(key));
|
||||
});
|
||||
const auto& hasContextCompoundExpression() {
|
||||
static auto signature = detail::makeSignature(
|
||||
"has", [](const EvaluationContext& params, const std::string& key) -> Result<bool> {
|
||||
if (!params.feature) {
|
||||
return EvaluationError{
|
||||
"Feature data is unavailable in the current evaluation "
|
||||
"context."};
|
||||
}
|
||||
|
||||
return static_cast<bool>(params.feature->getValue(key));
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& hasObjectCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("has", [](const std::string& key, const std::unordered_map<std::string, Value>& object) -> Result<bool> {
|
||||
return object.find(key) != object.end();
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"has", [](const std::string& key, const std::unordered_map<std::string, Value>& object) -> Result<bool> {
|
||||
return object.find(key) != object.end();
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& getContextCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("get", [](const EvaluationContext& params, const std::string& key) -> Result<Value> {
|
||||
if (!params.feature) {
|
||||
return EvaluationError {
|
||||
"Feature data is unavailable in the current evaluation context."
|
||||
};
|
||||
}
|
||||
static auto signature = detail::makeSignature(
|
||||
"get", [](const EvaluationContext& params, const std::string& key) -> Result<Value> {
|
||||
if (!params.feature) {
|
||||
return EvaluationError{
|
||||
"Feature data is unavailable in the current evaluation "
|
||||
"context."};
|
||||
}
|
||||
|
||||
auto propertyValue = params.feature->getValue(key);
|
||||
if (!propertyValue) {
|
||||
return Null;
|
||||
}
|
||||
return Value(toExpressionValue(*propertyValue));
|
||||
});
|
||||
auto propertyValue = params.feature->getValue(key);
|
||||
if (!propertyValue) {
|
||||
return Null;
|
||||
}
|
||||
return Value(toExpressionValue(*propertyValue));
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& getObjectCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("get", [](const std::string& key, const std::unordered_map<std::string, Value>& object) -> Result<Value> {
|
||||
if (object.find(key) == object.end()) {
|
||||
return Null;
|
||||
}
|
||||
return object.at(key);
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"get", [](const std::string& key, const std::unordered_map<std::string, Value>& object) -> Result<Value> {
|
||||
if (object.find(key) == object.end()) {
|
||||
return Null;
|
||||
}
|
||||
return object.at(key);
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& propertiesCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("properties", [](const EvaluationContext& params) -> Result<std::unordered_map<std::string, Value>> {
|
||||
if (!params.feature) {
|
||||
return EvaluationError {
|
||||
"Feature data is unavailable in the current evaluation context."
|
||||
};
|
||||
}
|
||||
std::unordered_map<std::string, Value> result;
|
||||
const PropertyMap& properties = params.feature->getProperties();
|
||||
result.reserve(properties.size());
|
||||
for (const auto& entry : properties) {
|
||||
result[entry.first] = toExpressionValue(entry.second);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"properties", [](const EvaluationContext& params) -> Result<std::unordered_map<std::string, Value>> {
|
||||
if (!params.feature) {
|
||||
return EvaluationError{
|
||||
"Feature data is unavailable in the current evaluation "
|
||||
"context."};
|
||||
}
|
||||
std::unordered_map<std::string, Value> result;
|
||||
const PropertyMap& properties = params.feature->getProperties();
|
||||
result.reserve(properties.size());
|
||||
for (const auto& entry : properties) {
|
||||
result[entry.first] = toExpressionValue(entry.second);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& geometryTypeCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("geometry-type", [](const EvaluationContext& params) -> Result<std::string> {
|
||||
if (!params.feature) {
|
||||
return EvaluationError {
|
||||
"Feature data is unavailable in the current evaluation context."
|
||||
};
|
||||
}
|
||||
static auto signature = detail::makeSignature("geometry-type",
|
||||
[](const EvaluationContext& params) -> Result<std::string> {
|
||||
if (!params.feature) {
|
||||
return EvaluationError{
|
||||
"Feature data is unavailable in the current evaluation "
|
||||
"context."};
|
||||
}
|
||||
|
||||
auto type = params.feature->getType();
|
||||
if (type == FeatureType::Point) {
|
||||
return "Point";
|
||||
} else if (type == FeatureType::LineString) {
|
||||
return "LineString";
|
||||
} else if (type == FeatureType::Polygon) {
|
||||
return "Polygon";
|
||||
} else {
|
||||
return "Unknown";
|
||||
}
|
||||
});
|
||||
auto type = params.feature->getType();
|
||||
if (type == FeatureType::Point) {
|
||||
return "Point";
|
||||
} else if (type == FeatureType::LineString) {
|
||||
return "LineString";
|
||||
} else if (type == FeatureType::Polygon) {
|
||||
return "Polygon";
|
||||
} else {
|
||||
return "Unknown";
|
||||
}
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& idCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("id", [](const EvaluationContext& params) -> Result<Value> {
|
||||
if (!params.feature) {
|
||||
return EvaluationError {
|
||||
"Feature data is unavailable in the current evaluation context."
|
||||
};
|
||||
return EvaluationError{
|
||||
"Feature data is unavailable in the current evaluation "
|
||||
"context."};
|
||||
}
|
||||
|
||||
auto id = params.feature->getID();
|
||||
return id.match(
|
||||
[](const auto& idValue) {
|
||||
return toExpressionValue(mbgl::Value(idValue));
|
||||
}
|
||||
);
|
||||
return id.match([](const auto& idValue) { return toExpressionValue(mbgl::Value(idValue)); });
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
@@ -626,16 +639,14 @@ const auto& isSupportedScriptCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& upcaseCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("upcase", [](const std::string& input) -> Result<std::string> {
|
||||
return platform::uppercase(input);
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"upcase", [](const std::string& input) -> Result<std::string> { return platform::uppercase(input); });
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& downcaseCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("downcase", [](const std::string& input) -> Result<std::string> {
|
||||
return platform::lowercase(input);
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"downcase", [](const std::string& input) -> Result<std::string> { return platform::lowercase(input); });
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -651,16 +662,14 @@ const auto& concatCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& resolvedLocaleCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("resolved-locale", [](const Collator& collator) -> Result<std::string> {
|
||||
return collator.resolvedLocale();
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"resolved-locale", [](const Collator& collator) -> Result<std::string> { return collator.resolvedLocale(); });
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& errorCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("error", [](const std::string& input) -> Result<type::ErrorType> {
|
||||
return EvaluationError { input };
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"error", [](const std::string& input) -> Result<type::ErrorType> { return EvaluationError{input}; });
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -681,33 +690,37 @@ const auto& featureStateCompoundExpression() {
|
||||
|
||||
// Legacy Filters
|
||||
const auto& filterEqualsCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-==", [](const EvaluationContext& params, const std::string& key, const Value &lhs) -> Result<bool> {
|
||||
const auto rhs = featurePropertyAsExpressionValue(params, key);
|
||||
return rhs ? lhs == *rhs : false;
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-==", [](const EvaluationContext& params, const std::string& key, const Value& lhs) -> Result<bool> {
|
||||
const auto rhs = featurePropertyAsExpressionValue(params, key);
|
||||
return rhs ? lhs == *rhs : false;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& filterIdEqualsCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-id-==", [](const EvaluationContext& params, const Value &lhs) -> Result<bool> {
|
||||
return lhs == featureIdAsExpressionValue(params);
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-id-==", [](const EvaluationContext& params, const Value& lhs) -> Result<bool> {
|
||||
return lhs == featureIdAsExpressionValue(params);
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& filterTypeEqualsCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-type-==", [](const EvaluationContext& params, const std::string &lhs) -> Result<bool> {
|
||||
if (!params.feature) return false;
|
||||
return featureTypeAsString(params.feature->getType()) == lhs;
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-type-==", [](const EvaluationContext& params, const std::string& lhs) -> Result<bool> {
|
||||
if (!params.feature) return false;
|
||||
return featureTypeAsString(params.feature->getType()) == lhs;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& filterLessThanNumberCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-<", [](const EvaluationContext& params, const std::string& key, double lhs) -> Result<bool> {
|
||||
auto rhs = featurePropertyAsDouble(params, key);
|
||||
return rhs ? rhs < lhs : false;
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-<", [](const EvaluationContext& params, const std::string& key, double lhs) -> Result<bool> {
|
||||
auto rhs = featurePropertyAsDouble(params, key);
|
||||
return rhs ? rhs < lhs : false;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -722,10 +735,11 @@ const auto& filterLessThanStringCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& filterIdLessThanNumberCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-id-<", [](const EvaluationContext& params, double lhs) -> Result<bool> {
|
||||
auto rhs = featureIdAsDouble(params);
|
||||
return rhs ? rhs < lhs : false;
|
||||
});
|
||||
static auto signature = detail::makeSignature("filter-id-<",
|
||||
[](const EvaluationContext& params, double lhs) -> Result<bool> {
|
||||
auto rhs = featureIdAsDouble(params);
|
||||
return rhs ? rhs < lhs : false;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -739,10 +753,11 @@ const auto& filterIdLessThanStringCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& filterMoreThanNumberCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter->", [](const EvaluationContext& params, const std::string& key, double lhs) -> Result<bool> {
|
||||
auto rhs = featurePropertyAsDouble(params, key);
|
||||
return rhs ? rhs > lhs : false;
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter->", [](const EvaluationContext& params, const std::string& key, double lhs) -> Result<bool> {
|
||||
auto rhs = featurePropertyAsDouble(params, key);
|
||||
return rhs ? rhs > lhs : false;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -757,10 +772,11 @@ const auto& filterMoreThanStringCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& filterIdMoreThanNumberCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-id->", [](const EvaluationContext& params, double lhs) -> Result<bool> {
|
||||
auto rhs = featureIdAsDouble(params);
|
||||
return rhs ? rhs > lhs : false;
|
||||
});
|
||||
static auto signature = detail::makeSignature("filter-id->",
|
||||
[](const EvaluationContext& params, double lhs) -> Result<bool> {
|
||||
auto rhs = featureIdAsDouble(params);
|
||||
return rhs ? rhs > lhs : false;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -774,10 +790,11 @@ const auto& filterIdMoreThanStringCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& filterLessOrEqualThanNumberCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-<=", [](const EvaluationContext& params, const std::string& key, double lhs) -> Result<bool> {
|
||||
auto rhs = featurePropertyAsDouble(params, key);
|
||||
return rhs ? rhs <= lhs : false;
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-<=", [](const EvaluationContext& params, const std::string& key, double lhs) -> Result<bool> {
|
||||
auto rhs = featurePropertyAsDouble(params, key);
|
||||
return rhs ? rhs <= lhs : false;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -792,10 +809,11 @@ const auto& filterLessOrEqualThanStringCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& filterIdLessOrEqualThanNumberCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-id-<=", [](const EvaluationContext& params, double lhs) -> Result<bool> {
|
||||
auto rhs = featureIdAsDouble(params);
|
||||
return rhs ? rhs <= lhs : false;
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-id-<=", [](const EvaluationContext& params, double lhs) -> Result<bool> {
|
||||
auto rhs = featureIdAsDouble(params);
|
||||
return rhs ? rhs <= lhs : false;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -809,10 +827,11 @@ const auto& filterIdLessOrEqualThanStringCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& filterGreaterOrEqualThanNumberCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter->=", [](const EvaluationContext& params, const std::string& key, double lhs) -> Result<bool> {
|
||||
auto rhs = featurePropertyAsDouble(params, key);
|
||||
return rhs ? rhs >= lhs : false;
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter->=", [](const EvaluationContext& params, const std::string& key, double lhs) -> Result<bool> {
|
||||
auto rhs = featurePropertyAsDouble(params, key);
|
||||
return rhs ? rhs >= lhs : false;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -827,10 +846,11 @@ const auto& filterGreaterOrEqualThanStringCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& filterIdGreaterOrEqualThanNumberCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-id->=", [](const EvaluationContext& params, double lhs) -> Result<bool> {
|
||||
auto rhs = featureIdAsDouble(params);
|
||||
return rhs ? rhs >= lhs : false;
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-id->=", [](const EvaluationContext& params, double lhs) -> Result<bool> {
|
||||
auto rhs = featureIdAsDouble(params);
|
||||
return rhs ? rhs >= lhs : false;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -844,10 +864,11 @@ const auto& filterIdGreaterOrEqualThanStringCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& filterHasCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-has", [](const EvaluationContext& params, const std::string& key) -> Result<bool> {
|
||||
assert(params.feature);
|
||||
return bool(params.feature->getValue(key));
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-has", [](const EvaluationContext& params, const std::string& key) -> Result<bool> {
|
||||
assert(params.feature);
|
||||
return bool(params.feature->getValue(key));
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -860,30 +881,32 @@ const auto& filterHasIdCompoundExpression() {
|
||||
}
|
||||
|
||||
const auto& filterTypeInCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-type-in", [](const EvaluationContext& params, const Varargs<std::string>& types) -> Result<bool> {
|
||||
assert(params.feature);
|
||||
std::optional<std::string> type = featureTypeAsString(params.feature->getType());
|
||||
return std::find(types.begin(), types.end(), type) != types.end();
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-type-in", [](const EvaluationContext& params, const Varargs<std::string>& types) -> Result<bool> {
|
||||
assert(params.feature);
|
||||
std::optional<std::string> type = featureTypeAsString(params.feature->getType());
|
||||
return std::find(types.begin(), types.end(), type) != types.end();
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& filterIdInCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-id-in", [](const EvaluationContext& params, const Varargs<Value>& ids) -> Result<bool> {
|
||||
auto id = featureIdAsExpressionValue(params);
|
||||
return std::find(ids.begin(), ids.end(), id) != ids.end();
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-id-in", [](const EvaluationContext& params, const Varargs<Value>& ids) -> Result<bool> {
|
||||
auto id = featureIdAsExpressionValue(params);
|
||||
return std::find(ids.begin(), ids.end(), id) != ids.end();
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
|
||||
const auto& filterInCompoundExpression() {
|
||||
static auto signature = detail::makeSignature("filter-in", [](const EvaluationContext& params, const Varargs<Value>& varargs) -> Result<bool> {
|
||||
if (varargs.size() < 2) return false;
|
||||
assert(varargs[0].is<std::string>());
|
||||
auto value = featurePropertyAsExpressionValue(params, varargs[0].get<std::string>());
|
||||
return value ? std::find(varargs.begin() + 1, varargs.end(), *value) != varargs.end() : false;
|
||||
});
|
||||
static auto signature = detail::makeSignature(
|
||||
"filter-in", [](const EvaluationContext& params, const Varargs<Value>& varargs) -> Result<bool> {
|
||||
if (varargs.size() < 2) return false;
|
||||
assert(varargs[0].is<std::string>());
|
||||
auto value = featurePropertyAsExpressionValue(params, varargs[0].get<std::string>());
|
||||
return value ? std::find(varargs.begin() + 1, varargs.end(), *value) != varargs.end() : false;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
@@ -971,38 +994,36 @@ using namespace mbgl::style::conversion;
|
||||
using DefinitionIterator = decltype(compoundExpressionRegistry)::const_iterator;
|
||||
using Definitions = std::pair<DefinitionIterator, DefinitionIterator>;
|
||||
|
||||
std::string expectedTypesError(const Definitions& definitions,
|
||||
const std::vector<std::unique_ptr<Expression>>& args) {
|
||||
std::vector<std::string> availableOverloads; // Only used if there are no overloads with matching number of args
|
||||
std::string expectedTypesError(const Definitions& definitions, const std::vector<std::unique_ptr<Expression>>& args) {
|
||||
std::vector<std::string> availableOverloads; // Only used if there are no overloads
|
||||
// with matching number of args
|
||||
std::vector<std::string> overloads;
|
||||
for (auto it = definitions.first; it != definitions.second; ++it) {
|
||||
const auto& signature = it->second();
|
||||
signature->params.match(
|
||||
[&](const VarargsType& varargs) {
|
||||
std::string overload = "(" + toString(varargs.type) + ")";
|
||||
overloads.push_back(overload);
|
||||
},
|
||||
[&](const std::vector<type::Type>& params) {
|
||||
std::string overload = "(";
|
||||
bool first = true;
|
||||
for (const type::Type& param : params) {
|
||||
if (!first) overload += ", ";
|
||||
overload += toString(param);
|
||||
first = false;
|
||||
}
|
||||
overload += ")";
|
||||
if (params.size() == args.size()) {
|
||||
[&](const VarargsType& varargs) {
|
||||
std::string overload = "(" + toString(varargs.type) + ")";
|
||||
overloads.push_back(overload);
|
||||
} else {
|
||||
availableOverloads.push_back(overload);
|
||||
}
|
||||
}
|
||||
);
|
||||
},
|
||||
[&](const std::vector<type::Type>& params) {
|
||||
std::string overload = "(";
|
||||
bool first = true;
|
||||
for (const type::Type& param : params) {
|
||||
if (!first) overload += ", ";
|
||||
overload += toString(param);
|
||||
first = false;
|
||||
}
|
||||
overload += ")";
|
||||
if (params.size() == args.size()) {
|
||||
overloads.push_back(overload);
|
||||
} else {
|
||||
availableOverloads.push_back(overload);
|
||||
}
|
||||
});
|
||||
}
|
||||
std::string signatures = overloads.empty() ?
|
||||
boost::algorithm::join(availableOverloads, " | ") :
|
||||
boost::algorithm::join(overloads, " | ");
|
||||
|
||||
std::string signatures = overloads.empty() ? boost::algorithm::join(availableOverloads, " | ")
|
||||
: boost::algorithm::join(overloads, " | ");
|
||||
|
||||
std::string actualTypes;
|
||||
for (const auto& arg : args) {
|
||||
if (!actualTypes.empty()) {
|
||||
@@ -1010,14 +1031,13 @@ std::string expectedTypesError(const Definitions& definitions,
|
||||
}
|
||||
actualTypes += toString(arg->getType());
|
||||
}
|
||||
|
||||
|
||||
return "Expected arguments of type " + signatures + ", but found (" + actualTypes + ") instead.";
|
||||
}
|
||||
|
||||
static ParseResult createCompoundExpression(const Definitions& definitions,
|
||||
std::vector<std::unique_ptr<Expression>> args,
|
||||
ParsingContext& ctx)
|
||||
{
|
||||
ParsingContext& ctx) {
|
||||
ParsingContext signatureContext(ctx.getKey());
|
||||
|
||||
for (auto it = definitions.first; it != definitions.second; ++it) {
|
||||
@@ -1027,10 +1047,8 @@ static ParseResult createCompoundExpression(const Definitions& definitions,
|
||||
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 " + util::toString(params.size()) +
|
||||
" arguments, but found " + util::toString(args.size()) + " instead."
|
||||
);
|
||||
signatureContext.error("Expected " + util::toString(params.size()) + " arguments, but found " +
|
||||
util::toString(args.size()) + " instead.");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1071,22 +1089,17 @@ ParseResult parseCompoundExpression(const std::string& name, const Convertible&
|
||||
|
||||
const auto definitions = compoundExpressionRegistry.equal_range(name.c_str());
|
||||
if (definitions.first == definitions.second) {
|
||||
ctx.error(
|
||||
R"(Unknown expression ")" + name + R"(". If you wanted a literal array, use ["literal", [...]].)",
|
||||
0
|
||||
);
|
||||
ctx.error(R"(Unknown expression ")" + name + R"(". If you wanted a literal array, use ["literal", [...]].)", 0);
|
||||
return ParseResult();
|
||||
}
|
||||
|
||||
auto length = arrayLength(value);
|
||||
|
||||
|
||||
for (auto it = definitions.first; it != definitions.second; ++it) {
|
||||
const auto& signature = it->second();
|
||||
|
||||
if (
|
||||
signature->params.is<VarargsType>() ||
|
||||
signature->params.get<std::vector<type::Type>>().size() == length - 1
|
||||
) {
|
||||
if (signature->params.is<VarargsType>() ||
|
||||
signature->params.get<std::vector<type::Type>>().size() == length - 1) {
|
||||
// First parse all the args, potentially coercing to the
|
||||
// types expected by this overload.
|
||||
ctx.clearErrors();
|
||||
@@ -1097,8 +1110,7 @@ ParseResult parseCompoundExpression(const std::string& name, const Convertible&
|
||||
for (std::size_t i = 1; i < length; i++) {
|
||||
std::optional<type::Type> expected = signature->params.match(
|
||||
[](const VarargsType& varargs) { return varargs.type; },
|
||||
[&](const std::vector<type::Type>& params_) { return params_[i - 1]; }
|
||||
);
|
||||
[&](const std::vector<type::Type>& params_) { return params_[i - 1]; });
|
||||
|
||||
auto parsed = ctx.parse(arrayMember(value, i), i, expected);
|
||||
if (!parsed) {
|
||||
@@ -1124,7 +1136,7 @@ ParseResult parseCompoundExpression(const std::string& name, const Convertible&
|
||||
ctx.clearErrors();
|
||||
std::vector<std::unique_ptr<Expression>> args;
|
||||
args.reserve(length - 1);
|
||||
|
||||
|
||||
for (std::size_t i = 1; i < length; i++) {
|
||||
auto parsed = ctx.parse(arrayMember(value, i), i);
|
||||
if (!parsed) {
|
||||
@@ -1140,16 +1152,15 @@ ParseResult parseCompoundExpression(const std::string& name, const Convertible&
|
||||
|
||||
ParseResult createCompoundExpression(const std::string& name,
|
||||
std::vector<std::unique_ptr<Expression>> args,
|
||||
ParsingContext& ctx)
|
||||
{
|
||||
ParsingContext& ctx) {
|
||||
return createCompoundExpression(compoundExpressionRegistry.equal_range(name.c_str()), std::move(args), ctx);
|
||||
}
|
||||
|
||||
CompoundExpression::CompoundExpression(const detail::SignatureBase& signature_, std::vector<std::unique_ptr<Expression>> args_) :
|
||||
Expression(Kind::CompoundExpression, signature_.result),
|
||||
signature(signature_),
|
||||
args(std::move(args_))
|
||||
{}
|
||||
CompoundExpression::CompoundExpression(const detail::SignatureBase& signature_,
|
||||
std::vector<std::unique_ptr<Expression>> args_)
|
||||
: Expression(Kind::CompoundExpression, signature_.result),
|
||||
signature(signature_),
|
||||
args(std::move(args_)) {}
|
||||
|
||||
std::string CompoundExpression::getOperator() const {
|
||||
return signature.name;
|
||||
@@ -1160,12 +1171,13 @@ EvaluationResult CompoundExpression::evaluate(const EvaluationContext& evaluatio
|
||||
}
|
||||
|
||||
std::optional<std::size_t> CompoundExpression::getParameterCount() const {
|
||||
return signature.params.match([&](const VarargsType&) -> std::optional<std::size_t> { return std::nullopt; },
|
||||
[&](const std::vector<type::Type>& p) -> std::optional<std::size_t> { return p.size(); });
|
||||
return signature.params.match(
|
||||
[&](const VarargsType&) -> std::optional<std::size_t> { return std::nullopt; },
|
||||
[&](const std::vector<type::Type>& p) -> std::optional<std::size_t> { return p.size(); });
|
||||
}
|
||||
|
||||
std::vector<std::optional<Value>> CompoundExpression::possibleOutputs() const {
|
||||
return { std::nullopt };
|
||||
return {std::nullopt};
|
||||
}
|
||||
|
||||
void CompoundExpression::eachChild(const std::function<void(const Expression&)>& visit) const {
|
||||
@@ -1177,10 +1189,8 @@ void CompoundExpression::eachChild(const std::function<void(const Expression&)>&
|
||||
bool CompoundExpression::operator==(const Expression& e) const {
|
||||
if (e.getKind() == Kind::CompoundExpression) {
|
||||
auto rhs = static_cast<const CompoundExpression*>(&e);
|
||||
return signature.name == rhs->signature.name &&
|
||||
signature.result == rhs->signature.result &&
|
||||
signature.params == rhs->signature.params &&
|
||||
Expression::childrenEqual(args, rhs->args);
|
||||
return signature.name == rhs->signature.name && signature.result == rhs->signature.result &&
|
||||
signature.params == rhs->signature.params && Expression::childrenEqual(args, rhs->args);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user