Add the lttng::eventfd class
[lttng-tools.git] / src / common / file-descriptor.hpp
index e8ddfe7a144815c15c8b729fd53027e0e923bd9d..164842f86ba8184d2cec46715a43be031ba8e606 100644 (file)
@@ -8,75 +8,45 @@
 #ifndef LTTNG_FILE_DESCRIPTOR_HPP
 #define LTTNG_FILE_DESCRIPTOR_HPP
 
-#include <common/error.hpp>
-#include <common/format.hpp>
-
-#include <algorithm>
-
-#include <unistd.h>
+#include <cstddef>
 
 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:
-       file_descriptor()
-       {
-       }
-
-       explicit file_descriptor(int raw_fd) noexcept : _raw_fd{ raw_fd }
-       {
-               LTTNG_ASSERT(_is_valid_fd(_raw_fd));
-       }
+       file_descriptor() noexcept;
 
+       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&& other)
-       {
-               _cleanup();
-               std::swap(_raw_fd, other._raw_fd);
-               return *this;
-       }
 
-       file_descriptor(file_descriptor&& other) noexcept
-       {
-               std::swap(_raw_fd, other._raw_fd);
-       }
+       file_descriptor(file_descriptor&& other) noexcept;
 
-       ~file_descriptor()
-       {
-               _cleanup();
-       }
+       file_descriptor& operator=(file_descriptor&& other) noexcept;
 
-       int fd() const noexcept
-       {
-               LTTNG_ASSERT(_is_valid_fd(_raw_fd));
-               return _raw_fd;
-       }
+       ~file_descriptor() noexcept;
 
-private:
-       static bool _is_valid_fd(int fd)
-       {
-               return fd >= 0;
-       }
-
-       void _cleanup()
-       {
-               if (!_is_valid_fd(_raw_fd)) {
-                       return;
-               }
+       /*
+        * 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);
 
-               const auto ret = ::close(_raw_fd);
-
-               _raw_fd = -1;
-               if (ret) {
-                       PERROR("Failed to close file descriptor: fd=%i", _raw_fd);
-               }
-       }
+protected:
+       int _fd() const noexcept;
+       void _cleanup() noexcept;
 
+private:
        int _raw_fd = -1;
 };
 
This page took 0.027193 seconds and 4 git commands to generate.