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,
41enum class uvw_clock_id : std::underlying_type_t<uv_clock_id> {
42 MONOTONIC = UV_CLOCK_MONOTONIC,
43 REALTIME = UV_CLOCK_REALTIME
47struct uv_type_wrapper {
50 constexpr uv_type_wrapper()
53 constexpr uv_type_wrapper(Type val)
56 constexpr operator Type() const noexcept {
60 bool operator==(uv_type_wrapper other)
const noexcept {
61 return value == other.value;
69bool operator==(uv_type_wrapper<T> lhs, uv_type_wrapper<T> rhs) {
88using pid_type = details::uv_type_wrapper<uv_pid_t>;
115 passwd_info(std::shared_ptr<uv_passwd_t> pwd);
127 decltype(uv_passwd_t::
uid)
uid() const noexcept;
133 decltype(uv_passwd_t::
gid)
gid() const noexcept;
151 operator
bool() const noexcept;
154 std::shared_ptr<uv_passwd_t> value;
167 uts_name(std::shared_ptr<uv_utsname_t> init);
194 std::shared_ptr<uv_utsname_t> uname;
223 using cpu_time =
decltype(uv_cpu_info_t::cpu_times);
250static constexpr std::size_t DEFAULT_SIZE = 128;
252template<
typename F,
typename... Args>
253std::string try_read(F &&f, Args &&...args)
noexcept {
254 std::size_t size = DEFAULT_SIZE;
255 char buf[DEFAULT_SIZE];
257 auto err = std::forward<F>(f)(args..., buf, &size);
259 if(UV_ENOBUFS == err) {
260 std::unique_ptr<char[]> data{
new char[size]};
261 err = std::forward<F>(f)(args..., data.get(), &size);
266 }
else if(0 == err) {
267 str.assign(buf, size);
273void common_alloc_callback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf);
275template<
typename Type, auto Alloc>
276void common_alloc_callback(uv_handle_t *handle, std::size_t suggested, uv_buf_t *buf) {
277 auto [alloc, size] = Alloc(*
static_cast<const Type *
>(handle->data), suggested);
278 *buf = uv_buf_init(alloc,
static_cast<unsigned int>(size));
281sockaddr ip_addr(
const char *addr,
unsigned int port);
282socket_address sock_addr(
const sockaddr_in &addr);
283socket_address sock_addr(
const sockaddr_in6 &addr);
284socket_address sock_addr(
const sockaddr &addr);
285socket_address sock_addr(
const sockaddr_storage &storage);
295 using malloc_func_type =
void *(*)(
size_t);
296 using realloc_func_type =
void *(*)(
void *,
size_t);
297 using calloc_func_type =
void *(*)(
size_t,
size_t);
298 using free_func_type = void (*)(
void *);
355 static std::
string env(const std::
string &name) noexcept;
364 static
bool env(const std::
string &name, const std::
string &value) noexcept;
379 template<typename Func>
380 static std::enable_if_t<std::is_invocable_v<Func, std::string_view, std::string_view>,
bool>
382 uv_env_item_t *items =
nullptr;
385 const bool ret = (uv_os_environ(&items, &count) == 0);
388 for(
int pos = 0; pos < count; ++pos) {
389 func(std::string_view{items[pos].name}, std::string_view{items[pos].value});
392 uv_os_free_environ(items, count);
558 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;
648 static std::
string path() noexcept;
654 static std::
string cwd() noexcept;
661 static
bool chdir(const std::
string &dir) noexcept;
674 static
void sleep(
unsigned int msec) noexcept;
688template<class... Func>
690 using Func::operator()...;
697template<
class... Func>
details::uvw_clock_id clock_id
details::uvw_handle_type handle_type
constexpr file_handle std_in
details::uv_type_wrapper< uv_pid_t > pid_type
uv_timespec64_t timespec64
details::uv_type_wrapper< uv_os_sock_t > os_socket_handle
details::uv_type_wrapper< uv_os_fd_t > os_file_descriptor
uv_rusage_t resource_usage
details::uv_type_wrapper< uv_handle_type > handle_category
constexpr file_handle std_err
details::uv_type_wrapper< uv_file > file_handle
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 homedir() noexcept
Gets the current user's home directory.
static std::string hostname() noexcept
Returns the hostname.
static passwd_info passwd() noexcept
Gets a subset of the password file entry.
static int priority(pid_type pid)
Retrieves the scheduling priority of a process.
static uts_name uname() noexcept
Gets name and information about the current kernel.
static pid_type ppid() noexcept
Returns the parent process id.
static std::string tmpdir() noexcept
Gets the temp directory.
static pid_type pid() noexcept
Returns the current process id.
static std::string env(const std::string &name) noexcept
Retrieves an environment variable.
static std::string process_title()
Gets the title of the current process.
static std::string cwd() noexcept
Gets the current working directory.
static std::vector< interface_address > interface_addresses() noexcept
Gets a set of descriptors of all the available interfaces.
static uint64_t hrtime() noexcept
Gets the current high-resolution real time.
static std::string index_to_name(unsigned int index) noexcept
IPv6-capable implementation of if_indextoname.
static timeval64 time_of_day() noexcept
Cross-platform implementation of gettimeofday
static std::vector< cpu_info > cpu() noexcept
Gets information about the CPUs on the system.
static uint64_t constrained_memory() noexcept
Gets the amount of memory available to the process (in bytes).
static uint64_t available_memory() noexcept
Gets the amount of free memory still available to the process.
static handle_type guess_handle(handle_category category) noexcept
Gets the type of the handle given a category.
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
Override the use of some standard library’s functions.
static char ** setup_args(int argc, char **argv)
Store the program arguments.
static resource_usage rusage() noexcept
Gets the resource usage measures for the current process.
static unsigned int available_parallelism() noexcept
Returns an estimate of the amount of parallelism a program should use (always a non-zero value).
static std::string path() noexcept
Gets the executable path.
static double uptime() noexcept
Gets the current system uptime.
static void sleep(unsigned int msec) noexcept
Causes the calling thread to sleep for a while.
static std::array< double, 3 > load_average() noexcept
Gets the load average.
static bool chdir(const std::string &dir) noexcept
Changes the current working directory.
static uint64_t total_memory() noexcept
Gets memory information (in bytes).
static std::string index_to_iid(unsigned int index) noexcept
Retrieves a network interface identifier.
static timespec64 gettime(clock_id source) noexcept
Gets the current system time from a high-resolution clock source.
std::string sysname() const noexcept
Gets the operating system name (like "Linux").
std::string machine() const noexcept
Gets the hardware identifier.
std::string release() const noexcept
Gets the operating system release (like "2.6.28").
std::string version() const noexcept
Gets the operating system version.
Windows size representation.