Veethree D7 SDK
Loading...
Searching...
No Matches
syscall_wrappers.h
Go to the documentation of this file.
1
5#ifndef SYSCALL_WRAPPERS_H
6#define SYSCALL_WRAPPERS_H
7
8#include <memory>
9#include <stdexcept>
10#include <string>
11#include <utility>
12
13#include <fcntl.h>
14#include <sys/stat.h>
15#include <sys/types.h>
16
17#ifdef __linux__
18#include <sys/ioctl.h>
19#endif
20
21namespace vsdk::utils {
22
27
34template<typename T, size_t N>
35T& at(T (&arr)[N], size_t index) // NOLINT
36{
37 if (index >= N)
38 throw std::logic_error("index out of bounds");
39
40 return arr[index];
41}
42
48template<typename T, size_t N>
49size_t array_size(T (&arr)[N]) // NOLINT
50{
51 return N;
52}
53
59template<typename T>
60using unique_ptr_free = std::unique_ptr<T, decltype([](T* t) { free(t); })>;
61
68int open(const std::string& path, int flags);
69
76#ifdef __linux__
77template<typename... Args>
78int ioctl(Args&&... args)
79{
80 // NOLINTNEXTLINE(*-vararg)
81 return ::ioctl(std::forward<Args>(args)...);
82}
83#else
84// NOLINTBEGIN(*missing-std-forward)
85template<typename... Args>
86int ioctl([[maybe_unused]] Args&&... args)
87{
88 return 0;
89}
90// NOLINTEND(*missing-std-forward)
91#endif
92
98int system(const std::string& cmd);
99
105std::string errno_string(int err);
106
116template<typename... Args>
117#ifdef __linux__
118int fcntl(int fd, int cmd, Args&&... args)
119{
120 // NOLINTNEXTLINE(*-vararg)
121 return ::fcntl(fd, cmd, std::forward<Args>(args)...);
122}
123#else
124int fcntl([[maybe_unused]] int fd, [[maybe_unused]] int cmd, [[maybe_unused]] Args&&... args)
125{
126 return 0;
127}
128#endif
129
131
132} // namespace vsdk::utils
133
134#endif // SYSCALL_WRAPPERS_H
@ system
System module.
Definition log.h:39
T & at(T(&arr)[N], size_t index)
Return a reference to an array element.
Definition syscall_wrappers.h:35
std::string errno_string(int err)
Wrapper for strerror that returns a std::string.
std::unique_ptr< T, decltype([](T *t) { free(t);})> unique_ptr_free
Convenience wrapper on top of std::unique_ptr that uses free() instead of delete() for managing objec...
Definition syscall_wrappers.h:60
int open(const std::string &path, int flags)
Wrapper implementation that deals with C++ linter (clang-tidy)
int fcntl(int fd, int cmd, Args &&... args)
Wrapper for fcntl that deals with C++ linter (clang-tidy)
Definition syscall_wrappers.h:124
int ioctl(Args &&... args)
Wrapper implementation that deals with C++ linter (clang-tidy)
Definition syscall_wrappers.h:86
size_t array_size(T(&arr)[N])
Return the size of a fixed size array.
Definition syscall_wrappers.h:49
Any entities that belong to the utils library.