[vte] lib: Rename some helper classes



commit 328ac0df7f98f51a8251fd88e819160ec362ea91
Author: Christian Persch <chpe src gnome org>
Date:   Mon Apr 27 20:49:04 2020 +0200

    lib: Rename some helper classes
    
    Move smart FD and errno helper classes to its own header, and
    rename them.

 src/libc-glue.hh | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/meson.build  |   8 +++--
 src/pty.cc       |  36 +++++++++----------
 src/pty.hh       |   7 ++--
 src/vtepty.cc    |  13 +++----
 src/vtetypes.cc  |  27 ++------------
 src/vtetypes.hh  |  36 -------------------
 7 files changed, 144 insertions(+), 88 deletions(-)
---
diff --git a/src/libc-glue.hh b/src/libc-glue.hh
new file mode 100644
index 00000000..4a088d48
--- /dev/null
+++ b/src/libc-glue.hh
@@ -0,0 +1,105 @@
+/*
+ * Copyright © 2015, 2020 Christian Persch
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <cassert>
+#include <cerrno>
+
+#include <unistd.h>
+
+namespace vte::libc {
+
+class ErrnoSaver {
+public:
+        ErrnoSaver() noexcept : m_errsv{errno} { }
+        ~ErrnoSaver() noexcept { errno = m_errsv; }
+
+        ErrnoSaver(ErrnoSaver const&) = delete;
+        ErrnoSaver(ErrnoSaver&&) = delete;
+        ErrnoSaver& operator=(ErrnoSaver const&) = delete;
+        ErrnoSaver& operator=(ErrnoSaver&&) = delete;
+
+        inline constexpr operator int () const noexcept { return m_errsv; }
+
+private:
+        int m_errsv;
+}; // class ErrnoSaver
+
+class FD {
+public:
+        constexpr FD() noexcept = default;
+        explicit constexpr FD(int fd) noexcept : m_fd{fd} { } // adopts the FD
+        constexpr FD(FD const&) = delete;
+        constexpr FD(FD&& rhs) noexcept : m_fd{rhs.release()} { }
+
+        ~FD() { reset(); }
+
+        // adopt the file descriptor
+        FD& operator=(int rhs) noexcept
+        {
+                reset();
+                m_fd = rhs;
+                return *this;
+        }
+
+        FD& operator=(FD& rhs) = delete;
+
+#if 0
+        FD& operator=(FD& rhs) noexcept
+        {
+                if (&rhs != this) {
+                        reset();
+                        m_fd = rhs.release();
+                }
+                return *this;
+        }
+#endif
+
+        FD& operator=(FD&& rhs) noexcept
+        {
+                reset();
+                m_fd = rhs.release();
+                return *this;
+        }
+
+        constexpr operator int () const noexcept { return m_fd; }
+        constexpr operator int* () noexcept      { assert(m_fd == -1); return &m_fd; }
+        constexpr operator bool() const noexcept { return m_fd != -1; }
+
+        constexpr int release() noexcept
+        {
+                auto fd = m_fd;
+                m_fd = -1;
+                return fd;
+        }
+
+        void reset()
+        {
+                if (m_fd != -1) {
+                        auto errsv = ErrnoSaver{};
+                        close(m_fd);
+                        m_fd = -1;
+                }
+        }
+
+private:
+        int m_fd{-1};
+
+}; // class FD
+
+} // namespace vte::libc
diff --git a/src/meson.build b/src/meson.build
index 2802aa8b..1c5cad90 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -41,6 +41,10 @@ icu_sources = files(
   'icu-glue.hh',
 )
 
+libc_glue_sources = files(
+  'libc-glue.hh',
+)
+
 modes_sources = files(
   'modes-ecma.hh',
   'modes-private.hh',
@@ -89,7 +93,7 @@ utf8_sources = files(
   'utf8.hh',
 )
 
-libvte_common_sources = debug_sources + glib_glue_sources + modes_sources + parser_sources + pty_sources + 
regex_sources + utf8_sources + files(
+libvte_common_sources = debug_sources + glib_glue_sources + libc_glue_sources + modes_sources + 
parser_sources + pty_sources + regex_sources + utf8_sources + files(
   'attr.hh',
   'bidi.cc',
   'bidi.hh',
@@ -484,7 +488,7 @@ test_utf8 = executable(
   install: false,
 )
 
-test_vtetypes_sources = files(
+test_vtetypes_sources = libc_glue_sources + files(
    'vtetypes.cc',
    'vtetypes.hh',
 )
diff --git a/src/pty.cc b/src/pty.cc
index b0f56af8..5948f167 100644
--- a/src/pty.cc
+++ b/src/pty.cc
@@ -30,9 +30,10 @@
 
 #include "pty.hh"
 
+#include "libc-glue.hh"
+
 #include <vte/vte.h>
 #include "vteptyinternal.hh"
-#include "vtetypes.hh"
 #include "vtespawn.hh"
 
 #include <assert.h>
@@ -405,7 +406,7 @@ Pty::spawn(char const* directory,
 #endif
 
         if (cancellable && !g_cancellable_make_pollfd(cancellable, &pollfd)) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 g_set_error(error,
                             G_IO_ERROR,
                             g_io_error_from_errno(errsv),
@@ -538,7 +539,7 @@ Pty::set_size(int rows,
         auto ret = ioctl(master, TIOCSWINSZ, &size);
 
         if (ret != 0) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY,
                                  "Failed to set size on %d: %m\n", master);
         }
@@ -579,7 +580,7 @@ Pty::get_size(int* rows,
                 return true;
        }
 
-        vte::util::restore_errno errsv;
+        auto errsv = vte::libc::ErrnoSaver{};
         _vte_debug_print(VTE_DEBUG_PTY,
                          "Failed to read size from fd %d: %m\n", master);
 
@@ -620,21 +621,21 @@ static int
 fd_setup(int fd)
 {
         if (fd_set_cloexec(fd) < 0) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY,
                                  "%s failed: %s", "Setting CLOEXEC flag", g_strerror(errsv));
                 return -1;
         }
 
         if (fd_set_nonblocking(fd) < 0) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY,
                                  "%s failed: %s", "Setting O_NONBLOCK flag", g_strerror(errsv));
                 return -1;
         }
 
         if (fd_set_cpkt(fd) < 0) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY,
                                  "%s failed: %s", "ioctl(TIOCPKT)", g_strerror(errsv));
                 return -1;
@@ -656,8 +657,7 @@ static int
 _vte_pty_open_posix(void)
 {
        /* Attempt to open the master. */
