1 #ifndef UVW_UTIL_INCLUDE_H
2 #define UVW_UTIL_INCLUDE_H
22 enum class UVHandleType: std::underlying_type_t<uv_handle_type> {
23 UNKNOWN = UV_UNKNOWN_HANDLE,
26 FS_EVENT = UV_FS_EVENT,
45 struct UVTypeWrapper {
48 constexpr UVTypeWrapper(): value{} {}
49 constexpr UVTypeWrapper(Type val): value{val} {}
51 constexpr
operator Type() const noexcept {
return value; }
53 bool operator==(UVTypeWrapper other)
const noexcept {
54 return value == other.value;
63 bool operator==(UVTypeWrapper<T> lhs, UVTypeWrapper<T> rhs) {
83 using InnerType = std::underlying_type_t<E>;
85 constexpr InnerType toInnerType(E flag)
const noexcept {
return static_cast<InnerType
>(flag); }
88 using Type = InnerType;
103 constexpr
Flags(E flag) noexcept: flags{toInnerType(flag)} {}
110 constexpr
Flags(Type f): flags{f} {}
117 constexpr
Flags(
const Flags &f) noexcept: flags{f.flags} { }
118 constexpr
Flags(
Flags &&f) noexcept: flags{std::move(f.flags)} { }
120 ~
Flags() noexcept { static_assert(std::is_enum_v<E>); }
122 constexpr
Flags & operator=(
const Flags &f) noexcept {
127 constexpr
Flags & operator=(
Flags &&f) noexcept {
128 flags = std::move(f.flags);
164 explicit constexpr
operator bool() const noexcept {
return !(flags == InnerType{}); }
170 constexpr
operator Type() const noexcept {
return flags; }
186 using HandleType = details::UVHandleType;
192 using PidType = details::UVTypeWrapper<uv_pid_t>;
218 Passwd(std::shared_ptr<uv_passwd_t> pwd);
230 decltype(uv_passwd_t::
uid)
uid() const noexcept;
236 decltype(uv_passwd_t::
gid)
gid() const noexcept;
254 operator
bool() const noexcept;
257 std::shared_ptr<uv_passwd_t> passwd;
271 UtsName(std::shared_ptr<uv_utsname_t> utsname);
283 std::
string release() const noexcept;
289 std::
string version() const noexcept;
295 std::
string machine() const noexcept;
298 std::shared_ptr<uv_utsname_t> utsname;
331 using CPUTime = decltype(uv_cpu_info_t::cpu_times);
361 static constexpr std::size_t DEFAULT_SIZE = 128;
369 struct IpTraits<
IPv4> {
370 using Type = sockaddr_in;
371 using AddrFuncType = int(*)(
const char *, int, Type *);
372 using NameFuncType = int(*)(
const Type *,
char *, std::size_t);
373 inline static const AddrFuncType addrFunc = &uv_ip4_addr;
374 inline static const NameFuncType nameFunc = &uv_ip4_name;
375 static constexpr
auto sinPort(
const Type *addr) {
return addr->sin_port; }
380 struct IpTraits<IPv6> {
381 using Type = sockaddr_in6;
382 using AddrFuncType = int(*)(
const char *, int, Type *);
383 using NameFuncType = int(*)(
const Type *,
char *, std::size_t);
384 inline static const AddrFuncType addrFunc = &uv_ip6_addr;
385 inline static const NameFuncType nameFunc = &uv_ip6_name;
386 static constexpr
auto sinPort(
const Type *addr) {
return addr->sin6_port; }
391 Addr address(
const typename details::IpTraits<I>::Type *aptr) noexcept {
393 char name[DEFAULT_SIZE];
395 int err = details::IpTraits<I>::nameFunc(aptr, name, DEFAULT_SIZE);
398 addr.port = ntohs(details::IpTraits<I>::sinPort(aptr));
399 addr.ip = std::string{name};
406 template<
typename I,
typename F,
typename H>
407 Addr address(F &&f,
const H *handle) noexcept {
408 sockaddr_storage ssto;
409 int len =
sizeof(ssto);
412 int err = std::forward<F>(f)(handle,
reinterpret_cast<sockaddr *
>(&ssto), &len);
415 typename IpTraits<I>::Type *aptr =
reinterpret_cast<typename IpTraits<I>::Type *
>(&ssto);
416 addr = address<I>(aptr);
423 template<
typename F,
typename... Args>
424 std::string tryRead(F &&f, Args&&... args) noexcept {
425 std::size_t size = DEFAULT_SIZE;
426 char buf[DEFAULT_SIZE];
428 auto err = std::forward<F>(f)(args..., buf, &size);
430 if(UV_ENOBUFS == err) {
431 std::unique_ptr<char[]> data{
new char[size]};
432 err = std::forward<F>(f)(args..., data.get(), &size);
437 }
else if(0 == err) {
438 str.assign(buf, size);
454 using MallocFuncType =
void*(*)(
size_t);
455 using ReallocFuncType =
void*(*)(
void*,
size_t);
456 using CallocFuncType =
void*(*)(
size_t,
size_t);
457 using FreeFuncType = void(*)(
void*);
506 static std::
string tmpdir() noexcept;
514 static std::
string env(const std::
string &name) noexcept;
523 static
bool env(const std::
string &name, const std::
string &value) noexcept;
538 template<typename Func>
539 static std::enable_if_t<std::is_invocable_v<Func, std::string_view, std::string_view>,
bool>
540 env(Func func) noexcept {
541 uv_env_item_t *items =
nullptr;
544 const bool ret = (uv_os_environ(&items, &count) == 0);
547 for(
int pos = 0; pos < count; ++pos) {
548 func(std::string_view{items[pos].name}, std::string_view{items[pos].value});
551 uv_os_free_environ(items, count);
619 static
bool osPriority(
PidType pid,
int prio);
655 static std::vector<
CPUInfo> cpuInfo() noexcept;
680 static std::
string indexToName(
unsigned int index) noexcept;
692 static std::
string indexToIid(
unsigned int index) noexcept;
717 static
bool replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept;
723 static std::array<
double, 3> loadAverage() noexcept;
732 static
char ** setupArgs(
int argc,
char** argv);
738 static std::
string processTitle();
745 static
bool processTitle(const std::
string &title);
751 static uint64_t totalMemory() noexcept;
764 static uint64_t constrainedMemory() noexcept;
770 static
double uptime() noexcept;
788 static uint64_t hrtime() noexcept;
794 static std::
string path() noexcept;
800 static std::
string cwd() noexcept;
807 static
bool chdir(const std::
string &dir) noexcept;
820 static
void sleep(
unsigned int msec) noexcept;
828 template<class... Func>
830 using Func::operator()...;
838 template<
class... Func>
Utility class to handle flags.
constexpr Flags()
Constructs an uninitialized Flags object.
static constexpr Flags< E > from()
Utility factory method to pack a set of values all at once.
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.
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
Overloaded(Func...) -> Overloaded< Func... >
Deduction guide.
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.