17 #define CONSTEXPR_SPECIFIER 19 #define CONSTEXPR_SPECIFIER constexpr 29 enum class UVHandleType: std::underlying_type_t<uv_handle_type> {
30 UNKNOWN = UV_UNKNOWN_HANDLE,
33 FS_EVENT = UV_FS_EVENT,
52 struct UVTypeWrapper {
55 constexpr UVTypeWrapper(Type val): value{val} {}
56 constexpr
operator Type() const noexcept {
return value; }
78 using InnerType = std::underlying_type_t<E>;
80 constexpr InnerType toInnerType(E flag)
const noexcept {
return static_cast<InnerType
>(flag); }
83 using Type = InnerType;
92 int _[] = { 0, (flags = flags | V, 0)... };
93 return void(_), flags;
100 constexpr
Flags(E flag) noexcept: flags{toInnerType(flag)} {}
107 constexpr
Flags(Type f): flags{f} {}
114 constexpr
Flags(
const Flags &f) noexcept: flags{f.flags} { }
115 constexpr
Flags(
Flags &&f) noexcept: flags{std::move(f.flags)} { }
117 ~
Flags() noexcept { static_assert(std::is_enum<E>::value,
"!"); }
119 CONSTEXPR_SPECIFIER
Flags & operator=(
const Flags &f) noexcept {
124 CONSTEXPR_SPECIFIER
Flags & operator=(
Flags &&f) noexcept {
125 flags = std::move(f.flags);
161 explicit constexpr
operator bool() const noexcept {
return !(flags == InnerType{}); }
167 constexpr
operator Type() const noexcept {
return flags; }
183 using HandleType = details::UVHandleType;
212 Passwd(std::shared_ptr<uv_passwd_t> pwd): passwd{pwd} {}
219 return ((passwd && passwd->username) ? passwd->username :
"");
226 auto uid() const noexcept {
227 return (passwd ? passwd->uid : decltype(uv_passwd_t::uid){});
234 auto gid() const noexcept {
235 return (passwd ? passwd->gid : decltype(uv_passwd_t::gid){});
242 std::string
shell() const noexcept {
243 return ((passwd && passwd->shell) ? passwd->shell :
"");
251 return ((passwd && passwd->homedir) ? passwd->homedir:
"");
258 operator bool() const noexcept {
259 return static_cast<bool>(passwd);
263 std::shared_ptr<uv_passwd_t> passwd;
296 using CPUTime = decltype(uv_cpu_info_t::cpu_times);
326 static constexpr std::size_t DEFAULT_SIZE = 128;
334 struct IpTraits<IPv4> {
335 using Type = sockaddr_in;
336 using AddrFuncType = int(*)(
const char *, int, Type *);
337 using NameFuncType = int(*)(
const Type *,
char *, std::size_t);
338 static constexpr AddrFuncType addrFunc = &uv_ip4_addr;
339 static constexpr NameFuncType nameFunc = &uv_ip4_name;
340 static constexpr
auto sinPort(
const Type *addr) {
return addr->sin_port; }
345 struct IpTraits<IPv6> {
346 using Type = sockaddr_in6;
347 using AddrFuncType = int(*)(
const char *, int, Type *);
348 using NameFuncType = int(*)(
const Type *,
char *, std::size_t);
349 static constexpr AddrFuncType addrFunc = &uv_ip6_addr;
350 static constexpr NameFuncType nameFunc = &uv_ip6_name;
351 static constexpr
auto sinPort(
const Type *addr) {
return addr->sin6_port; }
356 Addr address(
const typename details::IpTraits<I>::Type *aptr) noexcept {
358 char name[DEFAULT_SIZE];
360 int err = details::IpTraits<I>::nameFunc(aptr, name, DEFAULT_SIZE);
363 addr.
port = ntohs(details::IpTraits<I>::sinPort(aptr));
364 addr.
ip = std::string{name};
371 template<
typename I,
typename F,
typename H>
372 Addr address(F &&f,
const H *handle) noexcept {
373 sockaddr_storage ssto;
374 int len =
sizeof(ssto);
377 int err = std::forward<F>(f)(handle, reinterpret_cast<sockaddr *>(&ssto), &len);
380 typename IpTraits<I>::Type *aptr =
reinterpret_cast<typename IpTraits<I>::Type *
>(&ssto);
381 addr = address<I>(aptr);
388 template<
typename F,
typename... Args>
389 std::string tryRead(F &&f, Args&&... args) noexcept {
390 std::size_t size = DEFAULT_SIZE;
391 char buf[DEFAULT_SIZE];
393 auto err = std::forward<F>(f)(args..., buf, &size);
395 if(UV_ENOBUFS == err) {
396 std::unique_ptr<char[]> data{
new char[size]};
397 err = std::forward<F>(f)(args..., data.get(), &size);
402 }
else if(0 == err) {
403 str.assign(buf, size);
419 using MallocFuncType =
void*(*)(size_t);
420 using ReallocFuncType =
void*(*)(
void*, size_t);
421 using CallocFuncType =
void*(*)(size_t, size_t);
422 using FreeFuncType = void(*)(
void*);
439 return details::tryRead(&uv_os_homedir);
452 return details::tryRead(&uv_os_tmpdir);
461 static std::string
env(
const std::string &name) noexcept {
462 return details::tryRead(&uv_os_getenv, name.c_str());
472 static bool env(
const std::string &name,
const std::string &value) noexcept {
473 return (0 == (value.empty() ? uv_os_unsetenv(name.c_str()) : uv_os_setenv(name.c_str(), value.c_str())));
481 return details::tryRead(&uv_os_gethostname);
497 auto deleter = [](uv_passwd_t *passwd){
498 uv_os_free_passwd(passwd);
502 std::shared_ptr<uv_passwd_t> ptr{
new uv_passwd_t, std::move(deleter)};
503 uv_os_get_passwd(ptr.get());
516 return HandleType::ASYNC;
518 return HandleType::CHECK;
520 return HandleType::FS_EVENT;
522 return HandleType::FS_POLL;
524 return HandleType::HANDLE;
526 return HandleType::IDLE;
528 return HandleType::PIPE;
530 return HandleType::POLL;
532 return HandleType::PREPARE;
534 return HandleType::PROCESS;
536 return HandleType::STREAM;
538 return HandleType::TCP;
540 return HandleType::TIMER;
542 return HandleType::TTY;
544 return HandleType::UDP;
546 return HandleType::SIGNAL;
548 return HandleType::FILE;
550 return HandleType::UNKNOWN;
574 return guessHandle(category);
585 static std::vector<CPUInfo>
cpuInfo() noexcept {
586 std::vector<CPUInfo> cpuinfos;
588 uv_cpu_info_t *infos;
591 if(0 == uv_cpu_info(&infos, &count)) {
592 std::for_each(infos, infos+count, [&cpuinfos](
const auto &info) {
593 cpuinfos.push_back({ info.model, info.speed, info.cpu_times });
596 uv_free_cpu_info(infos, count);
612 std::vector<InterfaceAddress> interfaces;
614 uv_interface_address_t *ifaces{
nullptr};
617 if(0 == uv_interface_addresses(&ifaces, &count)) {
618 std::for_each(ifaces, ifaces+count, [&interfaces](
const auto &iface) {
621 interfaceAddress.
name = iface.name;
622 std::copy(iface.phys_addr, (iface.phys_addr+6), interfaceAddress.
physical);
623 interfaceAddress.
internal = iface.is_internal == 0 ? false :
true;
625 if(iface.address.address4.sin_family == AF_INET) {
626 interfaceAddress.
address = details::address<IPv4>(&iface.address.address4);
627 interfaceAddress.
netmask = details::address<IPv4>(&iface.netmask.netmask4);
628 }
else if(iface.address.address4.sin_family == AF_INET6) {
629 interfaceAddress.
address = details::address<IPv6>(&iface.address.address6);
630 interfaceAddress.
netmask = details::address<IPv6>(&iface.netmask.netmask6);
633 interfaces.push_back(std::move(interfaceAddress));
636 uv_free_interface_addresses(ifaces, count);
665 static bool replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept {
666 return (0 == uv_replace_allocator(mallocFunc, reallocFunc, callocFunc, freeFunc));
674 std::array<double, 3> avg;
675 uv_loadavg(avg.data());
687 return uv_setup_args(argc, argv);
695 std::size_t size = details::DEFAULT_SIZE;
696 char buf[details::DEFAULT_SIZE];
699 if(0 == uv_get_process_title(buf, size)) {
700 str.assign(buf, size);
712 return (0 == uv_set_process_title(title.c_str()));
720 return uv_get_total_memory();
730 if(0 != uv_uptime(&ret)) {
743 auto err = uv_getrusage(&ru);
744 return err ?
RUsage{} : ru;
765 static std::string
path() noexcept {
766 return details::tryRead(&uv_exepath);
773 static std::string
cwd() noexcept {
774 return details::tryRead(&uv_cwd);
782 static bool chdir(
const std::string &dir) noexcept {
783 return (0 == uv_chdir(dir.data()));
constexpr FileHandle StdERR
auto gid() const noexcept
Gets the gid.
static Passwd passwd() noexcept
Gets a subset of the password file entry.
static HandleType guessHandle(FileHandle file) noexcept
Gets the type of the stream to be used with the given descriptor.
static std::string hostname() noexcept
Returns the hostname.
static std::string env(const std::string &name) noexcept
Retrieves an environment variable.
static std::vector< InterfaceAddress > interfaceAddresses() noexcept
Gets a set of descriptors of all the available interfaces.
static double uptime() noexcept
Gets the current system uptime.
details::UVTypeWrapper< uv_file > FileHandle
static std::string tmpdir() noexcept
Gets the temp directory.
static std::string homedir() noexcept
Gets the current user's home directory.
Windows size representation.
details::UVTypeWrapper< uv_os_fd_t > OSFileDescriptor
constexpr FileHandle StdIN
static char ** setupArgs(int argc, char **argv)
Store the program arguments.
std::string username() const noexcept
Gets the username.
constexpr FileHandle StdOUT
Utility class to handle flags.
constexpr Flags operator &(const Flags &f) const noexcept
And operator.
constexpr Flags(E flag) noexcept
Constructs a Flags object from a value of the enum E.
constexpr Flags operator|(const Flags &f) const noexcept
Or operator.
static uint64_t hrtime() noexcept
Gets the current high-resolution real time.
static bool replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept
Override the use of some standard library’s functions.
details::UVTypeWrapper< uv_handle_type > HandleCategory
static bool env(const std::string &name, const std::string &value) noexcept
Creates, updates or deletes an environment variable.
static RUsage rusage() noexcept
Gets the resource usage measures for the current process.
static uint64_t totalMemory() noexcept
Gets memory information (in bytes).
auto uid() const noexcept
Gets the uid.
std::string shell() const noexcept
Gets the shell.
static std::string cwd() noexcept
Gets the current working directory.
static std::string path() noexcept
Gets the executable path.
details::UVTypeWrapper< uv_os_sock_t > OSSocketHandle
constexpr Flags()
Constructs an uninitialized Flags object.
static HandleType guessHandle(HandleCategory category) noexcept
Gets the type of the handle given a category.
static CONSTEXPR_SPECIFIER Flags< E > from()
Utility factory method to pack a set of values all at once.
constexpr Flags(Type f)
Constructs a Flags object from an instance of the underlying type of the enum E.
constexpr Flags operator|(E flag) const noexcept
Or operator.
static std::vector< CPUInfo > cpuInfo() noexcept
Gets information about the CPUs on the system.
static bool chdir(const std::string &dir) noexcept
Changes the current working directory.
constexpr Flags operator &(E flag) const noexcept
And operator.
static std::array< double, 3 > loadAverage() noexcept
Gets the load average.
static bool processTitle(std::string title)
Sets the current process title.
static std::string processTitle()
Gets the title of the current process.
std::string homedir() const noexcept
Gets the homedir.