-        vte::util::smart_fd fd;
-        fd = posix_openpt(O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC);
+        auto fd = vte::libc::FD{posix_openpt(O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC)};
 #ifndef __linux__
         /* Other kernels may not support CLOEXEC or NONBLOCK above, so try to fall back */
         bool need_cloexec = false, need_nonblocking = false;
@@ -674,7 +674,7 @@ _vte_pty_open_posix(void)
 #endif /* !linux */
 
         if (fd == -1) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY,
                                  "%s failed: %s", "posix_openpt", g_strerror(errsv));
                 return -1;
@@ -682,14 +682,14 @@ _vte_pty_open_posix(void)
 
 #ifndef __linux__
         if (need_cloexec && fd_set_cloexec(fd) < 0) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY,
                                  "%s failed: %s", "Setting CLOEXEC flag", g_strerror(errsv));
                 return -1;
         }
 
         if (need_nonblocking && fd_set_nonblocking(fd) < 0) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY,
                                  "%s failed: %s", "Setting NONBLOCK flag", g_strerror(errsv));
                 return -1;
@@ -697,7 +697,7 @@ _vte_pty_open_posix(void)
 #endif /* !linux */
 
         if (fd_set_cpkt(fd) < 0) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY,
                                  "%s failed: %s", "ioctl(TIOCPKT)", g_strerror(errsv));
                 return -1;
@@ -705,13 +705,13 @@ _vte_pty_open_posix(void)
 
        _vte_debug_print(VTE_DEBUG_PTY, "Allocated pty on fd %d.\n", (int)fd);
 
