gnucash stable: [Account.hpp] use std::optional for cached values

Christopher Lam clam at code.gnucash.org
Sat Apr 13 09:01:43 EDT 2024


Updated	 via  https://github.com/Gnucash/gnucash/commit/2251bf89 (commit)
	from  https://github.com/Gnucash/gnucash/commit/01bffa48 (commit)



commit 2251bf8966ec1cc92e9eed22cc2110990593bc41
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Sat Apr 13 20:56:21 2024 +0800

    [Account.hpp] use std::optional for cached values
    
    whereby {} denotes uncached values

diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp
index b54544a413..b456da0563 100644
--- a/libgnucash/engine/Account.cpp
+++ b/libgnucash/engine/Account.cpp
@@ -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;
@@ -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;
@@ -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;
     }
 }
@@ -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;
@@ -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;
     }
 }
@@ -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);
@@ -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);
     }
@@ -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
@@ -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);
diff --git a/libgnucash/engine/AccountP.hpp b/libgnucash/engine/AccountP.hpp
index 0e5618806d..d950569a66 100644
--- a/libgnucash/engine/AccountP.hpp
+++ b/libgnucash/engine/AccountP.hpp
@@ -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"
@@ -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
 {
@@ -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 */
 



Summary of changes:
 libgnucash/engine/Account.cpp  | 40 +++++++++++++++-------------------------
 libgnucash/engine/AccountP.hpp | 17 +++++------------
 2 files changed, 20 insertions(+), 37 deletions(-)



More information about the gnucash-changes mailing list