Skip to content

Commit

Permalink
[Account.hpp] use std::optional for cached values
Browse files Browse the repository at this point in the history
whereby {} denotes uncached values
  • Loading branch information
christopherlam committed Apr 13, 2024
1 parent 01bffa4 commit 2251bf8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 37 deletions.
40 changes: 15 additions & 25 deletions libgnucash/engine/Account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,9 @@ gnc_account_init(Account* acc)
priv->starting_reconciled_balance = gnc_numeric_zero();
priv->balance_dirty = FALSE;

priv->higher_balance_limit = gnc_numeric_create (1,0);
priv->higher_balance_cached = false;
priv->lower_balance_limit = gnc_numeric_create (1,0);
priv->lower_balance_cached = false;
priv->include_sub_account_balances = TriState::Unset;
priv->higher_balance_limit = {};
priv->lower_balance_limit = {};
priv->include_sub_account_balances = {};

priv->splits = nullptr;
priv->sort_dirty = FALSE;
Expand Down Expand Up @@ -4970,9 +4968,9 @@ xaccAccountGetHigherBalanceLimit (const Account *acc,
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), false);

if (GET_PRIVATE(acc)->higher_balance_cached)
if (GET_PRIVATE(acc)->higher_balance_limit.has_value())
{
*balance = GET_PRIVATE(acc)->higher_balance_limit;
*balance = GET_PRIVATE(acc)->higher_balance_limit.value();

if (gnc_numeric_check (*balance) == 0)
return true;
Expand Down Expand Up @@ -5000,7 +4998,6 @@ xaccAccountGetHigherBalanceLimit (const Account *acc,
g_value_unset (&v);

GET_PRIVATE(acc)->higher_balance_limit = bal;
GET_PRIVATE(acc)->higher_balance_cached = true;
return retval;
}
}
Expand All @@ -5011,9 +5008,9 @@ xaccAccountGetLowerBalanceLimit (const Account *acc,
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), false);

if (GET_PRIVATE(acc)->lower_balance_cached)
if (GET_PRIVATE(acc)->lower_balance_limit.has_value())
{
*balance = GET_PRIVATE(acc)->lower_balance_limit;
*balance = GET_PRIVATE(acc)->lower_balance_limit.value();

if (gnc_numeric_check (*balance) == 0)
return true;
Expand Down Expand Up @@ -5041,7 +5038,6 @@ xaccAccountGetLowerBalanceLimit (const Account *acc,
g_value_unset (&v);

GET_PRIVATE(acc)->lower_balance_limit = bal;
GET_PRIVATE(acc)->lower_balance_cached = true;
return retval;
}
}
Expand Down Expand Up @@ -5075,15 +5071,11 @@ set_balance_limits (Account *acc, gnc_numeric balance, gboolean higher)
qof_instance_set_path_kvp (QOF_INSTANCE(acc), &v, path);
if (higher)
{
GET_PRIVATE(acc)->higher_balance_limit.denom = balance.denom;
GET_PRIVATE(acc)->higher_balance_limit.num = balance.num;
GET_PRIVATE(acc)->higher_balance_cached = true;
GET_PRIVATE(acc)->higher_balance_limit = balance;
}
else
{
GET_PRIVATE(acc)->lower_balance_limit.denom = balance.denom;
GET_PRIVATE(acc)->lower_balance_limit.num = balance.num;
GET_PRIVATE(acc)->lower_balance_cached = true;
GET_PRIVATE(acc)->lower_balance_limit = balance;
}
mark_account (acc);
xaccAccountCommitEdit (acc);
Expand Down Expand Up @@ -5138,9 +5130,9 @@ clear_balance_limits (Account *acc, gboolean higher)
qof_instance_set_path_kvp (QOF_INSTANCE(acc), nullptr, path);
qof_instance_slot_path_delete_if_empty (QOF_INSTANCE(acc), {KEY_BALANCE_LIMIT});
if (higher)
GET_PRIVATE(acc)->higher_balance_cached = false;
GET_PRIVATE(acc)->higher_balance_limit.reset();
else
GET_PRIVATE(acc)->lower_balance_cached = false;
GET_PRIVATE(acc)->lower_balance_limit.reset();
mark_account (acc);
xaccAccountCommitEdit (acc);
}
Expand All @@ -5167,15 +5159,14 @@ xaccAccountGetIncludeSubAccountBalances (const Account *acc)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), false);

if (GET_PRIVATE(acc)->include_sub_account_balances == TriState::Unset)
if (!GET_PRIVATE(acc)->include_sub_account_balances.has_value())
{
gboolean inc_sub = boolean_from_key (acc, {KEY_BALANCE_LIMIT,
KEY_BALANCE_INCLUDE_SUB_ACCTS});

GET_PRIVATE(acc)->include_sub_account_balances = inc_sub ? TriState::True
: TriState::False;
GET_PRIVATE(acc)->include_sub_account_balances = inc_sub;
}
return GET_PRIVATE(acc)->include_sub_account_balances == TriState::True;
return GET_PRIVATE(acc)->include_sub_account_balances.value();
}

void
Expand All @@ -5195,8 +5186,7 @@ xaccAccountSetIncludeSubAccountBalances (Account *acc, gboolean inc_sub)
qof_instance_set_path_kvp (QOF_INSTANCE(acc), &v, path);
else
qof_instance_set_path_kvp (QOF_INSTANCE(acc), nullptr, path);
GET_PRIVATE(acc)->include_sub_account_balances =
inc_sub ? TriState::True : TriState::False;
GET_PRIVATE(acc)->include_sub_account_balances = inc_sub;
mark_account (acc);
xaccAccountCommitEdit (acc);
g_value_unset (&v);
Expand Down
17 changes: 5 additions & 12 deletions libgnucash/engine/AccountP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#ifndef XACC_ACCOUNT_P_H
#define XACC_ACCOUNT_P_H

#include <optional>

#include "Account.h"

#define GNC_ID_ROOT_ACCOUNT "RootAccount"
Expand All @@ -51,13 +53,6 @@
* No one outside of the engine should ever include this file.
*/

typedef enum
{
Unset = -1,
False,
True
} TriState;

/** \struct Account */
typedef struct AccountPrivate
{
Expand Down Expand Up @@ -117,11 +112,9 @@ typedef struct AccountPrivate
gnc_numeric cleared_balance;
gnc_numeric reconciled_balance;

gnc_numeric higher_balance_limit;
gboolean higher_balance_cached;
gnc_numeric lower_balance_limit;
gboolean lower_balance_cached;
TriState include_sub_account_balances;
std::optional<gnc_numeric> higher_balance_limit;
std::optional<gnc_numeric> lower_balance_limit;
std::optional<bool> include_sub_account_balances;

gboolean balance_dirty; /* balances in splits incorrect */

Expand Down

0 comments on commit 2251bf8

Please sign in to comment.