uvw 3.4.0
Loading...
Searching...
No Matches
util.h
1#ifndef UVW_UTIL_INCLUDE_H
2#define UVW_UTIL_INCLUDE_H
3
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <memory>
8#include <string>
9#include <string_view>
10#include <type_traits>
11#include <utility>
12#include <vector>
13#include <uv.h>
14#include "config.h"
15
16namespace uvw {
17
18namespace details {
19
20enum class uvw_handle_type : std::underlying_type_t<uv_handle_type> {
21 UNKNOWN = UV_UNKNOWN_HANDLE,
22 ASYNC = UV_ASYNC,
23 CHECK = UV_CHECK,
24 FS_EVENT = UV_FS_EVENT,
25 FS_POLL = UV_FS_POLL,
26 HANDLE = UV_HANDLE,
27 IDLE = UV_IDLE,
28 PIPE = UV_NAMED_PIPE,
29 POLL = UV_POLL,
30 PREPARE = UV_PREPARE,
31 PROCESS = UV_PROCESS,
32 STREAM = UV_STREAM,
33 TCP = UV_TCP,
34 TIMER = UV_TIMER,
35 TTY = UV_TTY,
36 UDP = UV_UDP,
37 SIGNAL = UV_SIGNAL,
38 FILE = UV_FILE
39};
40
41enum class uvw_clock_id : std::underlying_type_t<uv_clock_id> {
42 MONOTONIC = UV_CLOCK_MONOTONIC,
43 REALTIME = UV_CLOCK_REALTIME
44};
45
46template<typename T>
47struct uv_type_wrapper {
48 using Type = T;
49
50 constexpr uv_type_wrapper()
51 : value{} {}
52
53 constexpr uv_type_wrapper(Type val)
54 : value{val} {}
55
56 constexpr operator Type() const noexcept {
57 return value;
58 }
59
60 bool operator==(uv_type_wrapper other) const noexcept {
61 return value == other.value;
62 }
63
64private:
65 const Type value;
66};
67
68template<typename T>
69bool operator==(uv_type_wrapper<T> lhs, uv_type_wrapper<T> rhs) {
70 return !(lhs == rhs);
71}
72
73} // namespace details
74
78struct win_size {
79 int width;
80 int height;
81};
82
83using handle_type = details::uvw_handle_type;
84using handle_category = details::uv_type_wrapper<uv_handle_type>;
85using file_handle = details::uv_type_wrapper<uv_file>;
86using os_socket_handle = details::uv_type_wrapper<uv_os_sock_t>;
87using os_file_descriptor = details::uv_type_wrapper<uv_os_fd_t>;
88using pid_type = details::uv_type_wrapper<uv_pid_t>;
89using clock_id = details::uvw_clock_id;
90
91constexpr file_handle std_in{0};
92constexpr file_handle std_out{1};
93constexpr file_handle std_err{2};
94
95using time_spec = uv_timespec_t;
96using file_info = uv_stat_t;
97using fs_info = uv_statfs_t;
98using uid_type = uv_uid_t;
99using gid_type = uv_gid_t;
100
101using timeval = uv_timeval_t;
102using timeval64 = uv_timeval64_t;
103using timespec64 = uv_timespec64_t;
104using resource_usage = uv_rusage_t;
105
114struct passwd_info {
115 passwd_info(std::shared_ptr<uv_passwd_t> pwd);
116
121 std::string username() const noexcept;
122
127 decltype(uv_passwd_t::uid) uid() const noexcept;
128
133 decltype(uv_passwd_t::gid) gid() const noexcept;
134
139 std::string shell() const noexcept;
140
145 std::string homedir() const noexcept;
146
151 operator bool() const noexcept;
152
153private:
154 std::shared_ptr<uv_passwd_t> value;
155};
156
166struct uts_name {
167 uts_name(std::shared_ptr<uv_utsname_t> init);
168
173 std::string sysname() const noexcept;
174
179 std::string release() const noexcept;
180
185 std::string version() const noexcept;
186
191 std::string machine() const noexcept;
192
193private:
194 std::shared_ptr<uv_utsname_t> uname;
195};
196
202struct ipv4 {};
203
209struct ipv6 {};
210
215 std::string ip;
216 unsigned int port;
217};
218
222struct cpu_info {
223 using cpu_time = decltype(uv_cpu_info_t::cpu_times);
224
225 std::string model;
226 int speed;
227
234 cpu_time times;
235};
236
247
248namespace details {
249
250static constexpr std::size_t DEFAULT_SIZE = 128;
251
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];
256 std::string str{};
257 auto err = std::forward<F>(f)(args..., buf, &size);
258
259 if(UV_ENOBUFS == err) {
260 std::unique_ptr<char[]> data{new char[size]};
261 err = std::forward<F>(f)(args..., data.get(), &size);
262
263 if(0 == err) {
264 str = data.get();
265 }
266 } else if(0 == err) {
267 str.assign(buf, size);
268 }
269
270 return str;
271}
272
273void common_alloc_callback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf);
274
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));
279}
280
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);
286
287} // namespace details
288
294struct utilities {
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 *);
299
303 struct os {
313 static pid_type pid() noexcept;
314
324 static pid_type ppid() noexcept;
325
336 static std::string homedir() noexcept;
337
347 static std::string tmpdir() noexcept;
348
355 static std::string env(const std::string &name) noexcept;
356
364 static bool env(const std::string &name, const std::string &value) noexcept;
365
379 template<typename Func>
380 static std::enable_if_t<std::is_invocable_v<Func, std::string_view, std::string_view>, bool>
381 env(Func func) noexcept {
382 uv_env_item_t *items = nullptr;
383 int count{};
384
385 const bool ret = (uv_os_environ(&items, &count) == 0);
386
387 if(ret) {
388 for(int pos = 0; pos < count; ++pos) {
389 func(std::string_view{items[pos].name}, std::string_view{items[pos].value});
390 }
391
392 uv_os_free_environ(items, count);
393 }
394
395 return ret;
396 }
397
402 static std::string hostname() noexcept;
403
413 static uts_name uname() noexcept;
414
427 static passwd_info passwd() noexcept;
428
442 static int priority(pid_type pid);
443
459 static bool priority(pid_type pid, int prio);
460 };
461
467 static handle_type guess_handle(handle_category category) noexcept;
468
487 static handle_type guess_handle(file_handle file) noexcept;
488
496 static std::vector<cpu_info> cpu() noexcept;
497
506 static std::vector<interface_address> interface_addresses() noexcept;
507
521 static std::string index_to_name(unsigned int index) noexcept;
522
533 static std::string index_to_iid(unsigned int index) noexcept;
534
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;
559
564 static std::array<double, 3> load_average() noexcept;
565
573 static char **setup_args(int argc, char **argv);
574
579 static std::string process_title();
580
586 static bool process_title(const std::string &title);
587
592 static uint64_t total_memory() noexcept;
593
605 static uint64_t constrained_memory() noexcept;
606
611 static uint64_t available_memory() noexcept;
612
617 static double uptime() noexcept;
618
623 static resource_usage rusage() noexcept;
624
630 static timespec64 gettime(clock_id source) noexcept;
631
642 static uint64_t hrtime() noexcept;
643
648 static std::string path() noexcept;
649
654 static std::string cwd() noexcept;
655
661 static bool chdir(const std::string &dir) noexcept;
662
668 static timeval64 time_of_day() noexcept;
669
674 static void sleep(unsigned int msec) noexcept;
675
681 static unsigned int available_parallelism() noexcept;
682};
683
688template<class... Func>
689struct overloaded: Func... {
690 using Func::operator()...;
691};
692
697template<class... Func>
698overloaded(Func...) -> overloaded<Func...>;
699
700} // namespace uvw
701
702#ifndef UVW_AS_LIB
703# include "util.cpp"
704#endif
705
706#endif // UVW_UTIL_INCLUDE_H
uvw default namespace.
Definition async.h:8
uv_gid_t gid_type
Definition util.h:99
uv_uid_t uid_type
Definition util.h:98
details::uvw_clock_id clock_id
Definition util.h:89
uv_timeval_t timeval
Definition util.h:101
details::uvw_handle_type handle_type
Definition util.h:83
constexpr file_handle std_in
Definition util.h:91
details::uv_type_wrapper< uv_pid_t > pid_type
Definition util.h:88
uv_statfs_t fs_info
Definition util.h:97
uv_timespec64_t timespec64
Definition util.h:103
details::uv_type_wrapper< uv_os_sock_t > os_socket_handle
Definition util.h:86
uv_stat_t file_info
Definition util.h:96
details::uv_type_wrapper< uv_os_fd_t > os_file_descriptor
Definition util.h:87
uv_rusage_t resource_usage
Definition util.h:104
details::uv_type_wrapper< uv_handle_type > handle_category
Definition util.h:84
uv_timeval64_t timeval64
Definition util.h:102
constexpr file_handle std_err
Definition util.h:93
details::uv_type_wrapper< uv_file > file_handle
Definition util.h:85
constexpr file_handle std_out
Definition util.h:92
uv_timespec_t time_spec
Definition util.h:95
CPU information.
Definition util.h:222
std::string model
Definition util.h:225
int speed
Definition util.h:226
cpu_time times
CPU times.
Definition util.h:234
Interface address.
Definition util.h:240
socket_address netmask
Definition util.h:245
socket_address address
Definition util.h:244
std::string name
Definition util.h:241
The IPv4 tag.
Definition util.h:202
The IPv6 tag.
Definition util.h:209
Helper type for visitors.
Definition util.h:689
Utility class.
Definition util.h:114
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.
Address representation.
Definition util.h:214
std::string ip
Definition util.h:215
unsigned int port
Definition util.h:216
OS dedicated utilities.
Definition util.h:303
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.
Miscellaneous utilities.
Definition util.h:294
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.
Utility class.
Definition util.h:166
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.
Definition util.h:78
int width
Definition util.h:79
int height
Definition util.h:80