From 4ea47151796cb885fbcb2dd3803aa3f7132467bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 20 Jun 2017 11:40:56 -0700 Subject: [core] shared data among AnnotationTileFeature objects --- src/mbgl/annotation/annotation_manager.cpp | 2 +- src/mbgl/annotation/annotation_tile.cpp | 67 +++++++++++++++++++++----- src/mbgl/annotation/annotation_tile.hpp | 37 ++++++++------ src/mbgl/annotation/shape_annotation_impl.cpp | 4 +- src/mbgl/annotation/symbol_annotation_impl.cpp | 2 +- 5 files changed, 82 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/mbgl/annotation/annotation_manager.cpp b/src/mbgl/annotation/annotation_manager.cpp index 05b1b13d2d..bc23a94adf 100644 --- a/src/mbgl/annotation/annotation_manager.cpp +++ b/src/mbgl/annotation/annotation_manager.cpp @@ -133,7 +133,7 @@ std::unique_ptr AnnotationManager::getTileData(const Canonic auto tileData = std::make_unique(); - AnnotationTileLayer& pointLayer = tileData->layers.emplace(PointLayerID, PointLayerID).first->second; + AnnotationTileLayer& pointLayer = tileData->addLayer(PointLayerID); LatLngBounds tileBounds(tileID); diff --git a/src/mbgl/annotation/annotation_tile.cpp b/src/mbgl/annotation/annotation_tile.cpp index 67a2d128ba..e8c62effd8 100644 --- a/src/mbgl/annotation/annotation_tile.cpp +++ b/src/mbgl/annotation/annotation_tile.cpp @@ -19,26 +19,64 @@ AnnotationTile::~AnnotationTile() { annotationManager.removeTile(*this); } -void AnnotationTile::setNecessity(Necessity) {} +void AnnotationTile::setNecessity(Necessity) { +} + +class AnnotationTileFeatureData { +public: + AnnotationTileFeatureData(const AnnotationID id_, + FeatureType type_, + GeometryCollection&& geometries_, + std::unordered_map&& properties_) + : id(id_), + type(type_), + geometries(std::move(geometries_)), + properties(std::move(properties_)) { + } -AnnotationTileFeature::AnnotationTileFeature(const AnnotationID id_, - FeatureType type_, GeometryCollection geometries_, - std::unordered_map properties_) - : id(id_), - type(type_), - properties(std::move(properties_)), - geometries(std::move(geometries_)) {} + AnnotationID id; + FeatureType type; + GeometryCollection geometries; + std::unordered_map properties; +}; + +AnnotationTileFeature::AnnotationTileFeature(std::shared_ptr data_) + : data(std::move(data_)) { +} + +AnnotationTileFeature::~AnnotationTileFeature() = default; + +FeatureType AnnotationTileFeature::getType() const { + return data->type; +} optional AnnotationTileFeature::getValue(const std::string& key) const { - auto it = properties.find(key); - if (it != properties.end()) { + auto it = data->properties.find(key); + if (it != data->properties.end()) { return optional(it->second); } return optional(); } -AnnotationTileLayer::AnnotationTileLayer(std::string name_) - : name(std::move(name_)) {} +optional AnnotationTileFeature::getID() const { + return { static_cast(data->id) }; +} + +GeometryCollection AnnotationTileFeature::getGeometries() const { + return data->geometries; +} + +AnnotationTileLayer::AnnotationTileLayer(std::string name_) : name(std::move(name_)) { +} + +void AnnotationTileLayer::addFeature(const AnnotationID id, + FeatureType type, + GeometryCollection geometries, + std::unordered_map properties) { + + features.emplace_back(std::make_shared( + id, type, std::move(geometries), std::move(properties))); +} std::unique_ptr AnnotationTileData::clone() const { return std::make_unique(*this); @@ -52,4 +90,9 @@ const GeometryTileLayer* AnnotationTileData::getLayer(const std::string& name) c return nullptr; } +AnnotationTileLayer& AnnotationTileData::addLayer(const std::string& name) { + // Only constructs a new layer if it doesn't yet exist, otherwise, we'll use the existing one. + return layers.emplace(name, name).first->second; +} + } // namespace mbgl diff --git a/src/mbgl/annotation/annotation_tile.hpp b/src/mbgl/annotation/annotation_tile.hpp index ea4ff5ebd5..68a4d342ac 100644 --- a/src/mbgl/annotation/annotation_tile.hpp +++ b/src/mbgl/annotation/annotation_tile.hpp @@ -11,8 +11,7 @@ class TileParameters; class AnnotationTile : public GeometryTile { public: - AnnotationTile(const OverscaledTileID&, - const TileParameters&); + AnnotationTile(const OverscaledTileID&, const TileParameters&); ~AnnotationTile() override; void setNecessity(Necessity) final; @@ -21,37 +20,45 @@ private: AnnotationManager& annotationManager; }; +class AnnotationTileFeatureData; + class AnnotationTileFeature : public GeometryTileFeature { public: - AnnotationTileFeature(AnnotationID, FeatureType, GeometryCollection, - std::unordered_map properties = {{}}); + AnnotationTileFeature(std::shared_ptr); + ~AnnotationTileFeature() override; - FeatureType getType() const override { return type; } + FeatureType getType() const override; optional getValue(const std::string&) const override; - optional getID() const override { return { static_cast(id) }; } - GeometryCollection getGeometries() const override { return geometries; } + optional getID() const override; + GeometryCollection getGeometries() const override; - const AnnotationID id; - const FeatureType type; - const std::unordered_map properties; - const GeometryCollection geometries; +private: + std::shared_ptr data; }; class AnnotationTileLayer : public GeometryTileLayer { public: AnnotationTileLayer(std::string); - std::size_t featureCount() const override { return features.size(); } + std::size_t featureCount() const override { + return features.size(); + } std::unique_ptr getFeature(std::size_t i) const override { return std::make_unique(features.at(i)); } - std::string getName() const override { return name; }; + std::string getName() const override { + return name; + } - std::vector features; + void addFeature(const AnnotationID, + FeatureType, + GeometryCollection, + std::unordered_map properties = { {} }); private: + std::vector> features; std::string name; }; @@ -59,7 +66,9 @@ class AnnotationTileData : public GeometryTileData { public: std::unique_ptr clone() const override; const GeometryTileLayer* getLayer(const std::string&) const override; + AnnotationTileLayer& addLayer(const std::string&); +private: std::unordered_map layers; }; diff --git a/src/mbgl/annotation/shape_annotation_impl.cpp b/src/mbgl/annotation/shape_annotation_impl.cpp index d3ddf62b9e..05ffce382d 100644 --- a/src/mbgl/annotation/shape_annotation_impl.cpp +++ b/src/mbgl/annotation/shape_annotation_impl.cpp @@ -38,7 +38,7 @@ void ShapeAnnotationImpl::updateTileData(const CanonicalTileID& tileID, Annotati if (shapeTile.features.empty()) return; - AnnotationTileLayer& layer = data.layers.emplace(layerID, layerID).first->second; + AnnotationTileLayer& layer = data.addLayer(layerID); ToGeometryCollection toGeometryCollection; ToFeatureType toFeatureType; @@ -53,7 +53,7 @@ void ShapeAnnotationImpl::updateTileData(const CanonicalTileID& tileID, Annotati renderGeometry = fixupPolygons(renderGeometry); } - layer.features.emplace_back(id, featureType, renderGeometry); + layer.addFeature(id, featureType, renderGeometry); } } diff --git a/src/mbgl/annotation/symbol_annotation_impl.cpp b/src/mbgl/annotation/symbol_annotation_impl.cpp index e5ae5f4b91..c95eb481b5 100644 --- a/src/mbgl/annotation/symbol_annotation_impl.cpp +++ b/src/mbgl/annotation/symbol_annotation_impl.cpp @@ -18,7 +18,7 @@ void SymbolAnnotationImpl::updateLayer(const CanonicalTileID& tileID, Annotation LatLng latLng { annotation.geometry.y, annotation.geometry.x }; TileCoordinate coordinate = TileCoordinate::fromLatLng(0, latLng); GeometryCoordinate tilePoint = TileCoordinate::toGeometryCoordinate(UnwrappedTileID(0, tileID), coordinate.p); - layer.features.emplace_back(id, FeatureType::Point, GeometryCollection {{ {{ tilePoint }} }}, featureProperties); + layer.addFeature(id, FeatureType::Point, GeometryCollection {{ {{ tilePoint }} }}, featureProperties); } } // namespace mbgl -- cgit v1.2.1