From 12d1ea8138f38789994c36f1fe03dcf67c6fa99e Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 26 Jul 2016 11:45:50 +0200 Subject: [PATCH] added FsEvent + refactoring --- src/uvw.hpp | 1 + src/uvw/emitter.hpp | 6 ++-- src/uvw/event.hpp | 11 ------- src/uvw/fs.hpp | 1 - src/uvw/fs_event.hpp | 78 ++++++++++++++++++++++++++++++++++++++++++++ src/uvw/loop.hpp | 16 ++++++--- src/uvw/pipe.hpp | 21 ++++++++---- src/uvw/poll.hpp | 15 +++++++-- src/uvw/tcp.hpp | 17 +++++++--- src/uvw/tty.hpp | 19 ++++++++--- src/uvw/udp.hpp | 24 ++++++++------ 11 files changed, 161 insertions(+), 48 deletions(-) create mode 100644 src/uvw/fs_event.hpp diff --git a/src/uvw.hpp b/src/uvw.hpp index e862dc79..82d68378 100644 --- a/src/uvw.hpp +++ b/src/uvw.hpp @@ -2,6 +2,7 @@ #include "uvw/check.hpp" #include "uvw/event.hpp" #include "uvw/fs.hpp" +#include "uvw/fs_event.hpp" #include "uvw/fs_poll.hpp" #include "uvw/idle.hpp" #include "uvw/lib.hpp" diff --git a/src/uvw/emitter.hpp b/src/uvw/emitter.hpp index 010386c7..bf723584 100644 --- a/src/uvw/emitter.hpp +++ b/src/uvw/emitter.hpp @@ -87,6 +87,9 @@ protected: } public: + template + using Listener = typename Handler::Listener; + template struct Connection: private Handler::Connection { template friend class Emitter; @@ -95,9 +98,6 @@ public: { } }; - template - using Listener = typename Handler::Listener; - virtual ~Emitter() noexcept { static_assert(std::is_base_of, T>::value, "!"); } diff --git a/src/uvw/event.hpp b/src/uvw/event.hpp index 52b7b278..9a11b5c1 100644 --- a/src/uvw/event.hpp +++ b/src/uvw/event.hpp @@ -42,15 +42,4 @@ private: }; -template -struct FlagsEvent: Event> { - explicit FlagsEvent(Flags f) noexcept: flgs{std::move(f)} { } - - Flags flags() const noexcept { return flgs; } - -private: - Flags flgs; -}; - - } diff --git a/src/uvw/fs.hpp b/src/uvw/fs.hpp index 3ad46d3d..9cc16e53 100644 --- a/src/uvw/fs.hpp +++ b/src/uvw/fs.hpp @@ -53,7 +53,6 @@ class Fs final: public Request { public: using Time = std::chrono::seconds; - using Flags = int; using Mode = int; using Offset = int64_t; diff --git a/src/uvw/fs_event.hpp b/src/uvw/fs_event.hpp new file mode 100644 index 00000000..abd8b3a0 --- /dev/null +++ b/src/uvw/fs_event.hpp @@ -0,0 +1,78 @@ +#pragma once + + +#include +#include +#include +#include +#include "event.hpp" +#include "handle.hpp" +#include "util.hpp" + + +namespace uvw { + + +namespace details { + + +enum class UVFsEventFlags: std::underlying_type_t { + WATCH_ENTRY = UV_FS_EVENT_WATCH_ENTRY, + STAT = UV_FS_EVENT_STAT, + RECURSIVE = UV_FS_EVENT_RECURSIVE +}; + + +enum class UVFsEvent: std::underlying_type_t { + RENAME = UV_RENAME, + CHANGE = UV_CHANGE +}; + + +} + + +struct FsEventEvent: Event { + FsEventEvent(std::string fPath, Flags f) + : flgs{std::move(f)}, relPath{std::move(fPath)} + { } + + const char * filename() const noexcept { return relPath.data(); } + Flags flags() const noexcept { return flgs; } + +private: + Flags flgs; + std::string relPath; +}; + + +class FsEvent final: public Handle { + static void startCallback(uv_fs_event_t *handle, const char *filename, int events, int status) { + FsEvent &fsEvent = *(static_cast(handle->data)); + if(status) { fsEvent.publish(ErrorEvent{status}); } + else { fsEvent.publish(FsEventEvent{filename, static_cast>(events)}); } + } + + using Handle::Handle; + +public: + using Watch = details::UVFsEvent; + using Event = details::UVFsEventFlags; + + template + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new FsEvent{std::forward(args)...}}; + } + + bool init() { return initialize(&uv_fs_event_init); } + + void start(std::string path, Flags flags = Flags{}) { + invoke(&uv_fs_event_start, get(), &startCallback, path.data(), flags); + } + + void stop() { invoke(&uv_fs_event_stop, get()); } + std::string path() noexcept { return details::path(&uv_fs_event_getpath, get()); } +}; + + +} diff --git a/src/uvw/loop.hpp b/src/uvw/loop.hpp index 4af43055..e2f9baf0 100644 --- a/src/uvw/loop.hpp +++ b/src/uvw/loop.hpp @@ -14,6 +14,17 @@ namespace uvw { +namespace details { + + +enum class UVLoopOption: std::underlying_type_t { + BLOCK_SIGNAL = UV_LOOP_BLOCK_SIGNAL +}; + + +} + + class BaseHandle { public: virtual bool active() const noexcept = 0; @@ -37,10 +48,7 @@ class Loop final: public Emitter, public std::enable_shared_from_this { - BLOCK_SIGNAL = UV_LOOP_BLOCK_SIGNAL - }; + using Configure = details::UVLoopOption; static std::shared_ptr create() { auto ptr = std::unique_ptr{new uv_loop_t, [](uv_loop_t *l){ delete l; }}; diff --git a/src/uvw/pipe.hpp b/src/uvw/pipe.hpp index 4f2636ef..80c5d06b 100644 --- a/src/uvw/pipe.hpp +++ b/src/uvw/pipe.hpp @@ -15,16 +15,25 @@ namespace uvw { +namespace details { + + +enum class UVHandleType: std::underlying_type_t { + UNKNOWN = UV_UNKNOWN_HANDLE, + PIPE = UV_NAMED_PIPE, + TCP = UV_TCP, + UDP = UV_UDP +}; + + +} + + class Pipe final: public Stream { using Stream::Stream; public: - enum class Pending: std::underlying_type_t { - UNKNOWN = UV_UNKNOWN_HANDLE, - PIPE = UV_NAMED_PIPE, - TCP = UV_TCP, - UDP = UV_UDP - }; + using Pending = details::UVHandleType; template static std::shared_ptr create(Args&&... args) { diff --git a/src/uvw/poll.hpp b/src/uvw/poll.hpp index bca57df7..ef93b0e8 100644 --- a/src/uvw/poll.hpp +++ b/src/uvw/poll.hpp @@ -16,7 +16,7 @@ namespace uvw { namespace details { -enum class Event: std::underlying_type_t { +enum class UVPollEvent: std::underlying_type_t { READABLE = UV_READABLE, WRITABLE = UV_WRITABLE, DISCONNECT = UV_DISCONNECT @@ -26,7 +26,16 @@ enum class Event: std::underlying_type_t { } -using PollEvent = FlagsEvent; +struct PollEvent: Event { + explicit PollEvent(Flags f) noexcept + : flgs{std::move(f)} + { } + + Flags flags() const noexcept { return flgs; } + +private: + Flags flgs; +}; class Poll final: public Handle { @@ -39,7 +48,7 @@ class Poll final: public Handle { using Handle::Handle; public: - using Event = details::Event; + using Event = details::UVPollEvent; template static std::shared_ptr create(Args&&... args) { diff --git a/src/uvw/tcp.hpp b/src/uvw/tcp.hpp index 6d05ac24..e26a2c98 100644 --- a/src/uvw/tcp.hpp +++ b/src/uvw/tcp.hpp @@ -16,19 +16,26 @@ namespace uvw { +namespace details { + + +enum class UVTcpFlags: std::underlying_type_t { + IPV6ONLY = UV_TCP_IPV6ONLY +}; + + +} + + class Tcp final: public Stream { using Stream::Stream; public: using Time = std::chrono::seconds; - + using Bind = details::UVTcpFlags; using IPv4 = details::IPv4; using IPv6 = details::IPv6; - enum class Bind: std::underlying_type_t { - IPV6ONLY = UV_TCP_IPV6ONLY - }; - template static std::shared_ptr create(Args&&... args) { return std::shared_ptr{new Tcp{std::forward(args)...}}; diff --git a/src/uvw/tty.hpp b/src/uvw/tty.hpp index 01ba39a9..c1e2102a 100644 --- a/src/uvw/tty.hpp +++ b/src/uvw/tty.hpp @@ -13,6 +13,19 @@ namespace uvw { +namespace details { + + +enum class UVTTYModeT: std::underlying_type_t { + NORMAL = UV_TTY_MODE_NORMAL, + RAW = UV_TTY_MODE_RAW, + IO = UV_TTY_MODE_IO +}; + + +} + + class TTY final: public Stream { explicit TTY(std::shared_ptr ref, FileHandle desc, @@ -23,11 +36,7 @@ class TTY final: public Stream { { } public: - enum class Mode: std::underlying_type_t { - NORMAL = UV_TTY_MODE_NORMAL, - RAW = UV_TTY_MODE_RAW, - IO = UV_TTY_MODE_IO - }; + using Mode = details::UVTTYModeT; template static std::shared_ptr create(Args&&... args) { diff --git a/src/uvw/udp.hpp b/src/uvw/udp.hpp index 88f4cdbd..1a61e163 100644 --- a/src/uvw/udp.hpp +++ b/src/uvw/udp.hpp @@ -39,6 +39,18 @@ private: namespace details { +enum class UVUdpFlags: std::underlying_type_t { + IPV6ONLY = UV_UDP_IPV6ONLY, + REUSEADDR = UV_UDP_REUSEADDR +}; + + +enum class UVMembership: std::underlying_type_t { + LEAVE_GROUP = UV_LEAVE_GROUP, + JOIN_GROUP = UV_JOIN_GROUP +}; + + class Send final: public Request { using Request::Request; @@ -84,19 +96,11 @@ class Udp final: public Handle { } public: + using Membership = details::UVMembership; + using Bind = details::UVUdpFlags; using IPv4 = details::IPv4; using IPv6 = details::IPv6; - enum class Bind: std::underlying_type_t { - IPV6ONLY = UV_UDP_IPV6ONLY, - REUSEADDR = UV_UDP_REUSEADDR - }; - - enum class Membership: std::underlying_type_t { - LEAVE_GROUP = UV_LEAVE_GROUP, - JOIN_GROUP = UV_JOIN_GROUP - }; - template static std::shared_ptr create(Args&&... args) { return std::shared_ptr{new Udp{std::forward(args)...}};