[core] ValueFactory for expression::formatted, other improvements

This commit is contained in:
Mikhail Pozdnyakov
2019-09-26 17:53:49 +03:00
parent 34ab7be225
commit f3146e43cd
6 changed files with 69 additions and 48 deletions

View File

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

View File

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

View File

@@ -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}; }
};
@@ -343,6 +330,18 @@ struct ValueFactory<T, typename std::enable_if<std::is_enum<T>::value>::type> {
static Value make(T arg) { return {int64_t(arg)}; }
};
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));

View File

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

View File

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

View File

@@ -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 {