Skip to content

Commit a26a6c4

Browse files
[qofinstance.cpp] add overloaded qof_instance_set|get_path_kvp
these overloaded functions to kvp slots do not require GValue
1 parent ecabcef commit a26a6c4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

libgnucash/engine/qofinstance-p.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#ifdef __cplusplus
3737
#include "kvp-frame.hpp"
3838
#include <string>
39+
#include <optional>
3940
extern "C"
4041
{
4142
#endif
@@ -165,6 +166,12 @@ void qof_instance_get_path_kvp (QofInstance *, GValue *, std::vector<std::string
165166

166167
void qof_instance_set_path_kvp (QofInstance *, GValue const *, std::vector<std::string> const &);
167168

169+
template <typename T> std::optional<T>
170+
qof_instance_get_path_kvp (QofInstance*, const Path&);
171+
172+
template <typename T> void
173+
qof_instance_set_path_kvp (QofInstance*, std::optional<T>, const Path&);
174+
168175
bool qof_instance_has_path_slot (QofInstance const *, std::vector<std::string> const &);
169176

170177
void qof_instance_slot_path_delete (QofInstance const *, std::vector<std::string> const &);

libgnucash/engine/qofinstance.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,32 @@ qof_instance_set_kvp (QofInstance * inst, GValue const * value, unsigned count,
10631063
delete inst->kvp_data->set_path (path, kvp_value_from_gvalue (value));
10641064
}
10651065

1066+
template <typename T> std::optional<T>
1067+
qof_instance_get_path_kvp (QofInstance* inst, const Path& path)
1068+
{
1069+
g_return_val_if_fail (QOF_IS_INSTANCE(inst), std::nullopt);
1070+
auto kvp_value{inst->kvp_data->get_slot(path)};
1071+
return kvp_value ? std::make_optional<T>(kvp_value->get<T>()) : std::nullopt;
1072+
}
1073+
1074+
template <typename T> void
1075+
qof_instance_set_path_kvp (QofInstance* inst, std::optional<T> value, const Path& path)
1076+
{
1077+
g_return_if_fail (QOF_IS_INSTANCE(inst));
1078+
delete inst->kvp_data->set_path(path, value ? new KvpValue(*value) : nullptr);
1079+
qof_instance_set_dirty (inst);
1080+
}
1081+
1082+
template std::optional<const char*> qof_instance_get_path_kvp <const char*> (QofInstance*, const Path&);
1083+
template std::optional<gnc_numeric> qof_instance_get_path_kvp <gnc_numeric> (QofInstance*, const Path&);
1084+
template std::optional<GncGUID*> qof_instance_get_path_kvp <GncGUID*> (QofInstance*, const Path&);
1085+
template std::optional<int64_t> qof_instance_get_path_kvp <int64_t> (QofInstance*, const Path&);
1086+
1087+
template void qof_instance_set_path_kvp <const char*> (QofInstance*, std::optional<const char*>, const Path& path);
1088+
template void qof_instance_set_path_kvp <gnc_numeric> (QofInstance*, std::optional<gnc_numeric>, const Path& path);
1089+
template void qof_instance_set_path_kvp <GncGUID*> (QofInstance*, std::optional<GncGUID*>, const Path& path);
1090+
template void qof_instance_set_path_kvp <int64_t> (QofInstance*, std::optional<int64_t>, const Path& path);
1091+
10661092
void qof_instance_get_path_kvp (QofInstance * inst, GValue * value, std::vector<std::string> const & path)
10671093
{
10681094
gvalue_from_kvp_value (inst->kvp_data->get_slot (path), value);

0 commit comments

Comments
 (0)