From f3eee511e88d406091151cfd4c8122ca0bbc40c8 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Tue, 6 Aug 2019 15:29:58 -0700 Subject: [PATCH] Add free functions to create a new GncOptionDB and to register options. The objective of the free functions is to hide the GncOption from language bindings so that the GncOptions can be moved into the GncOptionDB instead of having shared ptrs splattered around the heap. Nearly all access to the options can then be mediated through the GncOptionDB container. Note that gnc_option_db_new creates the GncOptionDB on the heap and returns a raw ptr, so it's up to the creator of the GncOptionDB to call delete on it when it's no longer needed. --- libgnucash/app-utils/gnc-optiondb.cpp | 58 +++++++++++++++++++ libgnucash/app-utils/gnc-optiondb.hpp | 39 +++++++++++++ .../app-utils/test/gtest-gnc-optiondb.cpp | 9 +++ 3 files changed, 106 insertions(+) diff --git a/libgnucash/app-utils/gnc-optiondb.cpp b/libgnucash/app-utils/gnc-optiondb.cpp index 29b140f1a1..fbc08f410b 100644 --- a/libgnucash/app-utils/gnc-optiondb.cpp +++ b/libgnucash/app-utils/gnc-optiondb.cpp @@ -157,3 +157,61 @@ GncOptionDB::commit() }); }); } + +GncOptionDB* +gnc_option_db_new(void) +{ + return new GncOptionDB; +} + +void +gnc_register_string_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, std::string value) +{ + GncOption option{section, name, key, doc_string, value}; + db->register_option(section, std::move(option)); +} + +void +gnc_register_text_option(GncOptionDB* db, const char* section, const char* name, + const char* key, const char* doc_string, + std::string value) +{ + gnc_register_string_option(db, section, name, key, doc_string, value); +} + +void +gnc_register_budget_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, GncBudget *value) +{ + GncOption option{section, name, key, doc_string, QOF_INSTANCE(value)}; + db->register_option(section, std::move(option)); +} + +void +gnc_register_commodity_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, gnc_commodity *value) +{ + GncOption option{section, name, key, doc_string, QOF_INSTANCE(value)}; + db->register_option(section, std::move(option)); +} + + +void +gnc_register_currency_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, gnc_commodity *value) +{ + GncOption option{GncOptionValidatedValue{ + section, name, key, doc_string, QOF_INSTANCE(value), + [](QofInstance* new_value) -> bool + { + return GNC_IS_COMMODITY (new_value) && + gnc_commodity_is_currency(GNC_COMMODITY(new_value)); + } + }}; + db->register_option(section, std::move(option)); +} diff --git a/libgnucash/app-utils/gnc-optiondb.hpp b/libgnucash/app-utils/gnc-optiondb.hpp index 07e3846965..69503c22c6 100644 --- a/libgnucash/app-utils/gnc-optiondb.hpp +++ b/libgnucash/app-utils/gnc-optiondb.hpp @@ -66,6 +66,45 @@ private: GncOptionSectionPtr m_default_section; std::vector m_sections; bool m_dirty = false; + + std::function m_get_ui_value; + std::function m_set_ui_value; }; +/** + * Create an empty option database. + * + * It would be nice to use a std::shared_ptr here but Swig doesn't implement + * that for Guile. + * @return A newly allocated GncOptionDB. Use delete to destroy it. + */ +GncOptionDB *gnc_option_db_new(void); + +void gnc_register_string_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, std::string value); + + +void gnc_register_string_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, std::string value); + +void gnc_register_text_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, std::string value); + +void gnc_register_budget_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, GncBudget* value); + +void gnc_register_commodity_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, + gnc_commodity* value); + +void gnc_register_currency_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, gnc_commodity* value); + + #endif //GNC_OPTIONDB_HPP_ diff --git a/libgnucash/app-utils/test/gtest-gnc-optiondb.cpp b/libgnucash/app-utils/test/gtest-gnc-optiondb.cpp index d33311bb18..a09659fde7 100644 --- a/libgnucash/app-utils/test/gtest-gnc-optiondb.cpp +++ b/libgnucash/app-utils/test/gtest-gnc-optiondb.cpp @@ -56,3 +56,12 @@ TEST(GncOptionDB, test_unregister_option) optiondb.unregister_option("foo", "bar"); EXPECT_TRUE(optiondb.lookup_string_option("foo", "bar").empty()); } + +TEST(GncOptionDB, test_register_string_option) +{ + GncOptionDB* db = gnc_option_db_new(); + gnc_register_string_option(db, "foo", "bar", "baz", "Phony Option", + std::string{"waldo"}); + EXPECT_STREQ("waldo", db->lookup_string_option("foo", "bar").c_str()); + delete db; +}