[vte] lib: Add smart FD helper class



commit 2aa91abdd34ef884430ae3d7de11ef6e198e0adf
Author: Christian Persch <chpe gnome org>
Date:   Sun Nov 29 20:57:55 2015 +0100

    lib: Add smart FD helper class
    
    Add class that closes the FD in the destructor.

 src/vtetypes.cc |   24 ++++++++++++++++++++++++
 src/vtetypes.hh |   21 +++++++++++++++++++++
 2 files changed, 45 insertions(+), 0 deletions(-)
---
diff --git a/src/vtetypes.cc b/src/vtetypes.cc
index 40116b3..8dbd2fd 100644
--- a/src/vtetypes.cc
+++ b/src/vtetypes.cc
@@ -35,6 +35,8 @@ 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");
+
 /* Colours mix */
 vte::color::rgb::rgb(vte::color::rgb const& foreground,
                      vte::color::rgb const& background,
@@ -301,6 +303,27 @@ test_util_restore_errno(void)
         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[])
 {
@@ -311,6 +334,7 @@ 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 6acc86c..108dded 100644
--- a/src/vtetypes.hh
+++ b/src/vtetypes.hh
@@ -145,6 +145,27 @@ namespace util {
                 int m_errsv;
         };
 
+        class smart_fd {
+        public:
+                smart_fd() : m_fd(-1) { }
+                explicit smart_fd(int fd) : m_fd(fd) { }
+                ~smart_fd() { if (m_fd != -1) { restore_errno errsv; close(m_fd); } }
+
+                inline smart_fd& operator = (int rhs) { if (m_fd != -1) { restore_errno errsv; close(m_fd); 
} m_fd = rhs; return *this; }
+                inline smart_fd& operator = (smart_fd& rhs) { if (&rhs != this) { m_fd = rhs.m_fd; rhs.m_fd 
= -1; } return *this; }
+                inline operator int () const { return m_fd; }
+                inline operator int* () { g_assert(m_fd == -1); return &m_fd; }
+
+                int steal() { auto d = m_fd; m_fd = -1; return d; }
+
+                /* Prevent accidents */
+                smart_fd(smart_fd const&) = delete;
+                smart_fd& operator = (smart_fd const&) = delete;
+
+        private:
+                int m_fd;
+        };
+
 } /* namespace util */
 
 } /* namespace vte */


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