summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-09-26 17:53:49 +0300
committerMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-09-27 13:08:53 +0300
commitf3146e43cd19bdf85957d6d62132ac7c31eacc50 (patch)
tree34ba28b265d95911ccc07915b5162eb8edeba792
parent34ab7be2251bc8f6afca29c7a2ce3b6f1bc7cb10 (diff)
downloadqtlocation-mapboxgl-f3146e43cd19bdf85957d6d62132ac7c31eacc50.tar.gz
[core] ValueFactory for `expression::formatted`, other improvements
-rw-r--r--expression-test/expression_test_parser.cpp28
-rw-r--r--include/mbgl/style/conversion.hpp5
-rw-r--r--include/mbgl/style/conversion_impl.hpp29
-rw-r--r--include/mbgl/style/expression/formatted.hpp16
-rw-r--r--include/mbgl/util/traits.hpp10
-rw-r--r--src/mbgl/style/expression/formatted.cpp29
6 files changed, 69 insertions, 48 deletions
diff --git a/expression-test/expression_test_parser.cpp b/expression-test/expression_test_parser.cpp
index 3c194ffee0..546a96b3e0 100644
--- a/expression-test/expression_test_parser.cpp
+++ b/expression-test/expression_test_parser.cpp
@@ -439,33 +439,7 @@ optional<Value> toValue(const expression::Value& exprValue) {
std::vector<Value> color { double(c.r), double(c.g), double(c.b), double(c.a) };
return {Value{std::move(color)}};
},
- [](const expression::Formatted& formatted) -> optional<Value> {
- std::unordered_map<std::string, Value> serialized;
- std::vector<Value> sections;
- for (const auto& section : formatted.sections) {
- std::unordered_map<std::string, Value> serializedSection;
- serializedSection.emplace("text", section.text);
- if (section.fontScale) {
- serializedSection.emplace("scale", *section.fontScale);
- } else {
- serializedSection.emplace("scale", NullValue());
- }
- if (section.fontStack) {
- std::string fontStackString;
- serializedSection.emplace("fontStack", fontStackToString(*section.fontStack));
- } else {
- serializedSection.emplace("fontStack", NullValue());
- }
- if (section.textColor) {
- serializedSection.emplace("textColor", section.textColor->toObject());
- } else {
- serializedSection.emplace("textColor", NullValue());
- }
- sections.emplace_back(serializedSection);
- }
- serialized.emplace("sections", sections);
- return {Value{std::move(serialized)}};
- },
+ [](const expression::Formatted& formatted) -> optional<Value> { return {formatted.toObject()}; },
[](const std::vector<expression::Value>& values) -> optional<Value> {
std::vector<Value> mbglValues;
for (const auto& value : values) {
diff --git a/include/mbgl/style/conversion.hpp b/include/mbgl/style/conversion.hpp
index 2c83d1561b..29af9fac91 100644
--- a/include/mbgl/style/conversion.hpp
+++ b/include/mbgl/style/conversion.hpp
@@ -17,9 +17,12 @@ class ConversionTraits;
class Convertible;
-template <class T, class Enable = void>
+template <typename T, typename Enable = void>
struct Converter;
+template <typename T, typename Enable = void>
+struct ValueFactory;
+
} // namespace conversion
} // namespace style
} // namespace mbgl
diff --git a/include/mbgl/style/conversion_impl.hpp b/include/mbgl/style/conversion_impl.hpp
index ebeeee1c79..7abe7bf923 100644
--- a/include/mbgl/style/conversion_impl.hpp
+++ b/include/mbgl/style/conversion_impl.hpp
@@ -8,6 +8,7 @@
#include <mbgl/util/feature.hpp>
#include <mbgl/util/geojson.hpp>
#include <mbgl/util/optional.hpp>
+#include <mbgl/util/traits.hpp>
#include <mapbox/value.hpp>
@@ -297,20 +298,6 @@ optional<T> convert(const Convertible& value, Error& error, Args&&...args) {
return Converter<T>()(value, error, std::forward<Args>(args)...);
}
-template <typename T, typename = void>
-struct ValueFactory;
-
-template <typename T>
-struct ValueArrayFactory {
- static Value make(const T& arg) { return mapbox::base::ValueArray(arg.begin(), arg.end()); }
-};
-
-template <>
-struct ValueFactory<std::array<float, 2>> : public ValueArrayFactory<std::array<float, 2>> {};
-
-template <>
-struct ValueFactory<std::vector<float>> : public ValueArrayFactory<std::vector<float>> {};
-
template <>
struct ValueFactory<ColorRampPropertyValue> {
static Value make(const ColorRampPropertyValue& value) { return value.getExpression().serialize(); }
@@ -334,7 +321,7 @@ struct ValueFactory<Color> {
};
template <typename T>
-struct ValueFactory<T, typename std::enable_if<!std::is_enum<T>::value>::type> {
+struct ValueFactory<T, typename std::enable_if<(!std::is_enum<T>::value && !is_linear_container<T>::value)>::type> {
static Value make(const T& arg) { return {arg}; }
};
@@ -344,6 +331,18 @@ struct ValueFactory<T, typename std::enable_if<std::is_enum<T>::value>::type> {
};
template <typename T>
+struct ValueFactory<T, typename std::enable_if<is_linear_container<T>::value>::type> {
+ static Value make(const T& arg) {
+ mapbox::base::ValueArray result;
+ result.reserve(arg.size());
+ for (const auto& item : arg) {
+ result.emplace_back(ValueFactory<std::decay_t<decltype(item)>>::make(item));
+ }
+ return result;
+ }
+};
+
+template <typename T>
Value makeValue(T&& arg) {
return ValueFactory<std::decay_t<T>>::make(std::forward<T>(arg));
}
diff --git a/include/mbgl/style/expression/formatted.hpp b/include/mbgl/style/expression/formatted.hpp
index f4f08e9197..bb3d609c91 100644
--- a/include/mbgl/style/expression/formatted.hpp
+++ b/include/mbgl/style/expression/formatted.hpp
@@ -48,7 +48,8 @@ public:
bool operator==(const Formatted& ) const;
std::string toString() const;
-
+ mbgl::Value toObject() const;
+
bool empty() const {
return sections.empty() || sections.at(0).text.empty();
}
@@ -59,13 +60,18 @@ public:
} // namespace expression
namespace conversion {
-
+
template <>
-struct Converter<mbgl::style::expression::Formatted> {
+struct Converter<expression::Formatted> {
public:
- optional<mbgl::style::expression::Formatted> operator()(const Convertible& value, Error& error) const;
+ optional<expression::Formatted> operator()(const Convertible& value, Error& error) const;
};
-
+
+template <>
+struct ValueFactory<expression::Formatted> {
+ static Value make(const expression::Formatted& formatted) { return formatted.toObject(); }
+};
+
} // namespace conversion
} // namespace style
diff --git a/include/mbgl/util/traits.hpp b/include/mbgl/util/traits.hpp
index 5b9401aad7..e37144e60e 100644
--- a/include/mbgl/util/traits.hpp
+++ b/include/mbgl/util/traits.hpp
@@ -1,7 +1,9 @@
#pragma once
+#include <array>
#include <cstdint>
#include <type_traits>
+#include <vector>
namespace mbgl {
@@ -25,4 +27,12 @@ typename std::enable_if<is_utf16char_like<InChar>::value && is_utf16char_like_po
return reinterpret_cast<OutPointer>(in);
}
+template <typename T>
+struct is_linear_container : std::false_type {};
+
+template <typename T, std::size_t N>
+struct is_linear_container<std::array<T, N>> : std::true_type {};
+template <typename... Ts>
+struct is_linear_container<std::vector<Ts...>> : std::true_type {};
+
} // namespace mbgl
diff --git a/src/mbgl/style/expression/formatted.cpp b/src/mbgl/style/expression/formatted.cpp
index 5d45806ecb..4591a50ed1 100644
--- a/src/mbgl/style/expression/formatted.cpp
+++ b/src/mbgl/style/expression/formatted.cpp
@@ -35,6 +35,35 @@ std::string Formatted::toString() const {
return result;
}
+mbgl::Value Formatted::toObject() const {
+ mapbox::base::ValueObject result;
+ mapbox::base::ValueArray sectionValues;
+ sectionValues.reserve(sections.size());
+ for (const auto& section : sections) {
+ mapbox::base::ValueObject serializedSection;
+ serializedSection.emplace("text", section.text);
+ if (section.fontScale) {
+ serializedSection.emplace("scale", *section.fontScale);
+ } else {
+ serializedSection.emplace("scale", NullValue());
+ }
+ if (section.fontStack) {
+ std::string fontStackString;
+ serializedSection.emplace("fontStack", fontStackToString(*section.fontStack));
+ } else {
+ serializedSection.emplace("fontStack", NullValue());
+ }
+ if (section.textColor) {
+ serializedSection.emplace("textColor", section.textColor->toObject());
+ } else {
+ serializedSection.emplace("textColor", NullValue());
+ }
+ sectionValues.emplace_back(serializedSection);
+ }
+ result.emplace("sections", std::move(sectionValues));
+ return result;
+}
+
} // namespace expression
namespace conversion {