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.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
#include <mbgl/util/peer.hpp>
|
||||
#include <mbgl/util/immutable.hpp>
|
||||
#include <mbgl/util/optional.hpp>
|
||||
@@ -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<CircleLayer>("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<T>() ? reinterpret_cast<const T*>(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 <class VectorLayer>
|
||||
// void operator()(VectorLayer&) { ... }
|
||||
// };
|
||||
//
|
||||
template <class V>
|
||||
auto accept(V&& visitor) {
|
||||
switch (getType()) {
|
||||
case LayerType::Fill:
|
||||
return std::forward<V>(visitor)(*as<FillLayer>());
|
||||
case LayerType::Line:
|
||||
return std::forward<V>(visitor)(*as<LineLayer>());
|
||||
case LayerType::Circle:
|
||||
return std::forward<V>(visitor)(*as<CircleLayer>());
|
||||
case LayerType::Symbol:
|
||||
return std::forward<V>(visitor)(*as<SymbolLayer>());
|
||||
case LayerType::Raster:
|
||||
return std::forward<V>(visitor)(*as<RasterLayer>());
|
||||
case LayerType::Background:
|
||||
return std::forward<V>(visitor)(*as<BackgroundLayer>());
|
||||
case LayerType::Hillshade:
|
||||
return std::forward<V>(visitor)(*as<HillshadeLayer>());
|
||||
case LayerType::Custom:
|
||||
return std::forward<V>(visitor)(*as<CustomLayer>());
|
||||
case LayerType::FillExtrusion:
|
||||
return std::forward<V>(visitor)(*as<FillExtrusionLayer>());
|
||||
case LayerType::Heatmap:
|
||||
return std::forward<V>(visitor)(*as<HeatmapLayer>());
|
||||
}
|
||||
|
||||
// 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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) = 0;
|
||||
@@ -124,21 +93,25 @@ public:
|
||||
optional<conversion::Error> setVisibility(const conversion::Convertible& value);
|
||||
|
||||
// Private implementation
|
||||
// TODO : We should not have public mutable data members.
|
||||
class Impl;
|
||||
Immutable<Impl> baseImpl;
|
||||
|
||||
Layer(Immutable<Impl>);
|
||||
|
||||
// Create a layer, copying all properties except id and paint properties from this layer.
|
||||
virtual std::unique_ptr<Layer> 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<Impl>);
|
||||
virtual Mutable<Impl> mutableBaseImpl() const = 0;
|
||||
|
||||
LayerObserver* observer = nullptr;
|
||||
};
|
||||
|
||||
} // namespace style
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -58,6 +51,9 @@ public:
|
||||
Mutable<Impl> mutableImpl() const;
|
||||
BackgroundLayer(Immutable<Impl>);
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
protected:
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
template <>
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -114,6 +99,9 @@ public:
|
||||
Mutable<Impl> mutableImpl() const;
|
||||
CircleLayer(Immutable<Impl>);
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
protected:
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
template <>
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -88,10 +81,9 @@ public:
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
CustomLayer(const CustomLayer&) = delete;
|
||||
};
|
||||
|
||||
template <>
|
||||
bool Layer::is<CustomLayer>() const;
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -90,6 +75,9 @@ public:
|
||||
Mutable<Impl> mutableImpl() const;
|
||||
FillExtrusionLayer(Immutable<Impl>);
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
protected:
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
template <>
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -90,6 +75,9 @@ public:
|
||||
Mutable<Impl> mutableImpl() const;
|
||||
FillLayer(Immutable<Impl>);
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
protected:
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
template <>
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -79,6 +64,9 @@ public:
|
||||
Mutable<Impl> mutableImpl() const;
|
||||
HeatmapLayer(Immutable<Impl>);
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
protected:
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
template <>
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -79,6 +69,9 @@ public:
|
||||
Mutable<Impl> mutableImpl() const;
|
||||
HillshadeLayer(Immutable<Impl>);
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
protected:
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
template <>
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -86,6 +67,9 @@ public:
|
||||
Mutable<Impl> mutableImpl() const;
|
||||
<%- camelize(type) %>Layer(Immutable<Impl>);
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
protected:
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
template <>
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -135,6 +120,9 @@ public:
|
||||
Mutable<Impl> mutableImpl() const;
|
||||
LineLayer(Immutable<Impl>);
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
protected:
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
template <>
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -91,6 +81,9 @@ public:
|
||||
Mutable<Impl> mutableImpl() const;
|
||||
RasterLayer(Immutable<Impl>);
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
protected:
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
template <>
|
||||
|
||||
@@ -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<conversion::Error> setLayoutProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
optional<conversion::Error> setPaintProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
@@ -284,6 +269,9 @@ public:
|
||||
Mutable<Impl> mutableImpl() const;
|
||||
SymbolLayer(Immutable<Impl>);
|
||||
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
|
||||
|
||||
protected:
|
||||
Mutable<Layer::Impl> mutableBaseImpl() const final;
|
||||
};
|
||||
|
||||
template <>
|
||||
|
||||
@@ -39,6 +39,7 @@ private:
|
||||
|
||||
template <class S> friend class Immutable;
|
||||
template <class S, class... Args> friend Mutable<S> makeMutable(Args&&...);
|
||||
template <class S, class U> friend Mutable<S> staticMutableCast(const Mutable<U>&);
|
||||
};
|
||||
|
||||
template <class T, class... Args>
|
||||
@@ -46,6 +47,11 @@ Mutable<T> makeMutable(Args&&... args) {
|
||||
return Mutable<T>(std::make_shared<T>(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
template <class S, class U>
|
||||
Mutable<S> staticMutableCast(const Mutable<U>& u) {
|
||||
return Mutable<S>(std::static_pointer_cast<S>(u.ptr));
|
||||
}
|
||||
|
||||
/**
|
||||
* `Immutable<T>` is a non-nullable shared reference to a `const T`. Construction requires
|
||||
* a transfer of unique ownership from a `Mutable<T>`; once constructed it has the same behavior
|
||||
|
||||
Reference in New Issue
Block a user