Skip to content

Commit

Permalink
[Account.cpp] tightening loops, less g_list_free
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Jun 27, 2024
1 parent 9d40dcb commit 00f9aa6
Showing 1 changed file with 12 additions and 54 deletions.
66 changes: 12 additions & 54 deletions libgnucash/engine/Account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3394,34 +3394,12 @@ xaccAccountGetCommodity (const Account *acc)

gnc_commodity * gnc_account_get_currency_or_parent(const Account* account)
{
gnc_commodity * commodity;
g_return_val_if_fail (account, nullptr);
g_return_val_if_fail (GNC_IS_ACCOUNT (account), nullptr);

for (auto acc = account; acc; acc = gnc_account_get_parent (acc))
if (auto comm = xaccAccountGetCommodity (acc); gnc_commodity_is_currency (comm))
return comm;

commodity = xaccAccountGetCommodity (account);
if (gnc_commodity_is_currency(commodity))
return commodity;
else
{
const Account *parent_account = account;
/* Account commodity is not a currency, walk up the tree until
* we find a parent account that is a currency account and use
* it's currency.
*/
do
{
parent_account = gnc_account_get_parent (parent_account);
if (parent_account)
{
commodity = xaccAccountGetCommodity (parent_account);
if (gnc_commodity_is_currency(commodity))
{
return commodity;
//break;
}
}
}
while (parent_account);
}
return nullptr; // no suitable commodity found.
}

Expand Down Expand Up @@ -4007,19 +3985,14 @@ gpointer
xaccAccountForEachLot(const Account *acc,
gpointer (*proc)(GNCLot *lot, void *data), void *data)
{
AccountPrivate *priv;
LotList *node;
gpointer result = nullptr;

g_return_val_if_fail(GNC_IS_ACCOUNT(acc), nullptr);
g_return_val_if_fail(proc, nullptr);

priv = GET_PRIVATE(acc);
for (node = priv->lots; node; node = node->next)
if ((result = proc((GNCLot *)node->data, data)))
break;
for (auto node = GET_PRIVATE(acc)->lots; node; node = node->next)
if (auto result = proc(GNC_LOT(node->data), data))
return result;

return result;
return nullptr;
}

static void
Expand Down Expand Up @@ -4214,22 +4187,11 @@ xaccAccountSetIsOpeningBalance (Account *acc, gboolean val)
GNCPlaceholderType
xaccAccountGetDescendantPlaceholder (const Account *acc)
{
GList *descendants, *node;
GNCPlaceholderType ret = PLACEHOLDER_NONE;

g_return_val_if_fail(GNC_IS_ACCOUNT(acc), PLACEHOLDER_NONE);
if (xaccAccountGetPlaceholder(acc)) return PLACEHOLDER_THIS;

descendants = gnc_account_get_descendants(acc);
for (node = descendants; node; node = node->next)
if (xaccAccountGetPlaceholder((Account *) node->data))
{
ret = PLACEHOLDER_CHILD;
break;
}

g_list_free(descendants);
return ret;
return gnc_account_foreach_descendant_until (acc, (AccountCb2)xaccAccountGetPlaceholder, nullptr)
? PLACEHOLDER_CHILD : PLACEHOLDER_NONE;
}

/********************************************************************\
Expand Down Expand Up @@ -5436,11 +5398,7 @@ static void do_one_account (Account *account, gpointer data)
void
gnc_account_tree_begin_staged_transaction_traversals (Account *account)
{
GList *descendants;

descendants = gnc_account_get_descendants(account);
g_list_foreach(descendants, (GFunc)do_one_account, nullptr);
g_list_free(descendants);
gnc_account_foreach_descendant (account, do_one_account, nullptr);
}

int
Expand Down

0 comments on commit 00f9aa6

Please sign in to comment.