1#ifndef UVW_UTIL_INCLUDE_H
2#define UVW_UTIL_INCLUDE_H
19enum class UVHandleType : std::underlying_type_t<uv_handle_type> {
20 UNKNOWN = UV_UNKNOWN_HANDLE,
23 FS_EVENT = UV_FS_EVENT,
44 constexpr UVTypeWrapper()
47 constexpr UVTypeWrapper(Type val)
50 constexpr operator Type() const noexcept {
54 bool operator==(UVTypeWrapper other)
const noexcept {
55 return value == other.value;
63bool operator==(UVTypeWrapper<T> lhs, UVTypeWrapper<T> rhs) {
81 static_assert(std::is_enum_v<E>);
83 using InnerType = std::underlying_type_t<E>;
85 constexpr InnerType toInnerType(E flag)
const noexcept {
86 return static_cast<InnerType
>(flag);
90 using Type = InnerType;
106 : flags{toInnerType(flag)} {}
126 : flags{std::move(f.flags)} {}
128 constexpr Flags &operator=(
const Flags &f)
noexcept {
133 constexpr Flags &operator=(
Flags &&f)
noexcept {
134 flags = std::move(f.flags);
144 return Flags{flags | f.flags};
153 return Flags{flags | toInnerType(flag)};
162 return Flags{flags & f.flags};
171 return Flags{flags & toInnerType(flag)};
178 explicit constexpr operator bool() const noexcept {
179 return !(flags == InnerType{});
186 constexpr operator Type() const noexcept {
208using PidType = details::UVTypeWrapper<uv_pid_t>;
233 Passwd(std::shared_ptr<uv_passwd_t> pwd);
245 decltype(uv_passwd_t::
uid)
uid() const noexcept;
251 decltype(uv_passwd_t::
gid)
gid() const noexcept;
269 operator
bool() const noexcept;
272 std::shared_ptr<uv_passwd_t> passwd;
285 UtsName(std::shared_ptr<uv_utsname_t> utsname);
312 std::shared_ptr<uv_utsname_t> utsname;
341 using CPUTime =
decltype(uv_cpu_info_t::cpu_times);
368static constexpr std::size_t DEFAULT_SIZE = 128;
374struct IpTraits<
IPv4> {
375 using Type = sockaddr_in;
376 using AddrFuncType = int (*)(
const char *, int, Type *);
377 using NameFuncType = int (*)(
const Type *,
char *, std::size_t);
379 inline static const AddrFuncType addrFunc = &uv_ip4_addr;
380 inline static const NameFuncType nameFunc = &uv_ip4_name;
382 static constexpr auto sinPort(
const Type *addr) {
383 return addr->sin_port;
388struct IpTraits<IPv6> {
389 using Type = sockaddr_in6;
390 using AddrFuncType = int (*)(
const char *, int, Type *);
391 using NameFuncType = int (*)(
const Type *,
char *, std::size_t);
393 inline static const AddrFuncType addrFunc = &uv_ip6_addr;
394 inline static const NameFuncType nameFunc = &uv_ip6_name;
396 static constexpr auto sinPort(
const Type *addr) {
397 return addr->sin6_port;
402Addr address(
const typename details::IpTraits<I>::Type *aptr)
noexcept {
404 char name[DEFAULT_SIZE];
406 int err = details::IpTraits<I>::nameFunc(aptr, name, DEFAULT_SIZE);
409 addr.port = ntohs(details::IpTraits<I>::sinPort(aptr));
410 addr.ip = std::string{name};
416template<
typename I,
typename F,
typename H>
417Addr address(F &&f,
const H *handle)
noexcept {
418 sockaddr_storage ssto;
419 int len =
sizeof(ssto);
422 int err = std::forward<F>(f)(handle,
reinterpret_cast<sockaddr *
>(&ssto), &len);
425 typename IpTraits<I>::Type *aptr =
reinterpret_cast<typename IpTraits<I>::Type *
>(&ssto);
426 addr = address<I>(aptr);
432template<
typename F,
typename... Args>
433std::string tryRead(F &&f, Args &&...args)
noexcept {
434 std::size_t size = DEFAULT_SIZE;
435 char buf[DEFAULT_SIZE];
437 auto err = std::forward<F>(f)(args..., buf, &size);
439 if(UV_ENOBUFS == err) {
440 std::unique_ptr<char[]> data{
new char[size]};
441 err = std::forward<F>(f)(args..., data.get(), &size);
446 }
else if(0 == err) {
447 str.assign(buf, size);
461 using MallocFuncType =
void *(*)(
size_t);
462 using ReallocFuncType =
void *(*)(
void *,
size_t);
463 using CallocFuncType =
void *(*)(
size_t,
size_t);
464 using FreeFuncType = void (*)(
void *);
521 static std::
string env(const std::
string &name) noexcept;
530 static
bool env(const std::
string &name, const std::
string &value) noexcept;
545 template<typename Func>
546 static std::enable_if_t<std::is_invocable_v<Func, std::string_view, std::string_view>,
bool>
548 uv_env_item_t *items =
nullptr;
551 const bool ret = (uv_os_environ(&items, &count) == 0);
554 for(
int pos = 0; pos < count; ++pos) {
555 func(std::string_view{items[pos].name}, std::string_view{items[pos].value});
558 uv_os_free_environ(items, count);
724 static
bool replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept;
801 static std::
string path() noexcept;
807 static std::
string cwd() noexcept;
814 static
bool chdir(const std::
string &dir) noexcept;
827 static
void sleep(
unsigned int msec) noexcept;
841template<class... Func>
843 using Func::operator()...;
850template<
class... Func>
Utility class to handle flags.
constexpr Flags()
Constructs an uninitialized Flags object.
constexpr Flags operator|(const Flags &f) const noexcept
Or operator.
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.
constexpr Flags operator&(const Flags &f) const noexcept
And operator.
static constexpr Flags< E > from()
Utility factory method to pack a set of values all at once.
constexpr Flags(E flag) noexcept
Constructs a Flags object from a value of the enum E.
constexpr Flags operator&(E flag) const noexcept
And operator.
details::UVTypeWrapper< uv_os_fd_t > OSFileDescriptor
details::UVTypeWrapper< uv_handle_type > HandleCategory
constexpr FileHandle StdIN
details::UVTypeWrapper< uv_os_sock_t > OSSocketHandle
details::UVTypeWrapper< uv_pid_t > PidType
constexpr FileHandle StdOUT
details::UVHandleType HandleType
constexpr FileHandle StdERR
details::UVTypeWrapper< uv_file > FileHandle
Helper type for visitors.
std::string homedir() const noexcept
Gets the homedir.
std::string username() const noexcept
Gets the username.
decltype(uv_passwd_t::gid) gid() const noexcept
Gets the gid.
decltype(uv_passwd_t::uid) uid() const noexcept
Gets the uid.
std::string shell() const noexcept
Gets the shell.
static UtsName uname() noexcept
Gets name and information about the current kernel.
static PidType parent() noexcept
Returns the parent process id.
static std::string hostname() noexcept
Returns the hostname.
static int priority(PidType pid)
Retrieves the scheduling priority of a process.
static PidType pid() noexcept
Returns the current process id.
static std::string homedir() noexcept
Gets the current user's home directory.
static std::string tmpdir() noexcept
Gets the temp directory.
static std::string env(const std::string &name) noexcept
Retrieves an environment variable.
static Passwd passwd() noexcept
Gets a subset of the password file entry.
static std::string processTitle()
Gets the title of the current process.
static void sleep(unsigned int msec) noexcept
Causes the calling thread to sleep for a while.
static bool chdir(const std::string &dir) noexcept
Changes the current working directory.
static HandleType guessHandle(HandleCategory category) noexcept
Gets the type of the handle given a category.
static unsigned int availableParallelism() noexcept
Returns an estimate of the amount of parallelism a program should use (always a non-zero value).
static std::array< double, 3 > loadAverage() noexcept
Gets the load average.
static std::vector< InterfaceAddress > interfaceAddresses() noexcept
Gets a set of descriptors of all the available interfaces.
static bool replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept
Override the use of some standard library’s functions.
static uint64_t constrainedMemory() noexcept
Gets the amount of memory available to the process (in bytes).
static std::string indexToIid(unsigned int index) noexcept
Retrieves a network interface identifier.
static std::string indexToName(unsigned int index) noexcept
IPv6-capable implementation of if_indextoname.
static std::string path() noexcept
Gets the executable path.
static std::string cwd() noexcept
Gets the current working directory.
static uint64_t totalMemory() noexcept
Gets memory information (in bytes).
static char ** setupArgs(int argc, char **argv)
Store the program arguments.
static TimeVal64 timeOfDay() noexcept
Cross-platform implementation of gettimeofday
static double uptime() noexcept
Gets the current system uptime.
static std::vector< CPUInfo > cpuInfo() noexcept
Gets information about the CPUs on the system.
static uint64_t hrtime() noexcept
Gets the current high-resolution real time.
static RUsage rusage() noexcept
Gets the resource usage measures for the current process.
std::string version() const noexcept
Gets the operating system version.
std::string sysname() const noexcept
Gets the operating system name (like "Linux").
std::string machine() const noexcept
Gets the hardware identifier.
std::string release() const noexcept
Gets the operating system release (like "2.6.28").
Windows size representation.