diff options
Diffstat (limited to 'include/mbgl/util')
-rw-r--r-- | include/mbgl/util/compression.hpp | 15 | ||||
-rw-r--r-- | include/mbgl/util/filesource.hpp | 43 | ||||
-rw-r--r-- | include/mbgl/util/interpolate.hpp | 2 | ||||
-rw-r--r-- | include/mbgl/util/parsedate.h | 38 | ||||
-rw-r--r-- | include/mbgl/util/ptr.hpp | 29 | ||||
-rw-r--r-- | include/mbgl/util/queue.h | 92 | ||||
-rw-r--r-- | include/mbgl/util/raster.hpp | 8 | ||||
-rw-r--r-- | include/mbgl/util/sqlite3.hpp | 74 | ||||
-rw-r--r-- | include/mbgl/util/uv-channel.h | 29 | ||||
-rw-r--r-- | include/mbgl/util/uv-messenger.h | 32 | ||||
-rw-r--r-- | include/mbgl/util/uv-worker.h | 42 | ||||
-rw-r--r-- | include/mbgl/util/uv.hpp | 7 | ||||
-rw-r--r-- | include/mbgl/util/uv_detail.hpp | 52 |
13 files changed, 393 insertions, 70 deletions
diff --git a/include/mbgl/util/compression.hpp b/include/mbgl/util/compression.hpp new file mode 100644 index 0000000000..a33b2476a7 --- /dev/null +++ b/include/mbgl/util/compression.hpp @@ -0,0 +1,15 @@ +#ifndef MBGL_UTIL_COMPRESSION +#define MBGL_UTIL_COMPRESSION + +#include <string> + +namespace mbgl { +namespace util { + +std::string compress(const std::string &raw); +std::string decompress(const std::string &raw); + +} +} + +#endif diff --git a/include/mbgl/util/filesource.hpp b/include/mbgl/util/filesource.hpp deleted file mode 100644 index 18c63ddfeb..0000000000 --- a/include/mbgl/util/filesource.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef MBGL_UTIL_FILESOURCE -#define MBGL_UTIL_FILESOURCE - -#include <mbgl/util/uv.hpp> - -#include <string> -#include <memory> -#include <functional> - -namespace mbgl { - -namespace platform { -struct Response; -} - -enum class ResourceType : uint8_t { - Unknown, - Tile, - Glyphs, - Image, - JSON -}; - -class FileSource { -public: - FileSource(); - - void setBase(const std::string &value); - const std::string &getBase() const; - - void load(ResourceType type, const std::string &url, std::function<void(platform::Response *)> callback, const std::shared_ptr<uv::loop> loop = nullptr); - -private: - // Stores a URL that is used as a base for loading resources with relative path. - std::string base; - - // Stores the absolute path to the cache directory. - const std::string cache; -}; - -} - -#endif diff --git a/include/mbgl/util/interpolate.hpp b/include/mbgl/util/interpolate.hpp index e8c3389350..c9232db4eb 100644 --- a/include/mbgl/util/interpolate.hpp +++ b/include/mbgl/util/interpolate.hpp @@ -1,6 +1,8 @@ #ifndef MBGL_UTIL_INTERPOLATE #define MBGL_UTIL_INTERPOLATE +#include <array> + namespace mbgl { namespace util { diff --git a/include/mbgl/util/parsedate.h b/include/mbgl/util/parsedate.h new file mode 100644 index 0000000000..6905e361d4 --- /dev/null +++ b/include/mbgl/util/parsedate.h @@ -0,0 +1,38 @@ +#ifndef HEADER_PARSEDATE_H +#define HEADER_PARSEDATE_H +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ***************************************************************************/ + + +#ifdef __cplusplus +extern "C" { +#endif + +#include <time.h> + +time_t parse_date(const char *p); + +#ifdef __cplusplus +} +#endif + +#endif /* HEADER_PARSEDATE_H */ diff --git a/include/mbgl/util/ptr.hpp b/include/mbgl/util/ptr.hpp new file mode 100644 index 0000000000..6e02f956f3 --- /dev/null +++ b/include/mbgl/util/ptr.hpp @@ -0,0 +1,29 @@ +#ifndef MBGL_UTIL_PTR +#define MBGL_UTIL_PTR + +#include <memory> +#include <cassert> + +namespace mbgl { +namespace util { + +template <typename T> +class ptr : public ::std::shared_ptr<T> { +public: + template <typename... Args> + inline ptr(Args &&... args) + : ::std::shared_ptr<T>(::std::forward<Args>(args)...) {} + + inline auto operator->() const -> decltype(this->::std::shared_ptr<T>::operator->()) { + assert(*this); + return ::std::shared_ptr<T>::operator->(); + } + inline auto operator*() const -> decltype(this->::std::shared_ptr<T>::operator*()) { + assert(*this); + return ::std::shared_ptr<T>::operator*(); + } +}; +} +} + +#endif
\ No newline at end of file diff --git a/include/mbgl/util/queue.h b/include/mbgl/util/queue.h new file mode 100644 index 0000000000..fe02b454ea --- /dev/null +++ b/include/mbgl/util/queue.h @@ -0,0 +1,92 @@ +/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef QUEUE_H_ +#define QUEUE_H_ + +typedef void *QUEUE[2]; + +/* Private macros. */ +#define QUEUE_NEXT(q) (*(QUEUE **) &((*(q))[0])) +#define QUEUE_PREV(q) (*(QUEUE **) &((*(q))[1])) +#define QUEUE_PREV_NEXT(q) (QUEUE_NEXT(QUEUE_PREV(q))) +#define QUEUE_NEXT_PREV(q) (QUEUE_PREV(QUEUE_NEXT(q))) + +/* Public macros. */ +#define QUEUE_DATA(ptr, type, field) \ + ((type *) ((char *) (ptr) - ((char *) &((type *) 0)->field))) + +#define QUEUE_FOREACH(q, h) \ + for ((q) = QUEUE_NEXT(h); (q) != (h); (q) = QUEUE_NEXT(q)) + +#define QUEUE_EMPTY(q) \ + ((const QUEUE *) (q) == (const QUEUE *) QUEUE_NEXT(q)) + +#define QUEUE_HEAD(q) \ + (QUEUE_NEXT(q)) + +#define QUEUE_INIT(q) \ + do { \ + QUEUE_NEXT(q) = (q); \ + QUEUE_PREV(q) = (q); \ + } \ + while (0) + +#define QUEUE_ADD(h, n) \ + do { \ + QUEUE_PREV_NEXT(h) = QUEUE_NEXT(n); \ + QUEUE_NEXT_PREV(n) = QUEUE_PREV(h); \ + QUEUE_PREV(h) = QUEUE_PREV(n); \ + QUEUE_PREV_NEXT(h) = (h); \ + } \ + while (0) + +#define QUEUE_SPLIT(h, q, n) \ + do { \ + QUEUE_PREV(n) = QUEUE_PREV(h); \ + QUEUE_PREV_NEXT(n) = (n); \ + QUEUE_NEXT(n) = (q); \ + QUEUE_PREV(h) = QUEUE_PREV(q); \ + QUEUE_PREV_NEXT(h) = (h); \ + QUEUE_PREV(q) = (n); \ + } \ + while (0) + +#define QUEUE_INSERT_HEAD(h, q) \ + do { \ + QUEUE_NEXT(q) = QUEUE_NEXT(h); \ + QUEUE_PREV(q) = (h); \ + QUEUE_NEXT_PREV(q) = (q); \ + QUEUE_NEXT(h) = (q); \ + } \ + while (0) + +#define QUEUE_INSERT_TAIL(h, q) \ + do { \ + QUEUE_NEXT(q) = (h); \ + QUEUE_PREV(q) = QUEUE_PREV(h); \ + QUEUE_PREV_NEXT(q) = (q); \ + QUEUE_PREV(h) = (q); \ + } \ + while (0) + +#define QUEUE_REMOVE(q) \ + do { \ + QUEUE_PREV_NEXT(q) = QUEUE_NEXT(q); \ + QUEUE_NEXT_PREV(q) = QUEUE_PREV(q); \ + } \ + while (0) + +#endif /* QUEUE_H_ */ diff --git a/include/mbgl/util/raster.hpp b/include/mbgl/util/raster.hpp index e526ffa5ac..7051c30091 100644 --- a/include/mbgl/util/raster.hpp +++ b/include/mbgl/util/raster.hpp @@ -4,11 +4,11 @@ #include <mbgl/util/transition.hpp> #include <mbgl/util/texturepool.hpp> #include <mbgl/util/image.hpp> +#include <mbgl/util/ptr.hpp> #include <mbgl/renderer/prerendered_texture.hpp> #include <string> #include <mutex> -#include <memory> typedef struct uv_loop_s uv_loop_t; @@ -17,7 +17,7 @@ namespace mbgl { class Raster : public std::enable_shared_from_this<Raster> { public: - Raster(const std::shared_ptr<Texturepool> &texturepool); + Raster(const util::ptr<Texturepool> &texturepool); ~Raster(); // load image data @@ -57,7 +57,7 @@ private: bool loaded = false; // shared texture pool - std::shared_ptr<Texturepool> texturepool; + util::ptr<Texturepool> texturepool; // min/mag filter uint32_t filter = 0; @@ -66,7 +66,7 @@ private: std::unique_ptr<util::Image> img; // fade in transition - std::shared_ptr<util::transition> fade_transition = nullptr; + util::ptr<util::transition> fade_transition = nullptr; }; } diff --git a/include/mbgl/util/sqlite3.hpp b/include/mbgl/util/sqlite3.hpp new file mode 100644 index 0000000000..3e324f7ce1 --- /dev/null +++ b/include/mbgl/util/sqlite3.hpp @@ -0,0 +1,74 @@ +#pragma once + +#include <string> +#include <stdexcept> + +typedef struct sqlite3 sqlite3; +typedef struct sqlite3_stmt sqlite3_stmt; + +namespace mapbox { +namespace sqlite { + +enum OpenFlag : int { + ReadOnly = 0x00000001, + ReadWrite = 0x00000002, + Create = 0x00000004, + NoMutex = 0x00008000, + FullMutex = 0x00010000, + SharedCache = 0x00020000, + PrivateCache = 0x00040000, +}; + +struct Exception : std::runtime_error { + inline Exception(int err, const char *msg) : std::runtime_error(msg), code(err) {} + const int code = 0; +}; + +class Statement; + +class Database { +private: + Database(const Database &) = delete; + Database &operator=(const Database &) = delete; + +public: + Database(const std::string &filename, int flags = 0); + Database(Database &&); + ~Database(); + Database &operator=(Database &&); + + operator bool() const; + + void exec(const std::string &sql); + Statement prepare(const char *query); + +private: + sqlite3 *db = nullptr; +}; + +class Statement { +private: + Statement(const Statement &) = delete; + Statement &operator=(const Statement &) = delete; + +public: + Statement(sqlite3 *db, const char *sql); + Statement(Statement &&); + ~Statement(); + Statement &operator=(Statement &&); + + operator bool() const; + + template <typename T> void bind(int offset, T value); + void bind(int offset, const std::string &value, bool retain = true); + template <typename T> T get(int offset); + + bool run(); + void reset(); + +private: + sqlite3_stmt *stmt = nullptr; +}; + +} +} diff --git a/include/mbgl/util/uv-channel.h b/include/mbgl/util/uv-channel.h new file mode 100644 index 0000000000..ea5c279f65 --- /dev/null +++ b/include/mbgl/util/uv-channel.h @@ -0,0 +1,29 @@ +#ifndef MBGL_UTIL_UV_CHANNEL +#define MBGL_UTIL_UV_CHANNEL + +#ifdef __cplusplus +extern "C" { +#endif + +#include <uv.h> + +// Taken from http://navaneeth.github.io/blog/2013/08/02/channels-in-libuv/ + +typedef struct uv_chan_s uv_chan_t; + +struct uv_chan_s { + uv_mutex_t mutex; + uv_cond_t cond; + void *q[2]; +}; + +int uv_chan_init(uv_chan_t *chan); +void uv_chan_send(uv_chan_t *chan, void *data); +void *uv_chan_receive(uv_chan_t *chan); +void uv_chan_destroy(uv_chan_t *chan); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/mbgl/util/uv-messenger.h b/include/mbgl/util/uv-messenger.h new file mode 100644 index 0000000000..82b8ef2d9c --- /dev/null +++ b/include/mbgl/util/uv-messenger.h @@ -0,0 +1,32 @@ +#ifndef MBGL_UTIL_UV_MESSENGER +#define MBGL_UTIL_UV_MESSENGER + +#ifdef __cplusplus +extern "C" { +#endif + +#include <uv.h> + +typedef struct uv_messenger_s uv_messenger_t; +typedef void (*uv_messenger_cb)(void *arg); +typedef void (*uv_messenger_stop_cb)(uv_messenger_t *msgr); + +struct uv_messenger_s { + uv_mutex_t mutex; + uv_async_t async; + uv_messenger_cb callback; + void *data; + void *queue[2]; +}; + +int uv_messenger_init(uv_loop_t *loop, uv_messenger_t *msgr, uv_messenger_cb callback); +void uv_messenger_send(uv_messenger_t *msgr, void *arg); +void uv_messenger_stop(uv_messenger_t *msgr); +void uv_messenger_ref(uv_messenger_t *msgr); +void uv_messenger_unref(uv_messenger_t *msgr); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/mbgl/util/uv-worker.h b/include/mbgl/util/uv-worker.h new file mode 100644 index 0000000000..451b6fb04d --- /dev/null +++ b/include/mbgl/util/uv-worker.h @@ -0,0 +1,42 @@ +#ifndef MBGL_UTIL_UV_WORKER +#define MBGL_UTIL_UV_WORKER + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct uv_messenger_s uv_messenger_t; + +#include <mbgl/util/uv-channel.h> + +#include <stdlib.h> + +typedef struct uv_worker_s uv_worker_t; + +typedef void (*uv_worker_cb)(void *data); +typedef void (*uv_worker_after_cb)(void *data); +typedef void (*uv_worker_close_cb)(uv_worker_t *worker); + +struct uv_worker_s { +#ifndef NDEBUG + unsigned long thread_id; +#endif + uv_loop_t *loop; + uv_messenger_t *msgr; + uv_chan_t chan; + const char *name; + int count; + uv_worker_close_cb close_cb; + unsigned int active_items; +}; + +int uv_worker_init(uv_worker_t *worker, uv_loop_t *loop, int count, const char *name); +void uv_worker_send(uv_worker_t *worker, void *data, uv_worker_cb work_cb, + uv_worker_after_cb after_work_cb); +void uv_worker_close(uv_worker_t *worker, uv_worker_close_cb close_cb); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/mbgl/util/uv.hpp b/include/mbgl/util/uv.hpp index 4ceb8b873f..3c533cfbf8 100644 --- a/include/mbgl/util/uv.hpp +++ b/include/mbgl/util/uv.hpp @@ -4,6 +4,7 @@ #include <string> typedef struct uv_async_s uv_async_t; +typedef struct uv_timer_s uv_timer_t; typedef struct uv_handle_s uv_handle_t; typedef struct uv_loop_s uv_loop_t; @@ -11,9 +12,15 @@ namespace uv { std::string cwd(); +struct deleter { + void operator()(uv_async_t *async); + void operator()(uv_timer_t *timer); +}; + class thread; class rwlock; class loop; +class worker; } diff --git a/include/mbgl/util/uv_detail.hpp b/include/mbgl/util/uv_detail.hpp index b3fdbb3719..a80423f822 100644 --- a/include/mbgl/util/uv_detail.hpp +++ b/include/mbgl/util/uv_detail.hpp @@ -1,21 +1,13 @@ #ifndef MBGL_UTIL_UV_DETAIL #define MBGL_UTIL_UV_DETAIL +#include <mbgl/util/ptr.hpp> +#include <mbgl/util/uv-worker.h> + #include <uv.h> + #include <functional> #include <cassert> - -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunused-parameter" -#endif - -#include <boost/lockfree/queue.hpp> - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - #include <string> @@ -112,6 +104,24 @@ private: uv_once_t o = UV_ONCE_INIT; }; +class worker { +public: + inline worker(uv_loop_t *loop, unsigned int count, const char *name = nullptr) : w(new uv_worker_t) { + uv_worker_init(w, loop, count, name); + } + inline ~worker() { + uv_worker_close(w, [](uv_worker_t *worker) { + delete worker; + }); + } + inline void add(void *data, uv_worker_cb work_cb, uv_worker_after_cb after_work_cb) { + uv_worker_send(w, data, work_cb, after_work_cb); + } + +private: + uv_worker_t *w; +}; + template <typename T> class work { public: @@ -119,30 +129,26 @@ public: typedef void (*after_work_callback)(T &object); template<typename... Args> - work(const std::shared_ptr<loop> &loop, work_callback work_cb, after_work_callback after_work_cb, Args&&... args) - : loop(loop), - data(std::forward<Args>(args)...), + work(worker &worker, work_callback work_cb, after_work_callback after_work_cb, Args&&... args) + : data(std::forward<Args>(args)...), work_cb(work_cb), after_work_cb(after_work_cb) { - req.data = this; - uv_queue_work(**loop, &req, do_work, after_work); + worker.add(this, do_work, after_work); } private: - static void do_work(uv_work_t *req) { - work<T> *w = static_cast<work<T> *>(req->data); + static void do_work(void *data) { + work<T> *w = reinterpret_cast<work<T> *>(data); w->work_cb(w->data); } - static void after_work(uv_work_t *req, int) { - work<T> *w = static_cast<work<T> *>(req->data); + static void after_work(void *data) { + work<T> *w = reinterpret_cast<work<T> *>(data); w->after_work_cb(w->data); delete w; } private: - std::shared_ptr<uv::loop> loop; - uv_work_t req; T data; work_callback work_cb; after_work_callback after_work_cb; |