wip (see #174)
This commit is contained in:
parent
0d7cd09e95
commit
f590b9d03e
@ -6,7 +6,7 @@
|
||||
#include <string>
|
||||
#include <uv.h>
|
||||
#include "request.hpp"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
#include "loop.h"
|
||||
|
||||
|
||||
|
||||
@ -314,3 +314,8 @@ private:
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#ifndef UVW_AS_LIB
|
||||
#include "emitter.cpp"
|
||||
#endif
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
#include <chrono>
|
||||
#include <uv.h>
|
||||
#include "request.hpp"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
#include "loop.h"
|
||||
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include <string>
|
||||
#include <uv.h>
|
||||
#include "handle.hpp"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
#include "loop.h"
|
||||
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include <chrono>
|
||||
#include <uv.h>
|
||||
#include "handle.hpp"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
#include "loop.h"
|
||||
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
#include <memory>
|
||||
#include <uv.h>
|
||||
#include "resource.hpp"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
#include <chrono>
|
||||
#include <uv.h>
|
||||
#include "emitter.h"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
#include <uv.h>
|
||||
#include "request.hpp"
|
||||
#include "stream.h"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
#include "loop.h"
|
||||
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include <memory>
|
||||
#include <uv.h>
|
||||
#include "handle.hpp"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#include <uv.h>
|
||||
#include "handle.hpp"
|
||||
#include "stream.h"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
#include "loop.h"
|
||||
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
#include <uv.h>
|
||||
#include "request.hpp"
|
||||
#include "stream.h"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include <memory>
|
||||
#include <uv.h>
|
||||
#include "stream.h"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
#include <uv.h>
|
||||
#include "request.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
342
src/uvw/util.cpp
Normal file
342
src/uvw/util.cpp
Normal file
@ -0,0 +1,342 @@
|
||||
#include <algorithm>
|
||||
#include "util.h"
|
||||
#include "config.h"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
|
||||
UVW_INLINE Passwd::Passwd(std::shared_ptr<uv_passwd_t> 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<bool>(passwd);
|
||||
}
|
||||
|
||||
|
||||
UVW_INLINE UtsName::UtsName(std::shared_ptr<uv_utsname_t> 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_utsname_t>();
|
||||
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<uv_passwd_t> 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<CPUInfo> Utilities::cpuInfo() noexcept {
|
||||
std::vector<CPUInfo> 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<InterfaceAddress> Utilities::interfaceAddresses() noexcept {
|
||||
std::vector<InterfaceAddress> 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<IPv4>(&iface.address.address4);
|
||||
interfaceAddress.netmask = details::address<IPv4>(&iface.netmask.netmask4);
|
||||
} else if(iface.address.address4.sin_family == AF_INET6) {
|
||||
interfaceAddress.address = details::address<IPv6>(&iface.address.address6);
|
||||
interfaceAddress.netmask = details::address<IPv6>(&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<double, 3> Utilities::loadAverage() noexcept {
|
||||
std::array<double, 3> 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -3,8 +3,6 @@
|
||||
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
@ -216,55 +214,43 @@ using RUsage = uv_rusage_t; /*!< Library equivalent for uv_rusage_t. */
|
||||
* \sa Utilities::passwd
|
||||
*/
|
||||
struct Passwd {
|
||||
Passwd(std::shared_ptr<uv_passwd_t> pwd): passwd{pwd} {}
|
||||
Passwd(std::shared_ptr<uv_passwd_t> 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<bool>(passwd);
|
||||
}
|
||||
operator bool() const noexcept;
|
||||
|
||||
private:
|
||||
std::shared_ptr<uv_passwd_t> passwd;
|
||||
@ -281,39 +267,31 @@ private:
|
||||
* \sa Utilities::uname
|
||||
*/
|
||||
struct UtsName {
|
||||
UtsName(std::shared_ptr<uv_utsname_t> utsname): utsname{utsname} {}
|
||||
UtsName(std::shared_ptr<uv_utsname_t> 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<uv_utsname_t> 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_utsname_t>();
|
||||
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<uv_passwd_t> 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> cpuInfo() noexcept {
|
||||
std::vector<CPUInfo> 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> 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<InterfaceAddress> interfaceAddresses() noexcept {
|
||||
std::vector<InterfaceAddress> 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<IPv4>(&iface.address.address4);
|
||||
interfaceAddress.netmask = details::address<IPv4>(&iface.netmask.netmask4);
|
||||
} else if(iface.address.address4.sin_family == AF_INET6) {
|
||||
interfaceAddress.address = details::address<IPv6>(&iface.address.address6);
|
||||
interfaceAddress.netmask = details::address<IPv6>(&iface.netmask.netmask6);
|
||||
}
|
||||
|
||||
interfaces.push_back(std::move(interfaceAddress));
|
||||
});
|
||||
|
||||
uv_free_interface_addresses(ifaces, count);
|
||||
}
|
||||
|
||||
return interfaces;
|
||||
}
|
||||
static std::vector<InterfaceAddress> 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<double, 3> loadAverage() noexcept {
|
||||
std::array<double, 3> avg;
|
||||
uv_loadavg(avg.data());
|
||||
return avg;
|
||||
}
|
||||
static std::array<double, 3> 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
|
||||
Loading…
Reference in New Issue
Block a user