1#ifndef UVW_UTIL_INCLUDE_H
2#define UVW_UTIL_INCLUDE_H
18enum class UVHandleType : std::underlying_type_t<uv_handle_type> {
19 UNKNOWN = UV_UNKNOWN_HANDLE,
22 FS_EVENT = UV_FS_EVENT,
43 constexpr UVTypeWrapper()
46 constexpr UVTypeWrapper(Type val)
49 constexpr operator Type() const noexcept {
53 bool operator==(UVTypeWrapper other)
const noexcept {
54 return value == other.value;
62bool operator==(UVTypeWrapper<T> lhs, UVTypeWrapper<T> rhs) {
80 static_assert(std::is_enum_v<E>);
82 using InnerType = std::underlying_type_t<E>;
84 constexpr InnerType toInnerType(E flag)
const noexcept {
85 return static_cast<InnerType
>(flag);
89 using Type = InnerType;
105 : flags{toInnerType(flag)} {}
125 : flags{std::move(f.flags)} {}
127 constexpr Flags &operator=(
const Flags &f)
noexcept {
132 constexpr Flags &operator=(
Flags &&f)
noexcept {
133 flags = std::move(f.flags);
143 return Flags{flags | f.flags};
152 return Flags{flags | toInnerType(flag)};
161 return Flags{flags & f.flags};
170 return Flags{flags & toInnerType(flag)};
177 explicit constexpr operator bool() const noexcept {
178 return !(flags == InnerType{});
185 constexpr operator Type() const noexcept {
201using HandleType = details::UVHandleType;
207using PidType = details::UVTypeWrapper<uv_pid_t>;
232 Passwd(std::shared_ptr<uv_passwd_t> pwd);
244 decltype(uv_passwd_t::
uid)
uid() const noexcept;
250 decltype(uv_passwd_t::
gid)
gid() const noexcept;
268 operator
bool() const noexcept;
271 std::shared_ptr<uv_passwd_t> passwd;
284 UtsName(std::shared_ptr<uv_utsname_t> utsname);
296 std::
string release() const noexcept;
302 std::
string version() const noexcept;
308 std::
string machine() const noexcept;
311 std::shared_ptr<uv_utsname_t> utsname;
340 using CPUTime =
decltype(uv_cpu_info_t::cpu_times);
367static constexpr std::size_t DEFAULT_SIZE = 128;
373struct IpTraits<
IPv4> {
374 using Type = sockaddr_in;
375 using AddrFuncType = int (*)(
const char *, int, Type *);
376 using NameFuncType = int (*)(
const Type *,
char *, std::size_t);
378 inline static const AddrFuncType addrFunc = &uv_ip4_addr;
379 inline static const NameFuncType nameFunc = &uv_ip4_name;
381 static constexpr auto sinPort(
const Type *addr) {
382 return addr->sin_port;
387struct IpTraits<IPv6> {
388 using Type = sockaddr_in6;
389 using AddrFuncType = int (*)(
const char *, int, Type *);
390 using NameFuncType = int (*)(
const Type *,
char *, std::size_t);
392 inline static const AddrFuncType addrFunc = &uv_ip6_addr;
393 inline static const NameFuncType nameFunc = &uv_ip6_name;
395 static constexpr auto sinPort(
const Type *addr) {
396 return addr->sin6_port;
401Addr address(
const typename details::IpTraits<I>::Type *aptr)
noexcept {
403 char name[DEFAULT_SIZE];
405 int err = details::IpTraits<I>::nameFunc(aptr, name, DEFAULT_SIZE);
408 addr.port = ntohs(details::IpTraits<I>::sinPort(aptr));
409 addr.ip = std::string{name};
415template<
typename I,
typename F,
typename H>
416Addr address(F &&f,
const H *handle)
noexcept {
417 sockaddr_storage ssto;
418 int len =
sizeof(ssto);
421 int err = std::forward<F>(f)(handle,
reinterpret_cast<sockaddr *
>(&ssto), &len);
424 typename IpTraits<I>::Type *aptr =
reinterpret_cast<typename IpTraits<I>::Type *
>(&ssto);
425 addr = address<I>(aptr);
431template<
typename F,
typename... Args>
432std::string tryRead(F &&f, Args &&...args)
noexcept {
433 std::size_t size = DEFAULT_SIZE;
434 char buf[DEFAULT_SIZE];
436 auto err = std::forward<F>(f)(args..., buf, &size);
438 if(UV_ENOBUFS == err) {
439 std::unique_ptr<char[]> data{
new char[size]};
440 err = std::forward<F>(f)(args..., data.get(), &size);
445 }
else if(0 == err) {
446 str.assign(buf, size);
460 using MallocFuncType =
void *(*)(
size_t);
461 using ReallocFuncType =
void *(*)(
void *,
size_t);
462 using CallocFuncType =
void *(*)(
size_t,
size_t);
463 using FreeFuncType = void (*)(
void *);
501 static std::
string homedir() noexcept;
512 static std::
string tmpdir() noexcept;
520 static std::
string env(const std::
string &name) noexcept;
529 static
bool env(const std::
string &name, const std::
string &value) noexcept;
544 template<typename Func>
545 static std::enable_if_t<std::is_invocable_v<Func, std::string_view, std::string_view>,
bool>
546 env(Func func) noexcept {
547 uv_env_item_t *items =
nullptr;
550 const bool ret = (uv_os_environ(&items, &count) == 0);
553 for(
int pos = 0; pos < count; ++pos) {
554 func(std::string_view{items[pos].name}, std::string_view{items[pos].value});
557 uv_os_free_environ(items, count);
661 static std::vector<
CPUInfo> cpuInfo() noexcept;
686 static std::
string indexToName(
unsigned int index) noexcept;
698 static std::
string indexToIid(
unsigned int index) noexcept;
723 static
bool replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept;
729 static std::array<
double, 3> loadAverage() noexcept;
738 static
char **setupArgs(
int argc,
char **argv);
744 static std::
string processTitle();
751 static
bool processTitle(const std::
string &title);
757 static uint64_t totalMemory() noexcept;
770 static uint64_t constrainedMemory() noexcept;
776 static
double uptime() noexcept;
794 static uint64_t hrtime() noexcept;
800 static std::
string path() noexcept;
806 static std::
string cwd() noexcept;
813 static
bool chdir(const std::
string &dir) noexcept;
826 static
void sleep(
unsigned int msec) noexcept;
833 static
unsigned int availableParallelism() noexcept;
840template<class... Func>
842 using Func::operator()...;
849template<
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_file > FileHandle
details::UVTypeWrapper< uv_os_sock_t > OSSocketHandle
constexpr FileHandle StdIN
constexpr FileHandle StdOUT
details::UVTypeWrapper< uv_handle_type > HandleCategory
details::UVTypeWrapper< uv_pid_t > PidType
constexpr FileHandle StdERR
details::UVTypeWrapper< uv_os_fd_t > OSFileDescriptor
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.