X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Ffile-descriptor.hpp;h=b467e2e382d5bc293380ed880b779a2b02bc591b;hb=HEAD;hp=2a2d21b1ebb69a371b0d6474c388a44d522ed684;hpb=9d89db29f3bf6c826293350f8f1a8559ec906b24;p=lttng-tools.git diff --git a/src/common/file-descriptor.hpp b/src/common/file-descriptor.hpp index 2a2d21b1e..b467e2e38 100644 --- a/src/common/file-descriptor.hpp +++ b/src/common/file-descriptor.hpp @@ -5,60 +5,52 @@ * */ -#include -#include +#ifndef LTTNG_FILE_DESCRIPTOR_HPP +#define LTTNG_FILE_DESCRIPTOR_HPP -#include - -#include +#include namespace lttng { -/* - * RAII wrapper around a UNIX file descriptor. A file_descriptor's underlying - * file descriptor - */ +/* RAII wrapper around a UNIX file descriptor. */ class file_descriptor { public: - explicit file_descriptor(int raw_fd) noexcept : _raw_fd{raw_fd} - { - LTTNG_ASSERT(_is_valid_fd(_raw_fd)); - } + file_descriptor() noexcept = default; + explicit file_descriptor(int raw_fd) noexcept; file_descriptor(const file_descriptor&) = delete; file_descriptor& operator=(const file_descriptor&) = delete; - file_descriptor& operator=(file_descriptor&&) = delete; - file_descriptor(file_descriptor&& other) noexcept { - LTTNG_ASSERT(_is_valid_fd(_raw_fd)); - std::swap(_raw_fd, other._raw_fd); - } + file_descriptor(file_descriptor&& other) noexcept; - ~file_descriptor() - { - if (!_is_valid_fd(_raw_fd)) { - return; - } + file_descriptor& operator=(file_descriptor&& other) noexcept; - const auto ret = ::close(_raw_fd); - if (ret) { - PERROR("Failed to close file descriptor: fd=%i", _raw_fd); - } - } + ~file_descriptor() noexcept; - int fd() const noexcept - { - LTTNG_ASSERT(_is_valid_fd(_raw_fd)); - return _raw_fd; - } + /* + * Read `size` bytes from the underlying file descriptor, assuming + * raw_fd behaves as a blocking device. + * + * Throws an exception if the requested amount of bytes couldn't be read. + */ + void read(void *buffer, std::size_t size); + /* + * Write `size` bytes to the underlying file descriptor, assuming + * raw_fd behaves as a blocking device. + * + * Throws an exception if the requested amount of bytes couldn't be written. + */ + void write(const void *buffer, std::size_t size); -private: - static bool _is_valid_fd(int fd) - { - return fd >= 0; - } + int fd() const noexcept; +protected: + void _cleanup() noexcept; + +private: int _raw_fd = -1; }; } /* namespace lttng */ + +#endif /* LTTNG_FILE_DESCRIPTOR_HPP */