gnucash maint: Multiple changes pushed

Christopher Lam clam at code.gnucash.org
Tue Nov 22 19:56:57 EST 2022


Updated	 via  https://github.com/Gnucash/gnucash/commit/f13cfa21 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/3d8a2889 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/2ee0c982 (commit)
	from  https://github.com/Gnucash/gnucash/commit/af34ee3f (commit)



commit f13cfa214212c72aa96ca38fdb8741bb61fae887
Merge: af34ee3f5 3d8a28898
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Wed Nov 23 08:30:34 2022 +0800

    Merge branch 'maint-budget' into maint #1468


commit 3d8a28898d22d107610bbe1ce5f9954d61d12362
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Fri Nov 18 17:57:21 2022 +0800

    [gnc-budget] gnc_budget_get_account_period_note returns a const
    
    to harmonize with all other char getters

diff --git a/bindings/engine.i b/bindings/engine.i
index 6edd61abc..4d2f5e9bc 100644
--- a/bindings/engine.i
+++ b/bindings/engine.i
@@ -131,8 +131,6 @@ functions. */
 %newobject gnc_localtime;
 %newobject gnc_gmtime;
 
-%newobject gnc_budget_get_account_period_note;
-
 /* Parse the header file to generate wrappers */
 %inline {
   static QofIdType QOF_ID_BOOK_SCM (void) { return QOF_ID_BOOK; }
diff --git a/gnucash/gnome/gnc-budget-view.c b/gnucash/gnome/gnc-budget-view.c
index 26facbe9d..908964097 100644
--- a/gnucash/gnome/gnc-budget-view.c
+++ b/gnucash/gnome/gnc-budget-view.c
@@ -927,7 +927,7 @@ query_tooltip_tree_view_cb (GtkWidget *widget, gint x, gint y,
     GncBudgetViewPrivate *priv = GNC_BUDGET_VIEW_GET_PRIVATE(view);
     GtkTreePath          *path  = NULL;
     GtkTreeViewColumn    *column = NULL;
-    gchar                *note;
+    const gchar          *note;
     guint                 period_num;
     Account              *account;
 
@@ -964,7 +964,6 @@ query_tooltip_tree_view_cb (GtkWidget *widget, gint x, gint y,
     gtk_tooltip_set_text (tooltip, note);
     gtk_tree_view_set_tooltip_cell (tree_view, tooltip, path, column, NULL);
     gtk_tree_path_free (path);
-    g_free (note);
 
     return TRUE;
 }
@@ -1093,7 +1092,7 @@ budget_col_source (Account *account, GtkTreeViewColumn *col,
     guint period_num;
     gnc_numeric numeric;
     gchar amtbuff[100]; //FIXME: overkill, where's the #define?
-    gchar *note;
+    const gchar *note;
 
     budget_view = GNC_BUDGET_VIEW(g_object_get_data (G_OBJECT(col), "budget_view"));
     period_num = GPOINTER_TO_UINT(g_object_get_data (G_OBJECT(col), "period_num"));
@@ -1153,7 +1152,6 @@ budget_col_source (Account *account, GtkTreeViewColumn *col,
 
     note = gnc_budget_get_account_period_note (priv->budget, account, period_num);
     g_object_set (cell, "flagged", note != NULL, NULL);
-    g_free (note);
 
     return g_strdup (amtbuff);
 }
diff --git a/gnucash/gnome/gnc-plugin-page-budget.c b/gnucash/gnome/gnc-plugin-page-budget.c
index 6a873cf84..8a0901a17 100644
--- a/gnucash/gnome/gnc-plugin-page-budget.c
+++ b/gnucash/gnome/gnc-plugin-page-budget.c
@@ -1287,9 +1287,8 @@ gnc_plugin_page_budget_cmd_budget_note(GtkAction *action,
         GTK_WINDOW(gnc_plugin_page_get_window(GNC_PLUGIN_PAGE(page))));
 
     note = GTK_WIDGET(gtk_builder_get_object(builder, "BudgetNote"));
-    txt  = gnc_budget_get_account_period_note(priv->budget, acc, period_num);
-    xxxgtk_textview_set_text(GTK_TEXT_VIEW(note), txt);
-    g_free (txt);
+    xxxgtk_textview_set_text(GTK_TEXT_VIEW(note),
+                             gnc_budget_get_account_period_note(priv->budget, acc, period_num));
 
     gtk_widget_show_all(dialog);
     result = gtk_dialog_run(GTK_DIALOG(dialog));
diff --git a/libgnucash/engine/gnc-budget.cpp b/libgnucash/engine/gnc-budget.cpp
index b89d77522..c62552722 100644
--- a/libgnucash/engine/gnc-budget.cpp
+++ b/libgnucash/engine/gnc-budget.cpp
@@ -644,13 +644,13 @@ gnc_budget_set_account_period_note(GncBudget *budget, const Account *account,
 
 }
 
-gchar *
+const gchar *
 gnc_budget_get_account_period_note (const GncBudget *budget,
                                     const Account *account, guint period_num)
 {
     g_return_val_if_fail (period_num < GET_PRIVATE(budget)->num_periods, nullptr);
     auto& data = get_perioddata (budget, account, period_num);
-    return data.note.empty () ? nullptr : g_strdup (data.note.c_str());
+    return data.note.empty () ? nullptr : data.note.c_str();
 }
 
 time64
diff --git a/libgnucash/engine/gnc-budget.h b/libgnucash/engine/gnc-budget.h
index d9c1e814d..eee615303 100644
--- a/libgnucash/engine/gnc-budget.h
+++ b/libgnucash/engine/gnc-budget.h
@@ -161,11 +161,10 @@ gnc_numeric gnc_budget_get_account_period_value(
 gnc_numeric gnc_budget_get_account_period_actual_value(
     const GncBudget *budget, Account *account, guint period_num);
 
-/* get/set the budget account period's note, beware when retrieving
-   the period note, the latter must be g_freed by the caller */
+/* get/set the budget account period's note */
 void gnc_budget_set_account_period_note(GncBudget *budget,
     const Account *account, guint period_num, const gchar *note);
-gchar *gnc_budget_get_account_period_note (const GncBudget *budget,
+const gchar *gnc_budget_get_account_period_note (const GncBudget *budget,
     const Account *account, guint period_num);
 
 /* Returns some budget in the book, or NULL. */
diff --git a/libgnucash/engine/test/utest-Budget.c b/libgnucash/engine/test/utest-Budget.c
index f899b38a3..5ea9204c7 100644
--- a/libgnucash/engine/test/utest-Budget.c
+++ b/libgnucash/engine/test/utest-Budget.c
@@ -78,7 +78,7 @@ test_gnc_set_budget_num_periods_data_retention ()
     QofBook *book = qof_book_new();
     GncBudget* budget = gnc_budget_new(book);
     Account *acc = gnc_account_create_root(book);
-    gchar *note;
+    const gchar *note;
 
     /* initially has 20 periods */
     gnc_budget_set_num_periods(budget, 20);
@@ -100,7 +100,6 @@ test_gnc_set_budget_num_periods_data_retention ()
     g_assert (!gnc_budget_is_account_period_value_set(budget, acc, 15));
     note = gnc_budget_get_account_period_note (budget, acc, 11);
     g_assert_cmpstr (note, ==, NULL);
-    g_free (note);
 
     gnc_budget_destroy(budget);
     qof_book_destroy(book);

commit 2ee0c9820091511095f0aa1b6fdc90db5e8c36d2
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Fri Nov 18 17:55:38 2022 +0800

    [gnc-budget] use operator[] instead of find() and insert()
    
    - avoid hashing Account* twice. one call to operator[] is sufficient.
    - don't need use std::move with better constructors
    - sanity check - can't have a budget with num_periods = 0
    - remove unused headers

diff --git a/libgnucash/engine/gnc-budget.cpp b/libgnucash/engine/gnc-budget.cpp
index 88847b750..b89d77522 100644
--- a/libgnucash/engine/gnc-budget.cpp
+++ b/libgnucash/engine/gnc-budget.cpp
@@ -23,10 +23,6 @@
 \********************************************************************/
 
 #include <config.h>
-#include <glib.h>
-#include <glib/gprintf.h>
-#include <glib/gi18n.h>
-#include <time.h>
 #include <qof.h>
 #include <qofbookslots.h>
 #include <qofinstance-p.h>
@@ -67,6 +63,11 @@ struct PeriodData
     std::string note;
     bool value_is_set;
     gnc_numeric value;
+    PeriodData () = default;
+    PeriodData (const char* note, bool value_is_set, gnc_numeric value)
+        : note (note)
+        , value_is_set (value_is_set)
+        , value (value) {};
 };
 
 using PeriodDataVec = std::vector<PeriodData>;
@@ -470,6 +471,7 @@ gnc_budget_set_num_periods(GncBudget* budget, guint num_periods)
     GncBudgetPrivate* priv;
 
     g_return_if_fail(GNC_IS_BUDGET(budget));
+    g_return_if_fail(num_periods > 0);
 
     priv = GET_PRIVATE(budget);
     if ( priv->num_periods == num_periods ) return;
@@ -683,35 +685,27 @@ get_perioddata (const GncBudget *budget, const Account *account, guint period_nu
     if (period_num >= priv->num_periods)
         throw std::out_of_range("period_num >= num_periods");
 
-    auto& map = priv->acct_map;
-    auto map_iter = map->find (account);
+    auto& vec = priv->acct_map->operator[](account);
 
-    if (map_iter == map->end ())
+    if (vec.empty())
     {
         auto budget_kvp { QOF_INSTANCE (budget)->kvp_data };
-
-        PeriodDataVec vec {};
         vec.reserve (priv->num_periods);
 
         for (guint i = 0; i < priv->num_periods; i++)
         {
-            std::string note;
             auto kval1 { budget_kvp->get_slot (make_period_data_path (account, i)) };
             auto kval2 { budget_kvp->get_slot (make_period_note_path (account, i)) };
 
             auto is_set = kval1 && kval1->get_type() == KvpValue::Type::NUMERIC;
             auto num = is_set ? kval1->get<gnc_numeric>() : gnc_numeric_zero ();
+            auto note = (kval2 && kval2->get_type() == KvpValue::Type::STRING) ?
+                kval2->get<const char*>() : "";
 
-            if (kval2 && kval2->get_type() == KvpValue::Type::STRING)
-                note = kval2->get<const char*>();
-
-            PeriodData data { std::move (note), is_set, num };
-            vec.push_back (std::move(data));
+            vec.emplace_back (note, is_set, num);
         }
-        map_iter = map->insert_or_assign(account, std::move(vec)).first;
     }
 
-    auto& vec = map_iter->second;
     return vec.at(period_num);
 }
 



Summary of changes:
 bindings/engine.i                      |  2 --
 gnucash/gnome/gnc-budget-view.c        |  6 ++----
 gnucash/gnome/gnc-plugin-page-budget.c |  5 ++---
 libgnucash/engine/gnc-budget.cpp       | 32 +++++++++++++-------------------
 libgnucash/engine/gnc-budget.h         |  5 ++---
 libgnucash/engine/test/utest-Budget.c  |  3 +--
 6 files changed, 20 insertions(+), 33 deletions(-)



More information about the gnucash-changes mailing list