gnucash stable: [Account.cpp] overload gnc_account_foreach_descendant takes std::function
Christopher Lam
clam at code.gnucash.org
Fri Jul 12 08:30:03 EDT 2024
Updated via https://github.com/Gnucash/gnucash/commit/88d8f638 (commit)
from https://github.com/Gnucash/gnucash/commit/07e449a6 (commit)
commit 88d8f638534929b7791ad4a04a42c1fba19eafe6
Author: Christopher Lam <christopher.lck at gmail.com>
Date: Wed Jun 19 00:13:28 2024 +0800
[Account.cpp] overload gnc_account_foreach_descendant takes std::function
and separate account_foreach_descendant_sorted
diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp
index f125692816..09fb66176b 100644
--- a/libgnucash/engine/Account.cpp
+++ b/libgnucash/engine/Account.cpp
@@ -2752,23 +2752,35 @@ DxaccAccountSetCurrency (Account * acc, gnc_commodity * currency)
/********************************************************************\
\********************************************************************/
+void
+gnc_account_foreach_descendant (const Account *acc, std::function<void(Account*)> account_cb)
+{
+ g_return_if_fail (GNC_IS_ACCOUNT(acc));
+
+ // children must be a vector copy instead of reference because
+ // some callers e.g. xaccAccountTreeScrubLots will modify the
+ // children
+ auto children = GET_PRIVATE(acc)->children;
+ for (auto child : children)
+ {
+ account_cb (child);
+ gnc_account_foreach_descendant (child, account_cb);
+ }
+}
+
static void
-account_foreach_descendant (const Account *acc, AccountCb thunk,
- void* user_data, bool sort)
+account_foreach_descendant_sorted (const Account *acc, std::function<void(Account*)> account_cb)
{
g_return_if_fail (GNC_IS_ACCOUNT(acc));
- g_return_if_fail (thunk);
- auto priv{GET_PRIVATE(acc)};
- auto children = priv->children;
- if (sort)
- std::sort (children.begin(), children.end(),
- [](auto a, auto b) { return xaccAccountOrder (a, b) < 0; });
+ auto children = GET_PRIVATE(acc)->children;
+ std::sort (children.begin(), children.end(),
+ [](auto a, auto b) { return xaccAccountOrder (a, b) < 0; });
for (auto child : children)
{
- thunk (child, user_data);
- account_foreach_descendant (child, thunk, user_data, sort);
+ account_cb (child);
+ account_foreach_descendant_sorted (child, account_cb);
}
}
@@ -2945,18 +2957,11 @@ gnc_account_nth_child (const Account *parent, gint num)
return static_cast<Account*>(GET_PRIVATE(parent)->children.at (num));
}
-static void
-count_acct (Account *account, gpointer user_data)
-{
- auto count {static_cast<int*>(user_data)};
- ++*count;
-}
-
gint
gnc_account_n_descendants (const Account *account)
{
int count {0};
- account_foreach_descendant (account, count_acct, &count, FALSE);
+ gnc_account_foreach_descendant (account, [&count](auto acc){ count++; });
return count;
}
@@ -2994,18 +2999,11 @@ gnc_account_get_tree_depth (const Account *account)
{ return std::max (a, gnc_account_get_tree_depth (b)); });
}
-static void
-collect_acct (Account *account, gpointer user_data)
-{
- auto listptr{static_cast<GList**>(user_data)};
- *listptr = g_list_prepend (*listptr, account);
-}
-
GList *
gnc_account_get_descendants (const Account *account)
{
GList* list = nullptr;
- account_foreach_descendant (account, collect_acct, &list, FALSE);
+ gnc_account_foreach_descendant (account, [&list](auto a){ list = g_list_prepend (list, a); });
return g_list_reverse (list);
}
@@ -3013,7 +3011,7 @@ GList *
gnc_account_get_descendants_sorted (const Account *account)
{
GList* list = nullptr;
- account_foreach_descendant (account, collect_acct, &list, TRUE);
+ account_foreach_descendant_sorted (account, [&list](auto a){ list = g_list_prepend (list, a); });
return g_list_reverse (list);
}
@@ -3198,7 +3196,7 @@ gnc_account_foreach_descendant (const Account *acc,
AccountCb thunk,
gpointer user_data)
{
- account_foreach_descendant (acc, thunk, user_data, FALSE);
+ gnc_account_foreach_descendant (acc, [&](auto acc){ thunk (acc, user_data); });
}
gpointer
diff --git a/libgnucash/engine/Account.hpp b/libgnucash/engine/Account.hpp
index 4c5920428e..3a3c51addf 100644
--- a/libgnucash/engine/Account.hpp
+++ b/libgnucash/engine/Account.hpp
@@ -42,6 +42,8 @@ using AccountVec = std::vector<Account*>;
const SplitsVec xaccAccountGetSplits (const Account*);
+void gnc_account_foreach_descendant (const Account *, std::function<void(Account*)> func);
+
void gnc_account_foreach_split (const Account*, std::function<void(Split*)>, bool);
void gnc_account_foreach_split_until_date (const Account *acc, time64 end_date,
Summary of changes:
libgnucash/engine/Account.cpp | 54 +++++++++++++++++++++----------------------
libgnucash/engine/Account.hpp | 2 ++
2 files changed, 28 insertions(+), 28 deletions(-)
More information about the gnucash-changes
mailing list