[vte] lib: Simplify smart pointers for glib/pango/pcre types



commit 095876ffca23d2f389c0a83a552164599c2f6e81
Author: Christian Persch <chpe src gnome org>
Date:   Thu Oct 22 20:34:51 2020 +0200

    lib: Simplify smart pointers for glib/pango/pcre types

 src/app/app.cc                    |  8 ++--
 src/app/meson.build               |  2 +-
 src/cairo-glue.hh                 |  9 +++--
 src/cxx-utils.hh                  |  4 +-
 src/drawing-cairo.cc              | 27 ++++++-------
 src/drawing-cairo.hh              |  2 +-
 src/glib-glue.hh                  |  7 ++++
 src/image.hh                      |  4 +-
 src/meson.build                   | 17 ++++++--
 src/pango-glue.hh                 | 29 ++++++++++++++
 src/parser-cat.cc                 |  1 +
 src/{vtepcre2.h => pcre2-glue.hh} | 10 +++++
 src/regex.cc                      | 24 +++++------
 src/regex.hh                      |  9 ++---
 src/ring.cc                       |  2 +-
 src/ring.hh                       |  2 +-
 src/sixel-context.cc              | 12 +++---
 src/sixel-context.hh              |  2 +-
 src/std-glue.hh                   | 39 ++++++++++++++++++
 src/systemd.cc                    |  7 ++--
 src/vte.cc                        | 84 +++++++++++++++++----------------------
 src/vteinternal.hh                | 17 ++++----
 src/vteregex.cc                   |  5 +--
 src/widget.cc                     |  2 +-
 24 files changed, 204 insertions(+), 121 deletions(-)
---
diff --git a/src/app/app.cc b/src/app/app.cc
index a615fd5b..6cd4e844 100644
--- a/src/app/app.cc
+++ b/src/app/app.cc
@@ -33,13 +33,14 @@
 #include <gtk/gtk.h>
 #include <cairo/cairo-gobject.h>
 #include <vte/vte.h>
-#include "vtepcre2.h"
 
 #include <algorithm>
 #include <vector>
 
 #include "glib-glue.hh"
 #include "libc-glue.hh"
+#include "pango-glue.hh"
+#include "pcre2-glue.hh"
 #include "refptr.hh"
 
 /* options */
@@ -2128,9 +2129,8 @@ vteapp_window_constructed(GObject *object)
 
         /* Style */
         if (options.font_string != nullptr) {
-                auto desc = pango_font_description_from_string(options.font_string);
-                vte_terminal_set_font(window->terminal, desc);
-                pango_font_description_free(desc);
+                auto desc = vte::take_freeable(pango_font_description_from_string(options.font_string));
+                vte_terminal_set_font(window->terminal, desc.get());
         }
 
         auto fg = options.get_color_fg();
diff --git a/src/app/meson.build b/src/app/meson.build
index 406e1707..9b98288f 100644
--- a/src/app/meson.build
+++ b/src/app/meson.build
@@ -14,7 +14,7 @@
 # You should have received a copy of the GNU Lesser General Public License
 # along with this library.  If not, see <https://www.gnu.org/licenses/>.
 
