diff --git a/src/mbgl/util/uv_detail.hpp b/src/mbgl/util/uv_detail.hpp index f77bec0fa..059d7c0dd 100644 --- a/src/mbgl/util/uv_detail.hpp +++ b/src/mbgl/util/uv_detail.hpp @@ -28,13 +28,6 @@ UV_EXTERN void uv_key_set(uv_key_t* key, void* value); namespace uv { -template -void close(std::unique_ptr ptr) { - uv_close(reinterpret_cast(ptr.release()), [](uv_handle_t* handle) { - delete reinterpret_cast(handle); - }); -} - class loop : public mbgl::util::noncopyable { public: inline loop() { @@ -74,40 +67,50 @@ private: uv_loop_t *l = nullptr; }; -class async : public mbgl::util::noncopyable { +template +class handle : public mbgl::util::noncopyable { +public: + inline handle() : t(reinterpret_cast(new T)) { + t->data = this; + } + + inline ~handle() { + uv_close(t.release(), [](uv_handle_t* handle) { + delete reinterpret_cast(handle); + }); + } + + inline void ref() { + uv_ref(t.get()); + } + + inline void unref() { + uv_unref(t.get()); + } + + inline T* get() { + return reinterpret_cast(t.get()); + } + +private: + std::unique_ptr t; +}; + +class async : public handle { public: inline async(uv_loop_t* loop, std::function fn_) - : a(new uv_async_t) - , fn(fn_) - { - a->data = this; - if (uv_async_init(loop, a.get(), async_cb) != 0) { + : fn(fn_) { + if (uv_async_init(loop, get(), async_cb) != 0) { throw std::runtime_error("failed to initialize async"); } } - inline ~async() { - close(std::move(a)); - } - inline void send() { - if (uv_async_send(a.get()) != 0) { + if (uv_async_send(get()) != 0) { throw std::runtime_error("failed to async send"); } } - inline void ref() { - uv_ref(reinterpret_cast(a.get())); - } - - inline void unref() { - uv_unref(reinterpret_cast(a.get())); - } - - inline uv_async_t* get() { - return a.get(); - } - private: #if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 static void async_cb(uv_async_t* a, int) { @@ -117,51 +120,31 @@ private: reinterpret_cast(a->data)->fn(); } - std::unique_ptr a; std::function fn; }; -class timer : public mbgl::util::noncopyable { +class timer : public handle { public: - inline timer(uv_loop_t* loop) - : t(new uv_timer_t) - { - t->data = this; - if (uv_timer_init(loop, t.get()) != 0) { + inline timer(uv_loop_t* loop) { + if (uv_timer_init(loop, get()) != 0) { throw std::runtime_error("failed to initialize timer"); } } - inline timer() { - close(std::move(t)); - } - inline void start(uint64_t timeout, uint64_t repeat, std::function fn_) { fn = fn_; - if (uv_timer_start(t.get(), timer_cb, timeout, repeat) != 0) { + if (uv_timer_start(get(), timer_cb, timeout, repeat) != 0) { throw std::runtime_error("failed to start timer"); } } inline void stop() { fn = nullptr; - if (uv_timer_stop(t.get()) != 0) { + if (uv_timer_stop(get()) != 0) { throw std::runtime_error("failed to stop timer"); } } - inline void ref() { - uv_ref(reinterpret_cast(t.get())); - } - - inline void unref() { - uv_unref(reinterpret_cast(t.get())); - } - - inline uv_timer_t* get() { - return t.get(); - } - private: #if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 static void timer_cb(uv_timer_t* t, int) { @@ -171,7 +154,6 @@ private: reinterpret_cast(t->data)->fn(); } - std::unique_ptr t; std::function fn; };