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);
224 std::string
username()
const noexcept;
230 decltype(uv_passwd_t::uid)
uid()
const noexcept;
236 decltype(uv_passwd_t::gid)
gid()
const noexcept;
242 std::string
shell()
const noexcept;
248 std::string
homedir()
const noexcept;
254 operator bool()
const noexcept;
257 std::shared_ptr<uv_passwd_t> passwd;
271 UtsName(std::shared_ptr<uv_utsname_t> utsname);
277 std::string
sysname()
const noexcept;
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 static constexpr AddrFuncType addrFunc = &uv_ip4_addr;
374 static constexpr 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 static constexpr AddrFuncType addrFunc = &uv_ip6_addr;
385 static constexpr 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*);
495 static std::string
homedir() noexcept;
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>
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);
561 static std::string
hostname() 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);
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;
831 #endif // UVW_UTIL_INCLUDE_H
static std::string env(const std::string &name) noexcept
Retrieves an environment variable.
static int osPriority(PidType pid)
Retrieves the scheduling priority of a process.
static std::string indexToIid(unsigned int index) noexcept
Retrieves a network interface identifier.
Windows size representation.
std::string machine() const noexcept
Gets the hardware identifier.
static void sleep(unsigned int msec) noexcept
Causes the calling thread to sleep for a while.
static std::string homedir() noexcept
Gets the current user's home directory.
constexpr FileHandle StdIN
constexpr Flags()
Constructs an uninitialized Flags object.
constexpr Flags operator&(const Flags &f) const noexcept
And operator.
static TimeVal64 timeOfDay() noexcept
Cross-platform implementation of gettimeofday
static std::string tmpdir() noexcept
Gets the temp directory.
static std::string indexToName(unsigned int index) noexcept
IPv6-capable implementation of if_indextoname.
static PidType pid() noexcept
Returns the current process id.
static Passwd passwd() noexcept
Gets a subset of the password file entry.
constexpr Flags(Type f)
Constructs a Flags object from an instance of the underlying type of the enum E.
static RUsage rusage() noexcept
Gets the resource usage measures for the current process.
std::string homedir() const noexcept
Gets the homedir.
static std::enable_if_t< std::is_invocable_v< Func, std::string_view, std::string_view >, bool > env(Func func) noexcept
Retrieves all environment variables and iterates them.
static std::array< double, 3 > loadAverage() noexcept
Gets the load average.
static bool replaceAllocator(MallocFuncType mallocFunc, ReallocFuncType reallocFunc, CallocFuncType callocFunc, FreeFuncType freeFunc) noexcept
Override the use of some standard library’s functions.
std::string username() const noexcept
Gets the username.
static std::vector< InterfaceAddress > interfaceAddresses() noexcept
Gets a set of descriptors of all the available interfaces.
static constexpr Flags< E > from()
Utility factory method to pack a set of values all at once.
constexpr FileHandle StdERR
std::string sysname() const noexcept
Gets the operating system name (like "Linux").
details::UVTypeWrapper< uv_os_fd_t > OSFileDescriptor
static uint64_t hrtime() noexcept
Gets the current high-resolution real time.
static char ** setupArgs(int argc, char **argv)
Store the program arguments.
static std::string path() noexcept
Gets the executable path.
static UtsName uname() noexcept
Gets name and information about the current kernel.
static std::vector< CPUInfo > cpuInfo() noexcept
Gets information about the CPUs on the system.
constexpr Flags operator|(const Flags &f) const noexcept
Or operator.
static PidType parent() noexcept
Returns the parent process id.
Utility class to handle flags.
details::UVTypeWrapper< uv_handle_type > HandleCategory
constexpr Flags operator&(E flag) const noexcept
And operator.
constexpr Flags operator|(E flag) const noexcept
Or operator.
decltype(uv_passwd_t::gid) gid() const noexcept
Gets the gid.
std::string version() const noexcept
Gets the operating system version.
constexpr FileHandle StdOUT
static std::string processTitle()
Gets the title of the current process.
static double uptime() noexcept
Gets the current system uptime.
details::UVTypeWrapper< uv_file > FileHandle
static HandleType guessHandle(HandleCategory category) noexcept
Gets the type of the handle given a category.
constexpr Flags(E flag) noexcept
Constructs a Flags object from a value of the enum E.
details::UVTypeWrapper< uv_os_sock_t > OSSocketHandle
static std::string hostname() noexcept
Returns the hostname.
static bool chdir(const std::string &dir) noexcept
Changes the current working directory.
static std::string cwd() noexcept
Gets the current working directory.
static uint64_t totalMemory() noexcept
Gets memory information (in bytes).
decltype(uv_passwd_t::uid) uid() const noexcept
Gets the uid.
std::string release() const noexcept
Gets the operating system release (like "2.6.28").
static uint64_t constrainedMemory() noexcept
Gets the amount of memory available to the process (in bytes).
details::UVTypeWrapper< uv_pid_t > PidType
std::string shell() const noexcept
Gets the shell.