diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2020-05-08 18:00:02 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2020-05-26 20:35:04 +0300 |
commit | 03ecfa2644cca12048b5bb953f116c929c67bace (patch) | |
tree | fbf208ad2b2cf90c57ab702a0d28363a4e790558 | |
parent | 1237e91a16d6b22df88c3fa25f3bc29b7734f88e (diff) | |
download | qtlocation-mapboxgl-03ecfa2644cca12048b5bb953f116c929c67bace.tar.gz |
[core] Universal set/getProperty API for ImageSource
-rw-r--r-- | include/mbgl/style/conversion/coordinate.hpp | 8 | ||||
-rw-r--r-- | include/mbgl/style/conversion_impl.hpp | 8 | ||||
-rw-r--r-- | include/mbgl/style/sources/image_source.hpp | 3 | ||||
-rw-r--r-- | src/mbgl/sourcemanager/image_source_factory.cpp | 18 | ||||
-rw-r--r-- | src/mbgl/style/conversion/coordinate.cpp | 17 | ||||
-rw-r--r-- | src/mbgl/style/sources/image_source.cpp | 36 |
6 files changed, 68 insertions, 22 deletions
diff --git a/include/mbgl/style/conversion/coordinate.hpp b/include/mbgl/style/conversion/coordinate.hpp index 1346ed738a..77dfccc7a0 100644 --- a/include/mbgl/style/conversion/coordinate.hpp +++ b/include/mbgl/style/conversion/coordinate.hpp @@ -4,6 +4,8 @@ #include <mbgl/util/geo.hpp> #include <mbgl/util/optional.hpp> +#include <array> + namespace mbgl { namespace style { namespace conversion { @@ -14,6 +16,12 @@ public: optional<LatLng> operator() (const Convertible& value, Error& error) const; }; +template <> +struct Converter<std::array<LatLng, 4>> { +public: + optional<std::array<LatLng, 4>> operator()(const Convertible& value, Error& error) const; +}; + } // namespace conversion } // namespace style } // namespace mbgl diff --git a/include/mbgl/style/conversion_impl.hpp b/include/mbgl/style/conversion_impl.hpp index 51a7a7db57..b1855cd09a 100644 --- a/include/mbgl/style/conversion_impl.hpp +++ b/include/mbgl/style/conversion_impl.hpp @@ -352,6 +352,14 @@ struct ValueFactory<LatLngBounds> { } }; +template <> +struct ValueFactory<LatLng> { + static Value make(const LatLng& coordinate) { + std::vector<mapbox::base::Value> value{coordinate.longitude(), coordinate.latitude()}; + return value; + } +}; + template <typename T> Value makeValue(T&& arg) { return ValueFactory<std::decay_t<T>>::make(std::forward<T>(arg)); diff --git a/include/mbgl/style/sources/image_source.hpp b/include/mbgl/style/sources/image_source.hpp index cfe5a6521f..62597a9613 100644 --- a/include/mbgl/style/sources/image_source.hpp +++ b/include/mbgl/style/sources/image_source.hpp @@ -38,6 +38,9 @@ public: protected: Mutable<Source::Impl> createMutable() const noexcept final; + optional<conversion::Error> setPropertyInternal(const std::string& name, + const conversion::Convertible& value) override; + Value getPropertyInternal(const std::string&) const override; private: optional<std::string> url; diff --git a/src/mbgl/sourcemanager/image_source_factory.cpp b/src/mbgl/sourcemanager/image_source_factory.cpp index 3cd3c41914..f3de36bcf9 100644 --- a/src/mbgl/sourcemanager/image_source_factory.cpp +++ b/src/mbgl/sourcemanager/image_source_factory.cpp @@ -32,23 +32,13 @@ std::unique_ptr<style::Source> ImageSourceFactory::createSource(const std::strin return nullptr; } - if (!isArray(*coordinatesValue) || arrayLength(*coordinatesValue) != 4) { - error.message = "Image coordinates must be an array of four longitude latitude pairs"; - return nullptr; - } + auto coordinates = style::conversion::convert<std::array<LatLng, 4>>(*coordinatesValue, error); + if (!coordinates) return nullptr; - std::array<LatLng, 4> coordinates; - for (std::size_t i = 0; i < 4; i++) { - auto latLng = style::conversion::convert<LatLng>(arrayMember(*coordinatesValue, i), error); - if (!latLng) { - return nullptr; - } - coordinates[i] = *latLng; - } - auto result = std::make_unique<style::ImageSource>(id, coordinates); + auto result = std::make_unique<style::ImageSource>(id, *coordinates); result->setURL(*urlString); - return {std::move(result)}; + return result; } std::unique_ptr<RenderSource> ImageSourceFactory::createRenderSource(Immutable<style::Source::Impl> impl) noexcept { diff --git a/src/mbgl/style/conversion/coordinate.cpp b/src/mbgl/style/conversion/coordinate.cpp index ee03bffb30..9435403659 100644 --- a/src/mbgl/style/conversion/coordinate.cpp +++ b/src/mbgl/style/conversion/coordinate.cpp @@ -25,6 +25,23 @@ optional<LatLng> Converter<LatLng>::operator() (const Convertible& value, Error& return LatLng(*latitude, *longitude); } +optional<std::array<LatLng, 4>> Converter<std::array<LatLng, 4>>::operator()(const Convertible& value, + Error& error) const { + if (!isArray(value) || arrayLength(value) != 4u) { + error.message = "Image coordinates must be an array of four longitude latitude pairs"; + return nullopt; + } + std::array<LatLng, 4> coordinates; + for (std::size_t i = 0; i < 4u; ++i) { + auto latLng = convert<LatLng>(arrayMember(value, i), error); + if (!latLng) { + return nullopt; + } + coordinates[i] = *latLng; + } + return coordinates; +} + } // namespace conversion } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/image_source.cpp b/src/mbgl/style/sources/image_source.cpp index 7b62c6fb9d..2e2f8c0148 100644 --- a/src/mbgl/style/sources/image_source.cpp +++ b/src/mbgl/style/sources/image_source.cpp @@ -1,4 +1,7 @@ #include <mbgl/storage/file_source.hpp> +#include <mbgl/style/conversion/constant.hpp> +#include <mbgl/style/conversion/coordinate.hpp> +#include <mbgl/style/conversion_impl.hpp> #include <mbgl/style/layer.hpp> #include <mbgl/style/source_observer.hpp> #include <mbgl/style/sources/image_source.hpp> @@ -105,22 +108,39 @@ Mutable<Source::Impl> ImageSource::createMutable() const noexcept { } Value ImageSource::serialize() const { + using namespace conversion; auto value = Source::serialize(); assert(value.getObject()); auto object = value.getObject(); - if (url) object->insert({"url", url.value()}); + object->insert({"coordinates", makeValue(getCoordinates())}); - const auto source_coordinates = getCoordinates(); - std::vector<mapbox::base::Value> coordinates; - coordinates.reserve(source_coordinates.size()); + return value; +} - for (const auto& c : source_coordinates) { - coordinates.emplace_back(std::vector<mapbox::base::Value>{c.longitude(), c.latitude()}); +optional<conversion::Error> ImageSource::setPropertyInternal(const std::string& name, + const conversion::Convertible& value) { + using namespace conversion; + optional<Error> error; + if (name == "url") { + if (auto url_ = convert<std::string>(value, *error)) { + setURL(*url_); + return nullopt; + } + } else if (name == "coordinates") { + if (auto coordinates = convert<std::array<LatLng, 4>>(value, *error)) { + setCoordinates(*coordinates); + return nullopt; + } } - object->insert({"coordinates", coordinates}); + return error ? error : Source::setPropertyInternal(name, value); +} - return value; +Value ImageSource::getPropertyInternal(const std::string& name) const { + using namespace conversion; + if (name == "url" && url) return *url; + if (name == "coordinates") return makeValue(getCoordinates()); + return Value(); } } // namespace style |