From f3146e43cd19bdf85957d6d62132ac7c31eacc50 Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Thu, 26 Sep 2019 17:53:49 +0300 Subject: [core] ValueFactory for `expression::formatted`, other improvements --- expression-test/expression_test_parser.cpp | 28 +--------------------------- include/mbgl/style/conversion.hpp | 5 ++++- include/mbgl/style/conversion_impl.hpp | 29 ++++++++++++++--------------- include/mbgl/style/expression/formatted.hpp | 16 +++++++++++----- include/mbgl/util/traits.hpp | 10 ++++++++++ src/mbgl/style/expression/formatted.cpp | 29 +++++++++++++++++++++++++++++ 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 toValue(const expression::Value& exprValue) { std::vector color { double(c.r), double(c.g), double(c.b), double(c.a) }; return {Value{std::move(color)}}; }, - [](const expression::Formatted& formatted) -> optional { - std::unordered_map serialized; - std::vector sections; - for (const auto& section : formatted.sections) { - std::unordered_map 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 { return {formatted.toObject()}; }, [](const std::vector& values) -> optional { std::vector 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 +template struct Converter; +template +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 #include #include +#include #include @@ -297,20 +298,6 @@ optional convert(const Convertible& value, Error& error, Args&&...args) { return Converter()(value, error, std::forward(args)...); } -template -struct ValueFactory; - -template -struct ValueArrayFactory { - static Value make(const T& arg) { return mapbox::base::ValueArray(arg.begin(), arg.end()); } -}; - -template <> -struct ValueFactory> : public ValueArrayFactory> {}; - -template <> -struct ValueFactory> : public ValueArrayFactory> {}; - template <> struct ValueFactory { static Value make(const ColorRampPropertyValue& value) { return value.getExpression().serialize(); } @@ -334,7 +321,7 @@ struct ValueFactory { }; template -struct ValueFactory::value>::type> { +struct ValueFactory::value && !is_linear_container::value)>::type> { static Value make(const T& arg) { return {arg}; } }; @@ -343,6 +330,18 @@ struct ValueFactory::value>::type> { static Value make(T arg) { return {int64_t(arg)}; } }; +template +struct ValueFactory::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>::make(item)); + } + return result; + } +}; + template Value makeValue(T&& arg) { return ValueFactory>::make(std::forward(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 { +struct Converter { public: - optional operator()(const Convertible& value, Error& error) const; + optional operator()(const Convertible& value, Error& error) const; }; - + +template <> +struct ValueFactory { + 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 #include #include +#include namespace mbgl { @@ -25,4 +27,12 @@ typename std::enable_if::value && is_utf16char_like_po return reinterpret_cast(in); } +template +struct is_linear_container : std::false_type {}; + +template +struct is_linear_container> : std::true_type {}; +template +struct is_linear_container> : 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 { -- cgit v1.2.1