Skip to content

Commit

Permalink
[Account.cpp] overload gnc_account_foreach_descendant takes std::func…
Browse files Browse the repository at this point in the history
…tion

and separate account_foreach_descendant_sorted
  • Loading branch information
christopherlam committed Jul 5, 2024
1 parent c6cc1ad commit 1cdcb28
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
49 changes: 23 additions & 26 deletions libgnucash/engine/Account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2752,23 +2752,34 @@ 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));

// some callers eg 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; });
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);
}
}

Expand Down Expand Up @@ -2945,18 +2956,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;
}

Expand Down Expand Up @@ -2994,26 +2998,19 @@ 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);
}

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);
}

Expand Down Expand Up @@ -3198,7 +3195,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
Expand Down
2 changes: 2 additions & 0 deletions libgnucash/engine/Account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1cdcb28

Please sign in to comment.