-app_sources = glib_glue_sources + libc_glue_sources + refptr_sources + files(
+app_sources = glib_glue_sources + libc_glue_sources + pango_glue_sources + pcre2_glue_sources + 
refptr_sources + std_glue_sources + files(
   'app.cc',
 )
 
diff --git a/src/cairo-glue.hh b/src/cairo-glue.hh
index fcb0bc48..5fe0e764 100644
--- a/src/cairo-glue.hh
+++ b/src/cairo-glue.hh
@@ -11,7 +11,7 @@
  * 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
+ * You should have received a copy of the GNU Lesser General Public License
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
@@ -19,10 +19,11 @@
 
 #include <cairo.h>
 
-#include "cxx-utils.hh"
+#include "std-glue.hh"
 
-namespace vte::cairo {
+namespace vte {
 
-using Surface = vte::FreeablePtr<cairo_surface_t, decltype(&cairo_surface_destroy), &cairo_surface_destroy>;
+VTE_DECLARE_FREEABLE(cairo_t, cairo_destroy);
+VTE_DECLARE_FREEABLE(cairo_surface_t, cairo_surface_destroy);
 
 } // namespace vte::cairo
diff --git a/src/cxx-utils.hh b/src/cxx-utils.hh
index 32ecd4f2..f98bfd4d 100644
--- a/src/cxx-utils.hh
+++ b/src/cxx-utils.hh
@@ -55,7 +55,7 @@ inline void log_exception() noexcept { }
 #endif
 
 template <typename T, typename D, D func>
-class FreeableDeleter {
+class FreeablePtrDeleter {
 public:
         void operator()(T* obj) const
         {
@@ -65,6 +65,6 @@ public:
 };
 
 template <typename T, typename D, D func>
-using FreeablePtr = std::unique_ptr<T, FreeableDeleter<T, D, func>>;
+using FreeablePtr = std::unique_ptr<T, FreeablePtrDeleter<T, D, func>>;
 
 } // namespace vte
diff --git a/src/drawing-cairo.cc b/src/drawing-cairo.cc
index 4a3de9b5..4136d51d 100644
--- a/src/drawing-cairo.cc
+++ b/src/drawing-cairo.cc
@@ -509,9 +509,8 @@ DrawingContext::draw_undercurl(int x,
                 /* Cache the undercurl's look. The design assumes that until the cached look is
                  * invalidated (the font is changed), this method is always called with the "y"
                  * parameter having the same fractional part, and the same "line_width" parameter.
-                 * For caching, only the fractional part of "y" is used. */
-                cairo_t *undercurl_cr;
-
+                 * For caching, only the fractional part of "y" is used.
+                 */
                 double rad = _vte_draw_get_undercurl_rad(m_cell_width);
                 double y_bottom = y + _vte_draw_get_undercurl_height(m_cell_width, line_width);
                 double y_center = (y + y_bottom) / 2.;
@@ -521,19 +520,19 @@ DrawingContext::draw_undercurl(int x,
                                   "caching undercurl shape\n");
 
                 /* Add a line_width of margin horizontally on both sides, for nice antialias overflowing. */
-                m_undercurl_surface.reset(cairo_surface_create_similar (cairo_get_target (m_cr),
-                                                                        CAIRO_CONTENT_ALPHA,
-                                                                        m_cell_width + 2 * x_padding,
-                                                                        surface_bottom - surface_top));
-                undercurl_cr = cairo_create (m_undercurl_surface.get());
-                cairo_set_operator (undercurl_cr, CAIRO_OPERATOR_OVER);
+                m_undercurl_surface = vte::take_freeable
+                        (cairo_surface_create_similar(cairo_get_target(m_cr),
+                                                      CAIRO_CONTENT_ALPHA,
+                                                      m_cell_width + 2 * x_padding,
+                                                      surface_bottom - surface_top));
+                auto undercurl_cr = vte::take_freeable(cairo_create(m_undercurl_surface.get()));
+                cairo_set_operator(undercurl_cr.get(), CAIRO_OPERATOR_OVER);
                 /* First quarter circle, similar to the left half of the tilde symbol. */
-                cairo_arc (undercurl_cr, x_padding + m_cell_width / 4., y_center - surface_top + 
m_cell_width / 4., rad, M_PI * 5 / 4, M_PI * 7 / 4);
+                cairo_arc(undercurl_cr.get(), x_padding + m_cell_width / 4., y_center - surface_top + 
m_cell_width / 4., rad, M_PI * 5 / 4, M_PI * 7 / 4);
                 /* Second quarter circle, similar to the right half of the tilde symbol. */
-                cairo_arc_negative (undercurl_cr, x_padding + m_cell_width * 3 / 4., y_center - surface_top 
- m_cell_width / 4., rad, M_PI * 3 / 4, M_PI / 4);
-                cairo_set_line_width (undercurl_cr, line_width);
-                cairo_stroke (undercurl_cr);
-                cairo_destroy (undercurl_cr);
+                cairo_arc_negative(undercurl_cr.get(), x_padding + m_cell_width * 3 / 4., y_center - 
surface_top - m_cell_width / 4., rad, M_PI * 3 / 4, M_PI / 4);
+                cairo_set_line_width (undercurl_cr.get(), line_width);
+                cairo_stroke(undercurl_cr.get());
         }
 
         /* Paint the cached look of the undercurl using the desired look.
diff --git a/src/drawing-cairo.hh b/src/drawing-cairo.hh
index 62999a8e..8d862965 100644
--- a/src/drawing-cairo.hh
+++ b/src/drawing-cairo.hh
@@ -158,7 +158,7 @@ private:
         Minifont m_minifont{};
 
         /* Cache the undercurl's rendered look. */
-        vte::cairo::Surface m_undercurl_surface{};
+        vte::Freeable<cairo_surface_t> m_undercurl_surface{};
 
 }; // class DrawingContext
 
diff --git a/src/glib-glue.hh b/src/glib-glue.hh
index c50f67e1..4af9d199 100644
--- a/src/glib-glue.hh
+++ b/src/glib-glue.hh
@@ -25,6 +25,7 @@
 #include <glib.h>
 
 #include "cxx-utils.hh"
+#include "std-glue.hh"
 
 namespace vte::glib {
 
@@ -272,3 +273,9 @@ bool set_error_from_exception(GError** error
                               ) noexcept;
 
 } // namespace vte::glib
+
+namespace vte {
+
+VTE_DECLARE_FREEABLE(GVariant, g_variant_unref);
+
+} // namespace vte
diff --git a/src/image.hh b/src/image.hh
index 9537bbca..9571bd1f 100644
--- a/src/image.hh
+++ b/src/image.hh
@@ -28,7 +28,7 @@ namespace image {
 class Image {
 private:
         // Device-friendly Cairo surface
-        vte::cairo::Surface m_surface{};
+        vte::Freeable<cairo_surface_t> m_surface{};
 
         // Draw/prune priority, must be unique
         int m_priority;
@@ -46,7 +46,7 @@ private:
         int m_cell_height;
 
 public:
-        Image(vte::cairo::Surface surface,
+        Image(vte::Freeable<cairo_surface_t> surface,
               int priority,
               int width_pixels,
               int height_pixels,
diff --git a/src/meson.build b/src/meson.build
index 6e58f078..e0561faa 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -51,6 +51,10 @@ modes_sources = files(
   'modes.hh',
 )
 
+pango_glue_sources = files(
+  'pango-glue.hh',
+)
+
 parser_sources = files(
   'parser-arg.hh',
   'parser-charset-tables.hh',
@@ -83,6 +87,10 @@ parser_sources += custom_target(
   ],
 )
 
+pcre2_glue_sources = files(
+  'pcre2-glue.hh',
+)
+
 pty_sources = files(
   'pty.cc',
   'pty.hh',
@@ -113,6 +121,10 @@ sixel_sources = sixel_parser_sources + sixel_context_sources + files(
   'image.hh',
 )
 
+std_glue_sources = files(
+  'std-glue.hh',
+)
+
 systemd_sources = files(
   'systemd.cc',
   'systemd.hh',
@@ -123,7 +135,7 @@ utf8_sources = files(
   'utf8.hh',
 )
 
-libvte_common_sources = debug_sources + glib_glue_sources + libc_glue_sources + modes_sources + 
parser_sources + pty_sources + refptr_sources + regex_sources + utf8_sources + files(
+libvte_common_sources = debug_sources + glib_glue_sources + libc_glue_sources + modes_sources + 
pango_glue_sources + parser_sources + pcre2_glue_sources + pty_sources + refptr_sources + regex_sources + 
std_glue_sources + utf8_sources + files(
   'attr.hh',
   'bidi.cc',
   'bidi.hh',
@@ -163,7 +175,6 @@ libvte_common_sources = debug_sources + glib_glue_sources + libc_glue_sources +
   'vtegtk.cc',
   'vtegtk.hh',
   'vteinternal.hh',
-  'vtepcre2.h',
   'vteregex.cc',
   'vteregexinternal.hh',
   'vterowdata.cc',
@@ -323,7 +334,7 @@ decoder_cat = executable(
 
 # parser cat
 
-parser_cat_sources = glib_glue_sources + libc_glue_sources + parser_sources + utf8_sources + debug_sources + 
files(
+parser_cat_sources = glib_glue_sources + libc_glue_sources + parser_sources + std_glue_sources + 
utf8_sources + debug_sources + files(
   'parser-cat.cc',
   'vtedefines.hh',
 )
diff --git a/src/pango-glue.hh b/src/pango-glue.hh
new file mode 100644
index 00000000..55fa373a
--- /dev/null
+++ b/src/pango-glue.hh
@@ -0,0 +1,29 @@
+/*
+ * Copyright © 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 Lesser General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <pango/pango.h>
+
+#include "std-glue.hh"
+
+namespace vte {
+
+VTE_DECLARE_FREEABLE(PangoAttrList, pango_attr_list_unref);
+VTE_DECLARE_FREEABLE(PangoFontDescription, pango_font_description_free);
+
+} // namespace vte
diff --git a/src/parser-cat.cc b/src/parser-cat.cc
index 71175a32..6447584a 100644
--- a/src/parser-cat.cc
+++ b/src/parser-cat.cc
@@ -36,6 +36,7 @@
 #include "libc-glue.hh"
 #include "parser.hh"
 #include "parser-glue.hh"
+#include "std-glue.hh"
 #include "utf8.hh"
 #include "vtedefines.hh"
 
diff --git a/src/vtepcre2.h b/src/pcre2-glue.hh
similarity index 82%
rename from src/vtepcre2.h
rename to src/pcre2-glue.hh
index 1094edf0..dd235aae 100644
--- a/src/vtepcre2.h
+++ b/src/pcre2-glue.hh
@@ -26,3 +26,13 @@ static_assert(sizeof(PCRE2_UCHAR8) == sizeof (uint8_t), "PCRE2_UCHAR2 has wrong
 static_assert(sizeof(PCRE2_SIZE) == sizeof (size_t), "PCRE2_SIZE has wrong size");
 static_assert(PCRE2_UNSET == (size_t)-1, "PCRE2_UNSET has wrong value");
 static_assert(PCRE2_ZERO_TERMINATED == (size_t)-1, "PCRE2_ZERO_TERMINATED has wrong value");
+
+#include "std-glue.hh"
+
+namespace vte {
+
+VTE_DECLARE_FREEABLE(pcre2_code_8, pcre2_code_free_8);
+VTE_DECLARE_FREEABLE(pcre2_match_context_8, pcre2_match_context_free_8);
+VTE_DECLARE_FREEABLE(pcre2_match_data_8, pcre2_match_data_free_8);
+
+} // namespace vte
diff --git a/src/regex.cc b/src/regex.cc
index 52e4e0bf..c484de40 100644
--- a/src/regex.cc
+++ b/src/regex.cc
@@ -95,24 +95,24 @@ Regex::compile(Regex::Purpose purpose,
 
         int errcode;
         PCRE2_SIZE erroffset;
-        auto code = pcre2_compile_8((PCRE2_SPTR8)pattern.data(),
-                                    pattern.size(),
-                                    (uint32_t)flags |
-                                    PCRE2_UTF |
-                                    (flags & PCRE2_UTF ? PCRE2_NO_UTF_CHECK : 0) |
-                                    PCRE2_NEVER_BACKSLASH_C |
-                                    PCRE2_USE_OFFSET_LIMIT,
-                                    &errcode, &erroffset,
-                                    nullptr);
-
-        if (code == nullptr) {
+        auto code = vte::take_freeable(pcre2_compile_8((PCRE2_SPTR8)pattern.data(),
+                                                       pattern.size(),
+                                                       (uint32_t)flags |
+                                                       PCRE2_UTF |
+                                                       (flags & PCRE2_UTF ? PCRE2_NO_UTF_CHECK : 0) |
+                                                       PCRE2_NEVER_BACKSLASH_C |
+                                                       PCRE2_USE_OFFSET_LIMIT,
+                                                       &errcode, &erroffset,
+                                                       nullptr));
+
+        if (!code) {
                 set_gerror_from_pcre_error(errcode, error);
                 g_prefix_error(error, "Failed to compile pattern to regex at offset %" G_GSIZE_FORMAT ":",
                                erroffset);
                 return nullptr;
         }
 
-        return new Regex{code, purpose};
+        return new Regex{std::move(code), purpose};
 }
 
 /*
diff --git a/src/regex.hh b/src/regex.hh
index a115cdb4..d9fb6566 100644
--- a/src/regex.hh
+++ b/src/regex.hh
@@ -23,7 +23,7 @@
 
 #include <glib.h>
 
-#include "vtepcre2.h"
+#include "pcre2-glue.hh"
 
 #include "cxx-utils.hh"
 
@@ -48,15 +48,14 @@ public:
 private:
         mutable volatile int m_refcount{1};
 
-        using code_type = vte::FreeablePtr<pcre2_code_8, decltype(&pcre2_code_free_8), &pcre2_code_free_8>;
-        code_type m_code{};
+        vte::Freeable<pcre2_code_8> m_code{};
 
         Purpose m_purpose;
 
 public:
-        Regex(pcre2_code_8* code,
+        Regex(vte::Freeable<pcre2_code_8> code,
               Purpose purpose) noexcept :
-                m_code{code},
+                m_code{std::move(code)},
                 m_purpose{purpose}
         { }
 
diff --git a/src/ring.cc b/src/ring.cc
index be639f90..27fcec21 100644
--- a/src/ring.cc
+++ b/src/ring.cc
@@ -1677,7 +1677,7 @@ Ring::write_contents(GOutputStream* stream,
  * Append an image to the internal image list.
  */
 void
-Ring::append_image(vte::cairo::Surface surface,
+Ring::append_image(vte::Freeable<cairo_surface_t> surface,
                    int pixelwidth,
                    int pixelheight,
                    long left,
diff --git a/src/ring.hh b/src/ring.hh
index d84f75eb..281ec7c0 100644
--- a/src/ring.hh
+++ b/src/ring.hh
@@ -105,7 +105,7 @@ public:
                             GError** error);
 
 #ifdef WITH_SIXEL
-        void append_image (vte::cairo::Surface surface,
+        void append_image (vte::Freeable<cairo_surface_t> surface,
                            gint pixelwidth, gint pixelheight,
                            glong left, glong top,
                            glong cell_width, glong cell_height) /* throws */;
diff --git a/src/sixel-context.cc b/src/sixel-context.cc
index 50853944..7bfaeebe 100644
--- a/src/sixel-context.cc
+++ b/src/sixel-context.cc
@@ -457,7 +457,7 @@ Context::image_data() noexcept
                                                               [&](color_index_t pen) noexcept -> color_t { 
return m_colors[pen]; }));
 }
 
-vte::cairo::Surface
+vte::Freeable<cairo_surface_t>
 Context::image_cairo() noexcept
 {
         static cairo_user_data_key_t s_data_key;
@@ -467,11 +467,11 @@ Context::image_cairo() noexcept
                 return nullptr;
 
         auto const stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, image_width());
-        auto surface = vte::cairo::Surface{cairo_image_surface_create_for_data(data,
-                                                                               CAIRO_FORMAT_ARGB32,
-                                                                               image_width(),
-                                                                               image_height(),
-                                                                               stride)};
+        auto surface = vte::take_freeable(cairo_image_surface_create_for_data(data,
+                                                                              CAIRO_FORMAT_ARGB32,
+                                                                              image_width(),
+                                                                              image_height(),
+                                                                              stride));
 
 #ifdef VTE_DEBUG
         _VTE_DEBUG_IF(VTE_DEBUG_IMAGE) {
diff --git a/src/sixel-context.hh b/src/sixel-context.hh
index b1c910c5..23d43173 100644
--- a/src/sixel-context.hh
+++ b/src/sixel-context.hh
@@ -641,7 +641,7 @@ public:
         auto color(unsigned idx) const noexcept { return m_colors[idx]; }
 
 #ifdef VTE_COMPILATION
-        vte::cairo::Surface image_cairo() noexcept;
+        vte::Freeable<cairo_surface_t> image_cairo() noexcept;
 #endif
 
         void
diff --git a/src/std-glue.hh b/src/std-glue.hh
new file mode 100644
index 00000000..2a35b6a2
--- /dev/null
+++ b/src/std-glue.hh
@@ -0,0 +1,39 @@
+/*
+ * Copyright © 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 Lesser General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <memory>
+
+namespace vte {
+
+template<typename T>
+class FreeableDeleter;
+
+template<typename T>
+using Freeable = std::unique_ptr<T, FreeableDeleter<T>>;
+
+template<typename T>
+inline auto take_freeable(T* t) { return Freeable<T>{t}; }
+
+#define VTE_DECLARE_FREEABLE(T, func) \
+template<> \
+class FreeableDeleter<T> { \
+public: inline void operator()(T* t) { func(t); } \
+}
+
+} // namespace vte
diff --git a/src/systemd.cc b/src/systemd.cc
index fbd9d765..40d1ce5e 100644
--- a/src/systemd.cc
+++ b/src/systemd.cc
@@ -94,8 +94,8 @@ create_scope_for_pid_sync(pid_t pid,
         g_variant_builder_close(builder);
 
         // Create transient scope
-        auto reply = std::unique_ptr<GVariant, decltype(&g_variant_unref)>
-                {g_dbus_connection_call_sync(bus.get(),
+        auto reply = vte::take_freeable
+                (g_dbus_connection_call_sync(bus.get(),
                                              "org.freedesktop.systemd1",
                                              "/org/freedesktop/systemd1",
                                              "org.freedesktop.systemd1.Manager",
@@ -105,8 +105,7 @@ create_scope_for_pid_sync(pid_t pid,
                                              GDBusCallFlags{G_DBUS_CALL_FLAGS_NO_AUTO_START},
                                              timeout, // in ms
                                              cancellable,
-                                             error),
-                 &g_variant_unref};
+                                             error));
 
         return bool(reply);
 }
diff --git a/src/vte.cc b/src/vte.cc
index f02e4891..ddf5a251 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -1160,16 +1160,14 @@ Terminal::match_rowcol_to_offset(vte::grid::column_t column,
 }
 
 /* creates a pcre match context with appropriate limits */
-pcre2_match_context_8 *
+vte::Freeable<pcre2_match_context_8>
 Terminal::create_match_context()
 {
-        pcre2_match_context_8 *match_context;
+        auto context = vte::take_freeable(pcre2_match_context_create_8(nullptr /* general context */));
+        pcre2_set_match_limit_8(context.get(), 65536); /* should be plenty */
+        pcre2_set_recursion_limit_8(context.get(), 64); /* should be plenty */
 
-        match_context = pcre2_match_context_create_8(nullptr /* general context */);
-        pcre2_set_match_limit_8(match_context, 65536); /* should be plenty */
-        pcre2_set_recursion_limit_8(match_context, 64); /* should be plenty */
-
-        return match_context;
+        return context;
 }
 
 bool
@@ -1298,8 +1296,6 @@ Terminal::match_check_internal_pcre(vte::grid::column_t column,
                                     size_t* end)
 {
        gsize offset, sattr, eattr, start_blank, end_blank;
-        pcre2_match_data_8 *match_data;
-        pcre2_match_context_8 *match_context;
 
        _vte_debug_print(VTE_DEBUG_REGEX,
                          "Checking for pcre match at (%ld,%ld).\n", row, column);
@@ -1311,15 +1307,16 @@ Terminal::match_check_internal_pcre(vte::grid::column_t column,
        start_blank = sattr;
        end_blank = eattr;
 
-        match_context = create_match_context();
-        match_data = pcre2_match_data_create_8(256 /* should be plenty */, NULL /* general context */);
+        auto match_context = create_match_context();
+        auto match_data = vte::take_freeable(pcre2_match_data_create_8(256 /* should be plenty */,
+                                                                       nullptr /* general context */));
 
        /* Now iterate over each regex we need to match against. */
         char* dingu_match{nullptr};
         for (auto const& rem : m_match_regexes) {
                 gsize sblank, eblank;
 
-                if (match_check_pcre(match_data, match_context,
+                if (match_check_pcre(match_data.get(), match_context.get(),
                                      rem.regex(),
                                      rem.match_flags(),
                                      sattr, eattr, offset,
@@ -1361,9 +1358,6 @@ Terminal::match_check_internal_pcre(vte::grid::column_t column,
                 }
         }
 
-        pcre2_match_data_free_8(match_data);
-        pcre2_match_context_free_8(match_context);
-
        return dingu_match;
 }
 
@@ -1755,8 +1749,6 @@ Terminal::regex_match_check_extra(vte::platform::MouseEvent const& event,
                                   char** matches)
 {
        gsize offset, sattr, eattr;
-        pcre2_match_data_8 *match_data;
-        pcre2_match_context_8 *match_context;
         bool any_matches = false;
         long col, row;
         guint i;
@@ -1778,8 +1770,9 @@ Terminal::regex_match_check_extra(vte::platform::MouseEvent const& event,
                                     &offset, &sattr, &eattr))
                 return false;
 
-        match_context = create_match_context();
-        match_data = pcre2_match_data_create_8(256 /* should be plenty */, nullptr /* general context */);
+        auto match_context = create_match_context();
+        auto match_data = vte::take_freeable(pcre2_match_data_create_8(256 /* should be plenty */,
+                                                                       nullptr /* general context */));
 
         for (i = 0; i < n_regexes; i++) {
                 gsize start, end, sblank, eblank;
@@ -1787,7 +1780,7 @@ Terminal::regex_match_check_extra(vte::platform::MouseEvent const& event,
 
                 g_return_val_if_fail(regexes[i] != nullptr, false);
 
-                if (match_check_pcre(match_data, match_context,
+                if (match_check_pcre(match_data.get(), match_context.get(),
                                      regexes[i], match_flags,
                                      sattr, eattr, offset,
                                      &match_string,
@@ -1800,9 +1793,6 @@ Terminal::regex_match_check_extra(vte::platform::MouseEvent const& event,
                         matches[i] = nullptr;
         }
 
-        pcre2_match_data_free_8(match_data);
-        pcre2_match_context_free_8(match_context);
-
         return any_matches;
 }
 
@@ -3021,7 +3011,7 @@ not_inserted:
 #ifdef WITH_SIXEL
 
 void
-Terminal::insert_image(vte::cairo::Surface image_surface) /* throws */
+Terminal::insert_image(vte::Freeable<cairo_surface_t> image_surface) /* throws */
 {
         if (!image_surface)
                 return;
@@ -3030,15 +3020,17 @@ Terminal::insert_image(vte::cairo::Surface image_surface) /* throws */
         auto const image_height_px = cairo_image_surface_get_height(image_surface.get());
 
         /* Convert to device-compatible surface for m_widget */
-        auto device_surface = 
vte::cairo::Surface{gdk_window_create_similar_surface(gtk_widget_get_window(widget()->gtk()),
-                                                                                    
CAIRO_CONTENT_COLOR_ALPHA,
-                                                                                    image_width_px,
-                                                                                    image_height_px)};
+        auto device_surface = vte::take_freeable
+                (gdk_window_create_similar_surface(gtk_widget_get_window(widget()->gtk()),
+                                                   CAIRO_CONTENT_COLOR_ALPHA,
+                                                   image_width_px,
+                                                   image_height_px));
 
-        auto cr = cairo_create(device_surface.get());
-        cairo_set_source_surface(cr, image_surface.get(), 0, 0);
-        cairo_paint(cr);
-        cairo_destroy(cr);
+        {
+                auto cr = vte::take_freeable(cairo_create(device_surface.get()));
+                cairo_set_source_surface(cr.get(), image_surface.get(), 0, 0);
+                cairo_paint(cr.get());
+        }
 
         /* Reduce memory fragmentation by dropping the ref as soon as we no longer need it */
         image_surface.reset();
@@ -4399,7 +4391,7 @@ Terminal::im_preedit_reset() noexcept
 void
 Terminal::im_preedit_changed(std::string_view const& str,
                              int cursorpos,
-                             pango_attr_list_unique_type&& attrs) noexcept
+                             vte::Freeable<PangoAttrList> attrs) noexcept
 {
        /* Queue the area where the current preedit string is being displayed
         * for repainting. */
@@ -7211,16 +7203,16 @@ Terminal::update_font()
         if (!m_unscaled_font_desc)
                 return;
 
-        auto desc = pango_font_description_copy(m_unscaled_font_desc.get());
+        auto desc = vte::take_freeable(pango_font_description_copy(m_unscaled_font_desc.get()));
 
-        double size = pango_font_description_get_size(desc);
-        if (pango_font_description_get_size_is_absolute(desc)) {
-                pango_font_description_set_absolute_size(desc, m_font_scale * size);
+        double size = pango_font_description_get_size(desc.get());
+        if (pango_font_description_get_size_is_absolute(desc.get())) {
+                pango_font_description_set_absolute_size(desc.get(), m_font_scale * size);
         } else {
-                pango_font_description_set_size(desc, m_font_scale * size);
+                pango_font_description_set_size(desc.get(), m_font_scale * size);
         }
 
-        m_fontdesc.reset(desc); /* adopts */
+        m_fontdesc = std::move(desc);
         m_fontdirty = true;
         m_has_fonts = true;
 
@@ -10749,7 +10741,8 @@ Terminal::search_find (bool backward)
         */
 
         auto match_context = create_match_context();
-        auto match_data = pcre2_match_data_create_8(256 /* should be plenty */, nullptr /* general context 
*/);
+        auto match_data = vte::take_freeable(pcre2_match_data_create_8(256 /* should be plenty */,
+                                                                       nullptr /* general context */));
 
        buffer_start_row = _vte_ring_delta (m_screen->row_data);
        buffer_end_row = _vte_ring_next (m_screen->row_data);
@@ -10767,11 +10760,11 @@ Terminal::search_find (bool backward)
        /* If search fails, we make an empty selection at the last searched
         * position... */
        if (backward) {
-               if (search_rows_iter (match_context, match_data,
+               if (search_rows_iter(match_context.get(), match_data.get(),
                                       buffer_start_row, last_start_row, backward))
                        goto found;
                if (m_search_wrap_around &&
-                   search_rows_iter (match_context, match_data,
+                   search_rows_iter(match_context.get(), match_data.get(),
                                       last_end_row, buffer_end_row, backward))
                        goto found;
                 if (!m_selection_resolved.empty()) {
@@ -10782,11 +10775,11 @@ Terminal::search_find (bool backward)
                }
                 match_found = false;
        } else {
-               if (search_rows_iter (match_context, match_data,
+               if (search_rows_iter(match_context.get(), match_data.get(),
                                       last_end_row, buffer_end_row, backward))
                        goto found;
                if (m_search_wrap_around &&
-                   search_rows_iter (match_context, match_data,
+                   search_rows_iter(match_context.get(), match_data.get(),
                                       buffer_start_row, last_start_row, backward))
                        goto found;
                 if (!m_selection_resolved.empty()) {
@@ -10800,9 +10793,6 @@ Terminal::search_find (bool backward)
 
  found:
 
-        pcre2_match_data_free_8(match_data);
-        pcre2_match_context_free_8(match_context);
-
        return match_found;
 }
 
diff --git a/src/vteinternal.hh b/src/vteinternal.hh
index 08e20c01..bce1744c 100644
--- a/src/vteinternal.hh
+++ b/src/vteinternal.hh
@@ -32,6 +32,7 @@
 
 #include <glib.h>
 #include "glib-glue.hh"
+#include "pango-glue.hh"
 
 #include "debug.h"
 #include "clipboard-gtk.hh"
@@ -49,7 +50,7 @@
 #include "refptr.hh"
 #include "fwd.hh"
 
-#include "vtepcre2.h"
+#include "pcre2-glue.hh"
 #include "vteregexinternal.hh"
 
 #include "chunk.hh"
@@ -569,9 +570,8 @@ public:
 
        /* Data used when rendering the text which does not require server
         * resources and which can be kept after unrealizing. */
-        using pango_font_description_type = vte::FreeablePtr<PangoFontDescription, 
decltype(&pango_font_description_free), &pango_font_description_free>;
-        pango_font_description_type m_unscaled_font_desc{};
-        pango_font_description_type  m_fontdesc{};
+        vte::Freeable<PangoFontDescription> m_unscaled_font_desc{};
+        vte::Freeable<PangoFontDescription> m_fontdesc{};
         double m_font_scale{1.};
 
         auto unscaled_font_description() const noexcept { return m_unscaled_font_desc.get(); }
@@ -631,8 +631,7 @@ public:
        /* Input method support. */
         bool m_im_preedit_active;
         std::string m_im_preedit;
-        using pango_attr_list_unique_type = std::unique_ptr<PangoAttrList, decltype(&pango_attr_list_unref)>;
-        pango_attr_list_unique_type m_im_preedit_attrs{nullptr, &pango_attr_list_unref};
+        vte::Freeable<PangoAttrList> m_im_preedit_attrs{};
         int m_im_preedit_cursor;
 
         #ifdef WITH_A11Y
@@ -750,7 +749,7 @@ public:
                          bool invalidate_now);
 
         #ifdef WITH_SIXEL
-        void insert_image(vte::cairo::Surface image_surface) /* throws */;
+        void insert_image(vte::Freeable<cairo_surface_t> image_surface) /* throws */;
         #endif
 
         void invalidate_row(vte::grid::row_t row);
@@ -944,7 +943,7 @@ public:
         void im_preedit_reset() noexcept;
         void im_preedit_changed(std::string_view const& str,
                                 int cursorpos,
-                                pango_attr_list_unique_type&& attrs) noexcept;
+                                vte::Freeable<PangoAttrList> attrs) noexcept;
         bool im_retrieve_surrounding();
         bool im_delete_surrounding(int offset,
                                    int n_chars);
@@ -1133,7 +1132,7 @@ public:
                                     gsize *sattr_ptr,
                                     gsize *eattr_ptr);
 
-        pcre2_match_context_8 *create_match_context();
+        vte::Freeable<pcre2_match_context_8> create_match_context();
         bool match_check_pcre(pcre2_match_data_8 *match_data,
                               pcre2_match_context_8 *match_context,
                               vte::base::Regex const* regex,
diff --git a/src/vteregex.cc b/src/vteregex.cc
index a125df55..40ccc432 100644
--- a/src/vteregex.cc
+++ b/src/vteregex.cc
@@ -29,12 +29,11 @@
 #include "vtemacros.h"
 #include "vteenums.h"
 #include "vteregex.h"
-#include "vtepcre2.h"
 
+#include "glib-glue.hh"
+#include "pcre2-glue.hh"
 #include "regex.hh"
 #include "vteregexinternal.hh"
-#include "glib-glue.hh"
-#include "cxx-utils.hh"
 
 #define IMPL(wrapper) (regex_from_wrapper(wrapper))
 
diff --git a/src/widget.cc b/src/widget.cc
index d56e900d..9e7c16ec 100644
--- a/src/widget.cc
+++ b/src/widget.cc
@@ -357,7 +357,7 @@ Widget::im_preedit_changed() noexcept
                          str, cursorpos);
 
         if (str != nullptr)
-                m_terminal->im_preedit_changed(str, cursorpos, {attrs, &pango_attr_list_unref});
+                m_terminal->im_preedit_changed(str, cursorpos, vte::take_freeable(attrs));
         else
                 pango_attr_list_unref(attrs);
 


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