From bebc366e88344b5e226538d79908244900e19878 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Tue, 3 Aug 2021 22:07:18 +0800 Subject: [PATCH] [account.cpp] refactor gnc_account_n_descendants --- libgnucash/engine/Account.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index b74ca52421..215de97516 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -2894,20 +2894,18 @@ gnc_account_nth_child (const Account *parent, gint num) return static_cast(g_list_nth_data(GET_PRIVATE(parent)->children, num)); } +static void +count_acct (Account *account, gpointer user_data) +{ + auto count {static_cast(user_data)}; + ++*count; +} + gint gnc_account_n_descendants (const Account *account) { - AccountPrivate *priv; - GList *node; - gint count = 0; - - g_return_val_if_fail(GNC_IS_ACCOUNT(account), 0); - - priv = GET_PRIVATE(account); - for (node = priv->children; node; node = g_list_next(node)) - { - count += gnc_account_n_descendants(static_cast(node->data)) + 1; - } + int count {0}; + account_foreach_descendant (account, count_acct, &count, FALSE); return count; }