added miscellaneous utilities + odr check

This commit is contained in:
Michele Caini 2016-08-25 17:33:37 +02:00
parent 09285bd405
commit 0bffb21fff
4 changed files with 140 additions and 132 deletions

View File

@ -148,7 +148,7 @@ public:
*/
HandleType receive() noexcept {
auto type = uv_pipe_pending_type(get<uv_pipe_t>());
return guessHandle(type);
return Utilities::guessHandle(type);
}
private:

View File

@ -172,68 +172,6 @@ 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;
}
}
/**
* @brief The IPv4 tag.
*
@ -260,24 +198,15 @@ struct Addr {
/**
* TODO
*
* * uv_replace_allocator
* * uv_uptime
* * uv_getrusage
* * uv_cpu_info
* * uv_free_cpu_info
* * uv_loadavg
* * uv_exepath
* * uv_cwd
* * uv_chdir
* * uv_os_homedir
* * uv_os_tmpdir
* * uv_os_get_passwd
* * uv_os_free_passwd
* * uv_get_total_memory
* * uv_hrtime
* \brief Interface address.
*/
struct Interface {
std::string name; /*!< The name of the interface (as an example _eth0_). */
std::string physical; /*!< The physical address. */
bool internal; /*!< True if it is an internal interface (as an example _loopback_), false otherwise. */
Addr address; /*!< The address of the given interface. */
Addr netmask; /*!< The netmask of the given interface. */
};
namespace details {
@ -368,55 +297,133 @@ std::string path(F &&f, H *handle) noexcept {
/**
* \brief Interface address.
* @brief Miscellaneous utilities.
*
* Miscellaneous functions that dont really belong to any other class.
*/
struct Interface {
std::string name; /*!< The name of the interface (as an example _eth0_). */
std::string physical; /*!< The physical address. */
bool internal; /*!< True if it is an internal interface (as an example _loopback_), false otherwise. */
Addr address; /*!< The address of the given interface. */
Addr netmask; /*!< The netmask of the given interface. */
struct Utilities {
/**
* @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`
*/
static 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;
}
}
/**
* @brief Gets a set of descriptors of all the available interfaces.
*
* This function can be used to query the underlying system and get a set of
* descriptors of all the available interfaces, either internal or not.
*
* @return A set of descriptors of all the available interfaces.
*/
static std::vector<Interface> interfaces() noexcept {
std::vector<Interface> interfaces;
uv_interface_address_t *ifaces;
int count;
uv_interface_addresses(&ifaces, &count);
std::for_each(ifaces, ifaces+count, [&interfaces](const auto &iface) {
Interface interface;
interface.name = iface.name;
interface.physical = iface.phys_addr;
interface.internal = iface.is_internal;
if(iface.address.address4.sin_family == AF_INET) {
interface.address = details::address<IPv4>(&iface.address.address4);
interface.netmask = details::address<IPv4>(&iface.netmask.netmask4);
} else if(iface.address.address4.sin_family == AF_INET6) {
interface.address = details::address<IPv6>(&iface.address.address6);
interface.netmask = details::address<IPv6>(&iface.netmask.netmask6);
}
interfaces.push_back(std::move(interface));
});
uv_free_interface_addresses(ifaces, count);
return interfaces;
}
};
/**
* @brief Gets a set of descriptors of all the available interfaces.
* TODO
*
* This function can be used to query the underlying system and get a set of
* descriptors of all the available interfaces, either internal or not.
*
* @return A set of descriptors of all the available interfaces.
* * uv_replace_allocator
* * uv_uptime
* * uv_getrusage
* * uv_cpu_info
* * uv_free_cpu_info
* * uv_loadavg
* * uv_exepath
* * uv_cwd
* * uv_chdir
* * uv_os_homedir
* * uv_os_tmpdir
* * uv_os_get_passwd
* * uv_os_free_passwd
* * uv_get_total_memory
* * uv_hrtime
*/
std::vector<Interface> interfaces() noexcept {
std::vector<Interface> interfaces;
uv_interface_address_t *ifaces;
int count;
uv_interface_addresses(&ifaces, &count);
std::for_each(ifaces, ifaces+count, [&interfaces](const auto &iface) {
Interface interface;
interface.name = iface.name;
interface.physical = iface.phys_addr;
interface.internal = iface.is_internal;
if(iface.address.address4.sin_family == AF_INET) {
interface.address = details::address<IPv4>(&iface.address.address4);
interface.netmask = details::address<IPv4>(&iface.netmask.netmask4);
} else if(iface.address.address4.sin_family == AF_INET6) {
interface.address = details::address<IPv6>(&iface.address.address6);
interface.netmask = details::address<IPv6>(&iface.netmask.netmask6);
}
interfaces.push_back(std::move(interface));
});
uv_free_interface_addresses(ifaces, count);
return interfaces;
}
}

View File

