Skip to content

Commit

Permalink
[account.cpp] refactor qof_instance_get|set_path_kvp
Browse files Browse the repository at this point in the history
note these are overloaded functions returning type T instead of
getting/setting GValues
  • Loading branch information
christopherlam committed Oct 8, 2024
1 parent 8fa5694 commit ebb5a0a
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions libgnucash/engine/Account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2479,16 +2479,28 @@ xaccAccountSetDescription (Account *acc, const char *str)

using StrVec = std::vector<std::string>;

template <typename T> std::optional<T>
qof_instance_get_path_kvp (QofInstance* inst, StrVec path)
{
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, StrVec path, std::optional<T> value)
{
delete inst->kvp_data->set_path(path, value ? new KvpValue(*value) : nullptr);
qof_instance_set_dirty (inst);
}

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);
auto inst{QOF_INSTANCE(acc)};
delete inst->kvp_data->set_path (path, value ? new KvpValue(*value) : nullptr);
qof_instance_set_dirty (inst);
qof_instance_set_path_kvp<gnc_numeric> (QOF_INSTANCE(acc), path, value);
xaccAccountCommitEdit(acc);
}

Expand All @@ -2498,49 +2510,47 @@ set_kvp_string_path (Account *acc, const StrVec& path, const char *value)
g_return_if_fail(GNC_IS_ACCOUNT(acc));

xaccAccountBeginEdit(acc);
auto inst{QOF_INSTANCE(acc)};
delete inst->kvp_data->set_path (path, (value && *value) ? new KvpValue(g_strdup(value)) : nullptr);
qof_instance_set_dirty (inst);
if (value && *value)
qof_instance_set_path_kvp<const char*> (QOF_INSTANCE(acc), path, g_strdup(value));
else
qof_instance_set_path_kvp<const char*> (QOF_INSTANCE(acc), path, std::nullopt);
xaccAccountCommitEdit(acc);
}

static const char*
get_kvp_string_path (const Account *acc, const StrVec& path)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), nullptr);
auto slot{QOF_INSTANCE(acc)->kvp_data->get_slot(path)};
return slot ? slot->get<const char*>() : 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 StrVec& 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));

xaccAccountBeginEdit(acc);
auto inst{QOF_INSTANCE(acc)};
delete inst->kvp_data->set_path (path, kvp_account
? new KvpValue(guid_copy(xaccAccountGetGUID (kvp_account)))
: nullptr);
qof_instance_set_dirty (inst);
qof_instance_set_path_kvp<GncGUID*> (QOF_INSTANCE(acc), path, val);
xaccAccountCommitEdit(acc);
}

static std::optional<gnc_numeric>
get_kvp_gnc_numeric_path (const Account *acc, const std::vector<std::string>& path)
get_kvp_gnc_numeric_path (const Account *acc, const StrVec& path)
{
g_return_val_if_fail (acc, std::nullopt);
if (auto slot{QOF_INSTANCE(acc)->kvp_data->get_slot(path)})
return *slot->get_ptr<gnc_numeric>();
return {};
return qof_instance_get_path_kvp<gnc_numeric> (QOF_INSTANCE(acc), path);
}

static Account*
get_kvp_account_path (const Account *acc, const StrVec& path)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), nullptr);
auto slot{QOF_INSTANCE(acc)->kvp_data->get_slot(path)};
return slot ? xaccAccountLookup (slot->get<GncGUID*>(), gnc_account_get_book (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
Expand Down Expand Up @@ -4023,20 +4033,15 @@ set_kvp_int64_path (Account *acc, const StrVec& path, std::optional<gint64> valu
g_return_if_fail(GNC_IS_ACCOUNT(acc));

xaccAccountBeginEdit(acc);
auto inst{QOF_INSTANCE(acc)};
delete inst->kvp_data->set_path (path, value ? new KvpValue(*value) : nullptr);
qof_instance_set_dirty (inst);
qof_instance_set_path_kvp<int64_t> (QOF_INSTANCE(acc), path, value);
xaccAccountCommitEdit(acc);
}

static const std::optional<gint64>
get_kvp_int64_path (const Account *acc, const StrVec& path)
{
g_return_val_if_fail (GNC_IS_ACCOUNT(acc), std::nullopt);
if (auto slot{QOF_INSTANCE(acc)->kvp_data->get_slot(path)})
return slot->get<int64_t>();

return {};
return qof_instance_get_path_kvp<int64_t> (QOF_INSTANCE(acc), path);
}

gint64
Expand Down

0 comments on commit ebb5a0a

Please sign in to comment.