Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Opaque pointer pattern for layers
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Dec 11, 2015
1 parent 0ff9295 commit c510c68
Show file tree
Hide file tree
Showing 52 changed files with 193 additions and 171 deletions.
24 changes: 24 additions & 0 deletions include/mbgl/layer/layer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef MBGL_LAYER
#define MBGL_LAYER

#include <mbgl/util/noncopyable.hpp>

#include <memory>

namespace mbgl {

class Layer : public mbgl::util::noncopyable {
public:
class Impl;

Layer(std::unique_ptr<Impl> impl_)
: impl(std::move(impl_)) {}

virtual ~Layer() = default;

const std::unique_ptr<Impl> impl;
};

} // namespace mbgl

#endif
6 changes: 3 additions & 3 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Transform;
class PointAnnotation;
class ShapeAnnotation;
struct CameraOptions;
class StyleLayer;
class Layer;

namespace util {
template <class T> class Thread;
Expand Down Expand Up @@ -165,8 +165,8 @@ class Map : private util::noncopyable {
LatLngBounds getBoundsForAnnotations(const AnnotationIDs&);

// Style API
void addLayer(std::unique_ptr<StyleLayer>);
void addLayer(std::unique_ptr<StyleLayer>, const std::string& before);
void addLayer(std::unique_ptr<Layer>);
void addLayer(std::unique_ptr<Layer>, const std::string& before);

// Memory
void setSourceTileCacheSize(size_t);
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/annotation/annotation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void AnnotationManager::updateStyle(Style& style) {
layer->layout.icon.allowOverlap = true;
layer->spriteAtlas = &spriteAtlas;

style.addLayer(std::move(layer));
style.addLayer(std::make_unique<Layer>(std::move(layer)));
}

for (const auto& shape : shapeAnnotations) {
Expand Down
10 changes: 5 additions & 5 deletions src/mbgl/annotation/shape_annotation_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void ShapeAnnotationImpl::updateStyle(Style& style) {
layer->source = AnnotationManager::SourceID;
layer->sourceLayer = layer->id;

style.addLayer(std::move(layer), AnnotationManager::PointLayerID);
style.addLayer(std::make_unique<Layer>(std::move(layer)), AnnotationManager::PointLayerID);

} else if (shape.properties.is<FillAnnotationProperties>()) {
type = geojsonvt::ProjectedFeatureType::Polygon;
Expand All @@ -57,13 +57,13 @@ void ShapeAnnotationImpl::updateStyle(Style& style) {
layer->source = AnnotationManager::SourceID;
layer->sourceLayer = layer->id;

style.addLayer(std::move(layer), AnnotationManager::PointLayerID);
style.addLayer(std::make_unique<Layer>(std::move(layer)), AnnotationManager::PointLayerID);

} else {
const StyleLayer* sourceLayer = style.getLayer(shape.properties.get<std::string>());
const Layer::Impl* sourceLayer = style.getLayer(shape.properties.get<std::string>())->impl.get();
if (!sourceLayer) return;

std::unique_ptr<StyleLayer> layer = sourceLayer->clone();
std::unique_ptr<Layer::Impl> layer = sourceLayer->clone();

type = layer->is<LineLayer>()
? geojsonvt::ProjectedFeatureType::LineString
Expand All @@ -75,7 +75,7 @@ void ShapeAnnotationImpl::updateStyle(Style& style) {
layer->sourceLayer = layer->id;
layer->visibility = VisibilityType::Visible;

style.addLayer(std::move(layer), sourceLayer->id);
style.addLayer(std::make_unique<Layer>(std::move(layer)), sourceLayer->id);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/layer/background_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace mbgl {

std::unique_ptr<StyleLayer> BackgroundLayer::clone() const {
std::unique_ptr<Layer::Impl> BackgroundLayer::clone() const {
return std::make_unique<BackgroundLayer>(*this);
}

Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/layer/background_layer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MBGL_BACKGROUND_LAYER
#define MBGL_BACKGROUND_LAYER

#include <mbgl/style/style_layer.hpp>
#include <mbgl/layer/layer_impl.hpp>
#include <mbgl/style/paint_property.hpp>

namespace mbgl {
Expand All @@ -13,9 +13,9 @@ class BackgroundPaintProperties {
PaintProperty<std::string, Faded<std::string>> pattern { "" };
};

class BackgroundLayer : public StyleLayer {
class BackgroundLayer : public Layer::Impl {
public:
std::unique_ptr<StyleLayer> clone() const override;
std::unique_ptr<Layer::Impl> clone() const override;

void parseLayout(const JSVal&) override {};
void parsePaints(const JSVal&) override;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/layer/circle_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace mbgl {

std::unique_ptr<StyleLayer> CircleLayer::clone() const {
std::unique_ptr<Layer::Impl> CircleLayer::clone() const {
return std::make_unique<CircleLayer>(*this);
}

Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/layer/circle_layer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MBGL_CIRCLE_LAYER
#define MBGL_CIRCLE_LAYER

#include <mbgl/style/style_layer.hpp>
#include <mbgl/layer/layer_impl.hpp>
#include <mbgl/style/paint_property.hpp>

namespace mbgl {
Expand All @@ -20,9 +20,9 @@ class CirclePaintProperties {
}
};

class CircleLayer : public StyleLayer {
class CircleLayer : public Layer::Impl {
public:
std::unique_ptr<StyleLayer> clone() const override;
std::unique_ptr<Layer::Impl> clone() const override;

void parseLayout(const JSVal&) override {};
void parsePaints(const JSVal&) override;
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/layer/custom_layer.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef MBGL_CUSTOM_LAYER
#define MBGL_CUSTOM_LAYER

#include <mbgl/style/style_layer.hpp>
#include <mbgl/layer/layer_impl.hpp>

namespace mbgl {

class StyleRenderParameters;

class CustomLayer : public StyleLayer {
class CustomLayer : public Layer::Impl {
public:
CustomLayer(const std::string& id);
CustomLayer(const CustomLayer&) = default;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/layer/fill_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace mbgl {

std::unique_ptr<StyleLayer> FillLayer::clone() const {
std::unique_ptr<Layer::Impl> FillLayer::clone() const {
return std::make_unique<FillLayer>(*this);
}

Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/layer/fill_layer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MBGL_FILL_LAYER
#define MBGL_FILL_LAYER

#include <mbgl/style/style_layer.hpp>
#include <mbgl/layer/layer_impl.hpp>
#include <mbgl/style/paint_property.hpp>

namespace mbgl {
Expand All @@ -17,9 +17,9 @@ class FillPaintProperties {
PaintProperty<std::string, Faded<std::string>> pattern { "" };
};

class FillLayer : public StyleLayer {
class FillLayer : public Layer::Impl {
public:
std::unique_ptr<StyleLayer> clone() const override;
std::unique_ptr<Layer::Impl> clone() const override;

void parseLayout(const JSVal&) override {};
void parsePaints(const JSVal&) override;
Expand Down
13 changes: 13 additions & 0 deletions src/mbgl/layer/layer_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <mbgl/layer/layer_impl.hpp>

namespace mbgl {

const std::string& Layer::Impl::bucketName() const {
return ref.empty() ? id : ref;
}

bool Layer::Impl::hasRenderPass(RenderPass pass) const {
return bool(passes & pass);
}

} // namespace mbgl
13 changes: 7 additions & 6 deletions src/mbgl/style/style_layer.hpp → src/mbgl/layer/layer_impl.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef MBGL_STYLE_STYLE_LAYER
#define MBGL_STYLE_STYLE_LAYER

#include <mbgl/layer/layer.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/style/filter_expression.hpp>
#include <mbgl/renderer/render_pass.hpp>
Expand All @@ -21,9 +22,9 @@ class Bucket;

using JSVal = rapidjson::Value;

class StyleLayer {
class Layer::Impl {
public:
virtual ~StyleLayer() = default;
virtual ~Impl() = default;

// Check whether this layer is of the given subtype.
template <class T> bool is() const { return dynamic_cast<const T*>(this); }
Expand All @@ -33,7 +34,7 @@ class StyleLayer {
template <class T> const T* as() const { return dynamic_cast<const T*>(this); }

// Create a copy of this layer.
virtual std::unique_ptr<StyleLayer> clone() const = 0;
virtual std::unique_ptr<Layer::Impl> clone() const = 0;

virtual void parseLayout(const JSVal& value) = 0;
virtual void parsePaints(const JSVal& value) = 0;
Expand Down Expand Up @@ -64,9 +65,9 @@ class StyleLayer {
VisibilityType visibility = VisibilityType::Visible;

protected:
StyleLayer() = default;
StyleLayer(const StyleLayer&) = default;
StyleLayer& operator=(const StyleLayer&) = delete;
Impl() = default;
Impl(const Impl&) = default;
Impl& operator=(const Impl&) = delete;

// Stores what render passes this layer is currently enabled for. This depends on the
// evaluated StyleProperties object and is updated accordingly.
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/layer/line_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace mbgl {

std::unique_ptr<StyleLayer> LineLayer::clone() const {
std::unique_ptr<Layer::Impl> LineLayer::clone() const {
return std::make_unique<LineLayer>(*this);
}

Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/layer/line_layer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MBGL_LINE_LAYER
#define MBGL_LINE_LAYER

#include <mbgl/style/style_layer.hpp>
#include <mbgl/layer/layer_impl.hpp>
#include <mbgl/style/layout_property.hpp>
#include <mbgl/style/paint_property.hpp>

Expand Down Expand Up @@ -36,9 +36,9 @@ class LinePaintProperties {
}
};

class LineLayer : public StyleLayer {
class LineLayer : public Layer::Impl {
public:
std::unique_ptr<StyleLayer> clone() const override;
std::unique_ptr<Layer::Impl> clone() const override;

void parseLayout(const JSVal&) override;
void parsePaints(const JSVal&) override;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/layer/raster_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace mbgl {

std::unique_ptr<StyleLayer> RasterLayer::clone() const {
std::unique_ptr<Layer::Impl> RasterLayer::clone() const {
return std::make_unique<RasterLayer>(*this);
}

Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/layer/raster_layer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MBGL_RASTER_LAYER
#define MBGL_RASTER_LAYER

#include <mbgl/style/style_layer.hpp>
#include <mbgl/layer/layer_impl.hpp>
#include <mbgl/style/paint_property.hpp>

namespace mbgl {
Expand All @@ -17,9 +17,9 @@ class RasterPaintProperties {
PaintProperty<float> fadeDuration { 0.0f };
};

class RasterLayer : public StyleLayer {
class RasterLayer : public Layer::Impl {
public:
std::unique_ptr<StyleLayer> clone() const override;
std::unique_ptr<Layer::Impl> clone() const override;

void parseLayout(const JSVal&) override {};
void parsePaints(const JSVal&) override;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/layer/symbol_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace mbgl {

std::unique_ptr<StyleLayer> SymbolLayer::clone() const {
std::unique_ptr<Layer::Impl> SymbolLayer::clone() const {
return std::make_unique<SymbolLayer>(*this);
}

Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/layer/symbol_layer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MBGL_SYMBOL_LAYER
#define MBGL_SYMBOL_LAYER

#include <mbgl/style/style_layer.hpp>
#include <mbgl/layer/layer_impl.hpp>
#include <mbgl/style/layout_property.hpp>
#include <mbgl/style/paint_property.hpp>

Expand Down Expand Up @@ -82,9 +82,9 @@ class SymbolPaintProperties {
PaintProperties text { 16.0f };
};

class SymbolLayer : public StyleLayer {
class SymbolLayer : public Layer::Impl {
public:
std::unique_ptr<StyleLayer> clone() const override;
std::unique_ptr<Layer::Impl> clone() const override;

void parseLayout(const JSVal&) override;
void parsePaints(const JSVal&) override;
Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <mbgl/map/map_data.hpp>
#include <mbgl/annotation/point_annotation.hpp>
#include <mbgl/annotation/shape_annotation.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/layer/layer_impl.hpp>

#include <mbgl/util/projection.hpp>
#include <mbgl/util/thread.hpp>
Expand Down Expand Up @@ -425,11 +425,11 @@ LatLngBounds Map::getBoundsForAnnotations(const AnnotationIDs& annotations) {

#pragma mark - Style API

void Map::addLayer(std::unique_ptr<StyleLayer> layer) {
void Map::addLayer(std::unique_ptr<Layer> layer) {
context->invoke(&MapContext::addLayer, std::move(layer), mapbox::util::optional<std::string>());
}

void Map::addLayer(std::unique_ptr<StyleLayer> layer, const std::string& before) {
void Map::addLayer(std::unique_ptr<Layer> layer, const std::string& before) {
context->invoke(&MapContext::addLayer, std::move(layer), before);
}

Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/map/map_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <mbgl/storage/response.hpp>

#include <mbgl/style/style.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/layer/layer_impl.hpp>

#include <mbgl/sprite/sprite_atlas.hpp>
#include <mbgl/sprite/sprite_store.hpp>
Expand Down Expand Up @@ -278,7 +278,7 @@ double MapContext::getTopOffsetPixelsForAnnotationIcon(const std::string& name)
return data.getAnnotationManager()->getTopOffsetPixelsForIcon(name);
}

void MapContext::addLayer(std::unique_ptr<StyleLayer> layer, mapbox::util::optional<std::string> after) {
void MapContext::addLayer(std::unique_ptr<Layer> layer, mapbox::util::optional<std::string> after) {
style->addLayer(std::move(layer), after);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/map/map_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MapContext : public Style::Observer {
void updateAnnotations();

// Style API
void addLayer(std::unique_ptr<StyleLayer>,
void addLayer(std::unique_ptr<Layer>,
const mapbox::util::optional<std::string> before);

void setSourceTileCacheSize(size_t size);
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/map/raster_tile_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void RasterTileData::request(float pixelRatio,
});
}

Bucket* RasterTileData::getBucket(StyleLayer const&) {
Bucket* RasterTileData::getBucket(const Layer&) {
return bucket.get();
}

Expand Down
Loading

0 comments on commit c510c68

Please sign in to comment.