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 {
202using HandleType = details::UVHandleType;
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);
297 std::
string release() const noexcept;
303 std::
string version() const noexcept;
309 std::
string machine() const noexcept;
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 *);
502 static std::
string homedir() noexcept;
513 static std::
string tmpdir() noexcept;
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>
547 env(Func func) noexcept {
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);
662 static std::vector<
CPUInfo> cpuInfo() noexcept;
687 static std::
string indexToName(
unsigned int index) noexcept;
699 static std::
string indexToIid(
unsigned int index) noexcept;
724 static
bool replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept;
730 static std::array<
double, 3> loadAverage() noexcept;
739 static
char **setupArgs(
int argc,
char **argv);
745 static std::
string processTitle();
752 static
bool processTitle(const std::
string &title);
758 static uint64_t totalMemory() noexcept;
771 static uint64_t constrainedMemory() noexcept;
777 static
double uptime() noexcept;
795 static uint64_t hrtime() 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;
834 static
unsigned int availableParallelism() 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
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 std::string hostname() noexcept
Returns the hostname.
static PidType pid() noexcept
Returns the current process id.
std::string sysname() const noexcept
Gets the operating system name (like "Linux").
Windows size representation.