Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload account foreach descendant #1979

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 26 additions & 28 deletions libgnucash/engine/Account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
christopherlam marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -2994,26 +2999,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 +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
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
Loading