uvw 3.1.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;
91constexpr file_handle std_in{0};
92constexpr file_handle std_out{1};
93constexpr file_handle std_err{2};
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;
101using timeval = uv_timeval_t;
102using timeval64 = uv_timeval64_t;
103using timespec64 = uv_timespec64_t;
104using resource_usage = uv_rusage_t;
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;
234 cpu_time times;
235};
236
241 std::string name;
242 char physical[6];
243 bool internal;
246};
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
275sockaddr ip_addr(const char *addr, unsigned int port);
276socket_address sock_addr(const sockaddr_in &addr);
277socket_address sock_addr(const sockaddr_in6 &addr);
278socket_address sock_addr(const sockaddr &addr);
279socket_address sock_addr(const sockaddr_storage &storage);
280
281} // namespace details
282
288struct utilities {
289 using malloc_func_type = void *(*)(size_t);
290 using realloc_func_type = void *(*)(void *, size_t);
291 using calloc_func_type = void *(*)(size_t, size_t);
292 using free_func_type = void (*)(void *);
293
297 struct os {
307 static pid_type pid() noexcept;
308
318 static pid_type ppid() noexcept;
319
330 static std::string homedir() noexcept;
331
341 static std::string tmpdir() noexcept;
342
349 static std::string env(const std::string &name) noexcept;
350
358 static bool env(const std::string &name, const std::string &value) noexcept;
359
373 template<typename Func>
374 static std::enable_if_t<std::is_invocable_v<Func, std::string_view, std::string_view>, bool>
375 env(Func func) noexcept {
376 uv_env_item_t *items = nullptr;
377 int count{};
378
379 const bool ret = (uv_os_environ(&items, &count) == 0);
380
381 if(ret) {
382 for(int pos = 0; pos < count; ++pos) {
383 func(std::string_view{items[pos].name}, std::string_view{items[pos].value});
384 }
385
386 uv_os_free_environ(items, count);
387 }
388
389 return ret;
390 }
391
396 static std::string hostname() noexcept;
397
407 static uts_name uname() noexcept;
408
421 static passwd_info passwd() noexcept;
422
436 static int priority(pid_type pid);
437
453 static bool priority(pid_type pid, int prio);
454 };
455
461 static handle_type guess_handle(handle_category category) noexcept;
462
481 static handle_type guess_handle(file_handle file) noexcept;
482
490 static std::vector<cpu_info> cpu() noexcept;
491
500 static std::vector<interface_address> interface_addresses() noexcept;
501
515 static std::string index_to_name(unsigned int index) noexcept;
516
527 static std::string index_to_iid(unsigned int index) noexcept;
528
552 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;
553
558 static std::array<double, 3> load_average() noexcept;
559
567 static char **setup_args(int argc, char **argv);
568
573 static std::string process_title();
574
580 static bool process_title(const std::string &title);
581
586 static uint64_t total_memory() noexcept;
587
599 static uint64_t constrained_memory() noexcept;
600
605 static uint64_t available_memory() noexcept;
606
611 static double uptime() noexcept;
612
617 static resource_usage rusage() noexcept;
618
624 static timespec64 gettime(clock_id source) noexcept;
625
636 static uint64_t hrtime() noexcept;
637
642 static std::string path() noexcept;
643
648 static std::string cwd() noexcept;
649
655 static bool chdir(const std::string &dir) noexcept;
656
662 static timeval64 time_of_day() noexcept;
663
668 static void sleep(unsigned int msec) noexcept;
669
675 static unsigned int available_parallelism() noexcept;
676};
677
682template<class... Func>
683struct overloaded: Func... {
684 using Func::operator()...;
685};
686
691template<class... Func>
692overloaded(Func...) -> overloaded<Func...>;
693
694} // namespace uvw
695
696#ifndef UVW_AS_LIB
697# include "util.cpp"
698#endif
699
700#endif // UVW_UTIL_INCLUDE_H
uvw default namespace.
Definition: async.h:8
uv_uid_t uid_type
Definition: util.h:98
details::uv_type_wrapper< uv_os_fd_t > os_file_descriptor
Definition: util.h:87
details::uv_type_wrapper< uv_file > file_handle
Definition: util.h:85
details::uv_type_wrapper< uv_pid_t > pid_type
Definition: util.h:88
uv_timeval_t timeval
Definition: util.h:101
constexpr file_handle std_in
Definition: util.h:91
uv_timespec_t time_spec
Definition: util.h:95
uv_gid_t gid_type
Definition: util.h:99
uv_timeval64_t timeval64
Definition: util.h:102
uv_stat_t file_info
Definition: util.h:96
details::uv_type_wrapper< uv_handle_type > handle_category
Definition: util.h:84
uv_timespec64_t timespec64
Definition: util.h:103
constexpr file_handle std_err
Definition: util.h:93
details::uv_type_wrapper< uv_os_sock_t > os_socket_handle
Definition: util.h:86
uv_rusage_t resource_usage
Definition: util.h:104
uv_statfs_t fs_info
Definition: util.h:97
constexpr file_handle std_out
Definition: util.h:92
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:683
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:297
static std::string hostname() noexcept
Returns the hostname.
static pid_type pid() noexcept
Returns the current process id.
Miscellaneous utilities.
Definition: util.h:288
Utility class.
Definition: util.h:166
std::string sysname() const noexcept
Gets the operating system name (like "Linux").
Windows size representation.
Definition: util.h:78
int width
Definition: util.h:79
int height
Definition: util.h:80