From 6a87388bc8498e0683ec51fffd49e8a228091c10 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Tue, 5 Mar 2024 20:54:01 +0800 Subject: [PATCH] [import-main-matcher.cpp] use std::unique_ptr to benefit from RAII thus gtk_tree_row_reference_free is handled for us. IIUC this does not significantly affect the effort required to upgrade to gtk4 or gtkmm. --- gnucash/import-export/import-main-matcher.cpp | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/gnucash/import-export/import-main-matcher.cpp b/gnucash/import-export/import-main-matcher.cpp index cbac5d31f62..8eb59f60209 100644 --- a/gnucash/import-export/import-main-matcher.cpp +++ b/gnucash/import-export/import-main-matcher.cpp @@ -38,6 +38,7 @@ #include #include +#include #include #include @@ -805,12 +806,20 @@ gnc_gen_trans_assign_transfer_account (GtkTreeView *treeview, LEAVE(""); } -// bug 799246. return a vector of GtkTreeRowReference*. don't forget to -// gtk_tree_row_reference_free the elements before the vector is destroyed. -static std::vector +class TreeRowRefDestructor +{ +public: + void operator()(GtkTreeRowReference* ptr) const { gtk_tree_row_reference_free (ptr); } +}; + +using TreeRowReferencePtr = std::unique_ptr; + +// bug 799246. return a vector of TreeRowReferencePtr, from which +// get() will return the GtkTreeRowReference* +static std::vector get_treeview_selection_refs (GtkTreeView *treeview, GtkTreeModel *model) { - std::vector rv; + std::vector rv; g_return_val_if_fail (GTK_IS_TREE_VIEW (treeview) && GTK_IS_TREE_MODEL (model), rv); @@ -818,7 +827,7 @@ get_treeview_selection_refs (GtkTreeView *treeview, GtkTreeModel *model) auto selected_rows = gtk_tree_selection_get_selected_rows (selection, &model); for (auto n = selected_rows; n; n = g_list_next (n)) - rv.push_back (gtk_tree_row_reference_new (model, static_cast(n->data))); + rv.emplace_back (gtk_tree_row_reference_new (model, static_cast(n->data))); g_list_free_full (selected_rows, (GDestroyNotify)gtk_tree_path_free); return rv; @@ -843,7 +852,7 @@ gnc_gen_trans_assign_transfer_account_to_selection_cb (GtkMenuItem *menuitem, for (const auto& ref : selected_refs) { - auto path = gtk_tree_row_reference_get_path (ref); + auto path = gtk_tree_row_reference_get_path (ref.get()); if (debugging_enabled) { auto path_str = gtk_tree_path_to_string (path); @@ -871,10 +880,9 @@ gnc_gen_trans_assign_transfer_account_to_selection_cb (GtkMenuItem *menuitem, // now reselect the transaction rows. This is very slow if there are lots of transactions. for (const auto& ref : selected_refs) { - GtkTreePath *path = gtk_tree_row_reference_get_path (ref); + GtkTreePath *path = gtk_tree_row_reference_get_path (ref.get()); gtk_tree_selection_select_path (selection, path); gtk_tree_path_free (path); - gtk_tree_row_reference_free (ref); } LEAVE(""); @@ -887,9 +895,9 @@ class RowInfo { init_from_path (path, info); } - RowInfo (GtkTreeRowReference *ref, GNCImportMainMatcher *info) + RowInfo (const TreeRowReferencePtr &ref, GNCImportMainMatcher *info) { - auto path = gtk_tree_row_reference_get_path (ref); + auto path = gtk_tree_row_reference_get_path (ref.get()); init_from_path (path, info); gtk_tree_path_free (path); } @@ -1198,7 +1206,6 @@ gnc_gen_trans_edit_fields (GtkMenuItem *menuitem, GNCImportMainMatcher *info) g_free (new_memo); g_free (new_notes); } - std::for_each (selected_refs.begin(), selected_refs.end(), gtk_tree_row_reference_free); LEAVE(""); } @@ -1233,7 +1240,6 @@ gnc_gen_trans_reset_edits_cb (GtkMenuItem *menuitem, GNCImportMainMatcher *info) DOWNLOADED_COL_DESCRIPTION_STYLE, PANGO_STYLE_NORMAL, DOWNLOADED_COL_MEMO_STYLE, PANGO_STYLE_NORMAL, -1); - gtk_tree_row_reference_free (ref); }; LEAVE(""); }