From 727358628772e928812bb8ba398bb4b8b37a2153 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 5 Jul 2016 12:44:45 +0200 Subject: [PATCH] added handle Async --- src/uvw.hpp | 1 + src/uvw/async.hpp | 37 +++++++++++++++++++++++++++++++++++++ src/uvw/event.hpp | 1 + src/uvw/handle.hpp | 7 ++++--- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/uvw/async.hpp diff --git a/src/uvw.hpp b/src/uvw.hpp index d1b61699..b932f132 100644 --- a/src/uvw.hpp +++ b/src/uvw.hpp @@ -1,3 +1,4 @@ +#include "uvw/async.hpp" #include "uvw/check.hpp" #include "uvw/emitter.hpp" #include "uvw/event.hpp" diff --git a/src/uvw/async.hpp b/src/uvw/async.hpp new file mode 100644 index 00000000..23f4a5e6 --- /dev/null +++ b/src/uvw/async.hpp @@ -0,0 +1,37 @@ +#pragma once + + +#include +#include +#include +#include "event.hpp" +#include "handle.hpp" +#include "util.hpp" + + +namespace uvw { + + +class Async final: public Handle { + static void sendCallback(uv_async_t *handle) { + Async &async = *(static_cast(handle->data)); + async.publish(AsyncEvent{}); + } + + explicit Async(std::shared_ptr ref) + : Handle{HandleType{}, std::move(ref)} + { } + +public: + template + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new Async{std::forward(args)...}}; + } + + bool init() { return initialize(&uv_async_init, sendCallback); } + + void send() { invoke(&uv_async_send, get()); } +}; + + +} diff --git a/src/uvw/event.hpp b/src/uvw/event.hpp index 91c288d2..fb11693d 100644 --- a/src/uvw/event.hpp +++ b/src/uvw/event.hpp @@ -30,6 +30,7 @@ struct Event: BaseEvent { }; +struct AsyncEvent: Event { }; struct CheckEvent: Event { }; struct CloseEvent: Event { }; struct ConnectEvent: Event { }; diff --git a/src/uvw/handle.hpp b/src/uvw/handle.hpp index 5769bbba..c5f4a6c6 100644 --- a/src/uvw/handle.hpp +++ b/src/uvw/handle.hpp @@ -14,6 +14,7 @@ namespace uvw { template struct HandleType; +template<> struct HandleType { }; template<> struct HandleType { }; template<> struct HandleType { }; template<> struct HandleType { }; @@ -66,12 +67,12 @@ protected: template U* get() const noexcept { return reinterpret_cast(wrapper->get()); } - template - bool initialize(F &&f) { + template + bool initialize(F &&f, Args&&... args) { bool ret = true; if(!initialized) { - auto err = std::forward(f)(parent(), get()); + auto err = std::forward(f)(parent(), get(), std::forward(args)...); if(err) { this->publish(ErrorEvent{err});