From 117322d349d7ff9a1d0028b6f88dd7369027881f Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 1 Jul 2015 17:48:09 -0700 Subject: [PATCH] Don't inherit for code reuse --- src/mbgl/map/live_tile_data.cpp | 62 +++++++++++++++++-------------- src/mbgl/map/live_tile_data.hpp | 22 +++++++---- src/mbgl/map/source.cpp | 9 ++--- src/mbgl/map/vector_tile_data.hpp | 14 ++----- 4 files changed, 55 insertions(+), 52 deletions(-) diff --git a/src/mbgl/map/live_tile_data.cpp b/src/mbgl/map/live_tile_data.cpp index ad28c6c62..ad983343b 100644 --- a/src/mbgl/map/live_tile_data.cpp +++ b/src/mbgl/map/live_tile_data.cpp @@ -1,49 +1,40 @@ #include #include #include +#include +#include +#include #include #include +#include #include using namespace mbgl; LiveTileData::LiveTileData(const TileID& id_, - AnnotationManager& annotationManager_, + const LiveTile* tile, Style& style_, const SourceInfo& source_, - float angle_, - bool collisionDebug_) - : VectorTileData(id_, style_, source_, angle_, collisionDebug_), - annotationManager(annotationManager_) { - // live features are always loaded + std::function callback) + : TileData(id_), + worker(style_.workers), + tileWorker(id_, + style_, + style_.layers, + source_.max_zoom, + state, + std::make_unique(id_.z, 4096, + source_.tile_size * id.overscaling, + 0, false)) { state = State::loaded; -} -LiveTileData::~LiveTileData() { - cancel(); -} - -bool LiveTileData::reparse(std::function callback) { - if (parsing || (state != State::loaded && state != State::partial)) { - return false; - } - - const LiveTile* tile = annotationManager.getTile(id); if (!tile) { state = State::parsed; - return true; + return; } - parsing = true; - workRequest = worker.parseLiveTile(tileWorker, *tile, [this, callback] (TileParseResult result) { - parsing = false; - - if (state == State::obsolete) { - return; - } - if (result.is()) { state = result.get(); } else { @@ -53,6 +44,21 @@ bool LiveTileData::reparse(std::function callback) { callback(); }); - - return true; +} + +LiveTileData::~LiveTileData() { + cancel(); +} + +Bucket* LiveTileData::getBucket(const StyleLayer& layer) { + if (!isReady() || !layer.bucket) { + return nullptr; + } + + return tileWorker.getBucket(layer); +} + +void LiveTileData::cancel() { + state = State::obsolete; + workRequest.reset(); } diff --git a/src/mbgl/map/live_tile_data.hpp b/src/mbgl/map/live_tile_data.hpp index 2d6b01061..5c4220fd6 100644 --- a/src/mbgl/map/live_tile_data.hpp +++ b/src/mbgl/map/live_tile_data.hpp @@ -1,26 +1,32 @@ #ifndef MBGL_MAP_LIVE_TILE_DATA #define MBGL_MAP_LIVE_TILE_DATA -#include +#include +#include namespace mbgl { -class AnnotationManager; +class Style; +class SourceInfo; +class WorkRequest; +class LiveTile; -class LiveTileData : public VectorTileData { +class LiveTileData : public TileData { public: LiveTileData(const TileID&, - AnnotationManager&, + const LiveTile*, Style&, const SourceInfo&, - float angle_, - bool collisionDebug_); + std::function callback); ~LiveTileData(); - bool reparse(std::function callback) override; + void cancel() override; + Bucket* getBucket(const StyleLayer&) override; private: - AnnotationManager& annotationManager; + Worker& worker; + TileWorker tileWorker; + std::unique_ptr workRequest; }; } diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 7b6a35ed2..f596296fc 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -235,7 +235,7 @@ bool Source::handlePartialTile(const TileID& id, Worker&) { // Note: this uses a raw pointer; we don't want the callback binding to have a // shared pointer. - VectorTileData* data = static_cast(it->second.lock().get()); + VectorTileData* data = dynamic_cast(it->second.lock().get()); if (!data) { return true; } @@ -299,11 +299,8 @@ TileData::State Source::addTile(MapData& data, tileData->request(transformState.getPixelRatio(), callback); new_tile.data = tileData; } else if (info.type == SourceType::Annotations) { - auto tileData = std::make_shared(normalized_id, data.annotationManager, - style, info, - transformState.getAngle(), data.getCollisionDebug()); - tileData->reparse(callback); - new_tile.data = tileData; + new_tile.data = std::make_shared(normalized_id, + data.annotationManager.getTile(normalized_id), style, info, callback); } else { throw std::runtime_error("source type not implemented"); } diff --git a/src/mbgl/map/vector_tile_data.hpp b/src/mbgl/map/vector_tile_data.hpp index b0b5ec6e8..3b73ded0c 100644 --- a/src/mbgl/map/vector_tile_data.hpp +++ b/src/mbgl/map/vector_tile_data.hpp @@ -8,13 +8,10 @@ namespace mbgl { -class SourceInfo; -class Request; -class Bucket; -class SourceInfo; -class StyleLayer; class Style; +class SourceInfo; class WorkRequest; +class Request; class VectorTileData : public TileData { public: @@ -31,23 +28,20 @@ public: void request(float pixelRatio, const std::function& callback); - virtual bool reparse(std::function callback); + bool reparse(std::function callback); void redoPlacement(float angle, bool collisionDebug) override; void cancel() override; -protected: +private: Worker& worker; TileWorker tileWorker; std::unique_ptr workRequest; bool parsing = false; - -private: const SourceInfo& source; Request* req = nullptr; std::string data; - float lastAngle = 0; float currentAngle; bool lastCollisionDebug = 0;