-        return fd.steal();
+        return fd.release();
 }
 
 static int
 _vte_pty_open_foreign(int masterfd /* consumed */)
 {
-        vte::util::smart_fd fd(masterfd);
+        auto fd = vte::libc::FD{masterfd};
         if (fd == -1) {
                 errno = EBADF;
                 return -1;
@@ -720,7 +720,7 @@ _vte_pty_open_foreign(int masterfd /* consumed */)
         if (fd_setup(fd) < 0)
                 return -1;
 
-        return fd.steal();
+        return fd.release();
 }
 
 /*
@@ -739,7 +739,7 @@ Pty::set_utf8(bool utf8) const noexcept
 #ifdef IUTF8
        struct termios tio;
         if (tcgetattr(fd(), &tio) == -1) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m", "tcgetattr");
                 return false;
         }
@@ -754,7 +754,7 @@ Pty::set_utf8(bool utf8) const noexcept
         /* Only set the flag if it changes */
         if (saved_cflag != tio.c_iflag &&
             tcsetattr(fd(), TCSANOW, &tio) == -1) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m", "tcsetattr");
                 return false;
        }
diff --git a/src/pty.hh b/src/pty.hh
index f03d2791..69034a35 100644
--- a/src/pty.hh
+++ b/src/pty.hh
@@ -19,8 +19,11 @@
 
 #include <memory>
 
+#include <gio/gio.h>
+
 #include "vte/vteenums.h"
-#include "vtetypes.hh"
+
+#include "libc-glue.hh"
 
 namespace vte::base {
 
@@ -33,7 +36,7 @@ private:
                 void* data{nullptr};
         } m_extra_child_setup;
 
-        vte::util::smart_fd m_pty_fd{};
+        vte::libc::FD m_pty_fd{};
 
         VtePtyFlags m_flags{VTE_PTY_DEFAULT};
 
diff --git a/src/vtepty.cc b/src/vtepty.cc
index 5dfba033..8428b460 100644
--- a/src/vtepty.cc
+++ b/src/vtepty.cc
@@ -29,8 +29,6 @@
 #include <config.h>
 
 #include <vte/vte.h>
-#include "vtetypes.hh"
-#include "vtespawn.hh"
 
 #include <errno.h>
 #include <glib.h>
@@ -39,7 +37,10 @@
 
 #include <glib/gi18n-lib.h>
 
+#include "libc-glue.hh"
 #include "pty.hh"
+#include "vtespawn.hh"
+
 #include "vteptyinternal.hh"
 
 #if !GLIB_CHECK_VERSION(2, 42, 0)
