summaryrefslogtreecommitdiff
path: root/include/mbgl
diff options
context:
space:
mode:
Diffstat (limited to 'include/mbgl')
-rw-r--r--include/mbgl/style/expression/expression.hpp30
-rw-r--r--include/mbgl/style/expression/type.hpp (renamed from include/mbgl/style/function/type.hpp)14
-rw-r--r--include/mbgl/style/expression/value.hpp53
3 files changed, 69 insertions, 28 deletions
diff --git a/include/mbgl/style/expression/expression.hpp b/include/mbgl/style/expression/expression.hpp
index e36db1b3c7..99aaed8a14 100644
--- a/include/mbgl/style/expression/expression.hpp
+++ b/include/mbgl/style/expression/expression.hpp
@@ -6,8 +6,8 @@
#include <mbgl/util/optional.hpp>
#include <mbgl/util/variant.hpp>
#include <mbgl/util/color.hpp>
-#include <mbgl/style/function/type.hpp>
-#include <mbgl/util/feature.hpp>
+#include <mbgl/style/expression/type.hpp>
+#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/parsing_context.hpp>
#include <mbgl/style/conversion.hpp>
@@ -20,20 +20,6 @@ class GeometryTileFeature;
namespace style {
namespace expression {
-struct Value;
-using ValueBase = variant<
- NullValue,
- float,
- std::string,
- mbgl::Color,
- mapbox::util::recursive_wrapper<std::vector<Value>>,
- mapbox::util::recursive_wrapper<std::unordered_map<std::string, Value>>>;
-struct Value : ValueBase {
- using ValueBase::ValueBase;
-};
-
-constexpr NullValue Null = NullValue();
-
struct EvaluationError {
std::string message;
};
@@ -102,17 +88,7 @@ public:
template <class V>
static ParseResult parse(const V& value, const ParsingContext& ctx) {
const Value& parsedValue = parseValue(value);
- const type::Type& type = parsedValue.match(
- [&](float) -> type::Type { return type::Primitive::Number; },
- [&](const std::string&) -> type::Type { return type::Primitive::String; },
- [&](const mbgl::Color&) -> type::Type { return type::Primitive::Color; },
- [&](const NullValue&) -> type::Type { return type::Primitive::Null; },
- [&](const std::unordered_map<std::string, Value>&) -> type::Type { return type::Primitive::Object; },
- [&](const std::vector<Value>& arr) -> type::Type {
- // TODO
- return type::Array(type::Primitive::Value, arr.size());
- }
- );
+ const type::Type& type = typeOf(parsedValue);
return std::make_unique<LiteralExpression>(ctx.key(), type, parsedValue);
}
diff --git a/include/mbgl/style/function/type.hpp b/include/mbgl/style/expression/type.hpp
index 19897ac77f..72f36e2451 100644
--- a/include/mbgl/style/function/type.hpp
+++ b/include/mbgl/style/expression/type.hpp
@@ -30,6 +30,9 @@ using Type = variant<
Array,
NArgs,
Lambda>;
+
+template <class T>
+std::string toString(const T& t);
class Primitive {
public:
@@ -62,7 +65,13 @@ public:
Array(ValueType itemType_) : itemType(itemType_) {}
Array(ValueType itemType_, std::size_t N_) : itemType(itemType_), N(N_) {}
std::string getName() const {
- return "array";
+ if (N) {
+ return "Array<" + toString(itemType) + ", " + std::to_string(*N) + ">";
+ } else if (toString(itemType) == "Value") {
+ return "Array";
+ } else {
+ return "Array<" + toString(itemType) + ">";
+ }
}
private:
@@ -110,6 +119,9 @@ private:
std::vector<ValueType> params;
};
+template <class T>
+std::string toString(const T& t) { return t.match([&] (const auto& t) { return t.getName(); }); }
+
} // namespace type
} // namespace expression
diff --git a/include/mbgl/style/expression/value.hpp b/include/mbgl/style/expression/value.hpp
new file mode 100644
index 0000000000..0cac50c6ab
--- /dev/null
+++ b/include/mbgl/style/expression/value.hpp
@@ -0,0 +1,53 @@
+#pragma once
+
+#include <mbgl/util/variant.hpp>
+#include <mbgl/util/feature.hpp>
+#include <mbgl/style/expression/type.hpp>
+
+namespace mbgl {
+namespace style {
+namespace expression {
+
+struct Value;
+using ValueBase = variant<
+ NullValue,
+ float,
+ std::string,
+ mbgl::Color,
+ mapbox::util::recursive_wrapper<std::vector<Value>>,
+ mapbox::util::recursive_wrapper<std::unordered_map<std::string, Value>>>;
+struct Value : ValueBase {
+ using ValueBase::ValueBase;
+};
+
+constexpr NullValue Null = NullValue();
+
+type::Type typeOf(const Value& value) {
+ return value.match(
+ [&](float) -> type::Type { return type::Primitive::Number; },
+ [&](const std::string&) -> type::Type { return type::Primitive::String; },
+ [&](const mbgl::Color&) -> type::Type { return type::Primitive::Color; },
+ [&](const NullValue&) -> type::Type { return type::Primitive::Null; },
+ [&](const std::unordered_map<std::string, Value>&) -> type::Type { return type::Primitive::Object; },
+ [&](const std::vector<Value>& arr) -> type::Type {
+ optional<type::Type> itemType;
+ for (const auto& item : arr) {
+ const auto& t = typeOf(item);
+ if (!itemType) {
+ itemType = {t};
+ } else if (type::toString(*itemType) == type::toString(t)) {
+ continue;
+ } else {
+ itemType = {type::Primitive::Value};
+ break;
+ }
+ }
+
+ return type::Array(type::Primitive::Value, arr.size());
+ }
+ );
+}
+
+} // namespace expression
+} // namespace style
+} // namespace mbgl