[qofinstance.cpp] add overloaded qof_instance_set|get_path_kvp

these overloaded functions to kvp slots do not require GValue
This commit is contained in:
Christopher Lam 2024-10-13 08:23:16 +08:00
parent ecabcef084
commit a26a6c4a6f
2 changed files with 33 additions and 0 deletions

View File

@ -36,6 +36,7 @@
#ifdef __cplusplus #ifdef __cplusplus
#include "kvp-frame.hpp" #include "kvp-frame.hpp"
#include <string> #include <string>
#include <optional>
extern "C" extern "C"
{ {
#endif #endif
@ -165,6 +166,12 @@ void qof_instance_get_path_kvp (QofInstance *, GValue *, std::vector<std::string
void qof_instance_set_path_kvp (QofInstance *, GValue const *, std::vector<std::string> const &); void qof_instance_set_path_kvp (QofInstance *, GValue const *, std::vector<std::string> const &);
template <typename T> std::optional<T>
qof_instance_get_path_kvp (QofInstance*, const Path&);
template <typename T> void
qof_instance_set_path_kvp (QofInstance*, std::optional<T>, const Path&);
bool qof_instance_has_path_slot (QofInstance const *, std::vector<std::string> const &); bool qof_instance_has_path_slot (QofInstance const *, std::vector<std::string> const &);
void qof_instance_slot_path_delete (QofInstance const *, std::vector<std::string> const &); void qof_instance_slot_path_delete (QofInstance const *, std::vector<std::string> const &);

View File

@ -1063,6 +1063,32 @@ qof_instance_set_kvp (QofInstance * inst, GValue const * value, unsigned count,
delete inst->kvp_data->set_path (path, kvp_value_from_gvalue (value)); delete inst->kvp_data->set_path (path, kvp_value_from_gvalue (value));
} }
template <typename T> std::optional<T>
qof_instance_get_path_kvp (QofInstance* inst, const Path& path)
{
g_return_val_if_fail (QOF_IS_INSTANCE(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 (QOF_IS_INSTANCE(inst));
delete inst->kvp_data->set_path(path, value ? new KvpValue(*value) : nullptr);
qof_instance_set_dirty (inst);
}
template std::optional<const char*> qof_instance_get_path_kvp <const char*> (QofInstance*, const Path&);
template std::optional<gnc_numeric> qof_instance_get_path_kvp <gnc_numeric> (QofInstance*, const Path&);
template std::optional<GncGUID*> qof_instance_get_path_kvp <GncGUID*> (QofInstance*, const Path&);
template std::optional<int64_t> qof_instance_get_path_kvp <int64_t> (QofInstance*, const Path&);
template void qof_instance_set_path_kvp <const char*> (QofInstance*, std::optional<const char*>, const Path& path);
template void qof_instance_set_path_kvp <gnc_numeric> (QofInstance*, std::optional<gnc_numeric>, const Path& path);
template void qof_instance_set_path_kvp <GncGUID*> (QofInstance*, std::optional<GncGUID*>, const Path& path);
template void qof_instance_set_path_kvp <int64_t> (QofInstance*, std::optional<int64_t>, const Path& path);
void qof_instance_get_path_kvp (QofInstance * inst, GValue * value, std::vector<std::string> const & path) void qof_instance_get_path_kvp (QofInstance * inst, GValue * value, std::vector<std::string> const & path)
{ {
gvalue_from_kvp_value (inst->kvp_data->get_slot (path), value); gvalue_from_kvp_value (inst->kvp_data->get_slot (path), value);