@@ -224,7 +225,7 @@ vte_pty_set_size(VtePty *pty,
         if (impl->set_size(rows, columns))
                 return true;
 
-        vte::util::restore_errno errsv;
+        auto errsv = vte::libc::ErrnoSaver{};
         g_set_error(error, G_IO_ERROR,
                     g_io_error_from_errno(errsv),
                     "Failed to set window size: %s",
@@ -259,7 +260,7 @@ vte_pty_get_size(VtePty *pty,
         if (impl->get_size(rows, columns))
                 return true;
 
-        vte::util::restore_errno errsv;
+        auto errsv = vte::libc::ErrnoSaver{};
         g_set_error(error, G_IO_ERROR,
                     g_io_error_from_errno(errsv),
                     "Failed to get window size: %s",
@@ -291,7 +292,7 @@ vte_pty_set_utf8(VtePty *pty,
         if (impl->set_utf8(utf8))
                 return true;
 
-        vte::util::restore_errno errsv;
+        auto errsv = vte::libc::ErrnoSaver{};
         g_set_error(error, G_IO_ERROR, g_io_error_from_errno(errsv),
                     "%s failed: %s", "tc[sg]etattr", g_strerror(errsv));
         return false;
@@ -343,7 +344,7 @@ vte_pty_initable_init (GInitable *initable,
         }
 
         if (priv->pty == nullptr) {
-                vte::util::restore_errno errsv;
+                auto errsv = vte::libc::ErrnoSaver{};
                 g_set_error(error, G_IO_ERROR, g_io_error_from_errno(errsv),
                             "Failed to open PTY: %s", g_strerror(errsv));
                 return FALSE;
diff --git a/src/vtetypes.cc b/src/vtetypes.cc
index c657cc25..c9783529 100644
--- a/src/vtetypes.cc
+++ b/src/vtetypes.cc
@@ -20,6 +20,7 @@
 #include <glib.h>
 
 #include "vtetypes.hh"
+#include "libc-glue.hh"
 
 #include <type_traits>
 
@@ -33,7 +34,7 @@ static_assert(sizeof(vte::view::coords) == 2 * sizeof(vte::view::coord_t), "vte:
 static_assert(std::is_pod<vte::color::rgb>::value, "vte::color::rgb not POD");
 static_assert(sizeof(vte::color::rgb) == sizeof(PangoColor), "vte::color::rgb size wrong");
 
-static_assert(sizeof(vte::util::smart_fd) == sizeof(int), "vte::util::smart_fd size wrong");
+static_assert(sizeof(vte::libc::FD) == sizeof(int), "vte::libc::FD size wrong");
 
 vte::color::rgb::rgb(GdkRGBA const* rgba) {
         g_assert(rgba);
@@ -411,33 +412,12 @@ test_util_restore_errno(void)
 {
         errno = -42;
         {
-                vte::util::restore_errno errsv;
+                vte::libc::ErrnoSaver errsv;
                 errno = 36;
         }
         g_assert_cmpint(errno, ==, -42);
 }
 
-static void
-test_util_smart_fd(void)
-{
-        vte::util::smart_fd fd2;
-        g_assert_true(fd2 == -1);
-
-        fd2 = 42;
-        g_assert_true(fd2 == 42);
-
-        vte::util::smart_fd fd3(STDERR_FILENO);
-        g_assert_true(fd3 != -1);
-        g_assert_true(fd3 == STDERR_FILENO);
-
-        g_assert_cmpint(fd3.steal(), ==, STDERR_FILENO);
-        g_assert_true(fd3 == -1);
-
-        int *v = fd3;
-        *v = 42;
-        g_assert_true(fd3 == 42);
-}
-
 int
 main(int argc, char *argv[])
 {
@@ -449,7 +429,6 @@ main(int argc, char *argv[])
         g_test_add_func("/vte/c++/color/rgb", test_color_rgb);
         g_test_add_func("/vte/c++/view/coords", test_view_coords);
         g_test_add_func("/vte/c++/util/restore-errno", test_util_restore_errno);
-        g_test_add_func("/vte/c++/util/smart-fd", test_util_smart_fd);
 
         return g_test_run();
 }
diff --git a/src/vtetypes.hh b/src/vtetypes.hh
index 51dc424a..f211b525 100644
--- a/src/vtetypes.hh
+++ b/src/vtetypes.hh
@@ -199,40 +199,4 @@ namespace color {
 
 } /* namespace color */
 
-namespace util {
-
-        class restore_errno {
-        public:
-                restore_errno() { m_errsv = errno; }
-                ~restore_errno() { errno = m_errsv; }
-                operator int () const { return m_errsv; }
-        private:
-                int m_errsv;
-        };
-
-        class smart_fd {
-        public:
-                constexpr smart_fd() noexcept = default;
-                explicit constexpr smart_fd(int fd) noexcept : m_fd{fd} { }
-                ~smart_fd() noexcept { if (m_fd != -1) { restore_errno errsv; close(m_fd); } }
-
-                inline smart_fd& operator = (int rhs) noexcept { if (m_fd != -1) { restore_errno errsv; 
close(m_fd); } m_fd = rhs; return *this; }
-                inline smart_fd& operator = (smart_fd& rhs) noexcept { if (&rhs != this) { if (m_fd != -1) { 
restore_errno errsv; close(m_fd); } m_fd = rhs.m_fd; rhs.m_fd = -1; } return *this; }
-                inline constexpr operator int () const noexcept { return m_fd; }
-                inline constexpr operator int* () noexcept { assert(m_fd == -1); return &m_fd; }
-
-                int steal() noexcept { auto d = m_fd; m_fd = -1; return d; }
-
-                /* Prevent accidents */
-                smart_fd(smart_fd const&) = delete;
-                smart_fd(smart_fd&&) = delete;
-                smart_fd& operator = (smart_fd const&) = delete;
-                smart_fd& operator = (smart_fd&&) = delete;
-
-        private:
-                int m_fd{-1};
-        };
-
-} /* namespace util */
-
 } /* namespace vte */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]