From b9b32dd623e8580c2fe0b8509b34a90b1b42df5d Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Thu, 25 Oct 2018 16:15:31 +0300 Subject: Consolidate `style::Layer` properties API The `style::Layer` class now exposes all the properties contained at `style::LayerImpl`. This allowed to drop `style::Layer::accept()` method usage, avoid the repeated generated code and thus save some binary size. This patch is a part of the layers modularization effort. --- include/mbgl/style/layer.hpp | 73 ++++++------------ include/mbgl/style/layers/background_layer.hpp | 10 +-- include/mbgl/style/layers/circle_layer.hpp | 18 +---- include/mbgl/style/layers/custom_layer.hpp | 12 +-- include/mbgl/style/layers/fill_extrusion_layer.hpp | 18 +---- include/mbgl/style/layers/fill_layer.hpp | 18 +---- include/mbgl/style/layers/heatmap_layer.hpp | 18 +---- include/mbgl/style/layers/hillshade_layer.hpp | 13 +--- include/mbgl/style/layers/layer.hpp.ejs | 22 +----- include/mbgl/style/layers/line_layer.hpp | 18 +---- include/mbgl/style/layers/raster_layer.hpp | 13 +--- include/mbgl/style/layers/symbol_layer.hpp | 18 +---- include/mbgl/util/immutable.hpp | 6 ++ platform/android/src/style/layers/layer.cpp | 87 ++-------------------- platform/android/src/style/layers/layers.cpp | 82 +++++++++----------- platform/node/src/node_map.cpp | 27 +------ platform/qt/src/qmapboxgl.cpp | 45 +---------- src/mbgl/style/layer.cpp | 48 ++++++++++++ src/mbgl/style/layers/background_layer.cpp | 32 +------- src/mbgl/style/layers/circle_layer.cpp | 60 +-------------- src/mbgl/style/layers/custom_layer.cpp | 29 +------- src/mbgl/style/layers/fill_extrusion_layer.cpp | 60 +-------------- src/mbgl/style/layers/fill_layer.cpp | 60 +-------------- src/mbgl/style/layers/heatmap_layer.cpp | 60 +-------------- src/mbgl/style/layers/hillshade_layer.cpp | 38 +--------- src/mbgl/style/layers/layer.cpp.ejs | 64 +--------------- src/mbgl/style/layers/line_layer.cpp | 60 +-------------- src/mbgl/style/layers/raster_layer.cpp | 38 +--------- src/mbgl/style/layers/symbol_layer.cpp | 60 +-------------- src/mbgl/style/style_impl.cpp | 25 ++----- 30 files changed, 203 insertions(+), 929 deletions(-) diff --git a/include/mbgl/style/layer.hpp b/include/mbgl/style/layer.hpp index 3b7969ea79..dd8f1a1e0e 100644 --- a/include/mbgl/style/layer.hpp +++ b/include/mbgl/style/layer.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include #include @@ -27,6 +26,7 @@ class CustomLayer; class FillExtrusionLayer; class HeatmapLayer; class LayerObserver; +class Filter; /** * The runtime representation of a [layer](https://www.mapbox.com/mapbox-gl-style-spec/#layers) from the Mapbox Style @@ -44,8 +44,11 @@ class LayerObserver; * * auto circleLayer = std::make_unique("my-circle-layer"); */ -class Layer : public mbgl::util::noncopyable { +class Layer { public: + Layer(const Layer& ) = delete; + Layer& operator=(const Layer&) = delete; + virtual ~Layer(); // Check whether this layer is of the given subtype. @@ -63,60 +66,26 @@ public: return is() ? reinterpret_cast(this) : nullptr; } - // Convenience method for dynamic dispatch on the concrete layer type. Using - // method overloading, this allows consolidation of logic common to vector-based - // layers (Fill, FillExtrusion, Line, Circle, or Symbol). For example: - // - // struct Visitor { - // void operator()(CustomLayer&) { ... } - // void operator()(RasterLayer&) { ... } - // void operator()(BackgroundLayer&) { ... } - // template - // void operator()(VectorLayer&) { ... } - // }; - // - template - auto accept(V&& visitor) { - switch (getType()) { - case LayerType::Fill: - return std::forward(visitor)(*as()); - case LayerType::Line: - return std::forward(visitor)(*as()); - case LayerType::Circle: - return std::forward(visitor)(*as()); - case LayerType::Symbol: - return std::forward(visitor)(*as()); - case LayerType::Raster: - return std::forward(visitor)(*as()); - case LayerType::Background: - return std::forward(visitor)(*as()); - case LayerType::Hillshade: - return std::forward(visitor)(*as()); - case LayerType::Custom: - return std::forward(visitor)(*as()); - case LayerType::FillExtrusion: - return std::forward(visitor)(*as()); - case LayerType::Heatmap: - return std::forward(visitor)(*as()); - } - - // Not reachable, but placate GCC. - assert(false); - throw new std::runtime_error("unknown layer type"); - } - LayerType getType() const; std::string getID() const; + // Source + std::string getSourceID() const; + std::string getSourceLayer() const; + void setSourceLayer(const std::string& sourceLayer); + + // Filter + const Filter& getFilter() const; + void setFilter(const Filter&); // Visibility VisibilityType getVisibility() const; - virtual void setVisibility(VisibilityType) = 0; + void setVisibility(VisibilityType); // Zoom range float getMinZoom() const; float getMaxZoom() const; - virtual void setMinZoom(float) = 0; - virtual void setMaxZoom(float) = 0; + void setMinZoom(float); + void setMaxZoom(float); // Dynamic properties virtual optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) = 0; @@ -124,21 +93,25 @@ public: optional setVisibility(const conversion::Convertible& value); // Private implementation + // TODO : We should not have public mutable data members. class Impl; Immutable baseImpl; - Layer(Immutable); - // Create a layer, copying all properties except id and paint properties from this layer. virtual std::unique_ptr cloneRef(const std::string& id) const = 0; - LayerObserver* observer = nullptr; + void setObserver(LayerObserver*); // For use in SDK bindings, which store a reference to a platform-native peer // object here, so that separately-obtained references to this object share // identical platform-native peers. util::peer peer; +protected: + Layer(Immutable); + virtual Mutable mutableBaseImpl() const = 0; + + LayerObserver* observer = nullptr; }; } // namespace style diff --git a/include/mbgl/style/layers/background_layer.hpp b/include/mbgl/style/layers/background_layer.hpp index ef01ea41be..7d7753cbff 100644 --- a/include/mbgl/style/layers/background_layer.hpp +++ b/include/mbgl/style/layers/background_layer.hpp @@ -19,13 +19,6 @@ public: BackgroundLayer(const std::string& layerID); ~BackgroundLayer() final; - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -58,6 +51,9 @@ public: Mutable mutableImpl() const; BackgroundLayer(Immutable); std::unique_ptr cloneRef(const std::string& id) const final; + +protected: + Mutable mutableBaseImpl() const final; }; template <> diff --git a/include/mbgl/style/layers/circle_layer.hpp b/include/mbgl/style/layers/circle_layer.hpp index 0f8d1c0c13..909412dbab 100644 --- a/include/mbgl/style/layers/circle_layer.hpp +++ b/include/mbgl/style/layers/circle_layer.hpp @@ -19,21 +19,6 @@ public: CircleLayer(const std::string& layerID, const std::string& sourceID); ~CircleLayer() final; - // Source - const std::string& getSourceID() const; - const std::string& getSourceLayer() const; - void setSourceLayer(const std::string& sourceLayer); - - void setFilter(const Filter&); - const Filter& getFilter() const; - - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -114,6 +99,9 @@ public: Mutable mutableImpl() const; CircleLayer(Immutable); std::unique_ptr cloneRef(const std::string& id) const final; + +protected: + Mutable mutableBaseImpl() const final; }; template <> diff --git a/include/mbgl/style/layers/custom_layer.hpp b/include/mbgl/style/layers/custom_layer.hpp index 4b4c770489..4ae59dfae3 100644 --- a/include/mbgl/style/layers/custom_layer.hpp +++ b/include/mbgl/style/layers/custom_layer.hpp @@ -68,13 +68,6 @@ public: ~CustomLayer() final; - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -88,10 +81,9 @@ public: std::unique_ptr cloneRef(const std::string& id) const final; CustomLayer(const CustomLayer&) = delete; -}; -template <> -bool Layer::is() const; + Mutable mutableBaseImpl() const final; +}; } // namespace style } // namespace mbgl diff --git a/include/mbgl/style/layers/fill_extrusion_layer.hpp b/include/mbgl/style/layers/fill_extrusion_layer.hpp index d30ffa26da..8798738d90 100644 --- a/include/mbgl/style/layers/fill_extrusion_layer.hpp +++ b/include/mbgl/style/layers/fill_extrusion_layer.hpp @@ -19,21 +19,6 @@ public: FillExtrusionLayer(const std::string& layerID, const std::string& sourceID); ~FillExtrusionLayer() final; - // Source - const std::string& getSourceID() const; - const std::string& getSourceLayer() const; - void setSourceLayer(const std::string& sourceLayer); - - void setFilter(const Filter&); - const Filter& getFilter() const; - - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -90,6 +75,9 @@ public: Mutable mutableImpl() const; FillExtrusionLayer(Immutable); std::unique_ptr cloneRef(const std::string& id) const final; + +protected: + Mutable mutableBaseImpl() const final; }; template <> diff --git a/include/mbgl/style/layers/fill_layer.hpp b/include/mbgl/style/layers/fill_layer.hpp index 25c46d312f..033b451abc 100644 --- a/include/mbgl/style/layers/fill_layer.hpp +++ b/include/mbgl/style/layers/fill_layer.hpp @@ -19,21 +19,6 @@ public: FillLayer(const std::string& layerID, const std::string& sourceID); ~FillLayer() final; - // Source - const std::string& getSourceID() const; - const std::string& getSourceLayer() const; - void setSourceLayer(const std::string& sourceLayer); - - void setFilter(const Filter&); - const Filter& getFilter() const; - - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -90,6 +75,9 @@ public: Mutable mutableImpl() const; FillLayer(Immutable); std::unique_ptr cloneRef(const std::string& id) const final; + +protected: + Mutable mutableBaseImpl() const final; }; template <> diff --git a/include/mbgl/style/layers/heatmap_layer.hpp b/include/mbgl/style/layers/heatmap_layer.hpp index 347bb8a4aa..2315eac3f2 100644 --- a/include/mbgl/style/layers/heatmap_layer.hpp +++ b/include/mbgl/style/layers/heatmap_layer.hpp @@ -20,21 +20,6 @@ public: HeatmapLayer(const std::string& layerID, const std::string& sourceID); ~HeatmapLayer() final; - // Source - const std::string& getSourceID() const; - const std::string& getSourceLayer() const; - void setSourceLayer(const std::string& sourceLayer); - - void setFilter(const Filter&); - const Filter& getFilter() const; - - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -79,6 +64,9 @@ public: Mutable mutableImpl() const; HeatmapLayer(Immutable); std::unique_ptr cloneRef(const std::string& id) const final; + +protected: + Mutable mutableBaseImpl() const final; }; template <> diff --git a/include/mbgl/style/layers/hillshade_layer.hpp b/include/mbgl/style/layers/hillshade_layer.hpp index 697d4c71ad..4c18b5ed89 100644 --- a/include/mbgl/style/layers/hillshade_layer.hpp +++ b/include/mbgl/style/layers/hillshade_layer.hpp @@ -19,16 +19,6 @@ public: HillshadeLayer(const std::string& layerID, const std::string& sourceID); ~HillshadeLayer() final; - // Source - const std::string& getSourceID() const; - - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -79,6 +69,9 @@ public: Mutable mutableImpl() const; HillshadeLayer(Immutable); std::unique_ptr cloneRef(const std::string& id) const final; + +protected: + Mutable mutableBaseImpl() const final; }; template <> diff --git a/include/mbgl/style/layers/layer.hpp.ejs b/include/mbgl/style/layers/layer.hpp.ejs index 9d595d2035..f6dd08000a 100644 --- a/include/mbgl/style/layers/layer.hpp.ejs +++ b/include/mbgl/style/layers/layer.hpp.ejs @@ -35,25 +35,6 @@ public: <% } -%> ~<%- camelize(type) %>Layer() final; -<% if (type !== 'background') { -%> - // Source - const std::string& getSourceID() const; -<% if (type !== 'raster' && type !== 'hillshade') { -%> - const std::string& getSourceLayer() const; - void setSourceLayer(const std::string& sourceLayer); - - void setFilter(const Filter&); - const Filter& getFilter() const; -<% } -%> - -<% } -%> - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -86,6 +67,9 @@ public: Mutable mutableImpl() const; <%- camelize(type) %>Layer(Immutable); std::unique_ptr cloneRef(const std::string& id) const final; + +protected: + Mutable mutableBaseImpl() const final; }; template <> diff --git a/include/mbgl/style/layers/line_layer.hpp b/include/mbgl/style/layers/line_layer.hpp index 1e55561bbd..0e1d026e74 100644 --- a/include/mbgl/style/layers/line_layer.hpp +++ b/include/mbgl/style/layers/line_layer.hpp @@ -22,21 +22,6 @@ public: LineLayer(const std::string& layerID, const std::string& sourceID); ~LineLayer() final; - // Source - const std::string& getSourceID() const; - const std::string& getSourceLayer() const; - void setSourceLayer(const std::string& sourceLayer); - - void setFilter(const Filter&); - const Filter& getFilter() const; - - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -135,6 +120,9 @@ public: Mutable mutableImpl() const; LineLayer(Immutable); std::unique_ptr cloneRef(const std::string& id) const final; + +protected: + Mutable mutableBaseImpl() const final; }; template <> diff --git a/include/mbgl/style/layers/raster_layer.hpp b/include/mbgl/style/layers/raster_layer.hpp index b1c716c17f..2d10d65914 100644 --- a/include/mbgl/style/layers/raster_layer.hpp +++ b/include/mbgl/style/layers/raster_layer.hpp @@ -19,16 +19,6 @@ public: RasterLayer(const std::string& layerID, const std::string& sourceID); ~RasterLayer() final; - // Source - const std::string& getSourceID() const; - - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -91,6 +81,9 @@ public: Mutable mutableImpl() const; RasterLayer(Immutable); std::unique_ptr cloneRef(const std::string& id) const final; + +protected: + Mutable mutableBaseImpl() const final; }; template <> diff --git a/include/mbgl/style/layers/symbol_layer.hpp b/include/mbgl/style/layers/symbol_layer.hpp index 6d82e5df85..b75a943be0 100644 --- a/include/mbgl/style/layers/symbol_layer.hpp +++ b/include/mbgl/style/layers/symbol_layer.hpp @@ -21,21 +21,6 @@ public: SymbolLayer(const std::string& layerID, const std::string& sourceID); ~SymbolLayer() final; - // Source - const std::string& getSourceID() const; - const std::string& getSourceLayer() const; - void setSourceLayer(const std::string& sourceLayer); - - void setFilter(const Filter&); - const Filter& getFilter() const; - - // Visibility - void setVisibility(VisibilityType) final; - - // Zoom range - void setMinZoom(float) final; - void setMaxZoom(float) final; - // Dynamic properties optional setLayoutProperty(const std::string& name, const conversion::Convertible& value) final; optional setPaintProperty(const std::string& name, const conversion::Convertible& value) final; @@ -284,6 +269,9 @@ public: Mutable mutableImpl() const; SymbolLayer(Immutable); std::unique_ptr cloneRef(const std::string& id) const final; + +protected: + Mutable mutableBaseImpl() const final; }; template <> diff --git a/include/mbgl/util/immutable.hpp b/include/mbgl/util/immutable.hpp index eb26c0d282..b4117166dd 100644 --- a/include/mbgl/util/immutable.hpp +++ b/include/mbgl/util/immutable.hpp @@ -39,6 +39,7 @@ private: template friend class Immutable; template friend Mutable makeMutable(Args&&...); + template friend Mutable staticMutableCast(const Mutable&); }; template @@ -46,6 +47,11 @@ Mutable makeMutable(Args&&... args) { return Mutable(std::make_shared(std::forward(args)...)); } +template +Mutable staticMutableCast(const Mutable& u) { + return Mutable(std::static_pointer_cast(u.ptr)); +} + /** * `Immutable` is a non-nullable shared reference to a `const T`. Construction requires * a transfer of unique ownership from a `Mutable`; once constructed it has the same behavior diff --git a/platform/android/src/style/layers/layer.cpp b/platform/android/src/style/layers/layer.cpp index 5726873f40..bb543a7639 100644 --- a/platform/android/src/style/layers/layer.cpp +++ b/platform/android/src/style/layers/layer.cpp @@ -109,20 +109,6 @@ namespace android { } } - struct SetFilterEvaluator { - style::Filter filter; - - void operator()(style::BackgroundLayer&) { Log::Warning(mbgl::Event::JNI, "BackgroundLayer doesn't support filters"); } - void operator()(style::CustomLayer&) { Log::Warning(mbgl::Event::JNI, "CustomLayer doesn't support filters"); } - void operator()(style::RasterLayer&) { Log::Warning(mbgl::Event::JNI, "RasterLayer doesn't support filters"); } - void operator()(style::HillshadeLayer&) { Log::Warning(mbgl::Event::JNI, "HillshadeLayer doesn't support filters"); } - - template - void operator()(LayerType& layer) { - layer.setFilter(filter); - } - }; - void Layer::setFilter(jni::JNIEnv& env, const jni::Array>& jfilter) { using namespace mbgl::style; using namespace mbgl::style::conversion; @@ -134,31 +120,14 @@ namespace android { return; } - layer.accept(SetFilterEvaluator {std::move(*converted)}); + layer.setFilter(std::move(*converted)); } - struct GetFilterEvaluator { - mbgl::style::Filter noop(std::string layerType) { - Log::Warning(mbgl::Event::JNI, "%s doesn't support filter", layerType.c_str()); - return {}; - } - - mbgl::style::Filter operator()(style::BackgroundLayer&) { return noop("BackgroundLayer"); } - mbgl::style::Filter operator()(style::CustomLayer&) { return noop("CustomLayer"); } - mbgl::style::Filter operator()(style::RasterLayer&) { return noop("RasterLayer"); } - mbgl::style::Filter operator()(style::HillshadeLayer&) { return noop("HillshadeLayer"); } - - template - mbgl::style::Filter operator()(LayerType& layer) { - return layer.getFilter(); - } - }; - jni::Local> Layer::getFilter(jni::JNIEnv& env) { using namespace mbgl::style; using namespace mbgl::style::conversion; - Filter filter = layer.accept(GetFilterEvaluator()); + Filter filter = layer.getFilter(); if (filter.expression) { mbgl::Value expressionValue = (*filter.expression)->serialize(); return gson::JsonElement::New(env, expressionValue); @@ -167,62 +136,16 @@ namespace android { } } - struct SetSourceLayerEvaluator { - std::string sourceLayer; - - void operator()(style::BackgroundLayer&) { Log::Warning(mbgl::Event::JNI, "BackgroundLayer doesn't support source layer"); } - void operator()(style::CustomLayer&) { Log::Warning(mbgl::Event::JNI, "CustomLayer doesn't support source layer"); } - void operator()(style::RasterLayer&) { Log::Warning(mbgl::Event::JNI, "RasterLayer doesn't support source layer"); } - void operator()(style::HillshadeLayer&) { Log::Warning(mbgl::Event::JNI, "HillshadeLayer doesn't support source layer"); } - - template - void operator()(LayerType& layer) { - layer.setSourceLayer(sourceLayer); - } - }; - void Layer::setSourceLayer(jni::JNIEnv& env, const jni::String& sourceLayer) { - layer.accept(SetSourceLayerEvaluator {jni::Make(env, sourceLayer)}); + layer.setSourceLayer(jni::Make(env, sourceLayer)); } - struct GetSourceLayerEvaluator { - std::string noop(std::string layerType) { - Log::Warning(mbgl::Event::JNI, "%s doesn't support source layer", layerType.c_str()); - return {}; - } - - std::string operator()(style::BackgroundLayer&) { return noop("BackgroundLayer"); } - std::string operator()(style::CustomLayer&) { return noop("CustomLayer"); } - std::string operator()(style::RasterLayer&) { return noop("RasterLayer"); } - std::string operator()(style::HillshadeLayer&) { return noop("HillshadeLayer"); } - - template - std::string operator()(LayerType& layer) { - return layer.getSourceLayer(); - } - }; - jni::Local Layer::getSourceLayer(jni::JNIEnv& env) { - return jni::Make(env, layer.accept(GetSourceLayerEvaluator())); + return jni::Make(env, layer.getSourceLayer()); } - struct GetSourceIdEvaluator { - std::string noop(std::string layerType) { - Log::Warning(mbgl::Event::JNI, "%s doesn't support get source id", layerType.c_str()); - return {}; - } - - std::string operator()(style::BackgroundLayer&) { return noop("BackgroundLayer"); } - std::string operator()(style::CustomLayer&) { return noop("CustomLayer"); } - - template - std::string operator()(LayerType& layer) { - return layer.getSourceID(); - } - }; - jni::Local Layer::getSourceId(jni::JNIEnv& env) { - return jni::Make(env, layer.accept(GetSourceIdEvaluator())); + return jni::Make(env, layer.getSourceID()); } jni::jfloat Layer::getMinZoom(jni::JNIEnv&){ diff --git a/platform/android/src/style/layers/layers.cpp b/platform/android/src/style/layers/layers.cpp index 232e92a7c7..c0a3acdf42 100644 --- a/platform/android/src/style/layers/layers.cpp +++ b/platform/android/src/style/layers/layers.cpp @@ -26,66 +26,58 @@ #include "fill_extrusion_layer.hpp" namespace mbgl { -namespace android { - -// Mapping from style layers to peer classes -template struct PeerType {}; -template <> struct PeerType { using Type = android::BackgroundLayer; }; -template <> struct PeerType { using Type = android::CircleLayer; }; -template <> struct PeerType { using Type = android::FillExtrusionLayer; }; -template <> struct PeerType { using Type = android::FillLayer; }; -template <> struct PeerType { using Type = android::HeatmapLayer; }; -template <> struct PeerType { using Type = android::HillshadeLayer; }; -template <> struct PeerType { using Type = android::LineLayer; }; -template <> struct PeerType { using Type = android::RasterLayer; }; -template <> struct PeerType { using Type = android::SymbolLayer; }; -template <> struct PeerType { using Type = android::CustomLayer; }; -// Inititalizes a non-owning peer -struct LayerPeerIntitializer { - mbgl::Map& map; - - template - Layer* operator()(LayerType& layer) { - return new typename PeerType::Type(map, layer); - } -}; +namespace android { -static Layer* initializeLayerPeer(mbgl::Map& map, mbgl::style::Layer& coreLayer) { - Layer* layer = coreLayer.accept(LayerPeerIntitializer {map}); - return layer ? layer : new UnknownLayer(map, coreLayer); +template +inline std::unique_ptr to(std::unique_ptr layer) { + return std::unique_ptr(layer.release()->as()); } -// Initializes an owning peer -// Only usable once since it needs to pass on ownership -// of the given layer and thus enforced to be an rvalue -struct UniqueLayerPeerIntitializer { - mbgl::Map& map; - std::unique_ptr layer; +template +inline T& to(style::Layer& layer) { + return *layer.as(); +} - template - Layer* operator()(LayerType&) && { - return new typename PeerType::Type( - map, - std::unique_ptr(layer.release()->as()) - ); +template +std::unique_ptr initializeLayerPeer(Map& map, style::LayerType type, T&& layer) { + switch (type) { + case style::LayerType::Fill: + return std::unique_ptr(new FillLayer(map, to(std::forward(layer)))); + case style::LayerType::Line: + return std::unique_ptr(new LineLayer(map, to(std::forward(layer)))); + case style::LayerType::Circle: + return std::unique_ptr(new CircleLayer(map, to(std::forward(layer)))); + case style::LayerType::Symbol: + return std::unique_ptr(new SymbolLayer(map, to(std::forward(layer)))); + case style::LayerType::Raster: + return std::unique_ptr(new RasterLayer(map, to(std::forward(layer)))); + case style::LayerType::Background: + return std::unique_ptr(new BackgroundLayer(map, to(std::forward(layer)))); + case style::LayerType::Hillshade: + return std::unique_ptr(new HillshadeLayer(map, to(std::forward(layer)))); + case style::LayerType::Custom: + return std::unique_ptr(new CustomLayer(map, to(std::forward(layer)))); + case style::LayerType::FillExtrusion: + return std::unique_ptr(new FillExtrusionLayer(map, to(std::forward(layer)))); + case style::LayerType::Heatmap: + return std::unique_ptr(new HeatmapLayer(map, to(std::forward(layer)))); } -}; - -static Layer* initializeLayerPeer(Map& map, std::unique_ptr coreLayer) { - Layer* layer = coreLayer->accept(UniqueLayerPeerIntitializer {map, std::move(coreLayer)}); - return layer ? layer : new UnknownLayer(map, std::move(coreLayer)); + // Not reachable, but placate GCC. + assert(false); + return std::unique_ptr(new UnknownLayer(map, std::forward(layer))); } jni::Local> createJavaLayerPeer(jni::JNIEnv& env, Map& map, style::Layer& coreLayer) { - std::unique_ptr peerLayer = std::unique_ptr(initializeLayerPeer(map, coreLayer)); + std::unique_ptr peerLayer = initializeLayerPeer(map, coreLayer.getType(), coreLayer); jni::Local> result = peerLayer->createJavaPeer(env); peerLayer.release(); return result; } jni::Local> createJavaLayerPeer(jni::JNIEnv& env, mbgl::Map& map, std::unique_ptr coreLayer) { - std::unique_ptr peerLayer = std::unique_ptr(initializeLayerPeer(map, std::move(coreLayer))); + auto type = coreLayer->getType(); + std::unique_ptr peerLayer = initializeLayerPeer(map, type, std::move(coreLayer)); jni::Local> result = peerLayer->createJavaPeer(env); peerLayer.release(); return result; diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index 5693edbd03..6dccdf5292 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -892,31 +892,6 @@ void NodeMap::SetPaintProperty(const Nan::FunctionCallbackInfo& info) info.GetReturnValue().SetUndefined(); } -struct SetFilterVisitor { - mbgl::style::Filter& filter; - - void operator()(mbgl::style::CustomLayer&) { - Nan::ThrowTypeError("layer doesn't support filters"); - } - - void operator()(mbgl::style::RasterLayer&) { - Nan::ThrowTypeError("layer doesn't support filters"); - } - - void operator()(mbgl::style::HillshadeLayer&) { - Nan::ThrowTypeError("layer doesn't support filters"); - } - - void operator()(mbgl::style::BackgroundLayer&) { - Nan::ThrowTypeError("layer doesn't support filters"); - } - - template - void operator()(VectorLayer& layer) { - layer.setFilter(filter); - } -}; - void NodeMap::SetFilter(const Nan::FunctionCallbackInfo& info) { using namespace mbgl::style; using namespace mbgl::style::conversion; @@ -949,7 +924,7 @@ void NodeMap::SetFilter(const Nan::FunctionCallbackInfo& info) { filter = std::move(*converted); } - layer->accept(SetFilterVisitor { filter }); + layer->setFilter(filter); } void NodeMap::SetCenter(const Nan::FunctionCallbackInfo& info) { diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index ceed411ca3..d008ad86c9 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -1533,38 +1533,14 @@ void QMapboxGL::setFilter(const QString& layer, const QVariant& filter) return; } - Filter filter_; - Error error; mbgl::optional converted = convert(filter, error); if (!converted) { qWarning() << "Error parsing filter:" << error.message.c_str(); return; } - filter_ = std::move(*converted); - - if (layer_->is()) { - layer_->as()->setFilter(filter_); - return; - } - if (layer_->is()) { - layer_->as()->setFilter(filter_); - return; - } - if (layer_->is()) { - layer_->as()->setFilter(filter_); - return; - } - if (layer_->is()) { - layer_->as()->setFilter(filter_); - return; - } - if (layer_->is()) { - layer_->as()->setFilter(filter_); - return; - } - qWarning() << "Layer doesn't support filters"; + layer_->setFilter(std::move(*converted)); } QVariant QVariantFromValue(const mbgl::Value &value) { @@ -1617,24 +1593,7 @@ QVariant QMapboxGL::getFilter(const QString &layer) const { return QVariant(); } - Filter filter_; - - if (layer_->is()) { - filter_ = layer_->as()->getFilter(); - } else if (layer_->is()) { - filter_ = layer_->as()->getFilter(); - } else if (layer_->is()) { - filter_ = layer_->as()->getFilter(); - } else if (layer_->is()) { - filter_ = layer_->as()->getFilter(); - } else if (layer_->is()) { - filter_ = layer_->as()->getFilter(); - } else { - qWarning() << "Layer doesn't support filters"; - return QVariant(); - } - - auto serialized = filter_.serialize(); + auto serialized = layer_->getFilter().serialize(); return QVariantFromValue(serialized); } diff --git a/src/mbgl/style/layer.cpp b/src/mbgl/style/layer.cpp index e08b71e6b3..58c38403bc 100644 --- a/src/mbgl/style/layer.cpp +++ b/src/mbgl/style/layer.cpp @@ -24,10 +24,44 @@ std::string Layer::getID() const { return baseImpl->id; } +std::string Layer::getSourceID() const { + return baseImpl->source; +} + +std::string Layer::getSourceLayer() const { + return baseImpl->sourceLayer; +} + +void Layer::setSourceLayer(const std::string& sourceLayer) { + auto impl_ = mutableBaseImpl(); + impl_->sourceLayer = sourceLayer; + baseImpl = std::move(impl_); +} + +const Filter& Layer::getFilter() const { + return baseImpl->filter; +} + +void Layer::setFilter(const Filter& filter) { + auto impl_ = mutableBaseImpl(); + impl_->filter = filter; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + VisibilityType Layer::getVisibility() const { return baseImpl->visibility; } +void Layer::setVisibility(VisibilityType value) { + if (value == getVisibility()) + return; + auto impl_ = mutableBaseImpl(); + impl_->visibility = value; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + float Layer::getMinZoom() const { return baseImpl->minZoom; } @@ -36,6 +70,20 @@ float Layer::getMaxZoom() const { return baseImpl->maxZoom; } +void Layer::setMinZoom(float minZoom) { + auto impl_ = mutableBaseImpl(); + impl_->minZoom = minZoom; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + +void Layer::setMaxZoom(float maxZoom) { + auto impl_ = mutableBaseImpl(); + impl_->maxZoom = maxZoom; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} + void Layer::setObserver(LayerObserver* observer_) { observer = observer_ ? observer_ : &nullObserver; } diff --git a/src/mbgl/style/layers/background_layer.cpp b/src/mbgl/style/layers/background_layer.cpp index f2e85ce7e7..417d288107 100644 --- a/src/mbgl/style/layers/background_layer.cpp +++ b/src/mbgl/style/layers/background_layer.cpp @@ -42,34 +42,6 @@ std::unique_ptr BackgroundLayer::cloneRef(const std::string& id_) const { void BackgroundLayer::Impl::stringifyLayout(rapidjson::Writer&) const { } - -// Visibility - -void BackgroundLayer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void BackgroundLayer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -void BackgroundLayer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - // Layout properties @@ -294,5 +266,9 @@ optional BackgroundLayer::setLayoutProperty(const std::string& name, cons return Error { "layer doesn't support this property" }; } +Mutable BackgroundLayer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layers/circle_layer.cpp b/src/mbgl/style/layers/circle_layer.cpp index c301a83c9e..34ea80c54c 100644 --- a/src/mbgl/style/layers/circle_layer.cpp +++ b/src/mbgl/style/layers/circle_layer.cpp @@ -42,62 +42,6 @@ std::unique_ptr CircleLayer::cloneRef(const std::string& id_) const { void CircleLayer::Impl::stringifyLayout(rapidjson::Writer&) const { } -// Source - -const std::string& CircleLayer::getSourceID() const { - return impl().source; -} - -void CircleLayer::setSourceLayer(const std::string& sourceLayer) { - auto impl_ = mutableImpl(); - impl_->sourceLayer = sourceLayer; - baseImpl = std::move(impl_); -} - -const std::string& CircleLayer::getSourceLayer() const { - return impl().sourceLayer; -} - -// Filter - -void CircleLayer::setFilter(const Filter& filter) { - auto impl_ = mutableImpl(); - impl_->filter = filter; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -const Filter& CircleLayer::getFilter() const { - return impl().filter; -} - -// Visibility - -void CircleLayer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void CircleLayer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -void CircleLayer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - // Layout properties @@ -739,5 +683,9 @@ optional CircleLayer::setLayoutProperty(const std::string& name, const Co return Error { "layer doesn't support this property" }; } +Mutable CircleLayer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layers/custom_layer.cpp b/src/mbgl/style/layers/custom_layer.cpp index d1ac1a705c..a31586f708 100644 --- a/src/mbgl/style/layers/custom_layer.cpp +++ b/src/mbgl/style/layers/custom_layer.cpp @@ -25,31 +25,6 @@ std::unique_ptr CustomLayer::cloneRef(const std::string&) const { return nullptr; } -// Visibility - -void CustomLayer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void CustomLayer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); -} - -void CustomLayer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); -} - using namespace conversion; optional CustomLayer::setPaintProperty(const std::string&, const Convertible&) { @@ -60,6 +35,10 @@ optional CustomLayer::setLayoutProperty(const std::string&, const Convert return Error { "layer doesn't support this property" }; } +Mutable CustomLayer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + template <> bool Layer::is() const { return getType() == LayerType::Custom; diff --git a/src/mbgl/style/layers/fill_extrusion_layer.cpp b/src/mbgl/style/layers/fill_extrusion_layer.cpp index 9301f8dd00..3a06cb78c4 100644 --- a/src/mbgl/style/layers/fill_extrusion_layer.cpp +++ b/src/mbgl/style/layers/fill_extrusion_layer.cpp @@ -42,62 +42,6 @@ std::unique_ptr FillExtrusionLayer::cloneRef(const std::string& id_) cons void FillExtrusionLayer::Impl::stringifyLayout(rapidjson::Writer&) const { } -// Source - -const std::string& FillExtrusionLayer::getSourceID() const { - return impl().source; -} - -void FillExtrusionLayer::setSourceLayer(const std::string& sourceLayer) { - auto impl_ = mutableImpl(); - impl_->sourceLayer = sourceLayer; - baseImpl = std::move(impl_); -} - -const std::string& FillExtrusionLayer::getSourceLayer() const { - return impl().sourceLayer; -} - -// Filter - -void FillExtrusionLayer::setFilter(const Filter& filter) { - auto impl_ = mutableImpl(); - impl_->filter = filter; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -const Filter& FillExtrusionLayer::getFilter() const { - return impl().filter; -} - -// Visibility - -void FillExtrusionLayer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void FillExtrusionLayer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -void FillExtrusionLayer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - // Layout properties @@ -541,5 +485,9 @@ optional FillExtrusionLayer::setLayoutProperty(const std::string& name, c return Error { "layer doesn't support this property" }; } +Mutable FillExtrusionLayer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layers/fill_layer.cpp b/src/mbgl/style/layers/fill_layer.cpp index 69b3a16004..b244df6eea 100644 --- a/src/mbgl/style/layers/fill_layer.cpp +++ b/src/mbgl/style/layers/fill_layer.cpp @@ -42,62 +42,6 @@ std::unique_ptr FillLayer::cloneRef(const std::string& id_) const { void FillLayer::Impl::stringifyLayout(rapidjson::Writer&) const { } -// Source - -const std::string& FillLayer::getSourceID() const { - return impl().source; -} - -void FillLayer::setSourceLayer(const std::string& sourceLayer) { - auto impl_ = mutableImpl(); - impl_->sourceLayer = sourceLayer; - baseImpl = std::move(impl_); -} - -const std::string& FillLayer::getSourceLayer() const { - return impl().sourceLayer; -} - -// Filter - -void FillLayer::setFilter(const Filter& filter) { - auto impl_ = mutableImpl(); - impl_->filter = filter; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -const Filter& FillLayer::getFilter() const { - return impl().filter; -} - -// Visibility - -void FillLayer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void FillLayer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -void FillLayer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - // Layout properties @@ -541,5 +485,9 @@ optional FillLayer::setLayoutProperty(const std::string& name, const Conv return Error { "layer doesn't support this property" }; } +Mutable FillLayer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layers/heatmap_layer.cpp b/src/mbgl/style/layers/heatmap_layer.cpp index a90aab7009..b85cbf5f40 100644 --- a/src/mbgl/style/layers/heatmap_layer.cpp +++ b/src/mbgl/style/layers/heatmap_layer.cpp @@ -42,62 +42,6 @@ std::unique_ptr HeatmapLayer::cloneRef(const std::string& id_) const { void HeatmapLayer::Impl::stringifyLayout(rapidjson::Writer&) const { } -// Source - -const std::string& HeatmapLayer::getSourceID() const { - return impl().source; -} - -void HeatmapLayer::setSourceLayer(const std::string& sourceLayer) { - auto impl_ = mutableImpl(); - impl_->sourceLayer = sourceLayer; - baseImpl = std::move(impl_); -} - -const std::string& HeatmapLayer::getSourceLayer() const { - return impl().sourceLayer; -} - -// Filter - -void HeatmapLayer::setFilter(const Filter& filter) { - auto impl_ = mutableImpl(); - impl_->filter = filter; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -const Filter& HeatmapLayer::getFilter() const { - return impl().filter; -} - -// Visibility - -void HeatmapLayer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void HeatmapLayer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -void HeatmapLayer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - // Layout properties @@ -426,5 +370,9 @@ optional HeatmapLayer::setLayoutProperty(const std::string& name, const C return Error { "layer doesn't support this property" }; } +Mutable HeatmapLayer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layers/hillshade_layer.cpp b/src/mbgl/style/layers/hillshade_layer.cpp index aed49f6441..badc9ef30f 100644 --- a/src/mbgl/style/layers/hillshade_layer.cpp +++ b/src/mbgl/style/layers/hillshade_layer.cpp @@ -42,40 +42,6 @@ std::unique_ptr HillshadeLayer::cloneRef(const std::string& id_) const { void HillshadeLayer::Impl::stringifyLayout(rapidjson::Writer&) const { } -// Source - -const std::string& HillshadeLayer::getSourceID() const { - return impl().source; -} - - -// Visibility - -void HillshadeLayer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void HillshadeLayer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -void HillshadeLayer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - // Layout properties @@ -451,5 +417,9 @@ optional HillshadeLayer::setLayoutProperty(const std::string& name, const return Error { "layer doesn't support this property" }; } +Mutable HillshadeLayer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layers/layer.cpp.ejs b/src/mbgl/style/layers/layer.cpp.ejs index b5fb1a97a4..d4404ed949 100644 --- a/src/mbgl/style/layers/layer.cpp.ejs +++ b/src/mbgl/style/layers/layer.cpp.ejs @@ -59,66 +59,6 @@ void <%- camelize(type) %>Layer::Impl::stringifyLayout(rapidjson::Writer -<% if (type !== 'background') { -%> -// Source - -const std::string& <%- camelize(type) %>Layer::getSourceID() const { - return impl().source; -} - -<% if (type !== 'raster' && type !== 'hillshade') { -%> -void <%- camelize(type) %>Layer::setSourceLayer(const std::string& sourceLayer) { - auto impl_ = mutableImpl(); - impl_->sourceLayer = sourceLayer; - baseImpl = std::move(impl_); -} - -const std::string& <%- camelize(type) %>Layer::getSourceLayer() const { - return impl().sourceLayer; -} - -// Filter - -void <%- camelize(type) %>Layer::setFilter(const Filter& filter) { - auto impl_ = mutableImpl(); - impl_->filter = filter; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -const Filter& <%- camelize(type) %>Layer::getFilter() const { - return impl().filter; -} -<% } -%> -<% } -%> - -// Visibility - -void <%- camelize(type) %>Layer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void <%- camelize(type) %>Layer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -void <%- camelize(type) %>Layer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - // Layout properties <% for (const property of layoutProperties) { -%> @@ -316,5 +256,9 @@ optional <%- camelize(type) %>Layer::setLayoutProperty(const std::string& return Error { "layer doesn't support this property" }; } +Mutable <%- camelize(type) %>Layer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layers/line_layer.cpp b/src/mbgl/style/layers/line_layer.cpp index f5354e2bdb..f9fc058ed7 100644 --- a/src/mbgl/style/layers/line_layer.cpp +++ b/src/mbgl/style/layers/line_layer.cpp @@ -43,62 +43,6 @@ void LineLayer::Impl::stringifyLayout(rapidjson::Writer layout.stringify(writer); } -// Source - -const std::string& LineLayer::getSourceID() const { - return impl().source; -} - -void LineLayer::setSourceLayer(const std::string& sourceLayer) { - auto impl_ = mutableImpl(); - impl_->sourceLayer = sourceLayer; - baseImpl = std::move(impl_); -} - -const std::string& LineLayer::getSourceLayer() const { - return impl().sourceLayer; -} - -// Filter - -void LineLayer::setFilter(const Filter& filter) { - auto impl_ = mutableImpl(); - impl_->filter = filter; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -const Filter& LineLayer::getFilter() const { - return impl().filter; -} - -// Visibility - -void LineLayer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void LineLayer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -void LineLayer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - // Layout properties PropertyValue LineLayer::getDefaultLineCap() { @@ -880,5 +824,9 @@ optional LineLayer::setLayoutProperty(const std::string& name, const Conv return Error { "layer doesn't support this property" }; } +Mutable LineLayer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layers/raster_layer.cpp b/src/mbgl/style/layers/raster_layer.cpp index 7bd01c92e1..76e433aa73 100644 --- a/src/mbgl/style/layers/raster_layer.cpp +++ b/src/mbgl/style/layers/raster_layer.cpp @@ -42,40 +42,6 @@ std::unique_ptr RasterLayer::cloneRef(const std::string& id_) const { void RasterLayer::Impl::stringifyLayout(rapidjson::Writer&) const { } -// Source - -const std::string& RasterLayer::getSourceID() const { - return impl().source; -} - - -// Visibility - -void RasterLayer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void RasterLayer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -void RasterLayer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - // Layout properties @@ -540,5 +506,9 @@ optional RasterLayer::setLayoutProperty(const std::string& name, const Co return Error { "layer doesn't support this property" }; } +Mutable RasterLayer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layers/symbol_layer.cpp b/src/mbgl/style/layers/symbol_layer.cpp index 848678b5f1..d7d024d0e0 100644 --- a/src/mbgl/style/layers/symbol_layer.cpp +++ b/src/mbgl/style/layers/symbol_layer.cpp @@ -43,62 +43,6 @@ void SymbolLayer::Impl::stringifyLayout(rapidjson::WritersourceLayer = sourceLayer; - baseImpl = std::move(impl_); -} - -const std::string& SymbolLayer::getSourceLayer() const { - return impl().sourceLayer; -} - -// Filter - -void SymbolLayer::setFilter(const Filter& filter) { - auto impl_ = mutableImpl(); - impl_->filter = filter; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -const Filter& SymbolLayer::getFilter() const { - return impl().filter; -} - -// Visibility - -void SymbolLayer::setVisibility(VisibilityType value) { - if (value == getVisibility()) - return; - auto impl_ = mutableImpl(); - impl_->visibility = value; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -// Zoom range - -void SymbolLayer::setMinZoom(float minZoom) { - auto impl_ = mutableImpl(); - impl_->minZoom = minZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - -void SymbolLayer::setMaxZoom(float maxZoom) { - auto impl_ = mutableImpl(); - impl_->maxZoom = maxZoom; - baseImpl = std::move(impl_); - observer->onLayerChanged(*this); -} - // Layout properties PropertyValue SymbolLayer::getDefaultSymbolPlacement() { @@ -2030,5 +1974,9 @@ optional SymbolLayer::setLayoutProperty(const std::string& name, const Co return Error { "layer doesn't support this property" }; } +Mutable SymbolLayer::mutableBaseImpl() const { + return staticMutableCast(mutableImpl()); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/style_impl.cpp b/src/mbgl/style/style_impl.cpp index 5c9edc789f..760e2bc396 100644 --- a/src/mbgl/style/style_impl.cpp +++ b/src/mbgl/style/style_impl.cpp @@ -146,28 +146,13 @@ void Style::Impl::addSource(std::unique_ptr source) { sources.add(std::move(source)); } -struct SourceIdUsageEvaluator { - const std::string& sourceId; - - bool operator()(BackgroundLayer&) { return false; } - bool operator()(CustomLayer&) { return false; } - - template - bool operator()(LayerType& layer) { - return layer.getSourceID() == sourceId; - } -}; - std::unique_ptr Style::Impl::removeSource(const std::string& id) { // Check if source is in use - SourceIdUsageEvaluator sourceIdEvaluator {id}; - auto layerIt = std::find_if(layers.begin(), layers.end(), [&](const auto& layer) { - return layer->accept(sourceIdEvaluator); - }); - - if (layerIt != layers.end()) { - Log::Warning(Event::General, "Source '%s' is in use, cannot remove", id.c_str()); - return nullptr; + for (const auto& layer: layers) { + if (layer->getSourceID() == id) { + Log::Warning(Event::General, "Source '%s' is in use, cannot remove", id.c_str()); + return nullptr; + } } std::unique_ptr source = sources.remove(id); -- cgit v1.2.1