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.
pull/1191/head
John Ralls 7 years ago
parent 455d3c2d60
commit f3eee511e8

@ -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<QofInstance*>{
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));
}

@ -66,6 +66,45 @@ private:
GncOptionSectionPtr m_default_section;
std::vector<GncOptionSection> m_sections;
bool m_dirty = false;
std::function<void*()> m_get_ui_value;
std::function<void(void*)> 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_

@ -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;
}

Loading…
Cancel
Save