@ -33,7 +33,7 @@ set(TARGET_WORK work)
# Test TARGET_MAIN
set(TARGET_MAIN_SOURCES main.cpp)
set(TARGET_MAIN_SOURCES odr.cpp main.cpp)
add_executable(${TARGET_MAIN} ${TARGET_MAIN_SOURCES})
target_include_directories(${TARGET_MAIN} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_MAIN} PRIVATE ${COMMON_LINK_LIBS})
@ -41,7 +41,7 @@ add_test(NAME ${TARGET_MAIN} COMMAND ${TARGET_MAIN})
# Test TARGET_ASYNC
set(TARGET_ASYNC_SOURCES uvw/async.cpp)
set(TARGET_ASYNC_SOURCES odr.cpp uvw/async.cpp)
add_executable(${TARGET_ASYNC} ${TARGET_ASYNC_SOURCES})
target_include_directories(${TARGET_ASYNC} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_ASYNC} PRIVATE ${COMMON_LINK_LIBS})
@ -49,7 +49,7 @@ add_test(NAME ${TARGET_ASYNC} COMMAND ${TARGET_ASYNC})
# Test TARGET_CHECK
set(TARGET_CHECK_SOURCES uvw/check.cpp)
set(TARGET_CHECK_SOURCES odr.cpp uvw/check.cpp)
add_executable(${TARGET_CHECK} ${TARGET_CHECK_SOURCES})
target_include_directories(${TARGET_CHECK} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_CHECK} PRIVATE ${COMMON_LINK_LIBS})
@ -57,7 +57,7 @@ add_test(NAME ${TARGET_CHECK} COMMAND ${TARGET_CHECK})
# Test TARGET_EMITTER
set(TARGET_EMITTER_SOURCES uvw/emitter.cpp)
set(TARGET_EMITTER_SOURCES odr.cpp uvw/emitter.cpp)
add_executable(${TARGET_EMITTER} ${TARGET_EMITTER_SOURCES})
target_include_directories(${TARGET_EMITTER} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_EMITTER} PRIVATE ${COMMON_LINK_LIBS})
@ -65,7 +65,7 @@ add_test(NAME ${TARGET_EMITTER} COMMAND ${TARGET_EMITTER})
# Test TARGET_IDLE
set(TARGET_IDLE_SOURCES uvw/idle.cpp)
set(TARGET_IDLE_SOURCES odr.cpp uvw/idle.cpp)
add_executable(${TARGET_IDLE} ${TARGET_IDLE_SOURCES})
target_include_directories(${TARGET_IDLE} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_IDLE} PRIVATE ${COMMON_LINK_LIBS})
@ -73,7 +73,7 @@ add_test(NAME ${TARGET_IDLE} COMMAND ${TARGET_IDLE})
# Test TARGET_LOOP
set(TARGET_LOOP_SOURCES uvw/loop.cpp)
set(TARGET_LOOP_SOURCES odr.cpp uvw/loop.cpp)
add_executable(${TARGET_LOOP} ${TARGET_LOOP_SOURCES})
target_include_directories(${TARGET_LOOP} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_LOOP} PRIVATE ${COMMON_LINK_LIBS})
@ -81,7 +81,7 @@ add_test(NAME ${TARGET_LOOP} COMMAND ${TARGET_LOOP})
# Test TARGET_PREPARE
set(TARGET_PREPARE_SOURCES uvw/prepare.cpp)
set(TARGET_PREPARE_SOURCES odr.cpp uvw/prepare.cpp)
add_executable(${TARGET_PREPARE} ${TARGET_PREPARE_SOURCES})
target_include_directories(${TARGET_PREPARE} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_PREPARE} PRIVATE ${COMMON_LINK_LIBS})
@ -89,7 +89,7 @@ add_test(NAME ${TARGET_PREPARE} COMMAND ${TARGET_PREPARE})
# Test TARGET_SIGNAL
set(TARGET_SIGNAL_SOURCES uvw/signal.cpp)
set(TARGET_SIGNAL_SOURCES odr.cpp uvw/signal.cpp)
add_executable(${TARGET_SIGNAL} ${TARGET_SIGNAL_SOURCES})
target_include_directories(${TARGET_SIGNAL} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_SIGNAL} PRIVATE ${COMMON_LINK_LIBS})
@ -97,7 +97,7 @@ add_test(NAME ${TARGET_SIGNAL} COMMAND ${TARGET_SIGNAL})
# Test TARGET_TIMER
set(TARGET_TIMER_SOURCES uvw/timer.cpp)
set(TARGET_TIMER_SOURCES odr.cpp uvw/timer.cpp)
add_executable(${TARGET_TIMER} ${TARGET_TIMER_SOURCES})
target_include_directories(${TARGET_TIMER} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_TIMER} PRIVATE ${COMMON_LINK_LIBS})
@ -105,7 +105,7 @@ add_test(NAME ${TARGET_TIMER} COMMAND ${TARGET_TIMER})
# Test TARGET_WORK
set(TARGET_WORK_SOURCES uvw/work.cpp)
set(TARGET_WORK_SOURCES odr.cpp uvw/work.cpp)
add_executable(${TARGET_WORK} ${TARGET_WORK_SOURCES})
target_include_directories(${TARGET_WORK} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_WORK} PRIVATE ${COMMON_LINK_LIBS})

1
test/odr.cpp Normal file
View File

@ -0,0 +1 @@
#include <uvw.hpp>