added common HandleType enum + guessHandle util

This commit is contained in:
Michele Caini 2016-08-22 13:57:25 +02:00
parent c8d4177992
commit 3079ffe613
2 changed files with 108 additions and 38 deletions

View File

@ -15,20 +15,6 @@
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
};
}
/**
* @brief The PipeHandle handle.
*
@ -41,8 +27,6 @@ class PipeHandle final: public StreamHandle<PipeHandle, uv_pipe_t> {
{ }
public:
using Pending = details::UVHandleType;
/**
* @brief Creates a new poll handle.
* @param args
@ -152,24 +136,19 @@ public:
* Steps to be done:
*
* * Call `pending()`, if its greater than zero then proceed.
* * Initialize a handle of the given type, returned by `receive()`.
* * Initialize a handle of the given type, as returned by `receive()`.
* * Call `accept(pipe, handle)`.
*
* @return
* @return The type of the pending handle. Possible values are:
*
* * `HandleType::PIPE`
* * `HandleType::TCP`
* * `HandleType::UDP`
* * `HandleType::UNKNOWN`
*/
Pending receive() noexcept {
HandleType receive() noexcept {
auto type = uv_pipe_pending_type(get<uv_pipe_t>());
switch(type) {
case UV_NAMED_PIPE:
return Pending::PIPE;
case UV_TCP:
return Pending::TCP;
case UV_UDP:
return Pending::UDP;
default:
return Pending::UNKNOWN;
}
return guessHandle(type);
}
private:

View File

@ -12,6 +12,34 @@
namespace uvw {
namespace details {
enum class UVHandleType: std::underlying_type_t<uv_handle_type> {
UNKNOWN = UV_UNKNOWN_HANDLE,
ASYNC = UV_ASYNC,
CHECK = UV_CHECK,
FS_EVENT = UV_FS_EVENT,
FS_POLL = UV_FS_POLL,
HANDLE = UV_HANDLE,
IDLE = UV_IDLE,
PIPE = UV_NAMED_PIPE,
POLL = UV_POLL,
PREPARE = UV_PREPARE,
PROCESS = UV_PROCESS,
STREAM = UV_STREAM,
TCP = UV_TCP,
TIMER = UV_TIMER,
TTY = UV_TTY,
UDP = UV_UDP,
SIGNAL = UV_SIGNAL,
FILE = UV_FILE
};
}
/**
* @brief Utility class to handle flags.
*
@ -114,9 +142,9 @@ private:
*
* In particular, It is used for:
*
* * FileHandle (that is an alias for `UVTypeWrapper<uv_file>`)
* * OSSocketHandle (that is an alias for `UVTypeWrapper<uv_os_sock_t>`)
* * OSFileDescriptor (that is an alias for `UVTypeWrapper<uv_os_fd_t>`)
* * `FileHandle` (that is an alias for `UVTypeWrapper<uv_file>`)
* * `OSSocketHandle` (that is an alias for `UVTypeWrapper<uv_os_sock_t>`)
* * `OSFileDescriptor` (that is an alias for `UVTypeWrapper<uv_os_fd_t>`)
*
* It can be bound to each value of type `T` and it will be implicitly converted
* to the underlying type when needed.
@ -141,11 +169,6 @@ private:
};
using FileHandle = UVTypeWrapper<uv_file>;
using OSSocketHandle = UVTypeWrapper<uv_os_sock_t>;
using OSFileDescriptor = UVTypeWrapper<uv_os_fd_t>;
/**
* @brief Address representation.
*
@ -171,10 +194,78 @@ struct Addr { std::string ip; unsigned int port; };
struct WinSize { int width; int height; };
using HandleType = details::UVHandleType;
using FileHandle = UVTypeWrapper<uv_file>;
using OSSocketHandle = UVTypeWrapper<uv_os_sock_t>;
using OSFileDescriptor = UVTypeWrapper<uv_os_fd_t>;
using TimeSpec = uv_timespec_t;
using Stat = uv_stat_t;
using Uid = uv_uid_t;
using Gid = uv_gid_t;
/**
* @brief Gets the type of the stream to be used with the given descriptor.
*
* Returns the type of stream that should be used with a given file
* descriptor.<br/>
* Usually this will be used during initialization to guess the type of the
* stdio streams.
*
* @param file A valid descriptor.
* @return One of the following types:
*
* * `HandleType::UNKNOWN`
* * `HandleType::PIPE`
* * `HandleType::TCP`
* * `HandleType::TTY`
* * `HandleType::UDP`
* * `HandleType::FILE`
*/
HandleType guessHandle(FileHandle file) {
auto type = uv_guess_handle(file);
switch(type) {
case UV_ASYNC:
return HandleType::ASYNC;
case UV_CHECK:
return HandleType::CHECK;
case UV_FS_EVENT:
return HandleType::FS_EVENT;
case UV_FS_POLL:
return HandleType::FS_POLL;
case UV_HANDLE:
return HandleType::HANDLE;
case UV_IDLE:
return HandleType::IDLE;
case UV_NAMED_PIPE:
return HandleType::PIPE;
case UV_POLL:
return HandleType::POLL;
case UV_PREPARE:
return HandleType::PREPARE;
case UV_PROCESS:
return HandleType::PROCESS;
case UV_STREAM:
return HandleType::STREAM;
case UV_TCP:
return HandleType::TCP;
case UV_TIMER:
return HandleType::TIMER;
case UV_TTY:
return HandleType::TTY;
case UV_UDP:
return HandleType::UDP;
case UV_SIGNAL:
return HandleType::SIGNAL;
case UV_FILE:
return HandleType::FILE;
default:
return HandleType::UNKNOWN;
}
}
}