Replace the direct wrapping of GncOptionDB-set-option-string-foo.

With a simple type-free function gnc_option_set. This mirrors the current
(gnc:option-set(gnc:lookup-option... and takes care of SCM type conversion.
This commit is contained in:
John Ralls 2019-11-01 16:14:30 -07:00
parent 252ba9b477
commit 2f2ac99944
3 changed files with 62 additions and 7 deletions

View File

@ -540,9 +540,9 @@ public:
option.make_internal();
}, m_option);
}
const GncOptionVariant& _get_option() const { return m_option; }
GncOptionVariant& _get_option() const { return m_option; }
private:
GncOptionVariant m_option;
mutable GncOptionVariant m_option;
};
#endif //GNC_OPTION_HPP_

View File

@ -100,6 +100,7 @@ scm_from_value<const QofInstance*>(const QofInstance* value)
return scm_guid;
}
/* Not needed now, the default template will do this
template <> inline SCM
scm_from_value<QofQuer*>(const QofQuery* value)
@ -129,6 +130,44 @@ template <>inline SCM
return SCM_BOOL_F;
}
*/
template <typename ValueType> inline ValueType
scm_to_value(SCM new_value)
{
return ValueType{};
}
template <> inline std::string
scm_to_value<std::string>(SCM new_value)
{
auto strval = scm_to_utf8_stringn(new_value, nullptr);
std::string retval{strval};
free(strval);
return retval;
}
/*
template <> inline std::string
scm_to_value<char*>(SCM new_value)
{
auto strval = scm_to_utf8_stringn(new_value, nullptr);
std::string retval{strval};
free(strval);
return retval;
}
*/
template <> inline int
scm_to_value<int>(SCM new_value)
{
return scm_to_int(new_value);
}
template <> inline int64_t
scm_to_value<int64_t>(SCM new_value)
{
return scm_to_int64(new_value);
}
%}
%ignore OptionClassifier;
%ignore OptionUIItem;
@ -177,6 +216,12 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
return scm_from_value(static_cast<decltype(value)>(value));
}, $self->_get_option());
}
void set_value_from_scm(SCM new_value)
{
std::visit([new_value](auto& option) {
option.set_value(scm_to_value<std::decay_t<decltype(option.get_value())>>(new_value));
}, $self->_get_option());
}
};
%extend GncOptionDB {
@ -195,6 +240,17 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
return GncOption_get_scm_value(&(db_opt->get()));
}
void gnc_set_option(const GncOptionDBPtr& optiondb, const char* section,
const char* name, SCM new_value)
{
auto db_opt = optiondb->find_option(section, name);
if (!db_opt)
{
// PWARN("Attempt to write non-existent option %s/%s", section, name);
return;
}
GncOption_set_value_from_scm(&(db_opt->get()), new_value);
}
%}
/*
TEST(GncOption, test_string_scm_functions)

View File

@ -47,7 +47,7 @@
"Phony Option" "waldo")))
(test-equal "waldo" (gnc-option-value option-db "foo" "bar"))
(GncOptionDB-set-option-string (GncOptionDBPtr-get option-db) "foo" "bar" "pepper")
(gnc-set-option option-db "foo" "bar" "pepper")
(test-equal "pepper" (gnc-option-value option-db "foo" "bar")))
(test-end "test-gnc-make-string-option"))
@ -76,8 +76,7 @@
(multi-opt (gnc-register-multichoice-option option-db "foo" "bar" "baz"
"Phony Option" multichoice)))
(GncOptionDB-set-option-string
(GncOptionDBPtr-get option-db) "foo" "bar" "corge")
(gnc-set-option option-db "foo" "bar" "corge")
(test-equal "corge" (gnc-option-value option-db "foo" "bar")))
(test-end "test-gnc-test-multichoice-option"))
@ -88,7 +87,7 @@
"baz" "Phony Option"))
(a-time (gnc-dmy2time64 11 07 2019)))
(test-equal (current-time) (gnc-option-value option-db "foo" "bar"))
(GncOptionDB-set-option-time64 (GncOptionDBPtr-get option-db) "foo" "bar" a-time)
(gnc-set-option option-db "foo" "bar" a-time)
(test-equal a-time (gnc-option-value option-db "foo" "bar"))
(test-end "test-gnc-test-date-option")))
@ -99,6 +98,6 @@
"baz" "Phony Option"
15 5 30 1)))
(test-equal 15 (gnc-option-value option-db "foo" "bar"))
(GncOptionDB-set-option-int (GncOptionDBPtr-get option-db) "foo" "bar" 20)
(gnc-set-option option-db "foo" "bar" 20)
(test-equal 20 (gnc-option-value option-db "foo" "bar")))
(test-end "test-gnc-number-range-option"))