diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2019-09-26 17:53:49 +0300 |
---|---|---|
committer | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2019-09-27 13:08:53 +0300 |
commit | f3146e43cd19bdf85957d6d62132ac7c31eacc50 (patch) | |
tree | 34ba28b265d95911ccc07915b5162eb8edeba792 /include/mbgl | |
parent | 34ab7be2251bc8f6afca29c7a2ce3b6f1bc7cb10 (diff) | |
download | qtlocation-mapboxgl-f3146e43cd19bdf85957d6d62132ac7c31eacc50.tar.gz |
[core] ValueFactory for `expression::formatted`, other improvements
Diffstat (limited to 'include/mbgl')
-rw-r--r-- | include/mbgl/style/conversion.hpp | 5 | ||||
-rw-r--r-- | include/mbgl/style/conversion_impl.hpp | 29 | ||||
-rw-r--r-- | include/mbgl/style/expression/formatted.hpp | 16 | ||||
-rw-r--r-- | include/mbgl/util/traits.hpp | 10 |
4 files changed, 39 insertions, 21 deletions
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 |