summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsheem Mamoowala <asheem.mamoowala@mapbox.com>2017-10-09 14:58:19 -0700
committerAsheem Mamoowala <asheem.mamoowala@mapbox.com>2017-10-10 16:53:14 -0700
commit135217fcee675da1f9be6bca63c43753e19d01b4 (patch)
treeb3e7cccb5485f715e68bc84ae3b48c8a47f04191
parent3d31f816b052cfe69ee46495fe7a0aca71053b0e (diff)
downloadqtlocation-mapboxgl-135217fcee675da1f9be6bca63c43753e19d01b4.tar.gz
[core] Move CustomTileLoader to its own header and cpp file
-rw-r--r--cmake/core-files.cmake2
-rw-r--r--include/mbgl/style/sources/custom_vector_source.hpp26
-rw-r--r--src/mbgl/style/custom_tile_loader.cpp67
-rw-r--r--src/mbgl/style/custom_tile_loader.hpp37
-rw-r--r--src/mbgl/style/sources/custom_vector_source.cpp107
-rw-r--r--src/mbgl/style/sources/custom_vector_source_impl.hpp1
-rw-r--r--src/mbgl/tile/custom_tile.hpp2
7 files changed, 119 insertions, 123 deletions
diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake
index 629a9a554a..6a62d8fb40 100644
--- a/cmake/core-files.cmake
+++ b/cmake/core-files.cmake
@@ -335,6 +335,8 @@ set(MBGL_CORE_FILES
include/mbgl/style/types.hpp
include/mbgl/style/undefined.hpp
src/mbgl/style/collection.hpp
+ src/mbgl/style/custom_tile_loader.cpp
+ src/mbgl/style/custom_tile_loader.hpp
src/mbgl/style/image.cpp
src/mbgl/style/image_impl.cpp
src/mbgl/style/image_impl.hpp
diff --git a/include/mbgl/style/sources/custom_vector_source.hpp b/include/mbgl/style/sources/custom_vector_source.hpp
index 1a3ed12931..77d0a69bd3 100644
--- a/include/mbgl/style/sources/custom_vector_source.hpp
+++ b/include/mbgl/style/sources/custom_vector_source.hpp
@@ -4,7 +4,7 @@
#include <mbgl/util/geojson.hpp>
#include <mbgl/util/range.hpp>
#include <mbgl/util/constants.hpp>
-#include <mbgl/actor/actor_ref.hpp>
+#include <mbgl/actor/mailbox.hpp>
namespace mbgl {
@@ -13,24 +13,8 @@ class CanonicalTileID;
namespace style {
-using SetTileDataFunction = std::function<void(const mapbox::geojson::geojson&)>;
using TileFunction = std::function<void(const CanonicalTileID&)>;
-
-class CustomTileLoader : private util::noncopyable {
-public:
- CustomTileLoader(TileFunction fetchTileFn, TileFunction cancelTileFn);
- ~CustomTileLoader();
-
- void fetchTile(const OverscaledTileID& tileID, ActorRef<SetTileDataFunction> callbackRef);
- void cancelTile(const OverscaledTileID& tileID);
- void removeTile(const OverscaledTileID& tileID);
-
- void setTileData(const CanonicalTileID& tileID, const mapbox::geojson::geojson& data);
-
-private:
- class Impl;
- Impl* impl = nullptr;
-};
+class CustomTileLoader;
class CustomVectorSource : public Source {
public:
@@ -48,7 +32,7 @@ public:
};
public:
CustomVectorSource(std::string id, CustomVectorSource::Options options);
-
+ ~CustomVectorSource() final;
void loadDescription(FileSource&) final;
void setTileData(const CanonicalTileID&, const mapbox::geojson::geojson&);
@@ -56,8 +40,8 @@ public:
class Impl;
const Impl& impl() const;
private:
- std::shared_ptr<Mailbox> mailbox;
- CustomTileLoader loader;
+ std::shared_ptr<Mailbox> mailbox;
+ std::unique_ptr<CustomTileLoader> loader;
};
diff --git a/src/mbgl/style/custom_tile_loader.cpp b/src/mbgl/style/custom_tile_loader.cpp
new file mode 100644
index 0000000000..91832f82ba
--- /dev/null
+++ b/src/mbgl/style/custom_tile_loader.cpp
@@ -0,0 +1,67 @@
+#include <mbgl/style/custom_tile_loader.hpp>
+
+namespace mbgl {
+namespace style {
+
+CustomTileLoader::CustomTileLoader(const TileFunction& fetchTileFn, const TileFunction& cancelTileFn) {
+ fetchTileFunction = fetchTileFn;
+ cancelTileFunction = cancelTileFn;
+}
+
+void CustomTileLoader::fetchTile(const OverscaledTileID& tileID, ActorRef<SetTileDataFunction> callbackRef) {
+ auto cachedTileData = dataCache.find(tileID.canonical);
+ if (cachedTileData == dataCache.end()) {
+ fetchTileFunction(tileID.canonical);
+ } else {
+ callbackRef.invoke(&SetTileDataFunction::operator(), *(cachedTileData->second));
+ }
+ auto tileCallbacks = tileCallbackMap.find(tileID.canonical);
+ if (tileCallbacks == tileCallbackMap.end()) {
+ auto tuple = std::make_tuple(tileID.overscaledZ, tileID.wrap, callbackRef);
+ tileCallbackMap.insert({ tileID.canonical, std::vector<OverscaledIDFunctionTuple>(1, tuple) });
+ }
+ else {
+ for(auto iter = tileCallbacks->second.begin(); iter != tileCallbacks->second.end(); iter++) {
+ if(std::get<0>(*iter) == tileID.overscaledZ && std::get<1>(*iter) == tileID.wrap ) {
+ std::get<2>(*iter) = callbackRef;
+ return;
+ }
+ }
+ tileCallbacks->second.emplace_back(std::make_tuple(tileID.overscaledZ, tileID.wrap, callbackRef));
+ }
+}
+
+void CustomTileLoader::cancelTile(const OverscaledTileID& tileID) {
+ if(tileCallbackMap.find(tileID.canonical) != tileCallbackMap.end()) {
+ cancelTileFunction(tileID.canonical);
+ }
+}
+
+void CustomTileLoader::removeTile(const OverscaledTileID& tileID) {
+ auto tileCallbacks = tileCallbackMap.find(tileID.canonical);
+ if (tileCallbacks == tileCallbackMap.end()) return;
+ for(auto iter = tileCallbacks->second.begin(); iter != tileCallbacks->second.end(); iter++) {
+ if(std::get<0>(*iter) == tileID.overscaledZ && std::get<1>(*iter) == tileID.wrap ) {
+ tileCallbacks->second.erase(iter);
+ break;
+ }
+ }
+ if (tileCallbacks->second.size() == 0) {
+ tileCallbackMap.erase(tileCallbacks);
+ dataCache.erase(tileID.canonical);
+ }
+
+}
+
+void CustomTileLoader::setTileData(const CanonicalTileID& tileID, const mapbox::geojson::geojson& data) {
+ auto iter = tileCallbackMap.find(tileID);
+ if (iter == tileCallbackMap.end()) return;
+ dataCache[tileID] = std::make_unique<mapbox::geojson::geojson>(std::move(data));
+ for(auto tuple : iter->second) {
+ auto actor = std::get<2>(tuple);
+ actor.invoke(&SetTileDataFunction::operator(), data);
+ }
+}
+
+} // namespace style
+} // namespace mbgl
diff --git a/src/mbgl/style/custom_tile_loader.hpp b/src/mbgl/style/custom_tile_loader.hpp
new file mode 100644
index 0000000000..6f390cfe35
--- /dev/null
+++ b/src/mbgl/style/custom_tile_loader.hpp
@@ -0,0 +1,37 @@
+#pragma once
+
+#include <mbgl/style/sources/custom_vector_source.hpp>
+#include <mbgl/actor/scheduler.hpp>
+#include <mbgl/tile/tile_id.hpp>
+#include <mbgl/actor/actor_ref.hpp>
+
+#include <tuple>
+#include <map>
+
+namespace mbgl {
+namespace style {
+
+using SetTileDataFunction = std::function<void(const mapbox::geojson::geojson&)>;
+
+class CustomTileLoader : private util::noncopyable {
+public:
+
+ using OverscaledIDFunctionTuple = std::tuple<uint8_t, int16_t, ActorRef<SetTileDataFunction>>;
+
+ CustomTileLoader(const TileFunction& fetchTileFn, const TileFunction& cancelTileFn);
+
+ void fetchTile(const OverscaledTileID& tileID, ActorRef<SetTileDataFunction> callbackRef);
+ void cancelTile(const OverscaledTileID& tileID);
+
+ void removeTile(const OverscaledTileID& tileID);
+ void setTileData(const CanonicalTileID& tileID, const mapbox::geojson::geojson& data);
+
+private:
+ TileFunction fetchTileFunction;
+ TileFunction cancelTileFunction;
+ std::unordered_map<CanonicalTileID, std::vector<OverscaledIDFunctionTuple>> tileCallbackMap;
+ std::map<CanonicalTileID, std::unique_ptr<mapbox::geojson::geojson>> dataCache;
+};
+
+} // namespace style
+} // namespace mbgl
diff --git a/src/mbgl/style/sources/custom_vector_source.cpp b/src/mbgl/style/sources/custom_vector_source.cpp
index 7a1e6049da..fc2f8f548e 100644
--- a/src/mbgl/style/sources/custom_vector_source.cpp
+++ b/src/mbgl/style/sources/custom_vector_source.cpp
@@ -1,4 +1,5 @@
#include <mbgl/style/sources/custom_vector_source.hpp>
+#include <mbgl/style/custom_tile_loader.hpp>
#include <mbgl/style/sources/custom_vector_source_impl.hpp>
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/tile/tile_id.hpp>
@@ -9,122 +10,26 @@
namespace mbgl {
namespace style {
-class CustomTileLoader::Impl {
-public:
-
- using OverscaledIDFunctionTuple = std::tuple<uint8_t, int16_t, ActorRef<SetTileDataFunction>>;
-
- Impl(TileFunction&& fetchTileFn, TileFunction&& cancelTileFn) {
- fetchTileFunction = std::move(fetchTileFn);
- cancelTileFunction = std::move(cancelTileFn);
- }
-
- void fetchTile(const OverscaledTileID& tileID, ActorRef<SetTileDataFunction> callbackRef) {
- auto cachedTileData = dataCache.find(tileID.canonical);
- if (cachedTileData == dataCache.end()) {
- fetchTileFunction(tileID.canonical);
- } else {
- callbackRef.invoke(&SetTileDataFunction::operator(), *(cachedTileData->second));
- }
- auto tileCallbacks = tileCallbackMap.find(tileID.canonical);
- if (tileCallbacks == tileCallbackMap.end()) {
- auto tuple = std::make_tuple(tileID.overscaledZ, tileID.wrap, callbackRef);
- tileCallbackMap.insert({ tileID.canonical, std::vector<OverscaledIDFunctionTuple>(1, tuple) });
- }
- else {
- for(auto iter = tileCallbacks->second.begin(); iter != tileCallbacks->second.end(); iter++) {
- if(std::get<0>(*iter) == tileID.overscaledZ && std::get<1>(*iter) == tileID.wrap ) {
- std::get<2>(*iter) = callbackRef;
- return;
- }
- }
- tileCallbacks->second.emplace_back(std::make_tuple(tileID.overscaledZ, tileID.wrap, callbackRef));
- }
- }
-
- void cancelTile(const OverscaledTileID& tileID) {
- if(tileCallbackMap.find(tileID.canonical) != tileCallbackMap.end()) {
- cancelTileFunction(tileID.canonical);
- }
- }
-
- void removeTile(const OverscaledTileID& tileID) {
- auto tileCallbacks = tileCallbackMap.find(tileID.canonical);
- if (tileCallbacks == tileCallbackMap.end()) return;
- for(auto iter = tileCallbacks->second.begin(); iter != tileCallbacks->second.end(); iter++) {
- if(std::get<0>(*iter) == tileID.overscaledZ && std::get<1>(*iter) == tileID.wrap ) {
- tileCallbacks->second.erase(iter);
- break;
- }
- }
- if (tileCallbacks->second.size() == 0) {
- tileCallbackMap.erase(tileCallbacks);
- dataCache.erase(tileID.canonical);
- }
-
- }
-
- void setTileData(const CanonicalTileID& tileID, const mapbox::geojson::geojson& data) {
- auto iter = tileCallbackMap.find(tileID);
- if (iter == tileCallbackMap.end()) return;
- dataCache[tileID] = std::make_unique<mapbox::geojson::geojson>(std::move(data));
- for(auto tuple : iter->second) {
- auto actor = std::get<2>(tuple);
- actor.invoke(&SetTileDataFunction::operator(), data);
- }
- }
-
-private:
- TileFunction fetchTileFunction;
- TileFunction cancelTileFunction;
- std::unordered_map<CanonicalTileID, std::vector<OverscaledIDFunctionTuple>> tileCallbackMap;
- std::map<CanonicalTileID, std::unique_ptr<mapbox::geojson::geojson>> dataCache;
-};
-
-CustomTileLoader::CustomTileLoader(TileFunction fetchTileFn, TileFunction cancelTileFn)
- : impl(new CustomTileLoader::Impl(std::move(fetchTileFn), std::move(cancelTileFn))) {
-
-}
-
-CustomTileLoader::~CustomTileLoader() {
- delete impl;
- impl = nullptr;
-}
-
-void CustomTileLoader::fetchTile(const OverscaledTileID& tileID, ActorRef<SetTileDataFunction> callbackRef) {
- impl->fetchTile(tileID, callbackRef);
-}
-
-void CustomTileLoader::cancelTile(const OverscaledTileID& tileID) {
- impl->cancelTile(tileID);
-}
-
-void CustomTileLoader::setTileData(const CanonicalTileID& tileID, const mapbox::geojson::geojson& data) {
- impl->setTileData(tileID, data);
-}
-
-void CustomTileLoader::removeTile(const OverscaledTileID& tileID) {
- impl->removeTile(tileID);
-}
-
CustomVectorSource::CustomVectorSource(std::string id,
const CustomVectorSource::Options options)
: Source(makeMutable<CustomVectorSource::Impl>(std::move(id), options)),
mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())),
- loader(options.fetchTileFunction, options.cancelTileFunction) {
+ loader(std::make_unique<CustomTileLoader>(options.fetchTileFunction, options.cancelTileFunction)) {
}
+CustomVectorSource::~CustomVectorSource() = default;
+
const CustomVectorSource::Impl& CustomVectorSource::impl() const {
return static_cast<const CustomVectorSource::Impl&>(*baseImpl);
}
void CustomVectorSource::loadDescription(FileSource&) {
- baseImpl = makeMutable<CustomVectorSource::Impl>(impl(), ActorRef<CustomTileLoader>(loader, mailbox));
+ baseImpl = makeMutable<CustomVectorSource::Impl>(impl(), ActorRef<CustomTileLoader>(*loader, mailbox));
loaded = true;
}
void CustomVectorSource::setTileData(const CanonicalTileID& tileID,
const mapbox::geojson::geojson& data) {
- loader.setTileData(tileID, data);
+ loader->setTileData(tileID, data);
}
} // namespace style
diff --git a/src/mbgl/style/sources/custom_vector_source_impl.hpp b/src/mbgl/style/sources/custom_vector_source_impl.hpp
index 90c7b4a76e..f772ae6593 100644
--- a/src/mbgl/style/sources/custom_vector_source_impl.hpp
+++ b/src/mbgl/style/sources/custom_vector_source_impl.hpp
@@ -2,6 +2,7 @@
#include <mbgl/style/source_impl.hpp>
#include <mbgl/style/sources/custom_vector_source.hpp>
+#include <mbgl/style/custom_tile_loader.hpp>
#include <mbgl/actor/actor_ref.hpp>
namespace mbgl {
diff --git a/src/mbgl/tile/custom_tile.hpp b/src/mbgl/tile/custom_tile.hpp
index 039600e7a7..74f4fff3a5 100644
--- a/src/mbgl/tile/custom_tile.hpp
+++ b/src/mbgl/tile/custom_tile.hpp
@@ -3,7 +3,7 @@
#include <mbgl/tile/geometry_tile.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/style/sources/custom_vector_source.hpp>
-#include <mbgl/style/sources/geojson_source.hpp>
+#include <mbgl/style/custom_tile_loader.hpp>
namespace mbgl {