added FsEvent + refactoring
This commit is contained in:
parent
d684b0836a
commit
12d1ea8138
@ -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"
|
||||
|
||||
@ -87,6 +87,9 @@ protected:
|
||||
}
|
||||
|
||||
public:
|
||||
template<typename E>
|
||||
using Listener = typename Handler<E>::Listener;
|
||||
|
||||
template<typename E>
|
||||
struct Connection: private Handler<E>::Connection {
|
||||
template<typename> friend class Emitter;
|
||||
@ -95,9 +98,6 @@ public:
|
||||
{ }
|
||||
};
|
||||
|
||||
template<typename E>
|
||||
using Listener = typename Handler<E>::Listener;
|
||||
|
||||
virtual ~Emitter() noexcept {
|
||||
static_assert(std::is_base_of<Emitter<T>, T>::value, "!");
|
||||
}
|
||||
|
||||
@ -42,15 +42,4 @@ private:
|
||||
};
|
||||
|
||||
|
||||
template<typename E>
|
||||
struct FlagsEvent: Event<FlagsEvent<E>> {
|
||||
explicit FlagsEvent(Flags<E> f) noexcept: flgs{std::move(f)} { }
|
||||
|
||||
Flags<E> flags() const noexcept { return flgs; }
|
||||
|
||||
private:
|
||||
Flags<E> flgs;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -53,7 +53,6 @@ class Fs final: public Request<Fs, uv_fs_t> {
|
||||
|
||||
public:
|
||||
using Time = std::chrono::seconds;
|
||||
|
||||
using Flags = int;
|
||||
using Mode = int;
|
||||
using Offset = int64_t;
|
||||
|
||||
78
src/uvw/fs_event.hpp
Normal file
78
src/uvw/fs_event.hpp
Normal file
@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <uv.h>
|
||||
#include "event.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
|
||||
namespace details {
|
||||
|
||||
|
||||
enum class UVFsEventFlags: std::underlying_type_t<uv_fs_event_flags> {
|
||||
WATCH_ENTRY = UV_FS_EVENT_WATCH_ENTRY,
|
||||
STAT = UV_FS_EVENT_STAT,
|
||||
RECURSIVE = UV_FS_EVENT_RECURSIVE
|
||||
};
|
||||
|
||||
|
||||
enum class UVFsEvent: std::underlying_type_t<uv_fs_event> {
|
||||
RENAME = UV_RENAME,
|
||||
CHANGE = UV_CHANGE
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
struct FsEventEvent: Event<FsEventEvent> {
|
||||
FsEventEvent(std::string fPath, Flags<details::UVFsEvent> f)
|
||||
: flgs{std::move(f)}, relPath{std::move(fPath)}
|
||||
{ }
|
||||
|
||||
const char * filename() const noexcept { return relPath.data(); }
|
||||
Flags<details::UVFsEvent> flags() const noexcept { return flgs; }
|
||||
|
||||
private:
|
||||
Flags<details::UVFsEvent> flgs;
|
||||
std::string relPath;
|
||||
};
|
||||
|
||||
|
||||
class FsEvent final: public Handle<FsEvent, uv_fs_event_t> {
|
||||
static void startCallback(uv_fs_event_t *handle, const char *filename, int events, int status) {
|
||||
FsEvent &fsEvent = *(static_cast<FsEvent*>(handle->data));
|
||||
if(status) { fsEvent.publish(ErrorEvent{status}); }
|
||||
else { fsEvent.publish(FsEventEvent{filename, static_cast<std::underlying_type_t<Event>>(events)}); }
|
||||
}
|
||||
|
||||
using Handle::Handle;
|
||||
|
||||
public:
|
||||
using Watch = details::UVFsEvent;
|
||||
using Event = details::UVFsEventFlags;
|
||||
|
||||
template<typename... Args>
|
||||
static std::shared_ptr<FsEvent> create(Args&&... args) {
|
||||
return std::shared_ptr<FsEvent>{new FsEvent{std::forward<Args>(args)...}};
|
||||
}
|
||||
|
||||
bool init() { return initialize<uv_fs_event_t>(&uv_fs_event_init); }
|
||||
|
||||
void start(std::string path, Flags<Watch> flags = Flags<Watch>{}) {
|
||||
invoke(&uv_fs_event_start, get<uv_fs_event_t>(), &startCallback, path.data(), flags);
|
||||
}
|
||||
|
||||
void stop() { invoke(&uv_fs_event_stop, get<uv_fs_event_t>()); }
|
||||
std::string path() noexcept { return details::path(&uv_fs_event_getpath, get<uv_fs_event_t>()); }
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@ -14,6 +14,17 @@
|
||||
namespace uvw {
|
||||
|
||||
|
||||
namespace details {
|
||||
|
||||
|
||||
enum class UVLoopOption: std::underlying_type_t<uv_loop_option> {
|
||||
BLOCK_SIGNAL = UV_LOOP_BLOCK_SIGNAL
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
class BaseHandle {
|
||||
public:
|
||||
virtual bool active() const noexcept = 0;
|
||||
@ -37,10 +48,7 @@ class Loop final: public Emitter<Loop>, public std::enable_shared_from_this<Loop
|
||||
|
||||
public:
|
||||
using Time = std::chrono::milliseconds;
|
||||
|
||||
enum class Configure: std::underlying_type_t<uv_loop_option> {
|
||||
BLOCK_SIGNAL = UV_LOOP_BLOCK_SIGNAL
|
||||
};
|
||||
using Configure = details::UVLoopOption;
|
||||
|
||||
static std::shared_ptr<Loop> create() {
|
||||
auto ptr = std::unique_ptr<uv_loop_t, Deleter>{new uv_loop_t, [](uv_loop_t *l){ delete l; }};
|
||||
|
||||
@ -15,16 +15,25 @@
|
||||
namespace uvw {
|
||||
|
||||
|
||||
namespace details {
|
||||
|
||||
|
||||
enum class UVHandleType: std::underlying_type_t<uv_handle_type> {
|
||||
UNKNOWN = UV_UNKNOWN_HANDLE,
|
||||
PIPE = UV_NAMED_PIPE,
|
||||
TCP = UV_TCP,
|
||||
UDP = UV_UDP
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Pipe final: public Stream<Pipe, uv_pipe_t> {
|
||||
using Stream::Stream;
|
||||
|
||||
public:
|
||||
enum class Pending: std::underlying_type_t<uv_handle_type> {
|
||||
UNKNOWN = UV_UNKNOWN_HANDLE,
|
||||
PIPE = UV_NAMED_PIPE,
|
||||
TCP = UV_TCP,
|
||||
UDP = UV_UDP
|
||||
};
|
||||
using Pending = details::UVHandleType;
|
||||
|
||||
template<typename... Args>
|
||||
static std::shared_ptr<Pipe> create(Args&&... args) {
|
||||
|
||||
@ -16,7 +16,7 @@ namespace uvw {
|
||||
namespace details {
|
||||
|
||||
|
||||
enum class Event: std::underlying_type_t<uv_poll_event> {
|
||||
enum class UVPollEvent: std::underlying_type_t<uv_poll_event> {
|
||||
READABLE = UV_READABLE,
|
||||
WRITABLE = UV_WRITABLE,
|
||||
DISCONNECT = UV_DISCONNECT
|
||||
@ -26,7 +26,16 @@ enum class Event: std::underlying_type_t<uv_poll_event> {
|
||||
}
|
||||
|
||||
|
||||
using PollEvent = FlagsEvent<details::Event>;
|
||||
struct PollEvent: Event<PollEvent> {
|
||||
explicit PollEvent(Flags<details::UVPollEvent> f) noexcept
|
||||
: flgs{std::move(f)}
|
||||
{ }
|
||||
|
||||
Flags<details::UVPollEvent> flags() const noexcept { return flgs; }
|
||||
|
||||
private:
|
||||
Flags<details::UVPollEvent> flgs;
|
||||
};
|
||||
|
||||
|
||||
class Poll final: public Handle<Poll, uv_poll_t> {
|
||||
@ -39,7 +48,7 @@ class Poll final: public Handle<Poll, uv_poll_t> {
|
||||
using Handle::Handle;
|
||||
|
||||
public:
|
||||
using Event = details::Event;
|
||||
using Event = details::UVPollEvent;
|
||||
|
||||
template<typename... Args>
|
||||
static std::shared_ptr<Poll> create(Args&&... args) {
|
||||
|
||||
@ -16,19 +16,26 @@
|
||||
namespace uvw {
|
||||
|
||||
|
||||
namespace details {
|
||||
|
||||
|
||||
enum class UVTcpFlags: std::underlying_type_t<uv_tcp_flags> {
|
||||
IPV6ONLY = UV_TCP_IPV6ONLY
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Tcp final: public Stream<Tcp, uv_tcp_t> {
|
||||
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<uv_tcp_flags> {
|
||||
IPV6ONLY = UV_TCP_IPV6ONLY
|
||||
};
|
||||
|
||||
template<typename... Args>
|
||||
static std::shared_ptr<Tcp> create(Args&&... args) {
|
||||
return std::shared_ptr<Tcp>{new Tcp{std::forward<Args>(args)...}};
|
||||
|
||||
@ -13,6 +13,19 @@
|
||||
namespace uvw {
|
||||
|
||||
|
||||
namespace details {
|
||||
|
||||
|
||||
enum class UVTTYModeT: std::underlying_type_t<uv_tty_mode_t> {
|
||||
NORMAL = UV_TTY_MODE_NORMAL,
|
||||
RAW = UV_TTY_MODE_RAW,
|
||||
IO = UV_TTY_MODE_IO
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
class TTY final: public Stream<TTY, uv_tty_t> {
|
||||
explicit TTY(std::shared_ptr<Loop> ref,
|
||||
FileHandle desc,
|
||||
@ -23,11 +36,7 @@ class TTY final: public Stream<TTY, uv_tty_t> {
|
||||
{ }
|
||||
|
||||
public:
|
||||
enum class Mode: std::underlying_type_t<uv_tty_mode_t> {
|
||||
NORMAL = UV_TTY_MODE_NORMAL,
|
||||
RAW = UV_TTY_MODE_RAW,
|
||||
IO = UV_TTY_MODE_IO
|
||||
};
|
||||
using Mode = details::UVTTYModeT;
|
||||
|
||||
template<typename... Args>
|
||||
static std::shared_ptr<TTY> create(Args&&... args) {
|
||||
|
||||
@ -39,6 +39,18 @@ private:
|
||||
namespace details {
|
||||
|
||||
|
||||
enum class UVUdpFlags: std::underlying_type_t<uv_udp_flags> {
|
||||
IPV6ONLY = UV_UDP_IPV6ONLY,
|
||||
REUSEADDR = UV_UDP_REUSEADDR
|
||||
};
|
||||
|
||||
|
||||
enum class UVMembership: std::underlying_type_t<uv_membership> {
|
||||
LEAVE_GROUP = UV_LEAVE_GROUP,
|
||||
JOIN_GROUP = UV_JOIN_GROUP
|
||||
};
|
||||
|
||||
|
||||
class Send final: public Request<Send, uv_udp_send_t> {
|
||||
using Request::Request;
|
||||
|
||||
@ -84,19 +96,11 @@ class Udp final: public Handle<Udp, uv_udp_t> {
|
||||
}
|
||||
|
||||
public:
|
||||
using Membership = details::UVMembership;
|
||||
using Bind = details::UVUdpFlags;
|
||||
using IPv4 = details::IPv4;
|
||||
using IPv6 = details::IPv6;
|
||||
|
||||
enum class Bind: std::underlying_type_t<uv_udp_flags> {
|
||||
IPV6ONLY = UV_UDP_IPV6ONLY,
|
||||
REUSEADDR = UV_UDP_REUSEADDR
|
||||
};
|
||||
|
||||
enum class Membership: std::underlying_type_t<uv_membership> {
|
||||
LEAVE_GROUP = UV_LEAVE_GROUP,
|
||||
JOIN_GROUP = UV_JOIN_GROUP
|
||||
};
|
||||
|
||||
template<typename... Args>
|
||||
static std::shared_ptr<Udp> create(Args&&... args) {
|
||||
return std::shared_ptr<Udp>{new Udp{std::forward<Args>(args)...}};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user