From 32e0adedc697ff7b62bca61baf2dea6b263f04a5 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Tue, 18 Jun 2024 08:37:28 +0800 Subject: [PATCH] [window-reconcile.cpp] better c++ - use xaccAccountGetSplits instead of GList* xaccAccountGetSplitList - use gnc_account_foreach_descendant with std::function --- gnucash/gnome/window-reconcile.cpp | 51 +++++++++--------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/gnucash/gnome/window-reconcile.cpp b/gnucash/gnome/window-reconcile.cpp index ffed300629..66f6d651fd 100644 --- a/gnucash/gnome/window-reconcile.cpp +++ b/gnucash/gnome/window-reconcile.cpp @@ -2244,66 +2244,45 @@ recn_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) static Account * find_payment_account(Account *account) { - if (account == NULL) - return NULL; + if (account == nullptr) + return nullptr; - GList *list = xaccAccountGetSplitList (account); - Account *rv = nullptr; + const auto& splits = xaccAccountGetSplits (account); /* Search backwards to find the latest payment */ - for (GList *node = g_list_last (list); !rv && node; node = node->prev) + for (auto it = splits.rbegin(); it != splits.rend(); it++) { - Transaction *trans; - GList *n; - - auto split = GNC_SPLIT(node->data); - if (split == NULL) - continue; + auto split = *it; /* ignore 'purchases' */ if (!gnc_numeric_positive_p (xaccSplitGetAmount(split))) continue; - trans = xaccSplitGetParent(split); - if (trans == NULL) - continue; - - for (n = xaccTransGetSplitList (trans); n; n = n->next) + for (auto n = xaccTransGetSplitList (xaccSplitGetParent(split)); n; n = n->next) { - GNCAccountType type; - Account *a; - auto s = GNC_SPLIT(n->data); - if ((s == NULL) || (s == split)) + if (s == split) continue; - a = xaccSplitGetAccount(s); - if ((a == NULL) || (a == account)) + auto a = xaccSplitGetAccount(s); + if (a == account) continue; - type = xaccAccountGetType(a); - if ((type == ACCT_TYPE_BANK) || (type == ACCT_TYPE_CASH) || - (type == ACCT_TYPE_ASSET)) - rv = a; + auto type = xaccAccountGetType(a); + if (type == ACCT_TYPE_BANK || type == ACCT_TYPE_CASH || type == ACCT_TYPE_ASSET) + return a; } } - g_list_free (list); - return rv; -} - -typedef void (*AccountProc) (Account *a); -static void traverse_fn (Account *acct, AccountProc fn) -{ - fn (acct); + return nullptr; } static void -acct_traverse_descendants (Account *acct, AccountProc fn) +acct_traverse_descendants (Account *acct, std::function fn) { fn (acct); if (xaccAccountGetReconcileChildrenStatus (acct)) - gnc_account_foreach_descendant (acct, (AccountCb)traverse_fn, (gpointer)fn); + gnc_account_foreach_descendant (acct, fn); } /********************************************************************\