diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-11-12 15:51:45 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-11-16 12:25:47 -0800 |
commit | 7137239cbddb13e1c33240d13002973b5a222775 (patch) | |
tree | 99ad3ca2d8b5230eba3f5bacefe63098568dbdd4 /include | |
parent | 1caf89c32b80dc300b1fd349a2ece4557890c727 (diff) | |
download | qtlocation-mapboxgl-7137239cbddb13e1c33240d13002973b5a222775.tar.gz |
[core] Use std::unique_ptr for FileSource request
Diffstat (limited to 'include')
-rw-r--r-- | include/mbgl/storage/default_file_source.hpp | 10 | ||||
-rw-r--r-- | include/mbgl/storage/file_source.hpp | 24 | ||||
-rw-r--r-- | include/mbgl/storage/request.hpp | 52 |
3 files changed, 18 insertions, 68 deletions
diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index 5d018b720e..8d2832b33b 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -18,13 +18,13 @@ public: void setAccessToken(const std::string& t) { accessToken = t; } std::string getAccessToken() const { return accessToken; } - // FileSource API - Request* request(const Resource&, Callback) override; - void cancel(Request*) override; + std::unique_ptr<FileRequest> request(const Resource&, Callback) override; -public: - class Impl; private: + friend class DefaultFileRequest; + void cancel(const Resource&, FileRequest*); + + class Impl; const std::unique_ptr<util::Thread<Impl>> thread; std::string accessToken; }; diff --git a/include/mbgl/storage/file_source.hpp b/include/mbgl/storage/file_source.hpp index 0bb5c0fcaf..0167eccc08 100644 --- a/include/mbgl/storage/file_source.hpp +++ b/include/mbgl/storage/file_source.hpp @@ -1,31 +1,33 @@ #ifndef MBGL_STORAGE_FILE_SOURCE #define MBGL_STORAGE_FILE_SOURCE -#include "response.hpp" -#include "resource.hpp" +#include <mbgl/storage/response.hpp> +#include <mbgl/storage/resource.hpp> #include <mbgl/util/noncopyable.hpp> -#include <mbgl/util/util.hpp> #include <functional> +#include <memory> namespace mbgl { -class Request; +class FileRequest : private util::noncopyable { +public: + virtual ~FileRequest() = default; +}; class FileSource : private util::noncopyable { -protected: - MBGL_STORE_THREAD(tid) - public: virtual ~FileSource() = default; using Callback = std::function<void (Response)>; - // These can be called from any thread. The callback will be invoked in the loop. - // You can only cancel a request from the same thread it was created in. - virtual Request* request(const Resource&, Callback) = 0; - virtual void cancel(Request*) = 0; + // Request a resource. The callback will be called asynchronously, in the same + // thread as the request was made. This thread must have an active RunLoop. The + // request may be cancelled before completion by releasing the returned FileRequest. + // If the request is cancelled before the callback is executed, the callback will + // not be executed. + virtual std::unique_ptr<FileRequest> request(const Resource&, Callback) = 0; }; } diff --git a/include/mbgl/storage/request.hpp b/include/mbgl/storage/request.hpp deleted file mode 100644 index 0f178af9bb..0000000000 --- a/include/mbgl/storage/request.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef MBGL_STORAGE_DEFAULT_REQUEST -#define MBGL_STORAGE_DEFAULT_REQUEST - -#include <mbgl/storage/resource.hpp> - -#include <mbgl/util/util.hpp> -#include <mbgl/util/noncopyable.hpp> - -#include <mutex> -#include <thread> -#include <functional> -#include <memory> - -typedef struct uv_loop_s uv_loop_t; -namespace uv { class async; } - -namespace mbgl { - -class Response; - -class Request : private util::noncopyable { -public: - using Callback = std::function<void (Response)>; - Request(const Resource &resource, uv_loop_t *loop, Callback callback); - -public: - // May be called from any thread. - void notify(const std::shared_ptr<const Response> &response); - void destruct(); - - // May be called only from the thread the Request was created in. - void cancel(); - -private: - ~Request(); - void notifyCallback(); - -private: - std::mutex mtx; - bool canceled = false; - bool confirmed = false; - const std::unique_ptr<uv::async> async; - Callback callback; - std::shared_ptr<const Response> response; - -public: - const Resource resource; -}; - -} - -#endif |