summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2015-04-10 22:37:27 +0200
committerKonstantin Käfer <mail@kkaefer.com>2015-04-10 22:37:27 +0200
commit4c7132337f96486a154e75efe394f5a1c06a77a7 (patch)
treea1b49335556b5f09bd51173344899e9f2446597d /include
parentbd531b9e080f3300c6947b0e0e515218bb395f45 (diff)
downloadqtlocation-mapboxgl-4c7132337f96486a154e75efe394f5a1c06a77a7.tar.gz
convert DefaultFileSource to use util::Thread<>
Diffstat (limited to 'include')
-rw-r--r--include/mbgl/android/native_map_view.hpp2
-rw-r--r--include/mbgl/storage/default/asset_request.hpp2
-rw-r--r--include/mbgl/storage/default/http_request.hpp2
-rw-r--r--include/mbgl/storage/default/shared_request_base.hpp7
-rw-r--r--include/mbgl/storage/default_file_source.hpp45
-rw-r--r--include/mbgl/util/run_loop.hpp6
6 files changed, 26 insertions, 38 deletions
diff --git a/include/mbgl/android/native_map_view.hpp b/include/mbgl/android/native_map_view.hpp
index 4a8d9365bf..817b6bc8bb 100644
--- a/include/mbgl/android/native_map_view.hpp
+++ b/include/mbgl/android/native_map_view.hpp
@@ -63,7 +63,7 @@ private:
ANativeWindow *window = nullptr;
mbgl::util::Thread<mbgl::SQLiteCache> fileCache;
- mbgl::DefaultFileSource fileSource;
+ mbgl::util::Thread<mbgl::DefaultFileSource> fileSource;
mbgl::Map map;
EGLDisplay display = EGL_NO_DISPLAY;
diff --git a/include/mbgl/storage/default/asset_request.hpp b/include/mbgl/storage/default/asset_request.hpp
index c582c025fb..48f402fefe 100644
--- a/include/mbgl/storage/default/asset_request.hpp
+++ b/include/mbgl/storage/default/asset_request.hpp
@@ -9,7 +9,7 @@ class AssetRequest : public SharedRequestBase {
public:
AssetRequest(DefaultFileSource *source, const Resource &resource);
- void start(uv_loop_t *loop, std::unique_ptr<Response> response = nullptr);
+ void start(uv_loop_t *loop, std::shared_ptr<const Response> response = nullptr);
void cancel();
private:
diff --git a/include/mbgl/storage/default/http_request.hpp b/include/mbgl/storage/default/http_request.hpp
index 914e622f13..1cd38a8281 100644
--- a/include/mbgl/storage/default/http_request.hpp
+++ b/include/mbgl/storage/default/http_request.hpp
@@ -9,7 +9,7 @@ class HTTPRequest : public SharedRequestBase {
public:
HTTPRequest(DefaultFileSource *source, const Resource &resource);
- void start(uv_loop_t *loop, std::unique_ptr<Response> response = nullptr);
+ void start(uv_loop_t *loop, std::shared_ptr<const Response> response = nullptr);
void cancel();
void retryImmediately();
diff --git a/include/mbgl/storage/default/shared_request_base.hpp b/include/mbgl/storage/default/shared_request_base.hpp
index 59e38efc2f..9c5f87b33d 100644
--- a/include/mbgl/storage/default/shared_request_base.hpp
+++ b/include/mbgl/storage/default/shared_request_base.hpp
@@ -29,15 +29,14 @@ public:
SharedRequestBase(DefaultFileSource *source_, const Resource &resource_)
: resource(resource_), source(source_) {}
- virtual void start(uv_loop_t *loop, std::unique_ptr<Response> response = nullptr) = 0;
+ virtual void start(uv_loop_t *loop, std::shared_ptr<const Response> response = nullptr) = 0;
virtual void cancel() = 0;
- void notify(std::unique_ptr<Response> response, FileCache::Hint hint) {
+ void notify(std::shared_ptr<const Response> response, FileCache::Hint hint) {
MBGL_VERIFY_THREAD(tid);
if (source) {
- source->notify(this, observers, std::shared_ptr<const Response>(std::move(response)),
- hint);
+ source->notify(this, observers, response, hint);
}
}
diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp
index 7aab54f731..f6028217ea 100644
--- a/include/mbgl/storage/default_file_source.hpp
+++ b/include/mbgl/storage/default_file_source.hpp
@@ -3,25 +3,25 @@
#include <mbgl/storage/file_source.hpp>
#include <mbgl/storage/file_cache.hpp>
+#include <mbgl/util/run_loop.hpp>
#include <set>
#include <unordered_map>
-#include <thread>
-
-namespace mapbox { namespace util { template<typename... Types> class variant; } }
namespace mbgl {
-namespace util { template <typename T> class AsyncQueue; }
-
class SharedRequestBase;
-class DefaultFileSource : public FileSource {
-public:
+class DefaultFileSource : public FileSource, protected util::RunLoop {
+ friend class util::Thread<DefaultFileSource>;
+ friend class SharedRequestBase;
+
+private:
DefaultFileSource(FileCache *cache, const std::string &root = "");
- DefaultFileSource(FileCache *cache, uv_loop_t *loop, const std::string &root = "");
~DefaultFileSource() override;
+public:
+ // FileSource API
Request *request(const Resource &resource, uv_loop_t *loop, const Environment &env,
Callback callback) override;
void cancel(Request *request) override;
@@ -29,37 +29,22 @@ public:
void abort(const Environment &env) override;
+private:
void notify(SharedRequestBase *sharedRequest, const std::set<Request *> &observers,
std::shared_ptr<const Response> response, FileCache::Hint hint);
+ SharedRequestBase *find(const Resource &resource);
+
+ void processAdd(Request* request);
+ void processCancel(Request* request);
+ void processResult(const Resource& resource, std::shared_ptr<const Response> response);
+ void processAbort(const Environment& env);
public:
const std::string assetRoot;
private:
- struct ActionDispatcher;
- struct AddRequestAction;
- struct RemoveRequestAction;
- struct ResultAction;
- struct StopAction;
- struct AbortAction;
- using Action = mapbox::util::variant<AddRequestAction, RemoveRequestAction, ResultAction,
- StopAction, AbortAction>;
- using Queue = util::AsyncQueue<Action>;
-
- void process(AddRequestAction &action);
- void process(RemoveRequestAction &action);
- void process(ResultAction &action);
- void process(StopAction &action);
- void process(AbortAction &action);
-
- SharedRequestBase *find(const Resource &resource);
-
std::unordered_map<Resource, SharedRequestBase *, Resource::Hash> pending;
-
- uv_loop_t *loop = nullptr;
FileCache *cache = nullptr;
- Queue *queue = nullptr;
- std::thread thread;
};
}
diff --git a/include/mbgl/util/run_loop.hpp b/include/mbgl/util/run_loop.hpp
index 5aefcd1d3e..9bed417c38 100644
--- a/include/mbgl/util/run_loop.hpp
+++ b/include/mbgl/util/run_loop.hpp
@@ -1,6 +1,7 @@
#ifndef MBGL_UTIL_RUN_LOOP
#define MBGL_UTIL_RUN_LOOP
+#include <memory>
#include <mutex>
#include <functional>
#include <queue>
@@ -31,13 +32,16 @@ protected:
// Called by the Thread<> wrapper to terminate this loop.
void stop();
+ // Obtain the underlying loop object in case you want to attach additional listeners.
+ uv::loop& loop() { return *runloop; };
+
private:
// Invokes function in the run loop.
void process();
public:
// Schedules a function to be executed as part of this run loop.
- void invoke(std::function<void()> fn);
+ void invoke(std::function<void()>&& fn);
private:
const std::unique_ptr<uv::loop> runloop;