added handle Async

This commit is contained in:
Michele Caini 2016-07-05 12:44:45 +02:00
parent 38e8e162c5
commit 7273586287
4 changed files with 43 additions and 3 deletions

View File

@ -1,3 +1,4 @@
#include "uvw/async.hpp"
#include "uvw/check.hpp"
#include "uvw/emitter.hpp"
#include "uvw/event.hpp"

37
src/uvw/async.hpp Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include <utility>
#include <memory>
#include <uv.h>
#include "event.hpp"
#include "handle.hpp"
#include "util.hpp"
namespace uvw {
class Async final: public Handle<Async> {
static void sendCallback(uv_async_t *handle) {
Async &async = *(static_cast<Async*>(handle->data));
async.publish(AsyncEvent{});
}
explicit Async(std::shared_ptr<Loop> ref)
: Handle{HandleType<uv_async_t>{}, std::move(ref)}
{ }
public:
template<typename... Args>
static std::shared_ptr<Async> create(Args&&... args) {
return std::shared_ptr<Async>{new Async{std::forward<Args>(args)...}};
}
bool init() { return initialize<uv_async_t>(&uv_async_init, sendCallback); }
void send() { invoke(&uv_async_send, get<uv_async_t>()); }
};
}

View File

@ -30,6 +30,7 @@ struct Event: BaseEvent {
};
struct AsyncEvent: Event<AsyncEvent> { };
struct CheckEvent: Event<CheckEvent> { };
struct CloseEvent: Event<CloseEvent> { };
struct ConnectEvent: Event<ConnectEvent> { };

View File

@ -14,6 +14,7 @@ namespace uvw {
template<typename>
struct HandleType;
template<> struct HandleType<uv_async_t> { };
template<> struct HandleType<uv_check_t> { };
template<> struct HandleType<uv_idle_t> { };
template<> struct HandleType<uv_prepare_t> { };
@ -66,12 +67,12 @@ protected:
template<typename U>
U* get() const noexcept { return reinterpret_cast<U*>(wrapper->get()); }
template<typename U, typename F>
bool initialize(F &&f) {
template<typename U, typename F, typename... Args>
bool initialize(F &&f, Args&&... args) {
bool ret = true;
if(!initialized) {
auto err = std::forward<F>(f)(parent(), get<U>());
auto err = std::forward<F>(f)(parent(), get<U>(), std::forward<Args>(args)...);
if(err) {
this->publish(ErrorEvent{err});