Skip to content

Commit

Permalink
fixup! [account.cpp] don't nullcheck acc and inst
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Oct 15, 2024
1 parent 2c5a197 commit 793c14d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
37 changes: 23 additions & 14 deletions libgnucash/engine/Account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2481,8 +2481,6 @@ static void
set_kvp_gnc_numeric_path (Account *acc, const std::vector<std::string>& path,
std::optional<gnc_numeric> value)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));

xaccAccountBeginEdit(acc);
qof_instance_set_path_kvp<gnc_numeric> (QOF_INSTANCE(acc), value, path);
xaccAccountCommitEdit(acc);
Expand All @@ -2491,16 +2489,13 @@ set_kvp_gnc_numeric_path (Account *acc, const std::vector<std::string>& path,
static std::optional<gnc_numeric>
get_kvp_gnc_numeric_path (const Account *acc, const Path& path)
{
g_return_val_if_fail (acc, std::nullopt);
return qof_instance_get_path_kvp<gnc_numeric> (QOF_INSTANCE(acc), path);
}

static void
set_kvp_string_path (Account *acc, std::vector<std::string> const & path,
const char *value)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));

std::optional<const char*> val;
if (value && *value)
val = g_strdup(value);
Expand All @@ -2513,15 +2508,13 @@ set_kvp_string_path (Account *acc, std::vector<std::string> const & path,
static const char*
get_kvp_string_path (const Account *acc, const Path& path)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), nullptr);
auto rv{qof_instance_get_path_kvp<const char*> (QOF_INSTANCE(acc), path)};
return rv ? *rv : nullptr;
}

static void
set_kvp_account_path (Account* acc, const Path& path, const Account* kvp_account)
{
g_return_if_fail (GNC_IS_ACCOUNT(acc));
std::optional<GncGUID*> val;
if (kvp_account)
val = guid_copy(xaccAccountGetGUID (kvp_account));
Expand All @@ -2534,22 +2527,19 @@ set_kvp_account_path (Account* acc, const Path& path, const Account* kvp_account
static Account*
get_kvp_account_path (const Account *acc, const Path& path)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), nullptr);
auto val{qof_instance_get_path_kvp<GncGUID*> (QOF_INSTANCE(acc), path)};
return val ? xaccAccountLookup (*val, gnc_account_get_book (acc)) : nullptr;
}

static void
set_kvp_boolean_path (Account *acc, const Path& path, gboolean option)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_string_path (acc, path, option ? "true" : nullptr);
}

static gboolean
get_kvp_boolean_path (const Account *acc, const Path& path)
{
g_return_val_if_fail(GNC_IS_ACCOUNT(acc), FALSE);
auto slot{QOF_INSTANCE(acc)->kvp_data->get_slot(path)};
if (!slot) return false;
switch (slot->get_type())
Expand All @@ -2566,8 +2556,6 @@ get_kvp_boolean_path (const Account *acc, const Path& path)
static void
set_kvp_int64_path (Account *acc, const Path& path, std::optional<gint64> value)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));

xaccAccountBeginEdit(acc);
qof_instance_set_path_kvp<int64_t> (QOF_INSTANCE(acc), value, path);
xaccAccountCommitEdit(acc);
Expand All @@ -2576,31 +2564,34 @@ set_kvp_int64_path (Account *acc, const Path& path, std::optional<gint64> value)
static const std::optional<gint64>
get_kvp_int64_path (const Account *acc, const Path& path)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), std::nullopt);
return qof_instance_get_path_kvp<int64_t> (QOF_INSTANCE(acc), path);
}

void
xaccAccountSetColor (Account *acc, const char *str)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_string_path (acc, {"color"}, str);
}

void
xaccAccountSetFilter (Account *acc, const char *str)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_string_path (acc, {"filter"}, str);
}

void
xaccAccountSetSortOrder (Account *acc, const char *str)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_string_path (acc, {"sort-order"}, str);
}

void
xaccAccountSetSortReversed (Account *acc, gboolean sortreversed)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_boolean_path (acc, {"sort-reversed"}, sortreversed);
}

Expand All @@ -2625,6 +2616,7 @@ qofAccountSetParent (Account *acc, QofInstance *parent)
void
xaccAccountSetNotes (Account *acc, const char *str)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_string_path (acc, {"notes"}, str);
}

Expand Down Expand Up @@ -3904,7 +3896,8 @@ xaccAccountGetNoclosingBalanceChangeInCurrencyForPeriod (Account *acc, time64 t1
const SplitsVec
xaccAccountGetSplits (const Account *account)
{
return GNC_IS_ACCOUNT(account) ? GET_PRIVATE(account)->splits : SplitsVec{};
g_return_val_if_fail (GNC_IS_ACCOUNT(account), SplitsVec{});
return GET_PRIVATE(account)->splits;
}

SplitList *
Expand All @@ -3919,6 +3912,7 @@ xaccAccountGetSplitList (const Account *acc)
size_t
xaccAccountGetSplitsSize (const Account *account)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(account), 0);
return GNC_IS_ACCOUNT(account) ? GET_PRIVATE(account)->splits.size() : 0;
}

