1#ifndef UVW_UTIL_INCLUDE_H
2#define UVW_UTIL_INCLUDE_H
20enum class uvw_handle_type : std::underlying_type_t<uv_handle_type> {
21 UNKNOWN = UV_UNKNOWN_HANDLE,
24 FS_EVENT = UV_FS_EVENT,
42struct uv_type_wrapper {
45 constexpr uv_type_wrapper()
48 constexpr uv_type_wrapper(Type val)
51 constexpr operator Type() const noexcept {
55 bool operator==(uv_type_wrapper other)
const noexcept {
56 return value == other.value;
64bool operator==(uv_type_wrapper<T> lhs, uv_type_wrapper<T> rhs) {
78using handle_type = details::uvw_handle_type;
83using pid_type = details::uv_type_wrapper<uv_pid_t>;
120 decltype(uv_passwd_t::
uid)
uid() const noexcept;
126 decltype(uv_passwd_t::
gid)
gid() const noexcept;
144 operator
bool() const noexcept;
147 std::shared_ptr<uv_passwd_t> value;
160 uts_name(std::shared_ptr<uv_utsname_t> init);
172 std::
string release() const noexcept;
178 std::
string version() const noexcept;
184 std::
string machine() const noexcept;
187 std::shared_ptr<uv_utsname_t> uname;
216 using cpu_time =
decltype(uv_cpu_info_t::cpu_times);
243static constexpr std::size_t DEFAULT_SIZE = 128;
245template<
typename F,
typename... Args>
246std::string try_read(F &&f, Args &&...args)
noexcept {
247 std::size_t size = DEFAULT_SIZE;
248 char buf[DEFAULT_SIZE];
250 auto err = std::forward<F>(f)(args..., buf, &size);
252 if(UV_ENOBUFS == err) {
253 std::unique_ptr<char[]> data{
new char[size]};
254 err = std::forward<F>(f)(args..., data.get(), &size);
259 }
else if(0 == err) {
260 str.assign(buf, size);
266void common_alloc_callback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf);
268sockaddr ip_addr(
const char *addr,
unsigned int port);
269socket_address sock_addr(
const sockaddr_in &addr);
270socket_address sock_addr(
const sockaddr_in6 &addr);
271socket_address sock_addr(
const sockaddr &addr);
272socket_address sock_addr(
const sockaddr_storage &storage);
282 using malloc_func_type =
void *(*)(
size_t);
283 using realloc_func_type =
void *(*)(
void *,
size_t);
284 using calloc_func_type =
void *(*)(
size_t,
size_t);
285 using free_func_type = void (*)(
void *);
323 static std::
string homedir() noexcept;
334 static std::
string tmpdir() noexcept;
342 static std::
string env(const std::
string &name) noexcept;
351 static
bool env(const std::
string &name, const std::
string &value) noexcept;
366 template<typename Func>
367 static std::enable_if_t<std::is_invocable_v<Func, std::string_view, std::string_view>,
bool>
368 env(Func func) noexcept {
369 uv_env_item_t *items =
nullptr;
372 const bool ret = (uv_os_environ(&items, &count) == 0);
375 for(
int pos = 0; pos < count; ++pos) {
376 func(std::string_view{items[pos].name}, std::string_view{items[pos].value});
379 uv_os_free_environ(items, count);
508 static std::
string index_to_name(
unsigned int index) noexcept;
520 static std::
string index_to_iid(
unsigned int index) noexcept;
545 static
bool replace_allocator(malloc_func_type malloc_func, realloc_func_type realloc_func, calloc_func_type calloc_func, free_func_type free_func) noexcept;
551 static std::array<
double, 3> load_average() noexcept;
560 static
char **setup_args(
int argc,
char **argv);
566 static std::
string process_title();
573 static
bool process_title(const std::
string &title);
579 static uint64_t total_memory() noexcept;
592 static uint64_t constrained_memory() noexcept;
598 static
double uptime() noexcept;
616 static uint64_t hrtime() noexcept;
622 static std::
string path() noexcept;
628 static std::
string cwd() noexcept;
635 static
bool chdir(const std::
string &dir) noexcept;
648 static
void sleep(
unsigned int msec) noexcept;
655 static
unsigned int available_parallelism() noexcept;
662template<class... Func>
664 using Func::operator()...;
671template<
class... Func>
details::uv_type_wrapper< uv_os_fd_t > os_file_descriptor
details::uv_type_wrapper< uv_file > file_handle
details::uv_type_wrapper< uv_pid_t > pid_type
constexpr file_handle std_in
details::uv_type_wrapper< uv_handle_type > handle_category
constexpr file_handle std_err
details::uv_type_wrapper< uv_os_sock_t > os_socket_handle
uv_rusage_t resource_usage
constexpr file_handle std_out
Helper type for visitors.
decltype(uv_passwd_t::uid) uid() const noexcept
Gets the uid.
decltype(uv_passwd_t::gid) gid() const noexcept
Gets the gid.
std::string shell() const noexcept
Gets the shell.
std::string username() const noexcept
Gets the username.
std::string homedir() const noexcept
Gets the homedir.
static std::string hostname() noexcept
Returns the hostname.
static pid_type pid() noexcept
Returns the current process id.
std::string sysname() const noexcept
Gets the operating system name (like "Linux").
Windows size representation.