uvw 3.0.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
41template<typename T>
42struct uv_type_wrapper {
43 using Type = T;
44
45 constexpr uv_type_wrapper()
46 : value{} {}
47
48 constexpr uv_type_wrapper(Type val)
49 : value{val} {}
50
51 constexpr operator Type() const noexcept {
52 return value;
53 }
54
55 bool operator==(uv_type_wrapper other) const noexcept {
56 return value == other.value;
57 }
58
59private:
60 const Type value;
61};
62
63template<typename T>
64bool operator==(uv_type_wrapper<T> lhs, uv_type_wrapper<T> rhs) {
65 return !(lhs == rhs);
66}
67
68} // namespace details
69
73struct win_size {
74 int width;
75 int height;
76};
77
78using handle_type = details::uvw_handle_type;
79using handle_category = details::uv_type_wrapper<uv_handle_type>;
80using file_handle = details::uv_type_wrapper<uv_file>;
81using os_socket_handle = details::uv_type_wrapper<uv_os_sock_t>;
82using os_file_descriptor = details::uv_type_wrapper<uv_os_fd_t>;
83using pid_type = details::uv_type_wrapper<uv_pid_t>;
85constexpr file_handle std_in{0};
86constexpr file_handle std_out{1};
87constexpr file_handle std_err{2};
89using time_spec = uv_timespec_t;
90using file_info = uv_stat_t;
91using fs_info = uv_statfs_t;
92using uid_type = uv_uid_t;
93using gid_type = uv_gid_t;
95using timeval = uv_timeval_t;
96using timeval64 = uv_timeval64_t;
97using resource_usage = uv_rusage_t;
108 passwd_info(std::shared_ptr<uv_passwd_t> pwd);
109
114 std::string username() const noexcept;
115
120 decltype(uv_passwd_t::uid) uid() const noexcept;
121
126 decltype(uv_passwd_t::gid) gid() const noexcept;
127
132 std::string shell() const noexcept;
133
138 std::string homedir() const noexcept;
139
144 operator bool() const noexcept;
145
146private:
147 std::shared_ptr<uv_passwd_t> value;
148};
149
159struct uts_name {
160 uts_name(std::shared_ptr<uv_utsname_t> init);
161
166 std::string sysname() const noexcept;
167
172 std::string release() const noexcept;
173
178 std::string version() const noexcept;
179
184 std::string machine() const noexcept;
185
186private:
187 std::shared_ptr<uv_utsname_t> uname;
188};
189
195struct ipv4 {};
196
202struct ipv6 {};
203
208 std::string ip;
209 unsigned int port;
210};
211
215struct cpu_info {
216 using cpu_time = decltype(uv_cpu_info_t::cpu_times);
217
218 std::string model;
219 int speed;
227 cpu_time times;
228};
229
234 std::string name;
235 char physical[6];
236 bool internal;
239};
240
241namespace details {
242
243static constexpr std::size_t DEFAULT_SIZE = 128;
244
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];
249 std::string str{};
250 auto err = std::forward<F>(f)(args..., buf, &size);
251
252 if(UV_ENOBUFS == err) {
253 std::unique_ptr<char[]> data{new char[size]};
254 err = std::forward<F>(f)(args..., data.get(), &size);
255
256 if(0 == err) {
257 str = data.get();
258 }
259 } else if(0 == err) {
260 str.assign(buf, size);
261 }
262
263 return str;
264}
265
266void common_alloc_callback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf);
267
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);
273
274} // namespace details
275
281struct utilities {
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 *);
286
290 struct os {
300 static pid_type pid() noexcept;
301
311 static pid_type ppid() noexcept;
312
323 static std::string homedir() noexcept;
324
334 static std::string tmpdir() noexcept;
335
342 static std::string env(const std::string &name) noexcept;
343
351 static bool env(const std::string &name, const std::string &value) noexcept;
352
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;
370 int count{};
371
372 const bool ret = (uv_os_environ(&items, &count) == 0);
373
374 if(ret) {
375 for(int pos = 0; pos < count; ++pos) {
376 func(std::string_view{items[pos].name}, std::string_view{items[pos].value});
377 }
378
379 uv_os_free_environ(items, count);
380 }
381
382 return ret;
383 }
384
389 static std::string hostname() noexcept;
390
400 static uts_name uname() noexcept;
401
414 static passwd_info passwd() noexcept;
415
429 static int priority(pid_type pid);
430
446 static bool priority(pid_type pid, int prio);
447 };
448
454 static handle_type guess_handle(handle_category category) noexcept;
455
474 static handle_type guess_handle(file_handle file) noexcept;
475
483 static std::vector<cpu_info> cpu() noexcept;
484
493 static std::vector<interface_address> interface_addresses() noexcept;
494
508 static std::string index_to_name(unsigned int index) noexcept;
509
520 static std::string index_to_iid(unsigned int index) noexcept;
521
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;
546
551 static std::array<double, 3> load_average() noexcept;
552
560 static char **setup_args(int argc, char **argv);
561
566 static std::string process_title();
567
573 static bool process_title(const std::string &title);
574
579 static uint64_t total_memory() noexcept;
580
592 static uint64_t constrained_memory() noexcept;
593
598 static double uptime() noexcept;
599
604 static resource_usage rusage() noexcept;
605
616 static uint64_t hrtime() noexcept;
617
622 static std::string path() noexcept;
623
628 static std::string cwd() noexcept;
629
635 static bool chdir(const std::string &dir) noexcept;
636
642 static timeval64 time_of_day() noexcept;
643
648 static void sleep(unsigned int msec) noexcept;
649
655 static unsigned int available_parallelism() noexcept;
656};
657
662template<class... Func>
663struct overloaded: Func... {
664 using Func::operator()...;
665};
666
671template<class... Func>
672overloaded(Func...) -> overloaded<Func...>;
673
674} // namespace uvw
675
676#ifndef UVW_AS_LIB
677# include "util.cpp"
678#endif
679
680#endif // UVW_UTIL_INCLUDE_H
uvw default namespace.
Definition: async.h:8
uv_uid_t uid_type
Definition: util.h:92
details::uv_type_wrapper< uv_os_fd_t > os_file_descriptor
Definition: util.h:82
details::uv_type_wrapper< uv_file > file_handle
Definition: util.h:80
details::uv_type_wrapper< uv_pid_t > pid_type
Definition: util.h:83
uv_timeval_t timeval
Definition: util.h:95
constexpr file_handle std_in
Definition: util.h:85
uv_timespec_t time_spec
Definition: util.h:89
uv_gid_t gid_type
Definition: util.h:93
uv_timeval64_t timeval64
Definition: util.h:96
uv_stat_t file_info
Definition: util.h:90
details::uv_type_wrapper< uv_handle_type > handle_category
Definition: util.h:79
constexpr file_handle std_err
Definition: util.h:87
details::uv_type_wrapper< uv_os_sock_t > os_socket_handle
Definition: util.h:81
uv_rusage_t resource_usage
Definition: util.h:97
uv_statfs_t fs_info
Definition: util.h:91
constexpr file_handle std_out
Definition: util.h:86
CPU information.
Definition: util.h:215
std::string model
Definition: util.h:218
int speed
Definition: util.h:219
cpu_time times
CPU times.
Definition: util.h:227
Interface address.
Definition: util.h:233
socket_address netmask
Definition: util.h:238
socket_address address
Definition: util.h:237
std::string name
Definition: util.h:234
The IPv4 tag.
Definition: util.h:195
The IPv6 tag.
Definition: util.h:202
Helper type for visitors.
Definition: util.h:663
Utility class.
Definition: util.h:107
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:207
std::string ip
Definition: util.h:208
unsigned int port
Definition: util.h:209
OS dedicated utilities.
Definition: util.h:290
static std::string hostname() noexcept
Returns the hostname.
static pid_type pid() noexcept
Returns the current process id.
Miscellaneous utilities.
Definition: util.h:281
Utility class.
Definition: util.h:159
std::string sysname() const noexcept
Gets the operating system name (like "Linux").
Windows size representation.
Definition: util.h:73
int width
Definition: util.h:74
int height
Definition: util.h:75