Expand Down Expand Up @@ -3994,12 +3988,14 @@ xaccAccountForEachLot(const Account *acc,
gboolean
xaccAccountGetTaxRelated (const Account *acc)
{
g_return_val_if_fail(GNC_IS_ACCOUNT(acc), false);
return get_kvp_boolean_path(acc, {"tax-related"});
}

void
xaccAccountSetTaxRelated (Account *acc, gboolean tax_related)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_boolean_path(acc, {"tax-related"}, tax_related);
}

Expand All @@ -4013,6 +4009,7 @@ xaccAccountGetTaxUSCode (const Account *acc)
void
xaccAccountSetTaxUSCode (Account *acc, const char *code)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_string_path (acc, {"tax-US", "code"}, code);
}

Expand All @@ -4026,6 +4023,7 @@ xaccAccountGetTaxUSPayerNameSource (const Account *acc)
void
xaccAccountSetTaxUSPayerNameSource (Account *acc, const char *source)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_string_path (acc, {"tax-US", "payer-name-source"}, source);
}

Expand Down Expand Up @@ -4078,30 +4076,35 @@ const char *gnc_account_get_credit_string (GNCAccountType acct_type)
gboolean
xaccAccountGetPlaceholder (const Account *acc)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), false);
return get_kvp_boolean_path(acc, {"placeholder"});
}

void
xaccAccountSetPlaceholder (Account *acc, gboolean val)
{
g_return_if_fail (GNC_IS_ACCOUNT(acc));
set_kvp_boolean_path(acc, {"placeholder"}, val);
}

gboolean
xaccAccountGetAppendText (const Account *acc)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), false);
return get_kvp_boolean_path(acc, {"import-append-text"});
}

void
xaccAccountSetAppendText (Account *acc, gboolean val)
{
g_return_if_fail (GNC_IS_ACCOUNT(acc));
set_kvp_boolean_path(acc, {"import-append-text"}, val);
}

gboolean
xaccAccountGetIsOpeningBalance (const Account *acc)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), false);
if (GET_PRIVATE(acc)->type != ACCT_TYPE_EQUITY)
return false;

Expand All @@ -4111,6 +4114,7 @@ xaccAccountGetIsOpeningBalance (const Account *acc)
void
xaccAccountSetIsOpeningBalance (Account *acc, gboolean val)
{
g_return_if_fail (GNC_IS_ACCOUNT(acc));
if (GET_PRIVATE(acc)->type != ACCT_TYPE_EQUITY)
return;
set_kvp_string_path(acc, {"equity-type"}, val ? "opening-balance" : nullptr);
Expand All @@ -4132,12 +4136,14 @@ xaccAccountGetDescendantPlaceholder (const Account *acc)
gboolean
xaccAccountGetAutoInterest (const Account *acc)
{
g_return_val_if_fail(GNC_IS_ACCOUNT(acc), false);
return get_kvp_boolean_path (acc, {KEY_RECONCILE_INFO, "auto-interest-transfer"});
}

void
xaccAccountSetAutoInterest (Account *acc, gboolean val)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_boolean_path (acc, {KEY_RECONCILE_INFO, "auto-interest-transfer"}, val);
}

Expand All @@ -4147,12 +4153,14 @@ xaccAccountSetAutoInterest (Account *acc, gboolean val)
gboolean
xaccAccountGetHidden (const Account *acc)
{
g_return_val_if_fail(GNC_IS_ACCOUNT(acc), false);
return get_kvp_boolean_path (acc, {"hidden"});
}

void
xaccAccountSetHidden (Account *acc, gboolean val)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
set_kvp_boolean_path (acc, {"hidden"}, val);
}

Expand Down Expand Up @@ -5532,6 +5540,7 @@ gnc_account_imap_find_account_bayes (Account *acc, GList *tokens)
static void
change_imap_entry (Account *acc, std::string const & path, int64_t token_count)
{
g_return_if_fail(GNC_IS_ACCOUNT(acc));
PINFO("Source Account is '%s', Count is '%" G_GINT64_FORMAT "'",
xaccAccountGetName (acc), token_count);

Expand Down
2 changes: 0 additions & 2 deletions libgnucash/engine/qofinstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,15 +1066,13 @@ qof_instance_set_kvp (QofInstance * inst, GValue const * value, unsigned count,
template <typename T> std::optional<T>
qof_instance_get_path_kvp (QofInstance* inst, const Path& path)
{
g_return_val_if_fail(inst, std::nullopt);
auto kvp_value{inst->kvp_data->get_slot(path)};
return kvp_value ? std::make_optional<T>(kvp_value->get<T>()) : std::nullopt;
}

template <typename T> void
qof_instance_set_path_kvp (QofInstance* inst, std::optional<T> value, const Path& path)
{
g_return_if_fail(inst);
delete inst->kvp_data->set_path(path, value ? new KvpValue(*value) : nullptr);
qof_instance_set_dirty (inst);
}
Expand Down

0 comments on commit 793c14d

Please sign in to comment.