CsvImp - use std::optional instead of boost::optional

This commit is contained in:
Geert Janssens 2023-02-15 18:30:17 +01:00
parent c5109c67e5
commit 9efbdc15cf
9 changed files with 96 additions and 94 deletions

View File

@ -34,6 +34,7 @@
#include <exception>
#include <map>
#include <optional>
#include <string>
#include <boost/locale.hpp>
#include <boost/regex.hpp>
@ -160,17 +161,17 @@ void GncImportPrice::set (GncPricePropType prop_type, const std::string& value,
switch (prop_type)
{
case GncPricePropType::DATE:
m_date = boost::none;
m_date.reset();
m_date = GncDate(value, GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
break;
case GncPricePropType::AMOUNT:
m_amount = boost::none;
m_amount.reset();
m_amount = parse_amount_price (value, m_currency_format); // Throws if parsing fails
break;
case GncPricePropType::FROM_SYMBOL:
m_from_symbol = boost::none;
m_from_symbol.reset();
if (value.empty())
throw std::invalid_argument (_("'From Symbol' can not be empty."));
@ -190,7 +191,7 @@ void GncImportPrice::set (GncPricePropType prop_type, const std::string& value,
break;
case GncPricePropType::FROM_NAMESPACE:
m_from_namespace = boost::none;
m_from_namespace.reset();
if (value.empty())
throw std::invalid_argument (_("'From Namespace' can not be empty."));
@ -213,7 +214,7 @@ void GncImportPrice::set (GncPricePropType prop_type, const std::string& value,
break;
case GncPricePropType::TO_CURRENCY:
m_to_currency = boost::none;
m_to_currency.reset();
comm = parse_commodity_price_comm (value, GNC_COMMODITY_NS_CURRENCY); // Throws if parsing fails
if (comm)
{
@ -274,13 +275,13 @@ void GncImportPrice::reset (GncPricePropType prop_type)
std::string GncImportPrice::verify_essentials (void)
{
/* Make sure this price has the minimum required set of properties defined */
if (m_date == boost::none)
if (!m_date)
return _("No date column.");
else if (m_amount == boost::none)
else if (!m_amount)
return _("No amount column.");
else if (m_to_currency == boost::none)
else if (!m_to_currency)
return _("No 'Currency to'.");
else if (m_from_commodity == boost::none)
else if (!m_from_commodity)
return _("No 'Commodity from'.");
else if (gnc_commodity_equal (*m_from_commodity, *m_to_currency))
return _("'Commodity From' can not be the same as 'Currency To'.");

View File

@ -36,7 +36,7 @@
#include <string>
#include <map>
#include <memory>
#include <boost/optional.hpp>
#include <optional>
#include <gnc-datetime.hpp>
#include <gnc-numeric.hpp>
@ -94,22 +94,22 @@ public:
Result create_price (QofBook* book, GNCPriceDB *pdb, bool over);
gnc_commodity* get_from_commodity () { if (m_from_commodity) return *m_from_commodity; else return nullptr; }
void set_from_commodity (gnc_commodity* comm) { if (comm) m_from_commodity = comm; else m_from_commodity = boost::none; }
void set_from_commodity (gnc_commodity* comm) { if (comm) m_from_commodity = comm; else m_from_commodity.reset(); }
gnc_commodity* get_to_currency () { if (m_to_currency) return *m_to_currency; else return nullptr; }
void set_to_currency (gnc_commodity* curr) { if (curr) m_to_currency = curr; else m_to_currency = boost::none; }
void set_to_currency (gnc_commodity* curr) { if (curr) m_to_currency = curr; else m_to_currency.reset(); }
std::string errors();
private:
int m_date_format;
int m_currency_format;
boost::optional<GncDate> m_date;
boost::optional<GncNumeric> m_amount;
boost::optional<gnc_commodity*> m_from_commodity;
boost::optional<std::string> m_from_namespace;
boost::optional<std::string> m_from_symbol;
boost::optional<gnc_commodity*> m_to_currency;
std::optional<GncDate> m_date;
std::optional<GncNumeric> m_amount;
std::optional<gnc_commodity*> m_from_commodity;
std::optional<std::string> m_from_namespace;
std::optional<std::string> m_from_symbol;
std::optional<gnc_commodity*> m_to_currency;
bool created = false;
std::map<GncPricePropType, std::string> m_errors;

View File

@ -233,13 +233,13 @@ void GncPreTrans::set (GncTransPropType prop_type, const std::string& value)
switch (prop_type)
{
case GncTransPropType::UNIQUE_ID:
m_differ = boost::none;
m_differ.reset();
if (!value.empty())
m_differ = value;
break;
case GncTransPropType::DATE:
m_date = boost::none;
m_date.reset();
if (!value.empty())
m_date = GncDate(value, GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
else if (!m_multi_split)
@ -249,13 +249,13 @@ void GncPreTrans::set (GncTransPropType prop_type, const std::string& value)
break;
case GncTransPropType::NUM:
m_num = boost::none;
m_num.reset();
if (!value.empty())
m_num = value;
break;
case GncTransPropType::DESCRIPTION:
m_desc = boost::none;
m_desc.reset();
if (!value.empty())
m_desc = value;
else if (!m_multi_split)
@ -265,7 +265,7 @@ void GncPreTrans::set (GncTransPropType prop_type, const std::string& value)
break;
case GncTransPropType::NOTES:
m_notes = boost::none;
m_notes.reset();
if (!value.empty())
m_notes = value;
break;
@ -276,7 +276,7 @@ void GncPreTrans::set (GncTransPropType prop_type, const std::string& value)
break;
case GncTransPropType::VOID_REASON:
m_void_reason = boost::none;
m_void_reason.reset();
if (!value.empty())
m_void_reason = value;
break;
@ -433,19 +433,19 @@ void GncPreSplit::set (GncTransPropType prop_type, const std::string& value)
switch (prop_type)
{
case GncTransPropType::ACTION:
m_action = boost::none;
m_action.reset();
if (!value.empty())
m_action = value;
break;
case GncTransPropType::TACTION:
m_taction = boost::none;
m_taction.reset();
if (!value.empty())
m_taction = value;
break;
case GncTransPropType::ACCOUNT:
m_account = boost::none;
m_account.reset();
if (value.empty())
throw std::invalid_argument (_("Account value can't be empty."));
if ((acct = gnc_csv_account_map_search (value.c_str())) ||
@ -456,7 +456,7 @@ void GncPreSplit::set (GncTransPropType prop_type, const std::string& value)
break;
case GncTransPropType::TACCOUNT:
m_taccount = boost::none;
m_taccount.reset();
if (value.empty())
throw std::invalid_argument (_("Transfer account value can't be empty."));
@ -468,44 +468,44 @@ void GncPreSplit::set (GncTransPropType prop_type, const std::string& value)
break;
case GncTransPropType::MEMO:
m_memo = boost::none;
m_memo.reset();
if (!value.empty())
m_memo = value;
break;
case GncTransPropType::TMEMO:
m_tmemo = boost::none;
m_tmemo.reset();
if (!value.empty())
m_tmemo = value;
break;
case GncTransPropType::AMOUNT:
m_amount = boost::none;
m_amount.reset();
m_amount = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break;
case GncTransPropType::AMOUNT_NEG:
m_amount_neg = boost::none;
m_amount_neg.reset();
m_amount_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break;
case GncTransPropType::VALUE:
m_value = boost::none;
m_value.reset();
m_value = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break;
case GncTransPropType::VALUE_NEG:
m_value_neg = boost::none;
m_value_neg.reset();
m_value_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break;
case GncTransPropType::TAMOUNT:
m_tamount = boost::none;
m_tamount.reset();
m_tamount = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break;
case GncTransPropType::TAMOUNT_NEG:
m_tamount_neg = boost::none;
m_tamount_neg.reset();
m_tamount_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break;
@ -513,29 +513,29 @@ void GncPreSplit::set (GncTransPropType prop_type, const std::string& value)
/* Note while a price is not stricly a currency, it will likely use
* the same decimal point as currencies in the csv file, so parse
* using the same parser */
m_price = boost::none;
m_price.reset();
m_price = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break;
case GncTransPropType::REC_STATE:
m_rec_state = boost::none;
m_rec_state.reset();
m_rec_state = parse_reconciled (value); // Throws if parsing fails
break;
case GncTransPropType::TREC_STATE:
m_trec_state = boost::none;
m_trec_state.reset();
m_trec_state = parse_reconciled (value); // Throws if parsing fails
break;
case GncTransPropType::REC_DATE:
m_rec_date = boost::none;
m_rec_date.reset();
if (!value.empty())
m_rec_date = GncDate (value,
GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
break;
case GncTransPropType::TREC_DATE:
m_trec_date = boost::none;
m_trec_date.reset();
if (!value.empty())
m_trec_date = GncDate (value,
GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
@ -681,10 +681,10 @@ StrVec GncPreSplit::verify_essentials()
*/
static void trans_add_split (Transaction* trans, Account* account,
GncNumeric amount, GncNumeric value,
const boost::optional<std::string>& action,
const boost::optional<std::string>& memo,
const boost::optional<char>& rec_state,
const boost::optional<GncDate>& rec_date)
const std::optional<std::string>& action,
const std::optional<std::string>& memo,
const std::optional<char>& rec_state,
const std::optional<GncDate>& rec_date)
{
QofBook* book = xaccTransGetBook (trans);
auto split = xaccMallocSplit (book);
@ -740,7 +740,7 @@ void GncPreSplit::create_split (std::shared_ptr<DraftTransaction> draft_trans)
if (m_amount_neg)
amount -= *m_amount_neg;
boost::optional<GncNumeric> tamount;
std::optional<GncNumeric> tamount;
if (m_tamount || m_tamount_neg)
{
tamount = GncNumeric();
@ -876,7 +876,7 @@ void GncPreSplit::set_account (Account* acct)
if (acct)
m_account = acct;
else
m_account = boost::none;
m_account.reset();
UpdateCrossSplitCounters();
}

View File

@ -38,7 +38,7 @@
#include <string>
#include <map>
#include <memory>
#include <boost/optional.hpp>
#include <optional>
#include <gnc-datetime.hpp>
#include <gnc-numeric.hpp>
@ -138,15 +138,15 @@ struct DraftTransaction
~DraftTransaction () { if (trans) { xaccTransDestroy (trans); trans = nullptr; } }
Transaction* trans;
boost::optional<GncNumeric> m_price;
boost::optional<std::string> m_taction;
boost::optional<std::string> m_tmemo;
boost::optional<GncNumeric> m_tamount;
boost::optional<Account*> m_taccount;
boost::optional<char> m_trec_state;
boost::optional<GncDate> m_trec_date;
std::optional<GncNumeric> m_price;
std::optional<std::string> m_taction;
std::optional<std::string> m_tmemo;
std::optional<GncNumeric> m_tamount;
std::optional<Account*> m_taccount;
std::optional<char> m_trec_state;
std::optional<GncDate> m_trec_date;
boost::optional<std::string> void_reason;
std::optional<std::string> void_reason;
};
class GncPreTrans
@ -177,7 +177,7 @@ public:
* @returns true if this object is considered to be part of the parent, false otherwise.
*/
bool is_part_of (std::shared_ptr<GncPreTrans> parent);
boost::optional<std::string> get_void_reason() { return m_void_reason; }
std::optional<std::string> get_void_reason() { return m_void_reason; }
ErrMap errors();
void reset_cross_split_counters();
@ -190,13 +190,13 @@ public:
private:
int m_date_format;
bool m_multi_split;
boost::optional<std::string> m_differ;
boost::optional<GncDate> m_date;
boost::optional<std::string> m_num;
boost::optional<std::string> m_desc;
boost::optional<std::string> m_notes;
std::optional<std::string> m_differ;
std::optional<GncDate> m_date;
std::optional<std::string> m_num;
std::optional<std::string> m_desc;
std::optional<std::string> m_notes;
gnc_commodity *m_currency;
boost::optional<std::string> m_void_reason;
std::optional<std::string> m_void_reason;
bool created = false;
@ -241,23 +241,23 @@ private:
std::shared_ptr<GncPreTrans> m_pre_trans;
int m_date_format;
int m_currency_format;
boost::optional<std::string> m_action;
boost::optional<Account*> m_account;
boost::optional<GncNumeric> m_amount;
boost::optional<GncNumeric> m_amount_neg;
boost::optional<GncNumeric> m_value;
boost::optional<GncNumeric> m_value_neg;
boost::optional<GncNumeric> m_price;
boost::optional<std::string> m_memo;
boost::optional<char> m_rec_state;
boost::optional<GncDate> m_rec_date;
boost::optional<std::string> m_taction;
boost::optional<Account*> m_taccount;
boost::optional<GncNumeric> m_tamount;
boost::optional<GncNumeric> m_tamount_neg;
boost::optional<std::string> m_tmemo;
boost::optional<char> m_trec_state;
boost::optional<GncDate> m_trec_date;
std::optional<std::string> m_action;
std::optional<Account*> m_account;
std::optional<GncNumeric> m_amount;
std::optional<GncNumeric> m_amount_neg;
std::optional<GncNumeric> m_value;
std::optional<GncNumeric> m_value_neg;
std::optional<GncNumeric> m_price;
std::optional<std::string> m_memo;
std::optional<char> m_rec_state;
std::optional<GncDate> m_rec_date;
std::optional<std::string> m_taction;
std::optional<Account*> m_taccount;
std::optional<GncNumeric> m_tamount;
std::optional<GncNumeric> m_tamount_neg;
std::optional<std::string> m_tmemo;
std::optional<char> m_trec_state;
std::optional<GncDate> m_trec_date;
bool created = false;
ErrMap m_errors;

View File

@ -34,7 +34,7 @@
#include <string>
#include <vector>
#include <boost/optional.hpp>
#include <optional>
#include <gnc-datetime.hpp>
#include "gnc-tokenizer.hpp"

View File

@ -37,13 +37,13 @@
#include <exception>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include <vector>
#include <boost/regex.hpp>
#include <boost/regex/icu.hpp>
#include <boost/optional.hpp>
#include "gnc-import-price.hpp"
#include "gnc-imp-props-price.hpp"
@ -247,8 +247,8 @@ void GncPriceImport::encoding (const std::string& encoding)
std::string GncPriceImport::encoding () { return m_settings.m_encoding; }
void GncPriceImport::update_skipped_lines(boost::optional<uint32_t> start, boost::optional<uint32_t> end,
boost::optional<bool> alt, boost::optional<bool> errors)
void GncPriceImport::update_skipped_lines(std::optional<uint32_t> start, std::optional<uint32_t> end,
std::optional<bool> alt, std::optional<bool> errors)
{
if (start)
m_settings.m_skip_start_lines = *start;
@ -511,7 +511,7 @@ std::string GncPriceImport::verify ()
verify_column_selections (error_msg);
update_skipped_lines (boost::none, boost::none, boost::none, boost::none);
update_skipped_lines (std::nullopt, std::nullopt, std::nullopt, std::nullopt);
auto have_line_errors = false;
for (auto line : m_parsed_lines)

View File

@ -37,11 +37,11 @@
#include <set>
#include <map>
#include <memory>
#include <optional>
#include "gnc-tokenizer.hpp"
#include "gnc-imp-props-price.hpp"
#include "gnc-imp-settings-csv-price.hpp"
#include <boost/optional.hpp>
/* A set of currency formats that the user sees. */
extern const int num_currency_formats_price;
@ -106,8 +106,8 @@ public:
void encoding (const std::string& encoding);
std::string encoding ();
void update_skipped_lines (boost::optional<uint32_t> start, boost::optional<uint32_t> end,
boost::optional<bool> alt, boost::optional<bool> errors);
void update_skipped_lines (std::optional<uint32_t> start, std::optional<uint32_t> end,
std::optional<bool> alt, std::optional<bool> errors);
uint32_t skip_start_lines ();
uint32_t skip_end_lines ();
bool skip_alt_lines ();

View File

@ -36,6 +36,7 @@
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <string>
#include <tuple>
#include <utility>
@ -264,8 +265,8 @@ void GncTxImport::encoding (const std::string& encoding)
std::string GncTxImport::encoding () { return m_settings.m_encoding; }
void GncTxImport::update_skipped_lines(boost::optional<uint32_t> start, boost::optional<uint32_t> end,
boost::optional<bool> alt, boost::optional<bool> errors)
void GncTxImport::update_skipped_lines(std::optional<uint32_t> start, std::optional<uint32_t> end,
std::optional<bool> alt, std::optional<bool> errors)
{
if (start)
m_settings.m_skip_start_lines = *start;
@ -562,7 +563,7 @@ std::string GncTxImport::verify (bool with_acct_errors)
verify_column_selections (error_msg);
update_skipped_lines (boost::none, boost::none, boost::none, boost::none);
update_skipped_lines (std::nullopt, std::nullopt, std::nullopt, std::nullopt);
auto have_line_errors = false;
for (auto line : m_parsed_lines)

View File

@ -38,11 +38,11 @@
#include <set>
#include <map>
#include <memory>
#include <optional>
#include "gnc-tokenizer.hpp"
#include "gnc-imp-props-tx.hpp"
#include "gnc-imp-settings-csv-tx.hpp"
#include <boost/optional.hpp>
/* A set of currency formats that the user sees. */
@ -121,8 +121,8 @@ public:
void encoding (const std::string& encoding);
std::string encoding ();
void update_skipped_lines (boost::optional<uint32_t> start, boost::optional<uint32_t> end,
boost::optional<bool> alt, boost::optional<bool> errors);
void update_skipped_lines (std::optional<uint32_t> start, std::optional<uint32_t> end,
std::optional<bool> alt, std::optional<bool> errors);
uint32_t skip_start_lines ();
uint32_t skip_end_lines ();
bool skip_alt_lines ();