events: review

This commit is contained in:
Michele Caini 2016-07-26 11:26:22 +02:00
parent 4e87371513
commit d684b0836a
13 changed files with 106 additions and 86 deletions

View File

@ -12,6 +12,9 @@
namespace uvw {
struct AsyncEvent: Event<AsyncEvent> { };
class Async final: public Handle<Async, uv_async_t> {
static void sendCallback(uv_async_t *handle) {
Async &async = *(static_cast<Async*>(handle->data));

View File

@ -12,6 +12,9 @@
namespace uvw {
struct CheckEvent: Event<CheckEvent> { };
class Check final: public Handle<Check, uv_check_t> {
static void startCallback(uv_check_t *handle) {
Check &check = *(static_cast<Check*>(handle->data));

View File

@ -21,6 +21,7 @@ struct BaseEvent {
BaseEvent::~BaseEvent() noexcept { }
template<typename E>
struct Event: BaseEvent {
static std::size_t type() noexcept {
@ -30,29 +31,6 @@ struct Event: BaseEvent {
};
struct AsyncEvent: Event<AsyncEvent> { };
struct CheckEvent: Event<CheckEvent> { };
struct CloseEvent: Event<CloseEvent> { };
struct ConnectEvent: Event<ConnectEvent> { };
struct DataEvent: Event<DataEvent> {
explicit DataEvent(std::unique_ptr<const char[]> ptr, ssize_t l) noexcept
: dt{std::move(ptr)}, len{l}
{ }
const char * data() const noexcept { return dt.get(); }
ssize_t length() const noexcept { return len; }
private:
std::unique_ptr<const char[]> dt;
const ssize_t len;
};
struct EndEvent: Event<EndEvent> { };
struct ErrorEvent: Event<ErrorEvent> {
explicit ErrorEvent(int code = 0) noexcept: ec(code) { }
@ -75,61 +53,4 @@ private:
};
struct FsPollEvent: Event<FsPollEvent> {
explicit FsPollEvent(const Stat &p, const Stat &c) noexcept
: prev(p), curr(c)
{ }
const Stat & previous() const noexcept { return prev; }
const Stat & current() const noexcept { return curr; }
private:
Stat prev;
Stat curr;
};
struct IdleEvent: Event<IdleEvent> { };
struct ListenEvent: Event<ListenEvent> { };
struct PrepareEvent: Event<PrepareEvent> { };
struct SendEvent: Event<SendEvent> { };
struct ShutdownEvent: Event<ShutdownEvent> { };
struct SignalEvent: Event<SignalEvent> {
explicit SignalEvent(int sig) noexcept: signum(sig) { }
int signal() const noexcept { return signum; }
private:
const int signum;
};
struct TimerEvent: Event<TimerEvent> { };
struct UDPDataEvent: Event<UDPDataEvent> {
explicit UDPDataEvent(Addr addr, std::unique_ptr<const char[]> ptr, ssize_t l, bool trunc) noexcept
: dt{std::move(ptr)}, len{l}, sndr{addr}, part{trunc}
{ }
const char * data() const noexcept { return dt.get(); }
ssize_t length() const noexcept { return len; }
Addr sender() const noexcept { return sndr; }
bool partial() const noexcept { return part; }
private:
std::unique_ptr<const char[]> dt;
const ssize_t len;
Addr sndr;
const bool part;
};
struct UninitializedEvent: Event<UninitializedEvent> { };
struct WorkEvent: Event<WorkEvent> { };
struct WriteEvent: Event<WriteEvent> { };
}

View File

@ -13,6 +13,20 @@
namespace uvw {
struct FsPollEvent: Event<FsPollEvent> {
explicit FsPollEvent(const Stat &p, const Stat &c) noexcept
: prev(p), curr(c)
{ }
const Stat & previous() const noexcept { return prev; }
const Stat & current() const noexcept { return curr; }
private:
Stat prev;
Stat curr;
};
class FsPoll final: public Handle<FsPoll, uv_fs_poll_t> {
static void startCallback(uv_fs_poll_t *handle, int status, const uv_stat_t *prev, const uv_stat_t *curr) {
FsPoll &fsPoll = *(static_cast<FsPoll*>(handle->data));

View File

@ -11,6 +11,9 @@
namespace uvw {
struct CloseEvent: Event<CloseEvent> { };
template<typename T, typename U>
class Handle: public BaseHandle, public Resource<T, U>
{

View File

@ -12,6 +12,9 @@
namespace uvw {
struct IdleEvent: Event<IdleEvent> { };
class Idle final: public Handle<Idle, uv_idle_t> {
static void startCallback(uv_idle_t *handle) {
Idle &idle = *(static_cast<Idle*>(handle->data));

View File

@ -13,21 +13,33 @@
namespace uvw {
namespace details {
enum class Event: std::underlying_type_t<uv_poll_event> {
READABLE = UV_READABLE,
WRITABLE = UV_WRITABLE,
DISCONNECT = UV_DISCONNECT
};
}
using PollEvent = FlagsEvent<details::Event>;
class Poll final: public Handle<Poll, uv_poll_t> {
static void startCallback(uv_poll_t *handle, int status, int events) {
Poll &poll = *(static_cast<Poll*>(handle->data));
if(status) { poll.publish(ErrorEvent{status}); }
else { poll.publish(FlagsEvent<Event>{static_cast<std::underlying_type_t<Event>>(events)}); }
else { poll.publish(PollEvent{static_cast<std::underlying_type_t<Event>>(events)}); }
}
using Handle::Handle;
public:
enum class Event: std::underlying_type_t<uv_poll_event> {
READABLE = UV_READABLE,
WRITABLE = UV_WRITABLE,
DISCONNECT = UV_DISCONNECT
};
using Event = details::Event;
template<typename... Args>
static std::shared_ptr<Poll> create(Args&&... args) {

View File

@ -12,6 +12,9 @@
namespace uvw {
struct PrepareEvent: Event<PrepareEvent> { };
class Prepare final: public Handle<Prepare, uv_prepare_t> {
static void startCallback(uv_prepare_t *handle) {
Prepare &prepare = *(static_cast<Prepare*>(handle->data));

View File

@ -12,6 +12,16 @@
namespace uvw {
struct SignalEvent: Event<SignalEvent> {
explicit SignalEvent(int sig) noexcept: signum(sig) { }
int signal() const noexcept { return signum; }
private:
const int signum;
};
class Signal final: public Handle<Signal, uv_signal_t> {
static void startCallback(uv_signal_t *handle, int signum) {
Signal &signal = *(static_cast<Signal*>(handle->data));

View File

@ -15,6 +15,27 @@
namespace uvw {
struct ConnectEvent: Event<ConnectEvent> { };
struct EndEvent: Event<EndEvent> { };
struct ListenEvent: Event<ListenEvent> { };
struct ShutdownEvent: Event<ShutdownEvent> { };
struct WriteEvent: Event<WriteEvent> { };
struct DataEvent: Event<DataEvent> {
explicit DataEvent(std::unique_ptr<const char[]> ptr, ssize_t l) noexcept
: dt{std::move(ptr)}, len{l}
{ }
const char * data() const noexcept { return dt.get(); }
ssize_t length() const noexcept { return len; }
private:
std::unique_ptr<const char[]> dt;
const ssize_t len;
};
namespace details {

View File

@ -13,6 +13,9 @@
namespace uvw {
struct TimerEvent: Event<TimerEvent> { };
class Timer final: public Handle<Timer, uv_timer_t> {
static void startCallback(uv_timer_t *handle) {
Timer &timer = *(static_cast<Timer*>(handle->data));

View File

@ -15,6 +15,27 @@
namespace uvw {
struct SendEvent: Event<SendEvent> { };
struct UDPDataEvent: Event<UDPDataEvent> {
explicit UDPDataEvent(Addr addr, std::unique_ptr<const char[]> ptr, ssize_t l, bool trunc) noexcept
: dt{std::move(ptr)}, len{l}, sndr{addr}, part{trunc}
{ }
const char * data() const noexcept { return dt.get(); }
ssize_t length() const noexcept { return len; }
Addr sender() const noexcept { return sndr; }
bool partial() const noexcept { return part; }
private:
std::unique_ptr<const char[]> dt;
const ssize_t len;
Addr sndr;
const bool part;
};
namespace details {

View File

@ -13,6 +13,9 @@
namespace uvw {
struct WorkEvent: Event<WorkEvent> { };
class Work final: public Request<Work, uv_work_t> {
static void workCallback(uv_work_t *req) {
static_cast<Work*>(req->data)->task();