From 0bffb21fffb21f8a4b3eb41ff2aa768d99f202cc Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 25 Aug 2016 17:33:37 +0200 Subject: [PATCH] added miscellaneous utilities + odr check --- src/uvw/pipe.hpp | 2 +- src/uvw/util.hpp | 249 +++++++++++++++++++++++--------------------- test/CMakeLists.txt | 20 ++-- test/odr.cpp | 1 + 4 files changed, 140 insertions(+), 132 deletions(-) create mode 100644 test/odr.cpp diff --git a/src/uvw/pipe.hpp b/src/uvw/pipe.hpp index c3c0b62f..15258250 100644 --- a/src/uvw/pipe.hpp +++ b/src/uvw/pipe.hpp @@ -148,7 +148,7 @@ public: */ HandleType receive() noexcept { auto type = uv_pipe_pending_type(get()); - return guessHandle(type); + return Utilities::guessHandle(type); } private: diff --git a/src/uvw/util.hpp b/src/uvw/util.hpp index 96aac28c..97c0e8f0 100644 --- a/src/uvw/util.hpp +++ b/src/uvw/util.hpp @@ -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.
- * 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 don’t 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.
+ * 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 interfaces() noexcept { + std::vector 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(&iface.address.address4); + interface.netmask = details::address(&iface.netmask.netmask4); + } else if(iface.address.address4.sin_family == AF_INET6) { + interface.address = details::address(&iface.address.address6); + interface.netmask = details::address(&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 interfaces() noexcept { - std::vector 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(&iface.address.address4); - interface.netmask = details::address(&iface.netmask.netmask4); - } else if(iface.address.address4.sin_family == AF_INET6) { - interface.address = details::address(&iface.address.address6); - interface.netmask = details::address(&iface.netmask.netmask6); - } - - interfaces.push_back(std::move(interface)); - }); - - uv_free_interface_addresses(ifaces, count); - - return interfaces; -} } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d7a521d9..6a185f4c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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}) diff --git a/test/odr.cpp b/test/odr.cpp new file mode 100644 index 00000000..94105c5e --- /dev/null +++ b/test/odr.cpp @@ -0,0 +1 @@ +#include