file-desrcriptor: add assignment operator
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 29 Mar 2023 19:13:18 +0000 (15:13 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 11 Apr 2023 20:26:58 +0000 (16:26 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I65ec468e0825801db47da6b1298d0bc09028415d

src/common/file-descriptor.hpp

index 8d6b64be4f51772fbb50692ca0577330294c7c2f..0ddc2b91c8f30a8d689a5be1b9fe26b8b0b412b9 100644 (file)
@@ -20,14 +20,23 @@ namespace lttng {
  */
 class file_descriptor {
 public:
-       explicit file_descriptor(int raw_fd) noexcept : _raw_fd{raw_fd}
+       file_descriptor()
+       {
+       }
+
+       explicit file_descriptor(int raw_fd) noexcept : _raw_fd{ raw_fd }
        {
                LTTNG_ASSERT(_is_valid_fd(_raw_fd));
        }
 
        file_descriptor(const file_descriptor&) = delete;
        file_descriptor& operator=(const file_descriptor&) = delete;
-       file_descriptor& operator=(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
        {
@@ -36,14 +45,7 @@ public:
 
        ~file_descriptor()
        {
-               if (!_is_valid_fd(_raw_fd)) {
-                       return;
-               }
-
-               const auto ret = ::close(_raw_fd);
-               if (ret) {
-                       PERROR("Failed to close file descriptor: fd=%i", _raw_fd);
-               }
+               _cleanup();
        }
 
        int fd() const noexcept
@@ -58,6 +60,20 @@ private:
                return fd >= 0;
        }
 
+       void _cleanup()
+       {
+               if (!_is_valid_fd(_raw_fd)) {
+                       return;
+               }
+
+               const auto ret = ::close(_raw_fd);
+
+               _raw_fd = -1;
+               if (ret) {
+                       PERROR("Failed to close file descriptor: fd=%i", _raw_fd);
+               }
+       }
+
        int _raw_fd = -1;
 };
 
This page took 0.026573 seconds and 4 git commands to generate.