diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2020-05-14 16:47:58 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2020-05-26 20:35:05 +0300 |
commit | 0489a8d88abca0966b18b028ec0b5bc33180885d (patch) | |
tree | 6dc6cf64150be3617a94cfad2e2a175c9021f8b1 | |
parent | bbd2e0adeaa7a361e6891a029fc7e54ab3a754fc (diff) | |
download | qtlocation-mapboxgl-0489a8d88abca0966b18b028ec0b5bc33180885d.tar.gz |
[core] Introduce getPropertyDefaultValue() API for sources
-rw-r--r-- | include/mbgl/style/source.hpp | 2 | ||||
-rw-r--r-- | include/mbgl/style/sources/tileset_source.hpp | 1 | ||||
-rw-r--r-- | src/mbgl/style/source.cpp | 95 | ||||
-rw-r--r-- | src/mbgl/style/sources/tileset_source.cpp | 8 | ||||
-rw-r--r-- | test/style/conversion/source.test.cpp | 48 |
5 files changed, 132 insertions, 22 deletions
diff --git a/include/mbgl/style/source.hpp b/include/mbgl/style/source.hpp index 3d5b0b1cbb..409384d5df 100644 --- a/include/mbgl/style/source.hpp +++ b/include/mbgl/style/source.hpp @@ -166,6 +166,7 @@ public: optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value); Value getProperty(const std::string&) const; + Value getPropertyDefaultValue(const std::string&) const; virtual mapbox::base::WeakPtr<Source> makeWeakPtr() = 0; const SourceTypeInfo* getTypeInfo() const noexcept; @@ -179,6 +180,7 @@ protected: virtual optional<conversion::Error> setPropertyInternal(const std::string& name, const conversion::Convertible& value); virtual Value getPropertyInternal(const std::string&) const; + virtual Value getPropertyDefaultValueInternal(const std::string&) const; }; } // namespace style diff --git a/include/mbgl/style/sources/tileset_source.hpp b/include/mbgl/style/sources/tileset_source.hpp index 25f6efe4d0..46b255808c 100644 --- a/include/mbgl/style/sources/tileset_source.hpp +++ b/include/mbgl/style/sources/tileset_source.hpp @@ -21,6 +21,7 @@ public: protected: TilesetSource(Immutable<Impl>, variant<std::string, Tileset> urlOrTileset); Value getPropertyInternal(const std::string&) const override; + Value getPropertyDefaultValueInternal(const std::string&) const override; const variant<std::string, Tileset> urlOrTileset; }; diff --git a/src/mbgl/style/source.cpp b/src/mbgl/style/source.cpp index 9366117fc6..dd3ec4caa6 100644 --- a/src/mbgl/style/source.cpp +++ b/src/mbgl/style/source.cpp @@ -6,11 +6,79 @@ #include <mbgl/util/logging.hpp> #include <mbgl/util/tileset.hpp> +#include <mapbox/eternal.hpp> + namespace mbgl { namespace style { static SourceObserver nullObserver; +namespace { + +enum class Property : uint8_t { + Volatile, + Attribution, + TileSize, + PrefetchZoomDelta, + MaxOverscaleFactorForParentTiles, + MinTileUpdateInterval, + Type +}; + +constexpr const auto sourceProperties = mapbox::eternal::hash_map<mapbox::eternal::string, uint8_t>( + {{"volatile", underlying_type(Property::Volatile)}, + {"attribution", underlying_type(Property::Attribution)}, + {"tile-size", underlying_type(Property::TileSize)}, + {"prefetch-zoom-delta", underlying_type(Property::PrefetchZoomDelta)}, + {"max-overscale-factor-for-parent-tiles", underlying_type(Property::MaxOverscaleFactorForParentTiles)}, + {"minimum-tile-update-interval", underlying_type(Property::MinTileUpdateInterval)}, + {"type", underlying_type(Property::Type)}}); + +Value getSourcePropertyDefaultValue(Property property) { + switch (property) { + case Property::Volatile: + return false; + case Property::TileSize: + return util::tileSize; + case Property::PrefetchZoomDelta: + return util::DEFAULT_PREFETCH_ZOOM_DELTA; + case Property::Attribution: + case Property::MaxOverscaleFactorForParentTiles: + case Property::Type: + return {}; + case Property::MinTileUpdateInterval: + return 0.0; + } + assert(false); + return NullValue(); +} + +Value getSourceProperty(const Source* source, Property property) { + using namespace conversion; + switch (property) { + case Property::Volatile: + return source->isVolatile(); + case Property::Attribution: + return makeValue(source->getAttribution()); + case Property::TileSize: + return makeValue(source->getTileSize()); + case Property::PrefetchZoomDelta: + return makeValue(source->getPrefetchZoomDelta()); + case Property::MaxOverscaleFactorForParentTiles: + return makeValue(source->getMaxOverscaleFactorForParentTiles()); + case Property::MinTileUpdateInterval: { + auto seconds = + std::chrono::duration_cast<std::chrono::duration<float>>(source->getMinimumTileUpdateInterval()); + return makeValue(seconds.count()); + } + case Property::Type: + return makeValue(source->getTypeInfo()->type); + } + return NullValue(); +} + +} // namespace + Source::Source(Immutable<Impl> impl) : baseImpl(std::move(impl)), observer(&nullObserver) {} Source::~Source() = default; @@ -114,22 +182,21 @@ optional<conversion::Error> Source::setProperty(const std::string& name, const c } Value Source::getProperty(const std::string& name) const { - using namespace conversion; - if (name == "type") return makeValue(getTypeInfo()->type); - if (name == "volatile") return makeValue(isVolatile()); - if (name == "attribution") return makeValue(getAttribution()); - if (name == "tile-size") return makeValue(getTileSize()); - if (name == "prefetch-zoom-delta") return makeValue(getPrefetchZoomDelta()); - if (name == "max-overscale-factor-for-parent-tiles") { - return makeValue(getMaxOverscaleFactorForParentTiles()); - } - if (name == "minimum-tile-update-interval") { - auto seconds = std::chrono::duration_cast<std::chrono::duration<float>>(getMinimumTileUpdateInterval()); - return makeValue(seconds.count()); + const auto it = sourceProperties.find(name.c_str()); + if (it != sourceProperties.end()) { + return getSourceProperty(this, static_cast<Property>(it->second)); } return getPropertyInternal(name); } +Value Source::getPropertyDefaultValue(const std::string& name) const { + const auto it = sourceProperties.find(name.c_str()); + if (it != sourceProperties.end()) { + return getSourcePropertyDefaultValue(static_cast<Property>(it->second)); + } + return getPropertyDefaultValueInternal(name); +} + const SourceTypeInfo* Source::getTypeInfo() const noexcept { return baseImpl->getTypeInfo(); } @@ -153,5 +220,9 @@ Value Source::getPropertyInternal(const std::string&) const { return NullValue(); } +Value Source::getPropertyDefaultValueInternal(const std::string&) const { + return NullValue(); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/tileset_source.cpp b/src/mbgl/style/sources/tileset_source.cpp index a22ad0a6c7..45bad4e010 100644 --- a/src/mbgl/style/sources/tileset_source.cpp +++ b/src/mbgl/style/sources/tileset_source.cpp @@ -76,5 +76,13 @@ Value TilesetSource::getPropertyInternal(const std::string& name) const { return Value(); } +Value TilesetSource::getPropertyDefaultValueInternal(const std::string& name) const { + using namespace conversion; + if (name == "scheme") return makeValue(TilesetScheme::XYZ); + if (name == "minzoom") return 0u; + if (name == "maxzoom") return util::DEFAULT_MAX_ZOOM; + return Value(); +} + } // namespace style } // namespace mbgl diff --git a/test/style/conversion/source.test.cpp b/test/style/conversion/source.test.cpp index fcaff594e7..f969384746 100644 --- a/test/style/conversion/source.test.cpp +++ b/test/style/conversion/source.test.cpp @@ -16,11 +16,18 @@ std::unique_ptr<Source> parseSource(const std::string& src, const std::string& s return nullptr; } -void checkConstProperty(std::unique_ptr<Source>& source, const std::string& propertyName, const mbgl::Value& expected) { +void checkGetProperty(std::unique_ptr<Source>& source, const std::string& propertyName, const mbgl::Value& expected) { Value value = source->getProperty(propertyName); EXPECT_EQ(expected, value) << "get property: " << propertyName; } +void checkGetPropertyDefaultValue(std::unique_ptr<Source>& source, + const std::string& propertyName, + const mbgl::Value& expected) { + Value value = source->getPropertyDefaultValue(propertyName); + EXPECT_EQ(expected, value) << "get property: " << propertyName; +} + void checkSetProperty(std::unique_ptr<Source>& source, const std::string& propertyName, const JSValue& value) { auto error = source->setProperty(propertyName, Convertible(&value)); EXPECT_EQ(nullopt, error) << "set property: " << propertyName << ", error: " << error->message; @@ -41,21 +48,21 @@ TEST(StyleConversion, SetSourceGenericProperties) { "vector_source"); ASSERT_NE(nullptr, source); - checkConstProperty(source, "volatile", false); + checkGetProperty(source, "volatile", false); checkSetProperty(source, "volatile", JSValue(true)); - checkConstProperty(source, "volatile", true); + checkGetProperty(source, "volatile", true); - checkConstProperty(source, "minimum-tile-update-interval", 0.0); + checkGetProperty(source, "minimum-tile-update-interval", 0.0); checkSetProperty(source, "minimum-tile-update-interval", JSValue(10.5)); - checkConstProperty(source, "minimum-tile-update-interval", 10.5); + checkGetProperty(source, "minimum-tile-update-interval", 10.5); - checkConstProperty(source, "prefetch-zoom-delta", NullValue()); + checkGetProperty(source, "prefetch-zoom-delta", NullValue()); checkSetProperty(source, "prefetch-zoom-delta", JSValue(0)); - checkConstProperty(source, "prefetch-zoom-delta", 0u); + checkGetProperty(source, "prefetch-zoom-delta", 0u); - checkConstProperty(source, "max-overscale-factor-for-parent-tiles", NullValue()); + checkGetProperty(source, "max-overscale-factor-for-parent-tiles", NullValue()); checkSetProperty(source, "max-overscale-factor-for-parent-tiles", JSValue(2)); - checkConstProperty(source, "max-overscale-factor-for-parent-tiles", 2u); + checkGetProperty(source, "max-overscale-factor-for-parent-tiles", 2u); source = parseSource(R"JSON({ "type": "geojson", @@ -82,4 +89,25 @@ TEST(StyleConversion, SetSourceGenericProperties) { const JSValue* geometry = &document; checkSetProperty(source, "data", *geometry); EXPECT_TRUE(geojsonSource->getGeoJSONData()); -}
\ No newline at end of file +} + +TEST(StyleConversion, GetSourceGenericPropertyDefaultValues) { + auto source = parseSource(R"JSON({ + "type": "vector", + "tiles": ["http://example.com/{z}-{x}-{y}.vector.pbf"], + "scheme": "tms", + "minzoom": 11, + "maxzoom": 16, + "attribution": "mapbox", + "bounds": [-180, -73, -120, 73] + })JSON", + "vector_source"); + + ASSERT_NE(nullptr, source); + checkGetPropertyDefaultValue(source, "volatile", false); + checkGetPropertyDefaultValue(source, "minimum-tile-update-interval", 0.0); + checkGetPropertyDefaultValue(source, "prefetch-zoom-delta", 4u); + checkGetPropertyDefaultValue(source, "minzoom", 0u); + checkGetPropertyDefaultValue(source, "maxzoom", 22u); + checkGetPropertyDefaultValue(source, "scheme", "xyz"); +} |