[vte] lib: Add getter support for smart pointer
- From: Christian Persch <chpe src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vte] lib: Add getter support for smart pointer
- Date: Thu, 22 Oct 2020 18:35:03 +0000 (UTC)
commit bd5025524bf9ca3490ba3739312618a1f7092766
Author: Christian Persch <chpe src gnome org>
Date: Thu Oct 22 20:34:51 2020 +0200
lib: Add getter support for smart pointer
src/std-glue.hh | 43 +++++++++++++++++++++++++++++++++++++++++++
src/vte.cc | 17 +++++++++--------
src/widget.cc | 10 +++++-----
3 files changed, 57 insertions(+), 13 deletions(-)
---
diff --git a/src/std-glue.hh b/src/std-glue.hh
index 2a35b6a2..946f023f 100644
--- a/src/std-glue.hh
+++ b/src/std-glue.hh
@@ -18,6 +18,7 @@
#pragma once
#include <memory>
+#include <type_traits>
namespace vte {
@@ -36,4 +37,46 @@ class FreeableDeleter<T> { \
public: inline void operator()(T* t) { func(t); } \
}
+template<typename S, typename V, V default_v = 0>
+class ValueGetter {
+public:
+ explicit ValueGetter(S& storage,
+ V default_vv = default_v) noexcept
+ : m_storage{storage},
+ m_value{default_vv}
+ {
+ }
+
+ ~ValueGetter()
+ {
+ if constexpr (std::is_nothrow_assignable_v<S&, V>) {
+ m_storage = m_value;
+ } else {
+ m_storage.reset(m_value);
+ }
+ }
+
+ ValueGetter(ValueGetter const&) = delete;
+ ValueGetter(ValueGetter&&) = delete;
+
+ ValueGetter& operator=(ValueGetter const&) = delete;
+ ValueGetter& operator=(ValueGetter&&) = delete;
+
+ operator V*() noexcept { return &m_value; }
+ V* operator&() noexcept { return &m_value; }
+
+private:
+ S& m_storage;
+ V m_value;
+};
+
+template<typename S, typename V, V default_v = 0>
+auto get_value(S& s) { return vte::ValueGetter<S, V, default_v>{s}; }
+
+template<typename T>
+using FreeableGetter = ValueGetter<Freeable<T>, T*, nullptr>;
+
+template<typename T>
+auto get_freeable(vte::Freeable<T>& freeable) { return vte::FreeableGetter<T>{freeable}; }
+
} // namespace vte
diff --git a/src/vte.cc b/src/vte.cc
index ddf5a251..aa29348f 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -7235,22 +7235,23 @@ Terminal::update_font()
bool
Terminal::set_font_desc(PangoFontDescription const* font_desc)
{
- /* Create an owned font description. */
- PangoFontDescription *desc;
+ auto desc = vte::Freeable<PangoFontDescription>{};
auto context = gtk_widget_get_style_context(m_widget);
gtk_style_context_save(context);
gtk_style_context_set_state (context, GTK_STATE_FLAG_NORMAL);
- gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL, "font", &desc, nullptr);
+ gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL, "font",
+ &vte::get_freeable(desc),
+ nullptr);
gtk_style_context_restore(context);
- pango_font_description_set_family_static (desc, "monospace");
+ pango_font_description_set_family_static(desc.get(), "monospace");
if (font_desc != nullptr) {
- pango_font_description_merge (desc, font_desc, TRUE);
+ pango_font_description_merge(desc.get(), font_desc, TRUE);
_VTE_DEBUG_IF(VTE_DEBUG_MISC) {
if (desc) {
char *tmp;
- tmp = pango_font_description_to_string(desc);
+ tmp = pango_font_description_to_string(desc.get());
g_printerr("Using pango font \"%s\".\n", tmp);
g_free (tmp);
}
@@ -7261,7 +7262,7 @@ Terminal::set_font_desc(PangoFontDescription const* font_desc)
}
bool const same_desc = m_unscaled_font_desc &&
- pango_font_description_equal(m_unscaled_font_desc.get(), desc);
+ pango_font_description_equal(m_unscaled_font_desc.get(), desc.get());
/* Note that we proceed to recreating the font even if the description
* are the same. This is because maybe screen
@@ -7269,7 +7270,7 @@ Terminal::set_font_desc(PangoFontDescription const* font_desc)
* detected at font creation time and respected.
*/
- m_unscaled_font_desc.reset(desc); /* adopts */
+ m_unscaled_font_desc = std::move(desc);
update_font();
return !same_desc;
diff --git a/src/widget.cc b/src/widget.cc
index 9e7c16ec..31061413 100644
--- a/src/widget.cc
+++ b/src/widget.cc
@@ -349,17 +349,17 @@ void
Widget::im_preedit_changed() noexcept
{
char* str = nullptr;
- PangoAttrList* attrs = nullptr;
int cursorpos = 0;
- gtk_im_context_get_preedit_string(m_im_context.get(), &str, &attrs, &cursorpos);
+ auto attrs = vte::Freeable<PangoAttrList>{};
+ gtk_im_context_get_preedit_string(m_im_context.get(), &str,
+ vte::get_freeable(attrs),
+ &cursorpos);
_vte_debug_print(VTE_DEBUG_EVENTS, "Input method pre-edit changed (%s,%d).\n",
str, cursorpos);
if (str != nullptr)
- m_terminal->im_preedit_changed(str, cursorpos, vte::take_freeable(attrs));
- else
- pango_attr_list_unref(attrs);
+ m_terminal->im_preedit_changed(str, cursorpos, std::move(attrs));
g_free(str);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]