From 2f2ac99944e8326168ce8e086bfe1d0c6975861a Mon Sep 17 00:00:00 2001 From: John Ralls Date: Fri, 1 Nov 2019 16:14:30 -0700 Subject: [PATCH] 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. --- libgnucash/app-utils/gnc-option.hpp | 4 +- libgnucash/app-utils/gnc-optiondb.i | 56 +++++++++++++++++++ .../app-utils/test/test-gnc-optiondb.scm | 9 ++- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/libgnucash/app-utils/gnc-option.hpp b/libgnucash/app-utils/gnc-option.hpp index 463ec762dd..b4710719cf 100644 --- a/libgnucash/app-utils/gnc-option.hpp +++ b/libgnucash/app-utils/gnc-option.hpp @@ -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_ diff --git a/libgnucash/app-utils/gnc-optiondb.i b/libgnucash/app-utils/gnc-optiondb.i index 844ce542ef..2971ec9d45 100644 --- a/libgnucash/app-utils/gnc-optiondb.i +++ b/libgnucash/app-utils/gnc-optiondb.i @@ -100,6 +100,7 @@ scm_from_value(const QofInstance* value) return scm_guid; } + /* Not needed now, the default template will do this template <> inline SCM scm_from_value(const QofQuery* value) @@ -129,6 +130,44 @@ template <>inline SCM return SCM_BOOL_F; } */ + +template inline ValueType +scm_to_value(SCM new_value) +{ + return ValueType{}; +} + +template <> inline std::string +scm_to_value(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(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(SCM new_value) +{ + return scm_to_int(new_value); +} + +template <> inline int64_t +scm_to_value(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(value)); }, $self->_get_option()); } + void set_value_from_scm(SCM new_value) + { + std::visit([new_value](auto& option) { + option.set_value(scm_to_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) diff --git a/libgnucash/app-utils/test/test-gnc-optiondb.scm b/libgnucash/app-utils/test/test-gnc-optiondb.scm index fc523001f4..09fc1e6d93 100644 --- a/libgnucash/app-utils/test/test-gnc-optiondb.scm +++ b/libgnucash/app-utils/test/test-gnc-optiondb.scm @@ -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"))