From c2a5894f2dbe9982830066ab9347b059e6e7d845 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 25 Apr 2017 18:20:26 -0700 Subject: [core] Immutable Impls --- include/mbgl/style/layer.hpp | 31 ++--- include/mbgl/style/layers/background_layer.hpp | 16 ++- include/mbgl/style/layers/circle_layer.hpp | 16 ++- include/mbgl/style/layers/custom_layer.hpp | 13 ++- include/mbgl/style/layers/fill_extrusion_layer.hpp | 16 ++- include/mbgl/style/layers/fill_layer.hpp | 16 ++- include/mbgl/style/layers/layer.hpp.ejs | 16 ++- include/mbgl/style/layers/line_layer.hpp | 16 ++- include/mbgl/style/layers/raster_layer.hpp | 16 ++- include/mbgl/style/layers/symbol_layer.hpp | 16 ++- include/mbgl/style/light.hpp | 16 ++- include/mbgl/style/light.hpp.ejs | 16 ++- include/mbgl/style/source.hpp | 29 +++-- include/mbgl/style/sources/geojson_source.hpp | 16 ++- include/mbgl/style/sources/raster_source.hpp | 17 ++- include/mbgl/style/sources/vector_source.hpp | 17 ++- include/mbgl/util/immutable.hpp | 127 +++++++++++++++++++++ include/mbgl/util/tileset.hpp | 5 + 18 files changed, 336 insertions(+), 79 deletions(-) create mode 100644 include/mbgl/util/immutable.hpp (limited to 'include') diff --git a/include/mbgl/style/layer.hpp b/include/mbgl/style/layer.hpp index 56f2c48fa7..10176879c5 100644 --- a/include/mbgl/style/layer.hpp +++ b/include/mbgl/style/layer.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -19,6 +20,7 @@ class RasterLayer; class BackgroundLayer; class CustomLayer; class FillExtrusionLayer; +class LayerObserver; /** * The runtime representation of a [layer](https://www.mapbox.com/mapbox-gl-style-spec/#layers) from the Mapbox Style @@ -38,15 +40,6 @@ class FillExtrusionLayer; */ class Layer : public mbgl::util::noncopyable { public: - class Impl; - -protected: - - const LayerType type; - Layer(LayerType, std::unique_ptr); - -public: - virtual ~Layer(); // Check whether this layer is of the given subtype. @@ -78,7 +71,7 @@ public: // template auto accept(V&& visitor) { - switch (type) { + switch (getType()) { case LayerType::Fill: return visitor(*as()); case LayerType::Line: @@ -98,20 +91,30 @@ public: } } + LayerType getType() const; const std::string& getID() const; // Visibility VisibilityType getVisibility() const; - void setVisibility(VisibilityType); + virtual void setVisibility(VisibilityType) = 0; // Zoom range float getMinZoom() const; - void setMinZoom(float) const; float getMaxZoom() const; - void setMaxZoom(float) const; + virtual void setMinZoom(float) = 0; + virtual void setMaxZoom(float) = 0; // Private implementation - const std::unique_ptr baseImpl; + 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 diff --git a/include/mbgl/style/layers/background_layer.hpp b/include/mbgl/style/layers/background_layer.hpp index 6604a868f3..63708a449f 100644 --- a/include/mbgl/style/layers/background_layer.hpp +++ b/include/mbgl/style/layers/background_layer.hpp @@ -19,6 +19,13 @@ public: BackgroundLayer(const std::string& layerID); ~BackgroundLayer() final; + // Visibility + void setVisibility(VisibilityType) final; + + // Zoom range + void setMinZoom(float) final; + void setMaxZoom(float) final; + // Paint properties static PropertyValue getDefaultBackgroundColor(); @@ -42,15 +49,16 @@ public: // Private implementation class Impl; - Impl* const impl; + const Impl& impl() const; - BackgroundLayer(const Impl&); - BackgroundLayer(const BackgroundLayer&) = delete; + Mutable mutableImpl() const; + BackgroundLayer(Immutable); + std::unique_ptr cloneRef(const std::string& id) const final; }; template <> inline bool Layer::is() const { - return type == LayerType::Background; + return getType() == LayerType::Background; } } // namespace style diff --git a/include/mbgl/style/layers/circle_layer.hpp b/include/mbgl/style/layers/circle_layer.hpp index 3a3723249f..f40c507e28 100644 --- a/include/mbgl/style/layers/circle_layer.hpp +++ b/include/mbgl/style/layers/circle_layer.hpp @@ -27,6 +27,13 @@ public: void setFilter(const Filter&); const Filter& getFilter() const; + // Visibility + void setVisibility(VisibilityType) final; + + // Zoom range + void setMinZoom(float) final; + void setMaxZoom(float) final; + // Paint properties static DataDrivenPropertyValue getDefaultCircleRadius(); @@ -92,15 +99,16 @@ public: // Private implementation class Impl; - Impl* const impl; + const Impl& impl() const; - CircleLayer(const Impl&); - CircleLayer(const CircleLayer&) = delete; + Mutable mutableImpl() const; + CircleLayer(Immutable); + std::unique_ptr cloneRef(const std::string& id) const final; }; template <> inline bool Layer::is() const { - return type == LayerType::Circle; + return getType() == LayerType::Circle; } } // namespace style diff --git a/include/mbgl/style/layers/custom_layer.hpp b/include/mbgl/style/layers/custom_layer.hpp index edc8d43f89..79a353b047 100644 --- a/include/mbgl/style/layers/custom_layer.hpp +++ b/include/mbgl/style/layers/custom_layer.hpp @@ -55,12 +55,21 @@ public: void* context); ~CustomLayer() final; + // Visibility + void setVisibility(VisibilityType) final; + + // Zoom range + void setMinZoom(float) final; + void setMaxZoom(float) final; + // Private implementation class Impl; - Impl* impl; + const Impl& impl() const; + + Mutable mutableImpl() const; + std::unique_ptr cloneRef(const std::string& id) const final; - CustomLayer(const Impl&); CustomLayer(const CustomLayer&) = delete; }; diff --git a/include/mbgl/style/layers/fill_extrusion_layer.hpp b/include/mbgl/style/layers/fill_extrusion_layer.hpp index 1f79f87fac..bc6890e82f 100644 --- a/include/mbgl/style/layers/fill_extrusion_layer.hpp +++ b/include/mbgl/style/layers/fill_extrusion_layer.hpp @@ -27,6 +27,13 @@ public: void setFilter(const Filter&); const Filter& getFilter() const; + // Visibility + void setVisibility(VisibilityType) final; + + // Zoom range + void setMinZoom(float) final; + void setMaxZoom(float) final; + // Paint properties static PropertyValue getDefaultFillExtrusionOpacity(); @@ -74,15 +81,16 @@ public: // Private implementation class Impl; - Impl* const impl; + const Impl& impl() const; - FillExtrusionLayer(const Impl&); - FillExtrusionLayer(const FillExtrusionLayer&) = delete; + Mutable mutableImpl() const; + FillExtrusionLayer(Immutable); + std::unique_ptr cloneRef(const std::string& id) const final; }; template <> inline bool Layer::is() const { - return type == LayerType::FillExtrusion; + return getType() == LayerType::FillExtrusion; } } // namespace style diff --git a/include/mbgl/style/layers/fill_layer.hpp b/include/mbgl/style/layers/fill_layer.hpp index 8371ff7a8f..3d08e75a1c 100644 --- a/include/mbgl/style/layers/fill_layer.hpp +++ b/include/mbgl/style/layers/fill_layer.hpp @@ -27,6 +27,13 @@ public: void setFilter(const Filter&); const Filter& getFilter() const; + // Visibility + void setVisibility(VisibilityType) final; + + // Zoom range + void setMinZoom(float) final; + void setMaxZoom(float) final; + // Paint properties static PropertyValue getDefaultFillAntialias(); @@ -74,15 +81,16 @@ public: // Private implementation class Impl; - Impl* const impl; + const Impl& impl() const; - FillLayer(const Impl&); - FillLayer(const FillLayer&) = delete; + Mutable mutableImpl() const; + FillLayer(Immutable); + std::unique_ptr cloneRef(const std::string& id) const final; }; template <> inline bool Layer::is() const { - return type == LayerType::Fill; + return getType() == LayerType::Fill; } } // namespace style diff --git a/include/mbgl/style/layers/layer.hpp.ejs b/include/mbgl/style/layers/layer.hpp.ejs index 59d7cd6415..033ae88956 100644 --- a/include/mbgl/style/layers/layer.hpp.ejs +++ b/include/mbgl/style/layers/layer.hpp.ejs @@ -44,6 +44,13 @@ public: <% } -%> <% } -%> + // Visibility + void setVisibility(VisibilityType) final; + + // Zoom range + void setMinZoom(float) final; + void setMaxZoom(float) final; + <% if (layoutProperties.length) { -%> // Layout properties @@ -67,15 +74,16 @@ public: // Private implementation class Impl; - Impl* const impl; + const Impl& impl() const; - <%- camelize(type) %>Layer(const Impl&); - <%- camelize(type) %>Layer(const <%- camelize(type) %>Layer&) = delete; + Mutable mutableImpl() const; + <%- camelize(type) %>Layer(Immutable); + std::unique_ptr cloneRef(const std::string& id) const final; }; template <> inline bool Layer::is<<%- camelize(type) %>Layer>() const { - return type == LayerType::<%- camelize(type) %>; + return getType() == LayerType::<%- camelize(type) %>; } } // namespace style diff --git a/include/mbgl/style/layers/line_layer.hpp b/include/mbgl/style/layers/line_layer.hpp index a5f08e553c..6000839d1c 100644 --- a/include/mbgl/style/layers/line_layer.hpp +++ b/include/mbgl/style/layers/line_layer.hpp @@ -29,6 +29,13 @@ public: void setFilter(const Filter&); const Filter& getFilter() const; + // Visibility + void setVisibility(VisibilityType) final; + + // Zoom range + void setMinZoom(float) final; + void setMaxZoom(float) final; + // Layout properties static PropertyValue getDefaultLineCap(); @@ -112,15 +119,16 @@ public: // Private implementation class Impl; - Impl* const impl; + const Impl& impl() const; - LineLayer(const Impl&); - LineLayer(const LineLayer&) = delete; + Mutable mutableImpl() const; + LineLayer(Immutable); + std::unique_ptr cloneRef(const std::string& id) const final; }; template <> inline bool Layer::is() const { - return type == LayerType::Line; + return getType() == LayerType::Line; } } // namespace style diff --git a/include/mbgl/style/layers/raster_layer.hpp b/include/mbgl/style/layers/raster_layer.hpp index c0351da5d0..ad9a68f1b0 100644 --- a/include/mbgl/style/layers/raster_layer.hpp +++ b/include/mbgl/style/layers/raster_layer.hpp @@ -22,6 +22,13 @@ public: // Source const std::string& getSourceID() const; + // Visibility + void setVisibility(VisibilityType) final; + + // Zoom range + void setMinZoom(float) final; + void setMaxZoom(float) final; + // Paint properties static PropertyValue getDefaultRasterOpacity(); @@ -69,15 +76,16 @@ public: // Private implementation class Impl; - Impl* const impl; + const Impl& impl() const; - RasterLayer(const Impl&); - RasterLayer(const RasterLayer&) = delete; + Mutable mutableImpl() const; + RasterLayer(Immutable); + std::unique_ptr cloneRef(const std::string& id) const final; }; template <> inline bool Layer::is() const { - return type == LayerType::Raster; + return getType() == LayerType::Raster; } } // namespace style diff --git a/include/mbgl/style/layers/symbol_layer.hpp b/include/mbgl/style/layers/symbol_layer.hpp index ea6bda55d7..e7df5a92ae 100644 --- a/include/mbgl/style/layers/symbol_layer.hpp +++ b/include/mbgl/style/layers/symbol_layer.hpp @@ -29,6 +29,13 @@ public: void setFilter(const Filter&); const Filter& getFilter() const; + // Visibility + void setVisibility(VisibilityType) final; + + // Zoom range + void setMinZoom(float) final; + void setMaxZoom(float) final; + // Layout properties static PropertyValue getDefaultSymbolPlacement(); @@ -256,15 +263,16 @@ public: // Private implementation class Impl; - Impl* const impl; + const Impl& impl() const; - SymbolLayer(const Impl&); - SymbolLayer(const SymbolLayer&) = delete; + Mutable mutableImpl() const; + SymbolLayer(Immutable); + std::unique_ptr cloneRef(const std::string& id) const final; }; template <> inline bool Layer::is() const { - return type == LayerType::Symbol; + return getType() == LayerType::Symbol; } } // namespace style diff --git a/include/mbgl/style/light.hpp b/include/mbgl/style/light.hpp index 8212a58dcc..c82792b28d 100644 --- a/include/mbgl/style/light.hpp +++ b/include/mbgl/style/light.hpp @@ -1,20 +1,19 @@ // This file is generated. Do not edit. #pragma once + #include #include #include - -#include +#include namespace mbgl { namespace style { +class LightObserver; + class Light { public: - - class Impl; - Light(); ~Light(); @@ -42,7 +41,12 @@ public: void setIntensityTransition(const TransitionOptions&); TransitionOptions getIntensityTransition() const; - std::shared_ptr impl; + class Impl; + Immutable impl; + Mutable mutableImpl() const; + + LightObserver* observer = nullptr; + void setObserver(LightObserver*); }; } // namespace style diff --git a/include/mbgl/style/light.hpp.ejs b/include/mbgl/style/light.hpp.ejs index 601e0bd410..adc5b651e3 100644 --- a/include/mbgl/style/light.hpp.ejs +++ b/include/mbgl/style/light.hpp.ejs @@ -4,20 +4,19 @@ // This file is generated. Do not edit. #pragma once + #include #include #include - -#include +#include namespace mbgl { namespace style { +class LightObserver; + class Light { public: - - class Impl; - Light(); ~Light(); @@ -29,7 +28,12 @@ public: TransitionOptions get<%- camelize(property.name) %>Transition() const; <% } -%> - std::shared_ptr impl; + class Impl; + Immutable impl; + Mutable mutableImpl() const; + + LightObserver* observer = nullptr; + void setObserver(LightObserver*); }; } // namespace style diff --git a/include/mbgl/style/source.hpp b/include/mbgl/style/source.hpp index 4c82e472a6..d19dfd4e36 100644 --- a/include/mbgl/style/source.hpp +++ b/include/mbgl/style/source.hpp @@ -1,19 +1,25 @@ #pragma once -#include #include #include -#include #include +#include #include #include #include -#include namespace mbgl { + +class FileSource; + namespace style { +class VectorSource; +class RasterSource; +class GeoJSONSource; +class SourceObserver; + /** * The runtime representation of a [source](https://www.mapbox.com/mapbox-gl-style-spec/#sources) from the Mapbox Style * Specification. @@ -49,21 +55,28 @@ public: return is() ? reinterpret_cast(this) : nullptr; } + SourceType getType() const; const std::string& getID() const; optional getAttribution() const; // Private implementation class Impl; - const std::unique_ptr baseImpl; + Immutable baseImpl; + + Source(Immutable); + + void setObserver(SourceObserver*); + SourceObserver* observer = nullptr; + + virtual void loadDescription(FileSource&) = 0; + void dumpDebugLogs() const; + + bool loaded = false; // 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. any peer; - -protected: - const SourceType type; - Source(SourceType, std::unique_ptr); }; } // namespace style diff --git a/include/mbgl/style/sources/geojson_source.hpp b/include/mbgl/style/sources/geojson_source.hpp index 5b39d7821b..2dcfec51aa 100644 --- a/include/mbgl/style/sources/geojson_source.hpp +++ b/include/mbgl/style/sources/geojson_source.hpp @@ -5,6 +5,9 @@ #include namespace mbgl { + +class AsyncRequest; + namespace style { struct GeoJSONOptions { @@ -22,21 +25,26 @@ struct GeoJSONOptions { class GeoJSONSource : public Source { public: GeoJSONSource(const std::string& id, const GeoJSONOptions& = {}); + ~GeoJSONSource() final; void setURL(const std::string& url); void setGeoJSON(const GeoJSON&); optional getURL() const; - // Private implementation - class Impl; - Impl* const impl; + const Impl& impl() const; + + void loadDescription(FileSource&) final; + +private: + optional url; + std::unique_ptr req; }; template <> inline bool Source::is() const { - return type == SourceType::GeoJSON; + return getType() == SourceType::GeoJSON; } } // namespace style diff --git a/include/mbgl/style/sources/raster_source.hpp b/include/mbgl/style/sources/raster_source.hpp index 395f25e51d..7f23a7ca4b 100644 --- a/include/mbgl/style/sources/raster_source.hpp +++ b/include/mbgl/style/sources/raster_source.hpp @@ -5,23 +5,34 @@ #include namespace mbgl { + +class AsyncRequest; + namespace style { class RasterSource : public Source { public: RasterSource(std::string id, variant urlOrTileset, uint16_t tileSize); + ~RasterSource() final; + const variant& getURLOrTileset() const; optional getURL() const; - // Private implementation + uint16_t getTileSize() const; class Impl; - Impl* const impl; + const Impl& impl() const; + + void loadDescription(FileSource&) final; + +private: + const variant urlOrTileset; + std::unique_ptr req; }; template <> inline bool Source::is() const { - return type == SourceType::Raster; + return getType() == SourceType::Raster; } } // namespace style diff --git a/include/mbgl/style/sources/vector_source.hpp b/include/mbgl/style/sources/vector_source.hpp index 8626ce160a..6f16974b40 100644 --- a/include/mbgl/style/sources/vector_source.hpp +++ b/include/mbgl/style/sources/vector_source.hpp @@ -5,23 +5,32 @@ #include namespace mbgl { + +class AsyncRequest; + namespace style { class VectorSource : public Source { public: VectorSource(std::string id, variant urlOrTileset); + ~VectorSource() final; + const variant& getURLOrTileset() const; optional getURL() const; - // Private implementation - class Impl; - Impl* const impl; + const Impl& impl() const; + + void loadDescription(FileSource&) final; + +private: + const variant urlOrTileset; + std::unique_ptr req; }; template <> inline bool Source::is() const { - return type == SourceType::Vector; + return getType() == SourceType::Vector; } } // namespace style diff --git a/include/mbgl/util/immutable.hpp b/include/mbgl/util/immutable.hpp new file mode 100644 index 0000000000..1d6ff4079a --- /dev/null +++ b/include/mbgl/util/immutable.hpp @@ -0,0 +1,127 @@ +#pragma once + +#include + +namespace mbgl { + +/** + * `Mutable` is a non-nullable uniquely owning reference to a `T`. It can be efficiently converted + * to `Immutable`. + * + * The lifecycle of `Mutable` and `Immutable` is as follows: + * + * 1. Create a `Mutable` using `makeMutable(...)` + * 2. Mutate it freely + * 3. When you're ready to freeze its state and enable safe cross-thread sharing, move assign or + * move construct it to `Immutable` + * + * The reason that `Mutable` exists, rather than simply using a `std::unique_ptr`, is to take advantage + * of the underlying single-allocation optimization provided by `std::make_shared`. + */ +template +class Mutable { +public: + Mutable(Mutable&&) = default; + Mutable& operator=(Mutable&&) = default; + + Mutable(const Mutable&) = delete; + Mutable& operator=(const Mutable&) = delete; + + T* get() { return ptr.get(); } + T* operator->() { return ptr.get(); } + T& operator*() { return *ptr; } + +private: + Mutable(std::shared_ptr&& s) + : ptr(std::move(s)) {} + + std::shared_ptr ptr; + + template friend class Immutable; + template friend Mutable makeMutable(Args&&...); +}; + +template +Mutable makeMutable(Args&&... args) { + return Mutable(std::make_shared(std::forward(args)...)); +} + +/** + * `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 + * as `std::shared_ptr` but with better indication of intent. + * + * Normally one should not share state between threads because it's difficult to verify the + * absence of read/write data races. `Immutable` provides a guarantee that no writes are + * possible, and instances therefore can be freely transferred and shared between threads. + */ +template +class Immutable { +public: + template + Immutable(Mutable&& s) + : ptr(std::const_pointer_cast(std::move(s.ptr))) {} + + template + Immutable(Immutable&& s) + : ptr(std::move(s.ptr)) {} + + template + Immutable(const Immutable& s) + : ptr(s.ptr) {} + + template + Immutable& operator=(Mutable&& s) { + ptr = std::const_pointer_cast(std::move(s.ptr)); + return *this; + } + + template + Immutable& operator=(Immutable&& s) { + ptr = std::move(s.ptr); + return *this; + } + + template + Immutable& operator=(const Immutable& s) { + ptr = s.ptr; + return *this; + } + + const T* get() const { return ptr.get(); } + const T* operator->() const { return ptr.get(); } + const T& operator*() const { return *ptr; } + + friend bool operator==(const Immutable& lhs, const Immutable& rhs) { + return lhs.ptr == rhs.ptr; + } + + friend bool operator!=(const Immutable& lhs, const Immutable& rhs) { + return lhs.ptr != rhs.ptr; + } + +private: + Immutable(std::shared_ptr&& s) + : ptr(std::move(s)) {} + + std::shared_ptr ptr; + + template friend class Immutable; + template friend class EnableImmutableFromThis; + template friend Immutable staticImmutableCast(const Immutable&); +}; + +template +class EnableImmutableFromThis : public std::enable_shared_from_this { +public: + Immutable immutableFromThis() const { + return Immutable(this->shared_from_this()); + } +}; + +template +Immutable staticImmutableCast(const Immutable& u) { + return Immutable(std::static_pointer_cast(u.ptr)); +} + +} // namespace mbgl diff --git a/include/mbgl/util/tileset.hpp b/include/mbgl/util/tileset.hpp index 1f28a5039a..1256e9fe96 100644 --- a/include/mbgl/util/tileset.hpp +++ b/include/mbgl/util/tileset.hpp @@ -18,6 +18,11 @@ public: Scheme scheme = Scheme::XYZ; // TileJSON also includes center, zoom, and bounds, but they are not used by mbgl. + + friend bool operator==(const Tileset& lhs, const Tileset& rhs) { + return std::tie(lhs.tiles, lhs.zoomRange, lhs.attribution, lhs.scheme) + == std::tie(rhs.tiles, rhs.zoomRange, rhs.attribution, rhs.scheme); + } }; } // namespace mbgl -- cgit v1.2.1