From d2597ef1e292cd67c315c04a01d282ab2374b1ba Mon Sep 17 00:00:00 2001 From: Geert Janssens Date: Sun, 11 Dec 2016 16:02:19 +0100 Subject: [PATCH] Defer testing boost:optionals until really needed Similarly only lookup book when really needed --- src/import-export/csv-imp/gnc-trans-props.cpp | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/import-export/csv-imp/gnc-trans-props.cpp b/src/import-export/csv-imp/gnc-trans-props.cpp index df2781a2f6..99182586b0 100644 --- a/src/import-export/csv-imp/gnc-trans-props.cpp +++ b/src/import-export/csv-imp/gnc-trans-props.cpp @@ -379,20 +379,22 @@ std::string GncPreSplit::verify_essentials (void) * @param book The book where the split should be stored * @param amount The amount of the split */ -static void trans_add_split (Transaction* trans, Account* account, QofBook* book, - gnc_numeric amount, const std::string& action, const std::string& memo) +static void trans_add_split (Transaction* trans, Account* account, + gnc_numeric amount, const boost::optional& action, + const boost::optional& memo) { + auto book = xaccTransGetBook (trans); auto split = xaccMallocSplit (book); xaccSplitSetAccount (split, account); xaccSplitSetParent (split, trans); xaccSplitSetAmount (split, amount); xaccSplitSetValue (split, amount); - if (!memo.empty()) - xaccSplitSetMemo (split, memo.c_str()); + if (memo) + xaccSplitSetMemo (split, memo->c_str()); /* Note, this function assumes the num/action switch is done at a higher level * if needed by the book option */ - if (!action.empty()) - xaccSplitSetAction (split, action.c_str()); + if (action) + xaccSplitSetAction (split, action->c_str()); } boost::optional GncPreSplit::create_split (Transaction* trans) @@ -400,11 +402,6 @@ boost::optional GncPreSplit::create_split (Transaction* trans) if (created) return boost::none; - auto book = xaccTransGetBook (trans); - std::string action; - std::string taction; - std::string memo; - std::string tmemo; Account *account = nullptr; Account *taccount = nullptr; bool amount_set = false; @@ -412,18 +409,10 @@ boost::optional GncPreSplit::create_split (Transaction* trans) gnc_numeric withdrawal = { 0, 1 }; gnc_numeric amount = { 0, 1 }; - if (m_taction) - taction = *m_taction; - if (m_action) - action = *m_action; if (m_account) account = *m_account; if (m_taccount) taccount = *m_taccount; - if (m_memo) - memo = *m_memo; - if (m_tmemo) - tmemo = *m_tmemo; if (m_deposit) { deposit = *m_deposit; @@ -440,13 +429,13 @@ boost::optional GncPreSplit::create_split (Transaction* trans) GNC_HOW_RND_ROUND_HALF_UP); /* Add a split with the cumulative amount value. */ - trans_add_split (trans, account, book, amount, action, memo); + trans_add_split (trans, account, amount, m_action, m_memo); if (taccount) /* Note: the current importer assumes at most 2 splits. This means the second split amount * will be the negative of the the first split amount. */ - trans_add_split (trans, taccount, book, gnc_numeric_neg(amount), taction, tmemo); + trans_add_split (trans, taccount, gnc_numeric_neg(amount), m_taction, m_tmemo); created = true;