From 0bb792262ef50a1fa450fcf6ce41615bd5b50282 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 4 Jul 2016 10:19:18 +0200 Subject: [PATCH] clean up + refactoring --- src/uvw/check.hpp | 23 +++++-------------- src/uvw/handle.hpp | 55 +++++++++------------------------------------ src/uvw/idle.hpp | 20 +++++------------ src/uvw/loop.hpp | 2 +- src/uvw/prepare.hpp | 20 +++++------------ src/uvw/stream.hpp | 50 +++++++++++++++-------------------------- src/uvw/tcp.hpp | 31 +++++++++---------------- src/uvw/timer.hpp | 35 +++++++---------------------- 8 files changed, 62 insertions(+), 174 deletions(-) diff --git a/src/uvw/check.hpp b/src/uvw/check.hpp index a2df6bc3..3bf73f74 100644 --- a/src/uvw/check.hpp +++ b/src/uvw/check.hpp @@ -13,7 +13,8 @@ namespace uvw { class Check final: public Handle { - static void startCallback(Check &check, uv_check_t *) { + static void startCallback(uv_check_t *handle) { + Check &check = *(static_cast(handle->data)); check.publish(CheckEvent{}); } @@ -25,24 +26,10 @@ public: return std::shared_ptr{new Check{std::forward(args)...}}; } - bool init() { - return Handle::init(&uv_check_init); - } + bool init() { return initialize(&uv_check_init); } - void start() { - using CBF = CallbackFactory; - auto func = &CBF::template proto<&Check::startCallback>; - auto err = uv_check_start(get(), func); - if(err) publish(ErrorEvent{err}); - } - - void stop() { - auto err = uv_check_stop(get()); - if(err) publish(ErrorEvent{err}); - } - -private: - bool initialized = false; + void start() { invoke(&uv_check_start, get(), &startCallback); } + void stop() { invoke(&uv_check_stop, get()); } }; diff --git a/src/uvw/handle.hpp b/src/uvw/handle.hpp index c2469208..bc2d04f8 100644 --- a/src/uvw/handle.hpp +++ b/src/uvw/handle.hpp @@ -12,26 +12,6 @@ namespace uvw { -namespace details { - - -template void* get(T *handle) { return handle->data; } -void* get(uv_connect_t *conn) { return conn->handle->data; } - - -template -struct UVCallbackFactory; - -template -struct UVCallbackFactory { - template - static void proto(H, Args...) noexcept; -}; - - -} - - template struct HandleType; @@ -44,9 +24,6 @@ template<> struct HandleType { }; template class Handle: public Emitter, public Self { - template - friend struct details::UVCallbackFactory; - struct BaseWrapper { virtual ~BaseWrapper() = default; virtual void * get() const noexcept = 0; @@ -60,15 +37,13 @@ class Handle: public Emitter, public Self { std::unique_ptr handle; }; - static void closeCallback(T &ref, uv_handle_t *) { + static void closeCallback(uv_handle_t *handle) { + T &ref = *(static_cast(handle->data)); ref.publish(CloseEvent{}); ref.reset(); } protected: - template - using CallbackFactory = details::UVCallbackFactory; - template explicit Handle(HandleType, std::shared_ptr ref) : Emitter{}, Self{}, @@ -84,7 +59,7 @@ protected: U* get() const noexcept { return reinterpret_cast(wrapper->get()); } template - bool init(F &&f) { + bool initialize(F &&f) { bool ret = true; if(!active()) { @@ -101,6 +76,12 @@ protected: return ret; } + template + void invoke(F &&f, Args&&... args) { + auto err = std::forward(f)(std::forward(args)...); + if(err) { Emitter::publish(ErrorEvent{err}); } + } + public: virtual ~Handle() { static_assert(std::is_base_of, T>::value, "!"); @@ -117,9 +98,7 @@ public: void close() noexcept { if(!closing()) { - using CBF = CallbackFactory; - auto func = &CBF::template proto<&Handle::closeCallback>; - uv_close(get(), func); + uv_close(get(), &Handle::closeCallback); } } @@ -129,18 +108,4 @@ private: }; -namespace details { - - -template -template -void UVCallbackFactory::proto(H handle, Args... args) noexcept { - T &ref = *(static_cast(details::get(handle))); - F(ref, handle, args...); -} - - -} - - } diff --git a/src/uvw/idle.hpp b/src/uvw/idle.hpp index 3c1c5aba..e742bec7 100644 --- a/src/uvw/idle.hpp +++ b/src/uvw/idle.hpp @@ -13,7 +13,8 @@ namespace uvw { class Idle final: public Handle { - static void startCallback(Idle &idle, uv_idle_t *) { + static void startCallback(uv_idle_t *handle) { + Idle &idle = *(static_cast(handle->data)); idle.publish(IdleEvent{}); } @@ -25,21 +26,10 @@ public: return std::shared_ptr{new Idle{std::forward(args)...}}; } - bool init() { - return Handle::init(&uv_idle_init); - } + bool init() { return initialize(&uv_idle_init); } - void start() { - using CBF = CallbackFactory; - auto func = &CBF::template proto<&Idle::startCallback>; - auto err = uv_idle_start(get(), func); - if(err) publish(ErrorEvent{err}); - } - - void stop() { - auto err = uv_idle_stop(get()); - if(err) publish(ErrorEvent{err}); - } + void start() { invoke(&uv_idle_start, get(), &startCallback); } + void stop() { invoke(&uv_idle_stop, get()); } }; diff --git a/src/uvw/loop.hpp b/src/uvw/loop.hpp index 43d6e40b..5adb777f 100644 --- a/src/uvw/loop.hpp +++ b/src/uvw/loop.hpp @@ -73,7 +73,7 @@ public: void close() noexcept { auto err = uv_loop_close(loop.get()); - if(err) publish(ErrorEvent{err}); + if(err) { publish(ErrorEvent{err}); } } bool run() noexcept { diff --git a/src/uvw/prepare.hpp b/src/uvw/prepare.hpp index 8929c285..16c2da11 100644 --- a/src/uvw/prepare.hpp +++ b/src/uvw/prepare.hpp @@ -13,7 +13,8 @@ namespace uvw { class Prepare final: public Handle { - static void startCallback(Prepare &prepare, uv_prepare_t *) { + static void startCallback(uv_prepare_t *handle) { + Prepare &prepare = *(static_cast(handle->data)); prepare.publish(PrepareEvent{}); } @@ -25,21 +26,10 @@ public: return std::shared_ptr{new Prepare{std::forward(args)...}}; } - bool init() { - return Handle::init(&uv_prepare_init); - } + bool init() { return initialize(&uv_prepare_init); } - void start() { - using CBF = CallbackFactory; - auto func = &CBF::template proto<&Prepare::startCallback>; - auto err = uv_prepare_start(get(), func); - if(err) publish(ErrorEvent{err}); - } - - void stop() { - auto err = uv_prepare_stop(get()); - if(err) publish(ErrorEvent{err}); - } + void start() { invoke(&uv_prepare_start, get(), &startCallback); } + void stop() { invoke(&uv_prepare_stop, get()); } }; diff --git a/src/uvw/stream.hpp b/src/uvw/stream.hpp index 8b54eb7a..2dd15f5e 100644 --- a/src/uvw/stream.hpp +++ b/src/uvw/stream.hpp @@ -18,12 +18,13 @@ template class Stream: public Handle { static constexpr unsigned int DEFAULT_BACKLOG = 128; - static void allocCallback(T &, uv_handle_t *, std::size_t suggested, uv_buf_t *buf) { + static void allocCallback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) { buf->base = new char[suggested]; buf->len = suggested; } - static void readCallback(T &ref, uv_stream_t *, ssize_t nread, const uv_buf_t *cbuf) { + static void readCallback(uv_stream_t *handle, ssize_t nread, const uv_buf_t *cbuf) { + T &ref = *(static_cast(handle->data)); uv_buf_t *buf = const_cast(cbuf); if(nread == UV_EOF) { @@ -43,22 +44,21 @@ class Stream: public Handle { buf->len = 0; } - static void writeCallback(T &ref, uv_write_t *req, int status) { - if(status) { - ref.publish(ErrorEvent{status}); - } else { - ref.publish(WriteEvent{}); - } - + static void writeCallback(uv_write_t *req, int status) { + T &ref = *(static_cast(req->handle->data)); + if(status) { ref.publish(ErrorEvent{status}); } + else { ref.publish(WriteEvent{}); } delete req; } - static void shutdownCallback(T &ref, uv_shutdown_t *, int status) { - if(status) ref.publish(ErrorEvent{status}); - else ref.publish(ShutdownEvent{}); + static void shutdownCallback(uv_shutdown_t *req, int status) { + T &ref = *(static_cast(req->handle->data)); + if(status) { ref.publish(ErrorEvent{status}); } + else { ref.publish(ShutdownEvent{}); } } - static void listenCallback(T &ref, uv_stream_t *, int status) { + static void listenCallback(uv_stream_t *handle, int status) { + T &ref = *(static_cast(handle->data)); if(status) ref.publish(ErrorEvent{status}); else ref.publish(ListenEvent{}); } @@ -71,17 +71,11 @@ protected: public: void shutdown() noexcept { - using CBF = typename Handle::template CallbackFactory; - auto func = &CBF::template proto<&Stream::shutdownCallback>; - auto err = uv_shutdown(sdown.get(), this->template get(), func); - if(err) this->publish(ErrorEvent{err}); + this->invoke(&uv_shutdown, sdown.get(), this->template get(), &shutdownCallback); } void listen(int backlog) noexcept { - using CBF = typename Handle::template CallbackFactory; - auto func = &CBF::template proto<&Stream::listenCallback>; - auto err = uv_listen(this->template get(), backlog, func); - if(err) this->publish(ErrorEvent{err}); + this->invoke(&uv_listen, this->template get(), backlog, &listenCallback); } void listen() noexcept { @@ -89,25 +83,17 @@ public: } void read() { - using CBFAlloc = typename Handle::template CallbackFactory; - using CBFRead = typename Handle::template CallbackFactory; - auto allocFunc = &CBFAlloc::template proto<&Stream::allocCallback>; - auto readFunc = &CBFRead::template proto<&Stream::readCallback>; - auto err = uv_read_start(this->template get(), allocFunc, readFunc); - if(err) this->publish(ErrorEvent{err}); + this->invoke(&uv_read_start, this->template get(), &allocCallback, &readCallback); } void stop() noexcept { - auto err = uv_read_stop(this->template get()); - if(err) this->publish(ErrorEvent{err}); + this->invoke(&uv_read_stop, this->template get()); } void write(Buffer buf) { - using CBF = typename Handle::template CallbackFactory; - auto func = &CBF::template proto<&Stream::writeCallback>; uv_buf_t data[] = { buf.uvBuf() }; uv_write_t *req = new uv_write_t; - auto err = uv_write(req, this->template get(), data, 1, func); + auto err = uv_write(req, this->template get(), data, 1, &Stream::writeCallback); if(err) { delete req; this->publish(ErrorEvent{err}); diff --git a/src/uvw/tcp.hpp b/src/uvw/tcp.hpp index 2009e2e3..a059c67e 100644 --- a/src/uvw/tcp.hpp +++ b/src/uvw/tcp.hpp @@ -15,9 +15,10 @@ namespace uvw { class Tcp final: public Stream { - static void connectCallback(Tcp &tcp, uv_connect_t *, int status) { - if(status) tcp.publish(ErrorEvent{status}); - else tcp.publish(ConnectEvent{}); + static void connectCallback(uv_connect_t *req, int status) { + Tcp &tcp = *(static_cast(req->handle->data)); + if(status) { tcp.publish(ErrorEvent{status}); } + else { tcp.publish(ConnectEvent{}); } } explicit Tcp(std::shared_ptr ref) @@ -61,18 +62,14 @@ public: return std::shared_ptr{new Tcp{std::forward(args)...}}; } - bool init() { - return Stream::init(&uv_tcp_init); - } + bool init() { return initialize(&uv_tcp_init); } void noDelay(bool value = false) noexcept { - auto err = uv_tcp_nodelay(get(), value ? 1 : 0); - if(err) publish(ErrorEvent{err}); + invoke(&uv_tcp_nodelay, get(), value ? 1 : 0); } void keepAlive(bool enable = false, Time time = Time{0}) noexcept { - auto err = uv_tcp_keepalive(get(), enable ? 1 : 0, time.count()); - if(err) publish(ErrorEvent{err}); + invoke(&uv_tcp_keepalive, get(), enable ? 1 : 0, time.count()); } template> @@ -80,8 +77,7 @@ public: typename Traits::Type addr; Traits::AddrFunc(ip.c_str(), port, &addr); unsigned int flags = ipv6only ? UV_TCP_IPV6ONLY : 0; - auto err = uv_tcp_bind(get(), reinterpret_cast(&addr), flags); - if(err) publish(ErrorEvent{err}); + invoke(&uv_tcp_bind, get(), reinterpret_cast(&addr), flags); } template> @@ -103,10 +99,7 @@ public: void connect(std::string ip, unsigned int port) noexcept { typename Traits::Type addr; Traits::AddrFunc(ip.c_str(), port, &addr); - using CBF = CallbackFactory; - auto func = &CBF::proto<&Tcp::connectCallback>; - auto err = uv_tcp_connect(conn.get(), get(), reinterpret_cast(&addr), func); - if(err) publish(ErrorEvent{err}); + invoke(&uv_tcp_connect, conn.get(), get(), reinterpret_cast(&addr), &connectCallback); } template> @@ -115,15 +108,11 @@ public: } void accept(Tcp &tcp) noexcept { - auto err = uv_accept(get(), tcp.get()); - if(err) publish(ErrorEvent{err}); + invoke(&uv_accept, get(), tcp.get()); } - explicit operator bool() const noexcept { return initialized; } - private: std::unique_ptr conn; - bool initialized; }; diff --git a/src/uvw/timer.hpp b/src/uvw/timer.hpp index 6c41a41e..538adff4 100644 --- a/src/uvw/timer.hpp +++ b/src/uvw/timer.hpp @@ -14,7 +14,8 @@ namespace uvw { class Timer final: public Handle { - static void startCallback(Timer &timer, uv_timer_t *) { + static void startCallback(uv_timer_t *handle) { + Timer &timer = *(static_cast(handle->data)); timer.publish(TimerEvent{}); } @@ -28,34 +29,14 @@ public: return std::shared_ptr{new Timer{std::forward(args)...}}; } - bool init() { - return Handle::init(&uv_timer_init); - } + bool init() { return initialize(&uv_timer_init); } - void start(Time timeout, Time repeat) { - using CBF = CallbackFactory; - auto func = &CBF::template proto<&Timer::startCallback>; - auto err = uv_timer_start(get(), func, timeout.count(), repeat.count()); - if(err) publish(ErrorEvent{err}); - } + void start(Time timeout, Time repeat) { invoke(uv_timer_start, get(), &startCallback, timeout.count(), repeat.count()); } + void stop() { invoke(&uv_timer_stop, get()); } + void again() { invoke(&uv_timer_again, get()); } - void stop() { - auto err = uv_timer_stop(get()); - if(err) publish(ErrorEvent{err}); - } - - void again() { - auto err = uv_timer_again(get()); - if(err) publish(ErrorEvent{err}); - } - - void repeat(Time repeat) { - uv_timer_set_repeat(get(), repeat.count()); - } - - Time repeat() { - return Time{uv_timer_get_repeat(get())}; - } + void repeat(Time repeat) { uv_timer_set_repeat(get(), repeat.count()); } + Time repeat() { return Time{uv_timer_get_repeat(get())}; } };