diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2015-04-13 18:28:18 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2015-04-13 18:28:18 +0200 |
commit | 452d12e6e653d61c2182f4464d4365f0b96c9739 (patch) | |
tree | a96bcdba373ca23d832364d7bce6b5912864c1f5 /include/mbgl | |
parent | 16849a341b72c0633be1e3c89498c883d6efb000 (diff) | |
download | qtlocation-mapboxgl-452d12e6e653d61c2182f4464d4365f0b96c9739.tar.gz |
make implementation files private headers
Diffstat (limited to 'include/mbgl')
-rw-r--r-- | include/mbgl/android/native_map_view.hpp | 2 | ||||
-rw-r--r-- | include/mbgl/storage/default/asset_request.hpp | 24 | ||||
-rw-r--r-- | include/mbgl/storage/default/http_context.hpp | 76 | ||||
-rw-r--r-- | include/mbgl/storage/default/http_request.hpp | 26 | ||||
-rw-r--r-- | include/mbgl/storage/default/request.hpp | 60 | ||||
-rw-r--r-- | include/mbgl/storage/default/shared_request_base.hpp | 106 | ||||
-rw-r--r-- | include/mbgl/storage/default/thread_context.hpp | 78 | ||||
-rw-r--r-- | include/mbgl/storage/default_file_source.hpp | 5 | ||||
-rw-r--r-- | include/mbgl/storage/sqlite_cache.hpp (renamed from include/mbgl/storage/default/sqlite_cache.hpp) | 0 | ||||
-rw-r--r-- | include/mbgl/util/run_loop.hpp | 56 | ||||
-rw-r--r-- | include/mbgl/util/thread.hpp | 87 |
11 files changed, 5 insertions, 515 deletions
diff --git a/include/mbgl/android/native_map_view.hpp b/include/mbgl/android/native_map_view.hpp index 21784f5315..14ccaba3f7 100644 --- a/include/mbgl/android/native_map_view.hpp +++ b/include/mbgl/android/native_map_view.hpp @@ -4,7 +4,7 @@ #include <mbgl/map/map.hpp> #include <mbgl/map/view.hpp> #include <mbgl/util/noncopyable.hpp> -#include <mbgl/storage/default/sqlite_cache.hpp> +#include <mbgl/storage/sqlite_cache.hpp> #include <mbgl/storage/default_file_source.hpp> #include <string> diff --git a/include/mbgl/storage/default/asset_request.hpp b/include/mbgl/storage/default/asset_request.hpp deleted file mode 100644 index 48d421c3be..0000000000 --- a/include/mbgl/storage/default/asset_request.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef MBGL_STORAGE_DEFAULT_ASSET_REQUEST -#define MBGL_STORAGE_DEFAULT_ASSET_REQUEST - -#include "shared_request_base.hpp" - -namespace mbgl { - -class AssetRequest : public SharedRequestBase { -public: - AssetRequest(DefaultFileSource::Impl *source, const Resource &resource); - - void start(uv_loop_t *loop, std::shared_ptr<const Response> response = nullptr); - void cancel(); - -private: - ~AssetRequest(); - void *ptr = nullptr; - - friend class AssetRequestImpl; -}; - -} - -#endif diff --git a/include/mbgl/storage/default/http_context.hpp b/include/mbgl/storage/default/http_context.hpp deleted file mode 100644 index 6b9518dab3..0000000000 --- a/include/mbgl/storage/default/http_context.hpp +++ /dev/null @@ -1,76 +0,0 @@ -#ifndef MBGL_STORAGE_DEFAULT_HTTP_CONTEXT -#define MBGL_STORAGE_DEFAULT_HTTP_CONTEXT - -#include "thread_context.hpp" -#include <mbgl/storage/network_status.hpp> - -#include <set> - -namespace mbgl { - -class HTTPRequest; - -// This is a template class that provides a per-thread Context object. It can be used by HTTP -// implementations to store global state. It also implements the NetworkStatus mechanism and -// triggers immediate retries on all requests waiting for network status changes. - -template <typename Context> -class HTTPContext : public ThreadContext<Context> { -public: - HTTPContext(uv_loop_t *loop); - ~HTTPContext(); - - void addRequest(HTTPRequest *request); - void removeRequest(HTTPRequest *baton); - -public: - // Will be fired when the network status becomes reachable. - uv_async_t *reachability = nullptr; - - // A list of all pending HTTPRequestImpls that we need to notify when the network status - // changes. - std::set<HTTPRequest *> requests; -}; - -template <typename Context> -HTTPContext<Context>::HTTPContext(uv_loop_t *loop_) - : ThreadContext<Context>(loop_) { - reachability = new uv_async_t; - reachability->data = this; -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - uv_async_init(loop_, reachability, [](uv_async_t *async, int) { -#else - uv_async_init(loop_, reachability, [](uv_async_t *async) { -#endif - for (auto request : reinterpret_cast<Context *>(async->data)->requests) { - request->retryImmediately(); - } - }); - // Allow the loop to quit even though this handle is still active. - uv_unref(reinterpret_cast<uv_handle_t *>(reachability)); - NetworkStatus::Subscribe(reachability); -} - -template <typename Context> -HTTPContext<Context>::~HTTPContext() { - MBGL_VERIFY_THREAD(HTTPContext<Context>::tid); - - assert(requests.empty()); - - NetworkStatus::Unsubscribe(reachability); - uv::close(reachability); -} - -template <typename Context> -void HTTPContext<Context>::addRequest(HTTPRequest *request) { - requests.insert(request); -} - -template <typename Context> -void HTTPContext<Context>::removeRequest(HTTPRequest *request) { - requests.erase(request); -} - -} - -#endif diff --git a/include/mbgl/storage/default/http_request.hpp b/include/mbgl/storage/default/http_request.hpp deleted file mode 100644 index 54e9a77ef0..0000000000 --- a/include/mbgl/storage/default/http_request.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef MBGL_STORAGE_DEFAULT_HTTP_REQUEST -#define MBGL_STORAGE_DEFAULT_HTTP_REQUEST - -#include "shared_request_base.hpp" - -namespace mbgl { - -class HTTPRequest : public SharedRequestBase { -public: - HTTPRequest(DefaultFileSource::Impl *source, const Resource &resource); - - void start(uv_loop_t *loop, std::shared_ptr<const Response> response = nullptr); - void cancel(); - - void retryImmediately(); - -private: - ~HTTPRequest(); - void *ptr = nullptr; - - friend class HTTPRequestImpl; -}; - -} - -#endif diff --git a/include/mbgl/storage/default/request.hpp b/include/mbgl/storage/default/request.hpp deleted file mode 100644 index 00157329be..0000000000 --- a/include/mbgl/storage/default/request.hpp +++ /dev/null @@ -1,60 +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_async_s uv_async_t; -typedef struct uv_loop_s uv_loop_t; - -namespace mbgl { - -class Response; -class Environment; - -class Request : private util::noncopyable { - MBGL_STORE_THREAD(tid) - -public: - using Callback = std::function<void(const Response &)>; - Request(const Resource &resource, uv_loop_t *loop, const Environment &env, 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 invoke(); - void notifyCallback(); - -private: - uv_async_t *async = nullptr; - struct Canceled; - std::unique_ptr<Canceled> canceled; - Callback callback; - std::shared_ptr<const Response> response; - -public: - const Resource resource; - - // The environment ref is used to associate requests with a particular environment. This allows - // us to only terminate requests associated with that environment, e.g. when the map the env - // belongs to is discarded. - const Environment &env; -}; - -} - -#endif diff --git a/include/mbgl/storage/default/shared_request_base.hpp b/include/mbgl/storage/default/shared_request_base.hpp deleted file mode 100644 index efc78f603d..0000000000 --- a/include/mbgl/storage/default/shared_request_base.hpp +++ /dev/null @@ -1,106 +0,0 @@ -#ifndef MBGL_STORAGE_DEFAULT_SHARED_REQUEST_BASE -#define MBGL_STORAGE_DEFAULT_SHARED_REQUEST_BASE - -#include <mbgl/storage/resource.hpp> -#include <mbgl/storage/file_cache.hpp> -#include <mbgl/storage/default_file_source_impl.hpp> -#include <mbgl/storage/default/request.hpp> -#include <mbgl/util/util.hpp> -#include <mbgl/util/noncopyable.hpp> - -#include <string> -#include <set> -#include <vector> -#include <cassert> - -typedef struct uv_loop_s uv_loop_t; - -namespace mbgl { - -class Request; -class Response; -class DefaultFileSource; - -class SharedRequestBase : private util::noncopyable { -protected: - MBGL_STORE_THREAD(tid) - -public: - SharedRequestBase(DefaultFileSource::Impl *source_, const Resource &resource_) - : resource(resource_), source(source_) {} - - virtual void start(uv_loop_t *loop, std::shared_ptr<const Response> response = nullptr) = 0; - virtual void cancel() = 0; - - void notify(std::shared_ptr<const Response> response, FileCache::Hint hint) { - MBGL_VERIFY_THREAD(tid); - - if (source) { - source->notify(this, observers, response, hint); - } - } - - void subscribe(Request *request) { - MBGL_VERIFY_THREAD(tid); - - observers.insert(request); - } - - void unsubscribe(Request *request) { - MBGL_VERIFY_THREAD(tid); - - observers.erase(request); - - if (abandoned()) { - // There are no observers anymore. We are initiating cancelation. - if (source) { - // First, remove this SharedRequestBase from the source. - source->notify(this, observers, nullptr, FileCache::Hint::No); - } - - // Then, initiate cancelation of this request - cancel(); - } - } - - bool abandoned() const { - return observers.empty(); - } - - std::vector<Request *> removeAllInEnvironment(const Environment &env) { - MBGL_VERIFY_THREAD(tid); - - std::vector<Request *> result; - - // Removes all Requests in the supplied environment and returns a list - // of them. - util::erase_if(observers, [&](Request *req) -> bool { - if (&req->env == &env) { - result.push_back(req); - return true; - } else { - return false; - } - }); - - return result; - } - -protected: - virtual ~SharedRequestBase() { - MBGL_VERIFY_THREAD(tid); - } - -public: - const Resource resource; - -protected: - DefaultFileSource::Impl *source = nullptr; - -private: - std::set<Request *> observers; -}; - -} - -#endif diff --git a/include/mbgl/storage/default/thread_context.hpp b/include/mbgl/storage/default/thread_context.hpp deleted file mode 100644 index 763c83a25b..0000000000 --- a/include/mbgl/storage/default/thread_context.hpp +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef MBGL_STORAGE_DEFAULT_THREAD_CONTEXT -#define MBGL_STORAGE_DEFAULT_THREAD_CONTEXT - -#include <mbgl/util/noncopyable.hpp> -#include <mbgl/util/std.hpp> -#include <mbgl/util/util.hpp> -#include <mbgl/util/uv.hpp> - -#include <uv.h> -#include <pthread.h> - -#include <map> -#include <cassert> - -namespace mbgl { - -// This is a template class that provides a per-thread and per-loop Context object. It can be used -// by implementations to store global state. - -template <typename Context> -class ThreadContext : private util::noncopyable { -protected: - MBGL_STORE_THREAD(tid) - using Map = std::map<uv_loop_t *, std::unique_ptr<Context>>; - -public: - static Context *Get(uv_loop_t *loop); - -private: - static pthread_key_t key; - static pthread_once_t once; - -public: - ThreadContext(uv_loop_t *loop); - ~ThreadContext(); - -public: - uv_loop_t *loop; -}; - -template <typename Context> -Context *ThreadContext<Context>::Get(uv_loop_t *loop) { - pthread_once(&once, []() { - pthread_key_create(&key, [](void *ptr) { - assert(ptr); - delete reinterpret_cast<Map *>(ptr); - }); - }); - auto contexts = reinterpret_cast<Map *>(pthread_getspecific(key)); - if (!contexts) { - contexts = new Map(); - pthread_setspecific(key, contexts); - } - - // Now find a ThreadContext that matches the requested loop. - auto it = contexts->find(loop); - if (it == contexts->end()) { - auto result = contexts->emplace(loop, util::make_unique<Context>(loop)); - assert(result.second); // Make sure it was actually inserted. - return result.first->second.get(); - } else { - return it->second.get(); - } -} - -template <typename Context> -ThreadContext<Context>::ThreadContext(uv_loop_t *loop_) : loop(loop_) { -} - -template <typename Context> -ThreadContext<Context>::~ThreadContext() { - MBGL_VERIFY_THREAD(tid); -} - - -} - -#endif diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index 372dee909b..f393747168 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -3,10 +3,13 @@ #include <mbgl/storage/file_source.hpp> #include <mbgl/storage/file_cache.hpp> -#include <mbgl/util/run_loop.hpp> namespace mbgl { +namespace util { +template <typename T> class Thread; +} + class DefaultFileSource : public FileSource { public: DefaultFileSource(FileCache *cache, const std::string &root = ""); diff --git a/include/mbgl/storage/default/sqlite_cache.hpp b/include/mbgl/storage/sqlite_cache.hpp index db672b222b..db672b222b 100644 --- a/include/mbgl/storage/default/sqlite_cache.hpp +++ b/include/mbgl/storage/sqlite_cache.hpp diff --git a/include/mbgl/util/run_loop.hpp b/include/mbgl/util/run_loop.hpp deleted file mode 100644 index 9bed417c38..0000000000 --- a/include/mbgl/util/run_loop.hpp +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef MBGL_UTIL_RUN_LOOP -#define MBGL_UTIL_RUN_LOOP - -#include <memory> -#include <mutex> -#include <functional> -#include <queue> - -namespace uv { -class async; -class loop; -} - -namespace mbgl { -namespace util { - -template <typename T> class Thread; - -class RunLoop { - friend Thread<RunLoop>; - -protected: - // These are called by the Thread<> wrapper. - RunLoop(); - ~RunLoop(); - - // Called by the Thread<> wrapper to start the loop. When you implement this - // method in a child class, you *must* call this function as the last action. - void start(); - -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); - -private: - const std::unique_ptr<uv::loop> runloop; - const std::unique_ptr<uv::async> runloopAsync; - std::mutex runloopMutex; - std::queue<std::function<void()>> runloopQueue; -}; - -} -} - -#endif diff --git a/include/mbgl/util/thread.hpp b/include/mbgl/util/thread.hpp deleted file mode 100644 index 7c9ba70e11..0000000000 --- a/include/mbgl/util/thread.hpp +++ /dev/null @@ -1,87 +0,0 @@ -#ifndef MBGL_UTIL_THREAD -#define MBGL_UTIL_THREAD - -#include <future> -#include <thread> - -namespace { - -template <::std::size_t...> -struct index_sequence {}; - -template <::std::size_t N, ::std::size_t... I> -struct integer_sequence : integer_sequence<N - 1, N - 1, I...> {}; - -template <::std::size_t... I> -struct integer_sequence<0, I...> { - using type = index_sequence<I...>; -}; - -} - -namespace mbgl { -namespace util { - -// Manages a thread with Object. - -// Upon creation of this object, it launches a thread, creates an object of type Object in that -// thread, and then calls .start(); on that object. When the Thread<> object is destructed, the -// Object's .stop() function is called, and the destructor waits for thread termination. The -// Thread<> constructor blocks until the thread and the Object are fully created, so after the -// object creation, it's safe to obtain the Object stored in this thread. - -template <class Object> -class Thread { -public: - template <class... Args> - Thread(Args&&... args); - Thread(const Thread&) = delete; - Thread(Thread&&) = delete; - Thread& operator=(const Thread&) = delete; - Thread& operator=(Thread&&) = delete; - ~Thread(); - - inline Object* operator->() const { return &object; } - inline operator Object*() const { return &object; } - -private: - template <typename P, std::size_t... I> - void run(std::promise<Object&>& promise, P&& params, index_sequence<I...>) { - Object context(std::get<I>(std::forward<P>(params))...); - promise.set_value(context); - context.start(); - joinable.get_future().get(); - } - -private: - std::thread thread; - std::promise<void> joinable; - Object& object; -}; - -template <class Object> -template <class... Args> -Thread<Object>::Thread(Args&&... args) - : object([&](std::tuple<Args...>&& params) -> Object& { - // Note: We're using std::tuple<> to store the arguments because GCC 4.9 has a bug - // when expanding parameters packs captured in lambdas. - std::promise<Object&> promise; - constexpr auto seq = typename integer_sequence<sizeof...(Args)>::type(); - thread = std::thread([&] { - run(promise, std::move(params), seq); - }); - return promise.get_future().get(); - }(std::move(std::forward_as_tuple(::std::forward<Args>(args)...)))) { -} - -template <class Object> -Thread<Object>::~Thread() { - object.stop(); - joinable.set_value(); - thread.join(); -} - -} -} - -#endif |