From f590b9d03e0e585bace0d9e31313a37d3c250a79 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 2 Mar 2020 23:06:43 +0100 Subject: [PATCH] wip (see #174) --- src/uvw/dns.h | 2 +- src/uvw/emitter.h | 5 + src/uvw/fs.h | 2 +- src/uvw/fs_event.h | 2 +- src/uvw/fs_poll.h | 2 +- src/uvw/handle.hpp | 2 +- src/uvw/loop.h | 2 +- src/uvw/pipe.h | 2 +- src/uvw/poll.h | 2 +- src/uvw/process.h | 2 +- src/uvw/tcp.h | 2 +- src/uvw/tty.h | 2 +- src/uvw/udp.h | 2 +- src/uvw/util.cpp | 342 +++++++++++++++++++++++++++++++++++ src/uvw/{util.hpp => util.h} | 292 +++++------------------------- 15 files changed, 408 insertions(+), 255 deletions(-) create mode 100644 src/uvw/util.cpp rename src/uvw/{util.hpp => util.h} (74%) diff --git a/src/uvw/dns.h b/src/uvw/dns.h index b4888e35..fb39bbbf 100644 --- a/src/uvw/dns.h +++ b/src/uvw/dns.h @@ -6,7 +6,7 @@ #include #include #include "request.hpp" -#include "util.hpp" +#include "util.h" #include "loop.h" diff --git a/src/uvw/emitter.h b/src/uvw/emitter.h index 21790067..fbf0aa7f 100644 --- a/src/uvw/emitter.h +++ b/src/uvw/emitter.h @@ -314,3 +314,8 @@ private: } + + +#ifndef UVW_AS_LIB +#include "emitter.cpp" +#endif diff --git a/src/uvw/fs.h b/src/uvw/fs.h index 415669a9..0ad599a1 100644 --- a/src/uvw/fs.h +++ b/src/uvw/fs.h @@ -7,7 +7,7 @@ #include #include #include "request.hpp" -#include "util.hpp" +#include "util.h" #include "loop.h" diff --git a/src/uvw/fs_event.h b/src/uvw/fs_event.h index bbac0033..4e2a4e58 100644 --- a/src/uvw/fs_event.h +++ b/src/uvw/fs_event.h @@ -5,7 +5,7 @@ #include #include #include "handle.hpp" -#include "util.hpp" +#include "util.h" #include "loop.h" diff --git a/src/uvw/fs_poll.h b/src/uvw/fs_poll.h index 6cd0c92c..2fac8325 100644 --- a/src/uvw/fs_poll.h +++ b/src/uvw/fs_poll.h @@ -5,7 +5,7 @@ #include #include #include "handle.hpp" -#include "util.hpp" +#include "util.h" #include "loop.h" diff --git a/src/uvw/handle.hpp b/src/uvw/handle.hpp index 782ff4eb..16b2356f 100644 --- a/src/uvw/handle.hpp +++ b/src/uvw/handle.hpp @@ -6,7 +6,7 @@ #include #include #include "resource.hpp" -#include "util.hpp" +#include "util.h" namespace uvw { diff --git a/src/uvw/loop.h b/src/uvw/loop.h index b65fe6dd..29712d12 100644 --- a/src/uvw/loop.h +++ b/src/uvw/loop.h @@ -12,7 +12,7 @@ #include #include #include "emitter.h" -#include "util.hpp" +#include "util.h" namespace uvw { diff --git a/src/uvw/pipe.h b/src/uvw/pipe.h index ada1f5fd..55847f63 100644 --- a/src/uvw/pipe.h +++ b/src/uvw/pipe.h @@ -7,7 +7,7 @@ #include #include "request.hpp" #include "stream.h" -#include "util.hpp" +#include "util.h" #include "loop.h" diff --git a/src/uvw/poll.h b/src/uvw/poll.h index dfe64ddf..38acb265 100644 --- a/src/uvw/poll.h +++ b/src/uvw/poll.h @@ -5,7 +5,7 @@ #include #include #include "handle.hpp" -#include "util.hpp" +#include "util.h" namespace uvw { diff --git a/src/uvw/process.h b/src/uvw/process.h index c43081e2..9d16363e 100644 --- a/src/uvw/process.h +++ b/src/uvw/process.h @@ -8,7 +8,7 @@ #include #include "handle.hpp" #include "stream.h" -#include "util.hpp" +#include "util.h" #include "loop.h" diff --git a/src/uvw/tcp.h b/src/uvw/tcp.h index b55b555e..4b5e74f8 100644 --- a/src/uvw/tcp.h +++ b/src/uvw/tcp.h @@ -9,7 +9,7 @@ #include #include "request.hpp" #include "stream.h" -#include "util.hpp" +#include "util.h" namespace uvw { diff --git a/src/uvw/tty.h b/src/uvw/tty.h index 53144f09..39ee3dd3 100644 --- a/src/uvw/tty.h +++ b/src/uvw/tty.h @@ -5,7 +5,7 @@ #include #include #include "stream.h" -#include "util.hpp" +#include "util.h" namespace uvw { diff --git a/src/uvw/udp.h b/src/uvw/udp.h index a7c1df83..7d905bbc 100644 --- a/src/uvw/udp.h +++ b/src/uvw/udp.h @@ -9,7 +9,7 @@ #include #include "request.hpp" #include "handle.hpp" -#include "util.hpp" +#include "util.h" namespace uvw { diff --git a/src/uvw/util.cpp b/src/uvw/util.cpp new file mode 100644 index 00000000..8b7dfa3b --- /dev/null +++ b/src/uvw/util.cpp @@ -0,0 +1,342 @@ +#include +#include "util.h" +#include "config.h" + + +namespace uvw { + + +UVW_INLINE Passwd::Passwd(std::shared_ptr pwd) + : passwd{pwd} +{} + + +UVW_INLINE std::string Passwd::username() const noexcept { + return ((passwd && passwd->username) ? passwd->username : ""); +} + + +UVW_INLINE auto Passwd::uid() const noexcept { + return (passwd ? passwd->uid : decltype(uv_passwd_t::uid){}); +} + + +UVW_INLINE auto Passwd::gid() const noexcept { + return (passwd ? passwd->gid : decltype(uv_passwd_t::gid){}); +} + + +UVW_INLINE std::string Passwd::shell() const noexcept { + return ((passwd && passwd->shell) ? passwd->shell : ""); +} + + +UVW_INLINE std::string Passwd::homedir() const noexcept { + return ((passwd && passwd->homedir) ? passwd->homedir: ""); +} + + +UVW_INLINE Passwd::operator bool() const noexcept { + return static_cast(passwd); +} + + +UVW_INLINE UtsName::UtsName(std::shared_ptr utsname) + : utsname{utsname} +{} + + +UVW_INLINE std::string UtsName::sysname() const noexcept { + return utsname ? utsname->sysname : ""; +} + + +UVW_INLINE std::string UtsName::release() const noexcept { + return utsname ? utsname->release : ""; +} + + +UVW_INLINE std::string UtsName::version() const noexcept { + return utsname ? utsname->version : ""; +} + + +UVW_INLINE std::string UtsName::machine() const noexcept { + return utsname ? utsname->machine : ""; +} + + +UVW_INLINE PidType Utilities::OS::pid() noexcept { + return uv_os_getpid(); +} + + +UVW_INLINE PidType Utilities::OS::parent() noexcept { + return uv_os_getppid(); +} + + +UVW_INLINE std::string Utilities::OS::homedir() noexcept { + return details::tryRead(&uv_os_homedir); +} + + +UVW_INLINE std::string Utilities::OS::tmpdir() noexcept { + return details::tryRead(&uv_os_tmpdir); +} + + +UVW_INLINE std::string Utilities::OS::env(const std::string &name) noexcept { + return details::tryRead(&uv_os_getenv, name.c_str()); +} + + +UVW_INLINE bool Utilities::OS::env(const std::string &name, const std::string &value) noexcept { + return (0 == (value.empty() ? uv_os_unsetenv(name.c_str()) : uv_os_setenv(name.c_str(), value.c_str()))); +} + + +UVW_INLINE std::string Utilities::OS::hostname() noexcept { + return details::tryRead(&uv_os_gethostname); +} + + +UVW_INLINE UtsName Utilities::OS::uname() noexcept { + auto ptr = std::make_shared(); + uv_os_uname(ptr.get()); + return ptr; +} + + +UVW_INLINE Passwd Utilities::OS::passwd() noexcept { + auto deleter = [](uv_passwd_t *passwd){ + uv_os_free_passwd(passwd); + delete passwd; + }; + + std::shared_ptr ptr{new uv_passwd_t, std::move(deleter)}; + uv_os_get_passwd(ptr.get()); + return ptr; +} + + +UVW_INLINE int Utilities::osPriority(PidType pid) { + int prio = 0; + + if(uv_os_getpriority(pid, &prio)) { + prio = UV_PRIORITY_LOW + 1; + } + + return prio; +} + + +UVW_INLINE bool Utilities::osPriority(PidType pid, int prio) { + return 0 == uv_os_setpriority(pid, prio); +} + + +UVW_INLINE HandleType Utilities::guessHandle(HandleCategory category) noexcept { + switch(category) { + 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; + } +} + + +UVW_INLINE HandleType Utilities::guessHandle(FileHandle file) noexcept { + HandleCategory category = uv_guess_handle(file); + return guessHandle(category); +} + + +UVW_INLINE std::vector Utilities::cpuInfo() noexcept { + std::vector cpuinfos; + + uv_cpu_info_t *infos; + int count; + + if(0 == uv_cpu_info(&infos, &count)) { + std::for_each(infos, infos+count, [&cpuinfos](const auto &info) { + cpuinfos.push_back({ info.model, info.speed, info.cpu_times }); + }); + + uv_free_cpu_info(infos, count); + } + + return cpuinfos; +} + + +UVW_INLINE std::vector Utilities::interfaceAddresses() noexcept { + std::vector interfaces; + + uv_interface_address_t *ifaces{nullptr}; + int count{0}; + + if(0 == uv_interface_addresses(&ifaces, &count)) { + std::for_each(ifaces, ifaces+count, [&interfaces](const auto &iface) { + InterfaceAddress interfaceAddress; + + interfaceAddress.name = iface.name; + std::copy(iface.phys_addr, (iface.phys_addr+6), interfaceAddress.physical); + interfaceAddress.internal = iface.is_internal == 0 ? false : true; + + if(iface.address.address4.sin_family == AF_INET) { + interfaceAddress.address = details::address(&iface.address.address4); + interfaceAddress.netmask = details::address(&iface.netmask.netmask4); + } else if(iface.address.address4.sin_family == AF_INET6) { + interfaceAddress.address = details::address(&iface.address.address6); + interfaceAddress.netmask = details::address(&iface.netmask.netmask6); + } + + interfaces.push_back(std::move(interfaceAddress)); + }); + + uv_free_interface_addresses(ifaces, count); + } + + return interfaces; +} + + +UVW_INLINE std::string Utilities::indexToName(unsigned int index) noexcept { + return details::tryRead(&uv_if_indextoname, index); +} + + +UVW_INLINE std::string Utilities::indexToIid(unsigned int index) noexcept { + return details::tryRead(&uv_if_indextoiid, index); +} + + +UVW_INLINE bool Utilities::replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept { + return (0 == uv_replace_allocator(mallocFunc, reallocFunc, callocFunc, freeFunc)); +} + + +UVW_INLINE std::array Utilities::loadAverage() noexcept { + std::array avg; + uv_loadavg(avg.data()); + return avg; +} + + +UVW_INLINE char ** Utilities::setupArgs(int argc, char** argv) { + return uv_setup_args(argc, argv); +} + + +UVW_INLINE std::string Utilities::processTitle() { + std::size_t size = details::DEFAULT_SIZE; + char buf[details::DEFAULT_SIZE]; + std::string str{}; + + if(0 == uv_get_process_title(buf, size)) { + str.assign(buf, size); + } + + return str; +} + + +UVW_INLINE bool Utilities::processTitle(std::string title) { + return (0 == uv_set_process_title(title.c_str())); +} + + +UVW_INLINE uint64_t Utilities::totalMemory() noexcept { + return uv_get_total_memory(); +} + + +UVW_INLINE uint64_t Utilities::constrainedMemory() noexcept { + return uv_get_constrained_memory(); +} + + +UVW_INLINE double Utilities::uptime() noexcept { + double ret; + + if(0 != uv_uptime(&ret)) { + ret = 0; + } + + return ret; +} + + +UVW_INLINE RUsage Utilities::rusage() noexcept { + RUsage ru; + auto err = uv_getrusage(&ru); + return err ? RUsage{} : ru; +} + + +UVW_INLINE uint64_t Utilities::hrtime() noexcept { + return uv_hrtime(); +} + + +UVW_INLINE std::string Utilities::path() noexcept { + return details::tryRead(&uv_exepath); +} + + +UVW_INLINE std::string Utilities::cwd() noexcept { + return details::tryRead(&uv_cwd); +} + + +UVW_INLINE bool Utilities::chdir(const std::string &dir) noexcept { + return (0 == uv_chdir(dir.data())); +} + + +UVW_INLINE TimeVal64 Utilities::timeOfDay() noexcept { + uv_timeval64_t ret; + uv_gettimeofday(&ret); + return ret; +} + + +UVW_INLINE void Utilities::sleep(unsigned int msec) noexcept { + uv_sleep(msec); +} + + +} diff --git a/src/uvw/util.hpp b/src/uvw/util.h similarity index 74% rename from src/uvw/util.hpp rename to src/uvw/util.h index 6c9a3466..596aa1f9 100644 --- a/src/uvw/util.hpp +++ b/src/uvw/util.h @@ -3,8 +3,6 @@ #include #include -#include -#include #include #include #include @@ -216,55 +214,43 @@ using RUsage = uv_rusage_t; /*!< Library equivalent for uv_rusage_t. */ * \sa Utilities::passwd */ struct Passwd { - Passwd(std::shared_ptr pwd): passwd{pwd} {} + Passwd(std::shared_ptr pwd); /** * @brief Gets the username. * @return The username of the current effective uid (not the real uid). */ - std::string username() const noexcept { - return ((passwd && passwd->username) ? passwd->username : ""); - } + std::string username() const noexcept; /** * @brief Gets the uid. * @return The current effective uid (not the real uid). */ - auto uid() const noexcept { - return (passwd ? passwd->uid : decltype(uv_passwd_t::uid){}); - } + auto uid() const noexcept; /** * @brief Gets the gid. * @return The gid of the current effective uid (not the real uid). */ - auto gid() const noexcept { - return (passwd ? passwd->gid : decltype(uv_passwd_t::gid){}); - } + auto gid() const noexcept; /** * @brief Gets the shell. * @return The shell of the current effective uid (not the real uid). */ - std::string shell() const noexcept { - return ((passwd && passwd->shell) ? passwd->shell : ""); - } + std::string shell() const noexcept; /** * @brief Gets the homedir. * @return The homedir of the current effective uid (not the real uid). */ - std::string homedir() const noexcept { - return ((passwd && passwd->homedir) ? passwd->homedir: ""); - } + std::string homedir() const noexcept; /** * @brief Checks if the instance contains valid data. * @return True if data are all valid, false otherwise. */ - operator bool() const noexcept { - return static_cast(passwd); - } + operator bool() const noexcept; private: std::shared_ptr passwd; @@ -281,39 +267,31 @@ private: * \sa Utilities::uname */ struct UtsName { - UtsName(std::shared_ptr utsname): utsname{utsname} {} + UtsName(std::shared_ptr utsname); /** * @brief Gets the operating system name (like "Linux"). * @return The operating system name. */ - std::string sysname() const noexcept { - return utsname ? utsname->sysname : ""; - } + std::string sysname() const noexcept; /** * @brief Gets the operating system release (like "2.6.28"). * @return The operating system release. */ - std::string release() const noexcept { - return utsname ? utsname->release : ""; - } + std::string release() const noexcept; /** * @brief Gets the operating system version. * @return The operating system version */ - std::string version() const noexcept { - return utsname ? utsname->version : ""; - } + std::string version() const noexcept; /** * @brief Gets the hardware identifier. * @return The hardware identifier. */ - std::string machine() const noexcept { - return utsname ? utsname->machine : ""; - } + std::string machine() const noexcept; private: std::shared_ptr utsname; @@ -490,9 +468,7 @@ struct Utilities { * * @return The current process id. */ - static PidType pid() noexcept { - return uv_os_getpid(); - } + static PidType pid() noexcept; /** * @brief Returns the parent process id. @@ -503,9 +479,7 @@ struct Utilities { * * @return The parent process id. */ - static PidType parent() noexcept { - return uv_os_getppid(); - } + static PidType parent() noexcept; /** * @brief Gets the current user's home directory. @@ -517,9 +491,7 @@ struct Utilities { * @return The current user's home directory, an empty string in case of * errors. */ - static std::string homedir() noexcept { - return details::tryRead(&uv_os_homedir); - } + static std::string homedir() noexcept; /** * @brief Gets the temp directory. @@ -530,9 +502,7 @@ struct Utilities { * * @return The temp directory, an empty string in case of errors. */ - static std::string tmpdir() noexcept { - return details::tryRead(&uv_os_tmpdir); - } + static std::string tmpdir() noexcept; /** * @brief Retrieves an environment variable. @@ -540,9 +510,7 @@ struct Utilities { * @return The value of the environment variable, an empty string in * case of errors. */ - static std::string env(const std::string &name) noexcept { - return details::tryRead(&uv_os_getenv, name.c_str()); - } + static std::string env(const std::string &name) noexcept; /** * @brief Creates, updates or deletes an environment variable. @@ -551,9 +519,7 @@ struct Utilities { * to unset it). * @return True in case of success, false otherwise. */ - static bool env(const std::string &name, const std::string &value) noexcept { - return (0 == (value.empty() ? uv_os_unsetenv(name.c_str()) : uv_os_setenv(name.c_str(), value.c_str()))); - } + static bool env(const std::string &name, const std::string &value) noexcept; /** * @brief Retrieves all environment variables and iterates them. @@ -591,9 +557,7 @@ struct Utilities { * @brief Returns the hostname. * @return The hostname, an empty string in case of errors. */ - static std::string hostname() noexcept { - return details::tryRead(&uv_os_gethostname); - } + static std::string hostname() noexcept; /** * @brief Gets name and information about the current kernel. @@ -604,11 +568,7 @@ struct Utilities { * * @return Name and information about the current kernel. */ - static UtsName uname() noexcept { - auto ptr = std::make_shared(); - uv_os_uname(ptr.get()); - return ptr; - } + static UtsName uname() noexcept; /** * @brief Gets a subset of the password file entry. @@ -622,16 +582,7 @@ struct Utilities { * * @return The accessible subset of the password file entry. */ - static Passwd passwd() noexcept { - auto deleter = [](uv_passwd_t *passwd){ - uv_os_free_passwd(passwd); - delete passwd; - }; - - std::shared_ptr ptr{new uv_passwd_t, std::move(deleter)}; - uv_os_get_passwd(ptr.get()); - return ptr; - } + static Passwd passwd() noexcept; }; /** @@ -647,15 +598,7 @@ struct Utilities { * @param pid A valid process id. * @return The scheduling priority of the process. */ - static int osPriority(PidType pid) { - int prio = 0; - - if(uv_os_getpriority(pid, &prio)) { - prio = UV_PRIORITY_LOW + 1; - } - - return prio; - } + static int osPriority(PidType pid); /** * @brief Sets the scheduling priority of a process. @@ -672,55 +615,14 @@ struct Utilities { * @param prio The scheduling priority to set to the process. * @return True in case of success, false otherwise. */ - static bool osPriority(PidType pid, int prio) { - return 0 == uv_os_setpriority(pid, prio); - } + static bool osPriority(PidType pid, int prio); /** * @brief Gets the type of the handle given a category. * @param category A properly initialized handle category. * @return The actual type of the handle as defined by HandleType */ - static HandleType guessHandle(HandleCategory category) noexcept { - switch(category) { - 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; - } - } + static HandleType guessHandle(HandleCategory category) noexcept; /** * @brief Gets the type of the stream to be used with the given descriptor. @@ -740,10 +642,7 @@ struct Utilities { * * `HandleType::UDP` * * `HandleType::FILE` */ - static HandleType guessHandle(FileHandle file) noexcept { - HandleCategory category = uv_guess_handle(file); - return guessHandle(category); - } + static HandleType guessHandle(FileHandle file) noexcept; /** @brief Gets information about the CPUs on the system. @@ -753,22 +652,7 @@ struct Utilities { * * @return A set of descriptors of all the available CPUs. */ - static std::vector cpuInfo() noexcept { - std::vector cpuinfos; - - uv_cpu_info_t *infos; - int count; - - if(0 == uv_cpu_info(&infos, &count)) { - std::for_each(infos, infos+count, [&cpuinfos](const auto &info) { - cpuinfos.push_back({ info.model, info.speed, info.cpu_times }); - }); - - uv_free_cpu_info(infos, count); - } - - return cpuinfos; - } + static std::vector cpuInfo() noexcept; /** * @brief Gets a set of descriptors of all the available interfaces. @@ -778,36 +662,7 @@ struct Utilities { * * @return A set of descriptors of all the available interfaces. */ - static std::vector interfaceAddresses() noexcept { - std::vector interfaces; - - uv_interface_address_t *ifaces{nullptr}; - int count{0}; - - if(0 == uv_interface_addresses(&ifaces, &count)) { - std::for_each(ifaces, ifaces+count, [&interfaces](const auto &iface) { - InterfaceAddress interfaceAddress; - - interfaceAddress.name = iface.name; - std::copy(iface.phys_addr, (iface.phys_addr+6), interfaceAddress.physical); - interfaceAddress.internal = iface.is_internal == 0 ? false : true; - - if(iface.address.address4.sin_family == AF_INET) { - interfaceAddress.address = details::address(&iface.address.address4); - interfaceAddress.netmask = details::address(&iface.netmask.netmask4); - } else if(iface.address.address4.sin_family == AF_INET6) { - interfaceAddress.address = details::address(&iface.address.address6); - interfaceAddress.netmask = details::address(&iface.netmask.netmask6); - } - - interfaces.push_back(std::move(interfaceAddress)); - }); - - uv_free_interface_addresses(ifaces, count); - } - - return interfaces; - } + static std::vector interfaceAddresses() noexcept; /** * @brief IPv6-capable implementation of @@ -822,9 +677,7 @@ struct Utilities { * @param index Network interface index. * @return Network interface name. */ - static std::string indexToName(unsigned int index) noexcept { - return details::tryRead(&uv_if_indextoname, index); - } + static std::string indexToName(unsigned int index) noexcept; /** * @brief Retrieves a network interface identifier. @@ -836,9 +689,7 @@ struct Utilities { * @param index Network interface index. * @return Network interface identifier. */ - static std::string indexToIid(unsigned int index) noexcept { - return details::tryRead(&uv_if_indextoiid, index); - } + static std::string indexToIid(unsigned int index) noexcept; /** * @brief Override the use of some standard library’s functions. @@ -863,19 +714,13 @@ struct Utilities { * @param freeFunc Replacement function for _free_. * @return True in case of success, false otherwise. */ - static bool replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept { - return (0 == uv_replace_allocator(mallocFunc, reallocFunc, callocFunc, freeFunc)); - } + static bool replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept; /** * @brief Gets the load average. * @return `[0,0,0]` on Windows (not available), the load average otherwise. */ - static std::array loadAverage() noexcept { - std::array avg; - uv_loadavg(avg.data()); - return avg; - } + static std::array loadAverage() noexcept; /** * @brief Store the program arguments. @@ -884,42 +729,26 @@ struct Utilities { * * @return Arguments that haven't been consumed internally. */ - static char ** setupArgs(int argc, char** argv) { - return uv_setup_args(argc, argv); - } + static char ** setupArgs(int argc, char** argv); /** * @brief Gets the title of the current process. * @return The process title. */ - static std::string processTitle() { - std::size_t size = details::DEFAULT_SIZE; - char buf[details::DEFAULT_SIZE]; - std::string str{}; - - if(0 == uv_get_process_title(buf, size)) { - str.assign(buf, size); - } - - return str; - } + static std::string processTitle(); /** * @brief Sets the current process title. * @param title The process title to be set. * @return True in case of success, false otherwise. */ - static bool processTitle(std::string title) { - return (0 == uv_set_process_title(title.c_str())); - } + static bool processTitle(std::string title); /** * @brief Gets memory information (in bytes). * @return Memory information. */ - static uint64_t totalMemory() noexcept { - return uv_get_total_memory(); - } + static uint64_t totalMemory() noexcept; /** * @brief Gets the amount of memory available to the process (in bytes). @@ -932,33 +761,19 @@ struct Utilities { * * @return Amount of memory available to the process. */ - static uint64_t constrainedMemory() noexcept { - return uv_get_constrained_memory(); - } + static uint64_t constrainedMemory() noexcept; /** * @brief Gets the current system uptime. * @return The current system uptime or 0 in case of errors. */ - static double uptime() noexcept { - double ret; - - if(0 != uv_uptime(&ret)) { - ret = 0; - } - - return ret; - } + static double uptime() noexcept; /** * @brief Gets the resource usage measures for the current process. * @return Resource usage measures, zeroes-filled object in case of errors. */ - static RUsage rusage() noexcept { - RUsage ru; - auto err = uv_getrusage(&ru); - return err ? RUsage{} : ru; - } + static RUsage rusage() noexcept; /** * @brief Gets the current high-resolution real time. @@ -970,54 +785,45 @@ struct Utilities { * * @return The current high-resolution real time. */ - static uint64_t hrtime() noexcept { - return uv_hrtime(); - } + static uint64_t hrtime() noexcept; /** * @brief Gets the executable path. * @return The executable path, an empty string in case of errors. */ - static std::string path() noexcept { - return details::tryRead(&uv_exepath); - } + static std::string path() noexcept; /** * @brief Gets the current working directory. * @return The current working directory, an empty string in case of errors. */ - static std::string cwd() noexcept { - return details::tryRead(&uv_cwd); - } + static std::string cwd() noexcept; /** * @brief Changes the current working directory. * @param dir The working directory to be set. * @return True in case of success, false otherwise. */ - static bool chdir(const std::string &dir) noexcept { - return (0 == uv_chdir(dir.data())); - } + static bool chdir(const std::string &dir) noexcept; /** * @brief Cross-platform implementation of * [`gettimeofday`](https://linux.die.net/man/2/gettimeofday) * @return The current time. */ - static TimeVal64 timeOfDay() noexcept { - uv_timeval64_t ret; - uv_gettimeofday(&ret); - return ret; - } + static TimeVal64 timeOfDay() noexcept; /** * @brief Causes the calling thread to sleep for a while. * @param msec Number of milliseconds to sleep. */ - static void sleep(unsigned int msec) noexcept { - uv_sleep(msec); - } + static void sleep(unsigned int msec) noexcept; }; } + + +#ifndef UVW_AS_LIB +#include "util.cpp" +#endif