diff options
Diffstat (limited to 'src/modules/serial_echo/unique_file.hpp')
-rw-r--r-- | src/modules/serial_echo/unique_file.hpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/modules/serial_echo/unique_file.hpp b/src/modules/serial_echo/unique_file.hpp new file mode 100644 index 000000000..0735f7389 --- /dev/null +++ b/src/modules/serial_echo/unique_file.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include <unistd.h> + +class unique_file { +private: + int fd; + +public: + inline + unique_file() : fd(-1) {} + + inline + unique_file(int fd) : fd(fd) {} + + inline + unique_file(unique_file && other) : fd(other.fd) { + other.fd = -1; + } + + inline + ~unique_file() { + if (fd > -1) close(fd); + } + + inline + int get() { return fd; } + + friend inline std::size_t + read(unique_file & uf, void * buf, size_t buf_size) + { return ::read(uf.fd, buf, buf_size); } + + friend inline std::size_t + write(unique_file & uf, const void * buf, size_t buf_size) + { return ::write(uf.fd, buf, buf_size); } +}; |