mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Convert GncOptionSection from a std::pair to a class.
Provides find_option(const char*) and foreach_option(func) for easy iteration. find_option and find_section now return plain const ptrs instead of std::optionals. Much less cumbersome though the compiler won't nag if you forget the nullptr check.
This commit is contained in:
parent
c63db36a60
commit
3b4785e744
@ -41,7 +41,25 @@ extern "C"
|
||||
}
|
||||
|
||||
using GncOptionVec = std::vector<GncOption>;
|
||||
using GncOptionSection = std::pair<std::string, GncOptionVec>;
|
||||
|
||||
class GncOptionSection
|
||||
{
|
||||
std::string m_name;
|
||||
GncOptionVec m_options;
|
||||
public:
|
||||
GncOptionSection(const char* name) : m_name{name}, m_options{} {}
|
||||
~GncOptionSection() = default;
|
||||
|
||||
void foreach_option(std::function<void(GncOption&)> func);
|
||||
const std::string& get_name() const noexcept { return m_name; }
|
||||
size_t get_num_options() const noexcept { return m_options.size(); }
|
||||
void add_option(GncOption&& option);
|
||||
void remove_option(const char* name);
|
||||
const GncOption* find_option(const char* name) const;
|
||||
};
|
||||
|
||||
using GncOptionSectionPtr = std::shared_ptr<GncOptionSection>;
|
||||
|
||||
class GncOptionDB
|
||||
{
|
||||
public:
|
||||
@ -49,15 +67,27 @@ public:
|
||||
GncOptionDB(QofBook* book);
|
||||
~GncOptionDB() = default;
|
||||
|
||||
/* The non-const version can't be redirected to the const one because the
|
||||
* function parameters are incompatible.
|
||||
*/
|
||||
void foreach_section(std::function<void(GncOptionSectionPtr&)> func)
|
||||
{
|
||||
for (auto& section : m_sections)
|
||||
func(section);
|
||||
}
|
||||
void foreach_section(std::function<void(const GncOptionSectionPtr&)> func) const
|
||||
{
|
||||
for (auto& section : m_sections)
|
||||
func(section);
|
||||
}
|
||||
size_t num_sections() const noexcept { return m_sections.size(); }
|
||||
void save_to_book(QofBook* book, bool do_clear) const;
|
||||
int num_sections() const noexcept { return m_sections.size(); }
|
||||
bool get_changed() const noexcept { return m_dirty; }
|
||||
void register_option(const char* section, GncOption&& option);
|
||||
void unregister_option(const char* section, const char* name);
|
||||
void set_default_section(const char* section);
|
||||
const GncOptionSection* const get_default_section() const noexcept;
|
||||
std::string lookup_string_option(const char* section,
|
||||
const char* name);
|
||||
std::string lookup_string_option(const char* section, const char* name);
|
||||
template <typename ValueType>
|
||||
bool set_option(const char* section, const char* name, ValueType value)
|
||||
{
|
||||
@ -66,7 +96,7 @@ public:
|
||||
auto option{find_option(section, name)};
|
||||
if (!option)
|
||||
return false;
|
||||
option->get().set_value(value);
|
||||
option->set_value(value);
|
||||
return true;
|
||||
}
|
||||
catch(const std::invalid_argument& err)
|
||||
@ -78,11 +108,16 @@ public:
|
||||
// void set_selectable(const char* section, const char* name);
|
||||
void make_internal(const char* section, const char* name);
|
||||
void commit() {};
|
||||
std::optional<std::reference_wrapper<GncOptionSection>> find_section(const std::string& section);
|
||||
std::optional<std::reference_wrapper<GncOption>> find_option(const std::string& section, const std::string& name) {
|
||||
return static_cast<const GncOptionDB&>(*this).find_option(section, name);
|
||||
GncOptionSection* find_section(const std::string& sectname)
|
||||
{
|
||||
return const_cast<GncOptionSection*>(static_cast<const GncOptionDB&>(*this).find_section(sectname));
|
||||
}
|
||||
std::optional<std::reference_wrapper<GncOption>> find_option(const std::string& section, const std::string& name) const;
|
||||
const GncOptionSection* find_section(const std::string& sectname) const;
|
||||
GncOption* find_option(const std::string& section, const char* name)
|
||||
{
|
||||
return const_cast<GncOption*>(static_cast<const GncOptionDB&>(*this).find_option(section, name));
|
||||
}
|
||||
const GncOption* find_option(const std::string& section, const char* name) const;
|
||||
std::ostream& save_to_scheme(std::ostream& oss,
|
||||
const char* options_prolog) const noexcept;
|
||||
std::istream& load_from_scheme(std::istream& iss) noexcept;
|
||||
@ -100,8 +135,8 @@ public:
|
||||
const std::string& name) const noexcept;
|
||||
std::istream& load_option_key_value(std::istream& iss);
|
||||
private:
|
||||
std::optional<std::reference_wrapper<GncOptionSection>> m_default_section;
|
||||
std::vector<GncOptionSection> m_sections;
|
||||
GncOptionSection* m_default_section;
|
||||
std::vector<GncOptionSectionPtr> m_sections;
|
||||
bool m_dirty = false;
|
||||
|
||||
std::function<GncOptionUIItem*()> m_get_ui_value;
|
||||
|
@ -30,100 +30,107 @@
|
||||
#include "gnc-option-ui.hpp"
|
||||
|
||||
auto constexpr stream_max = std::numeric_limits<std::streamsize>::max();
|
||||
GncOptionDB::GncOptionDB() : m_default_section{std::nullopt} {}
|
||||
|
||||
GncOptionDB::GncOptionDB(QofBook* book) : GncOptionDB() {}
|
||||
static bool
|
||||
operator==(const std::string& str, const char* cstr)
|
||||
{
|
||||
return strcmp(str.c_str(), cstr) == 0;
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionSection::foreach_option(std::function<void(GncOption&)> func)
|
||||
{
|
||||
std::for_each(m_options.begin(), m_options.end(), func);
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionSection::add_option(GncOption&& option)
|
||||
{
|
||||
m_options.emplace_back(std::move(option));
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionSection::remove_option(const char* name)
|
||||
{
|
||||
m_options.erase(std::remove_if(m_options.begin(), m_options.end(),
|
||||
[name](const auto& option) -> bool
|
||||
{
|
||||
return option.get_name() == name;
|
||||
}));
|
||||
}
|
||||
|
||||
const GncOption*
|
||||
GncOptionSection::find_option(const char* name) const
|
||||
{
|
||||
auto option = std::find_if(m_options.begin(), m_options.end(),
|
||||
[name](auto& option) -> bool {
|
||||
return option.get_name() == name;
|
||||
});
|
||||
return (option == m_options.end() ? nullptr : &*option);
|
||||
}
|
||||
|
||||
GncOptionDB::GncOptionDB() : m_default_section{} {}
|
||||
|
||||
GncOptionDB::GncOptionDB(QofBook* book) : GncOptionDB() {}
|
||||
|
||||
void
|
||||
GncOptionDB::save_to_book(QofBook* book, bool do_clear) const
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::register_option(const char* section, GncOption&& option)
|
||||
GncOptionDB::register_option(const char* sectname, GncOption&& option)
|
||||
{
|
||||
auto db_section = find_section(section);
|
||||
auto section = find_section(sectname);
|
||||
|
||||
if (db_section)
|
||||
if (section)
|
||||
{
|
||||
auto& sec_vec = db_section->get().second;
|
||||
sec_vec.emplace_back(std::move(option));
|
||||
section->add_option(std::move(option));
|
||||
return;
|
||||
}
|
||||
|
||||
m_sections.emplace_back(std::make_pair(std::string{section},
|
||||
GncOptionVec{}));
|
||||
auto new_section = std::prev(m_sections.end());
|
||||
new_section->second.emplace_back(std::move(option));
|
||||
m_sections.emplace_back(std::make_shared<GncOptionSection>(sectname));
|
||||
m_sections.back()->add_option(std::move(option));
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::unregister_option(const char* section, const char* name)
|
||||
GncOptionDB::unregister_option(const char* sectname, const char* name)
|
||||
{
|
||||
auto db_section = find_section(section);
|
||||
if (db_section)
|
||||
{
|
||||
auto& sec_vec = db_section->get().second;
|
||||
sec_vec.erase(
|
||||
std::remove_if(
|
||||
sec_vec.begin(), sec_vec.end(),
|
||||
[name](const auto& option) -> bool
|
||||
{
|
||||
return option.get_name() == std::string{name};
|
||||
}));
|
||||
}
|
||||
auto section = find_section(sectname);
|
||||
if (section)
|
||||
section->remove_option(name);
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::set_default_section(const char* section)
|
||||
GncOptionDB::set_default_section(const char* sectname)
|
||||
{
|
||||
m_default_section = find_section(section);
|
||||
m_default_section = find_section(sectname);
|
||||
}
|
||||
|
||||
const GncOptionSection* const
|
||||
GncOptionDB::get_default_section() const noexcept
|
||||
{
|
||||
if (m_default_section)
|
||||
return &(m_default_section.value().get());
|
||||
return nullptr;
|
||||
return m_default_section;
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<GncOptionSection>>
|
||||
GncOptionDB::find_section(const std::string& section)
|
||||
const GncOptionSection*
|
||||
GncOptionDB::find_section(const std::string& section) const
|
||||
{
|
||||
auto db_section = std::find_if(
|
||||
m_sections.begin(), m_sections.end(),
|
||||
[§ion](auto& sect) -> bool
|
||||
{
|
||||
return section.compare(0, classifier_size_max, sect.first) == 0;
|
||||
});
|
||||
if (db_section == m_sections.end())
|
||||
return std::nullopt;
|
||||
return *db_section;
|
||||
auto db_section = std::find_if(m_sections.begin(), m_sections.end(),
|
||||
[§ion](auto& sect) -> bool
|
||||
{
|
||||
return section == sect->get_name();
|
||||
});
|
||||
return db_section == m_sections.end() ? nullptr : db_section->get();
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<GncOption>>
|
||||
GncOptionDB::find_option(const std::string& section, const std::string& name) const
|
||||
const GncOption*
|
||||
GncOptionDB::find_option(const std::string& section, const char* name) const
|
||||
{
|
||||
auto db_section = const_cast<GncOptionDB*>(this)->find_section(section);
|
||||
if (!db_section)
|
||||
return std::nullopt;
|
||||
auto& sec_vec = db_section->get().second;
|
||||
auto db_opt = std::find_if(
|
||||
sec_vec.begin(), sec_vec.end(),
|
||||
[&name](GncOption& option) -> bool
|
||||
{
|
||||
return name.compare(0, classifier_size_max - 1,
|
||||
option.get_name()) == 0;
|
||||
});
|
||||
if (db_opt == sec_vec.end())
|
||||
return std::nullopt;
|
||||
return *db_opt;
|
||||
return nullptr;
|
||||
return db_section->find_option(name);
|
||||
}
|
||||
|
||||
std::string
|
||||
@ -134,7 +141,7 @@ GncOptionDB::lookup_string_option(const char* section, const char* name)
|
||||
auto db_opt = find_option(section, name);
|
||||
if (!db_opt)
|
||||
return empty_string;
|
||||
return db_opt->get().get_value<std::string>();
|
||||
return db_opt->get_value<std::string>();
|
||||
}
|
||||
|
||||
void
|
||||
@ -143,7 +150,7 @@ GncOptionDB::make_internal(const char* section, const char* name)
|
||||
|
||||
auto db_opt = find_option(section, name);
|
||||
if (db_opt)
|
||||
db_opt->get().make_internal();
|
||||
db_opt->make_internal();
|
||||
}
|
||||
|
||||
std::ostream&
|
||||
@ -152,15 +159,15 @@ GncOptionDB::save_option_scheme(std::ostream& oss,
|
||||
const std::string& section,
|
||||
const std::string& name) const noexcept
|
||||
{
|
||||
auto db_opt = find_option(section, name);
|
||||
auto db_opt = find_option(section, name.c_str());
|
||||
|
||||
if (!db_opt || !db_opt->get().is_changed())
|
||||
if (!db_opt || !db_opt->is_changed())
|
||||
return oss;
|
||||
oss << scheme_tags[0] << option_prolog << "\n";
|
||||
oss << scheme_tags[1] << '"' << section.substr(0, classifier_size_max) << "\"\n";
|
||||
oss << scheme_tags[1] << '"' << name.substr(0, classifier_size_max) << '"';
|
||||
oss << scheme_tags[2] << "\n" << scheme_tags[3];
|
||||
db_opt->get().to_scheme(oss);
|
||||
db_opt->to_scheme(oss);
|
||||
oss << scheme_tags[4] << "\n\n";
|
||||
|
||||
return oss;
|
||||
@ -421,7 +428,7 @@ GncOptionDB::load_option_scheme(std::istream& iss)
|
||||
throw std::runtime_error("Malformed option classifier.");
|
||||
const auto& section = unquote_scheme_string(classifier[1].m_name);
|
||||
const auto& name = unquote_scheme_string(classifier[2].m_name);
|
||||
auto option = find_option(section.c_str(), name.c_str());
|
||||
auto option = find_option(section, name.c_str());
|
||||
std::string option_str{section};
|
||||
option_str += ':';
|
||||
option_str += name;
|
||||
@ -439,29 +446,30 @@ GncOptionDB::load_option_scheme(std::istream& iss)
|
||||
throw std::runtime_error(err + " malformed value lambda form.");
|
||||
}
|
||||
std::istringstream value_iss{value_id->get().m_ids[1].m_name};
|
||||
option->get().from_scheme(value_iss);
|
||||
option->from_scheme(value_iss);
|
||||
return iss;
|
||||
}
|
||||
|
||||
std::ostream&
|
||||
GncOptionDB::save_to_scheme(std::ostream& oss, const char* options_prolog) const noexcept
|
||||
{
|
||||
for (auto& section : m_sections)
|
||||
{
|
||||
const auto& [s_name, s_vec] = section;
|
||||
oss << "\n; Section: " << s_name << "\n\n";
|
||||
for (auto& option : s_vec)
|
||||
foreach_section(
|
||||
[&oss, options_prolog](const GncOptionSectionPtr& section)
|
||||
{
|
||||
if (!option.is_changed())
|
||||
continue;
|
||||
oss << scheme_tags[0] << options_prolog << "\n";
|
||||
oss << scheme_tags[1] << '"' << section.first.substr(0, classifier_size_max) << "\"\n";
|
||||
oss << scheme_tags[1] << '"' << option.get_name().substr(0, classifier_size_max) << '"';
|
||||
oss << scheme_tags[2] << "\n" << scheme_tags[3];
|
||||
option.to_scheme(oss);
|
||||
oss << scheme_tags[4] << "\n\n";
|
||||
}
|
||||
}
|
||||
oss << "\n; Section: " << section->get_name() << "\n\n";
|
||||
section->foreach_option(
|
||||
[&oss, options_prolog, §ion](auto& option)
|
||||
{
|
||||
if (!option.is_changed())
|
||||
return;
|
||||
oss << scheme_tags[0] << options_prolog << "\n";
|
||||
oss << scheme_tags[1] << '"' << section->get_name().substr(0, classifier_size_max) << "\"\n";
|
||||
oss << scheme_tags[1] << '"' << option.get_name().substr(0, classifier_size_max) << '"';
|
||||
oss << scheme_tags[2] << "\n" << scheme_tags[3];
|
||||
option.to_scheme(oss);
|
||||
oss << scheme_tags[4] << "\n\n";
|
||||
});
|
||||
});
|
||||
return oss;
|
||||
}
|
||||
|
||||
@ -487,11 +495,11 @@ GncOptionDB::save_option_key_value(std::ostream& oss,
|
||||
const std::string& name) const noexcept
|
||||
{
|
||||
|
||||
auto db_opt = find_option(section, name);
|
||||
if (!db_opt || !db_opt->get().is_changed())
|
||||
auto db_opt = find_option(section, name.c_str());
|
||||
if (!db_opt || !db_opt->is_changed())
|
||||
return oss;
|
||||
oss << section.substr(0, classifier_size_max) << ":" <<
|
||||
name.substr(0, classifier_size_max) << "=" << db_opt->get() << ";";
|
||||
name.substr(0, classifier_size_max) << "=" << *db_opt << ";";
|
||||
return oss;
|
||||
}
|
||||
|
||||
@ -512,7 +520,7 @@ GncOptionDB::load_option_key_value(std::istream& iss)
|
||||
std::string value;
|
||||
std::getline(iss, value, ';');
|
||||
std::istringstream item_iss{value};
|
||||
item_iss >> option->get();
|
||||
item_iss >> *option;
|
||||
}
|
||||
return iss;
|
||||
}
|
||||
@ -521,18 +529,19 @@ std::ostream&
|
||||
GncOptionDB::save_to_key_value(std::ostream& oss) const noexcept
|
||||
{
|
||||
|
||||
for (auto& section : m_sections)
|
||||
{
|
||||
const auto& [s_name, s_vec] = section;
|
||||
oss << "[Options]\n";
|
||||
for (auto& option : s_vec)
|
||||
foreach_section(
|
||||
[&oss](const GncOptionSectionPtr& section)
|
||||
{
|
||||
if (option.is_changed())
|
||||
oss << section.first.substr(0, classifier_size_max) <<
|
||||
':' << option.get_name().substr(0, classifier_size_max) <<
|
||||
'=' << option << '\n';
|
||||
}
|
||||
}
|
||||
oss << "[Options]\n";
|
||||
section->foreach_option(
|
||||
[&oss, §ion](auto& option)
|
||||
{
|
||||
if (option.is_changed())
|
||||
oss << section->get_name().substr(0, classifier_size_max) <<
|
||||
':' << option.get_name().substr(0, classifier_size_max) <<
|
||||
'=' << option << '\n';
|
||||
});
|
||||
});
|
||||
return oss;
|
||||
}
|
||||
|
||||
@ -559,85 +568,89 @@ GncOptionDB::save_to_kvp(QofBook* book, bool clear_options) const noexcept
|
||||
{
|
||||
if (clear_options)
|
||||
qof_book_options_delete(book, nullptr);
|
||||
for (auto& section : m_sections)
|
||||
{
|
||||
const auto& [s_name, s_vec] = section;
|
||||
for (auto& option : s_vec)
|
||||
if (option.is_changed())
|
||||
{
|
||||
// qof_book_set_option wants a GSList path. Let's avoid allocating and make one here.
|
||||
GSList list_tail{(void*)option.get_name().c_str(), nullptr};
|
||||
GSList list_head{(void*)s_name.c_str(), &list_tail};
|
||||
auto type{option.get_ui_type()};
|
||||
if (type == GncOptionUIType::BOOLEAN)
|
||||
{
|
||||
auto val{option.get_value<bool>()};
|
||||
auto kvp{new KvpValue(val ? "t" : "f")};
|
||||
qof_book_set_option(book, kvp, &list_head);
|
||||
}
|
||||
else if (type > GncOptionUIType::DATE_FORMAT)
|
||||
{
|
||||
const QofInstance* inst{QOF_INSTANCE(option.get_value<const QofInstance*>())};
|
||||
auto guid = guid_copy(qof_instance_get_guid(inst));
|
||||
auto kvp{new KvpValue(guid)};
|
||||
qof_book_set_option(book, kvp, &list_head);
|
||||
}
|
||||
else if (type == GncOptionUIType::NUMBER_RANGE)
|
||||
{
|
||||
auto kvp{new KvpValue(option.get_value<int64_t>())};
|
||||
qof_book_set_option(book, kvp, &list_head);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto kvp{new KvpValue{g_strdup(option.get_value<std::string>().c_str())}};
|
||||
qof_book_set_option(book, kvp, &list_head);
|
||||
}
|
||||
}
|
||||
}
|
||||
const_cast<GncOptionDB*>(this)->foreach_section(
|
||||
[book](GncOptionSectionPtr& section)
|
||||
{
|
||||
section->foreach_option(
|
||||
[book, §ion](auto& option) {
|
||||
if (option.is_changed())
|
||||
{
|
||||
// qof_book_set_option wants a GSList path. Let's avoid
|
||||
// allocating and make one here.
|
||||
GSList list_tail{(void*)option.get_name().c_str(), nullptr};
|
||||
GSList list_head{(void*)section->get_name().c_str(), &list_tail};
|
||||
auto type{option.get_ui_type()};
|
||||
if (type == GncOptionUIType::BOOLEAN)
|
||||
{
|
||||
auto val{option.template get_value<bool>()};
|
||||
auto kvp{new KvpValue(val ? "t" : "f")};
|
||||
qof_book_set_option(book, kvp, &list_head);
|
||||
}
|
||||
else if (type > GncOptionUIType::DATE_FORMAT)
|
||||
{
|
||||
const QofInstance* inst{QOF_INSTANCE(option.template get_value<const QofInstance*>())};
|
||||
auto guid = guid_copy(qof_instance_get_guid(inst));
|
||||
auto kvp{new KvpValue(guid)};
|
||||
qof_book_set_option(book, kvp, &list_head);
|
||||
}
|
||||
else if (type == GncOptionUIType::NUMBER_RANGE)
|
||||
{
|
||||
auto kvp{new KvpValue(option.template get_value<int64_t>())};
|
||||
qof_book_set_option(book, kvp, &list_head);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto kvp{new KvpValue{g_strdup(option.template get_value<std::string>().c_str())}};
|
||||
qof_book_set_option(book, kvp, &list_head);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::load_from_kvp(QofBook* book) noexcept
|
||||
{
|
||||
for (auto& section : m_sections)
|
||||
{
|
||||
auto& [s_name, s_vec] = section;
|
||||
for (auto& option : s_vec)
|
||||
foreach_section(
|
||||
[book](GncOptionSectionPtr& section)
|
||||
{
|
||||
/* qof_book_set_option wants a GSList path. Let's avoid allocating
|
||||
* and make one here.
|
||||
*/
|
||||
GSList list_tail{(void*)option.get_name().c_str(), nullptr};
|
||||
GSList list_head{(void*)s_name.c_str(), &list_tail};
|
||||
auto kvp = qof_book_get_option(book, &list_head);
|
||||
if (!kvp)
|
||||
continue;
|
||||
switch (kvp->get_type())
|
||||
{
|
||||
case KvpValue::Type::INT64:
|
||||
option.set_value(kvp->get<int64_t>());
|
||||
break;
|
||||
case KvpValue::Type::STRING:
|
||||
section->foreach_option(
|
||||
[book, §ion](auto& option)
|
||||
{
|
||||
auto str{kvp->get<const char*>()};
|
||||
if (option.get_ui_type() == GncOptionUIType::BOOLEAN)
|
||||
option.set_value(*str == 't' ? true : false);
|
||||
else
|
||||
option.set_value(str);
|
||||
break;
|
||||
}
|
||||
case KvpValue::Type::GUID:
|
||||
{
|
||||
auto guid{kvp->get<GncGUID*>()};
|
||||
option.set_value((const QofInstance*)qof_instance_from_guid(guid, option.get_ui_type()));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* qof_book_set_option wants a GSList path. Let's avoid allocating
|
||||
* and make one here.
|
||||
*/
|
||||
GSList list_tail{(void*)option.get_name().c_str(), nullptr};
|
||||
GSList list_head{(void*)section->get_name().c_str(), &list_tail};
|
||||
auto kvp = qof_book_get_option(book, &list_head);
|
||||
if (!kvp)
|
||||
return;
|
||||
switch (kvp->get_type())
|
||||
{
|
||||
case KvpValue::Type::INT64:
|
||||
option.set_value(kvp->get<int64_t>());
|
||||
break;
|
||||
case KvpValue::Type::STRING:
|
||||
{
|
||||
auto str{kvp->get<const char*>()};
|
||||
if (option.get_ui_type() == GncOptionUIType::BOOLEAN)
|
||||
option.set_value(*str == 't' ? true : false);
|
||||
else
|
||||
option.set_value(str);
|
||||
break;
|
||||
}
|
||||
case KvpValue::Type::GUID:
|
||||
{
|
||||
auto guid{kvp->get<GncGUID*>()};
|
||||
option.set_value((const QofInstance*)qof_instance_from_guid(guid, option.get_ui_type()));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return;
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
GncOptionDBPtr
|
||||
|
@ -342,6 +342,9 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
|
||||
}
|
||||
%}
|
||||
|
||||
%ignore gnc_option_to_scheme;
|
||||
%ignore gnc_option_from_scheme;
|
||||
|
||||
%include "gnc-option-date.hpp"
|
||||
%include "gnc-option.hpp"
|
||||
%include "gnc-option-impl.hpp"
|
||||
@ -387,7 +390,7 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
|
||||
auto db_opt = optiondb->find_option(section, name);
|
||||
if (!db_opt)
|
||||
return SCM_BOOL_F;
|
||||
return GncOption_get_scm_value(&(db_opt->get()));
|
||||
return GncOption_get_scm_value(db_opt);
|
||||
}
|
||||
|
||||
static SCM
|
||||
@ -397,7 +400,7 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
|
||||
auto db_opt = optiondb->find_option(section, name);
|
||||
if (!db_opt)
|
||||
return SCM_BOOL_F;
|
||||
return GncOption_get_scm_default_value(&(db_opt->get()));
|
||||
return GncOption_get_scm_default_value(db_opt);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -410,7 +413,7 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
|
||||
// PWARN("Attempt to write non-existent option %s/%s", section, name);
|
||||
return;
|
||||
}
|
||||
GncOption_set_value_from_scm(&(db_opt->get()), new_value);
|
||||
GncOption_set_value_from_scm(db_opt, new_value);
|
||||
}
|
||||
%}
|
||||
|
||||
|
@ -131,8 +131,8 @@ TEST_F(GncOptionDBTest, test_register_account_list_option)
|
||||
auto acclist{gnc_account_list_from_types(book.m_book, {ACCT_TYPE_STOCK})};
|
||||
gnc_register_account_list_option(m_db, "foo", "bar", "baz", "Phony Option",
|
||||
acclist);
|
||||
EXPECT_EQ(4U, m_db->find_option("foo", "bar")->get().get_value<GncOptionAccountList>().size());
|
||||
EXPECT_EQ(acclist[3], m_db->find_option("foo", "bar")->get().get_value<GncOptionAccountList>().at(3));
|
||||
EXPECT_EQ(4U, m_db->find_option("foo", "bar")->get_value<GncOptionAccountList>().size());
|
||||
EXPECT_EQ(acclist[3], m_db->find_option("foo", "bar")->get_value<GncOptionAccountList>().at(3));
|
||||
}
|
||||
|
||||
TEST_F(GncOptionDBTest, test_register_account_list_limited_option)
|
||||
@ -142,8 +142,8 @@ TEST_F(GncOptionDBTest, test_register_account_list_limited_option)
|
||||
gnc_register_account_list_limited_option(m_db, "foo", "bar", "baz",
|
||||
"Phony Option", acclist,
|
||||
{ACCT_TYPE_STOCK});
|
||||
EXPECT_EQ(4, m_db->find_option("foo", "bar")->get().get_value<GncOptionAccountList>().size());
|
||||
EXPECT_EQ(acclist[3], m_db->find_option("foo", "bar")->get().get_value<GncOptionAccountList>().at(3));
|
||||
EXPECT_EQ(4, m_db->find_option("foo", "bar")->get_value<GncOptionAccountList>().size());
|
||||
EXPECT_EQ(acclist[3], m_db->find_option("foo", "bar")->get_value<GncOptionAccountList>().at(3));
|
||||
}
|
||||
|
||||
TEST_F(GncOptionDBTest, test_register_account_sel_limited_option)
|
||||
@ -154,8 +154,8 @@ TEST_F(GncOptionDBTest, test_register_account_sel_limited_option)
|
||||
gnc_register_account_list_limited_option(m_db, "foo", "bar", "baz",
|
||||
"Phony Option", accsel,
|
||||
{ACCT_TYPE_STOCK});
|
||||
EXPECT_EQ(1, m_db->find_option("foo", "bar")->get().get_value<GncOptionAccountList>().size());
|
||||
EXPECT_EQ(accsel[0], m_db->find_option("foo", "bar")->get().get_value<GncOptionAccountList>().at(0));
|
||||
EXPECT_EQ(1, m_db->find_option("foo", "bar")->get_value<GncOptionAccountList>().size());
|
||||
EXPECT_EQ(accsel[0], m_db->find_option("foo", "bar")->get_value<GncOptionAccountList>().at(0));
|
||||
}
|
||||
|
||||
TEST_F(GncOptionDBTest, test_register_account_sel_limited_option_fail_construct)
|
||||
@ -204,7 +204,7 @@ TEST_F(GncOptionDBTest, test_register_relative_date_option)
|
||||
gnc_gdate_set_prev_year_start(&prev_year_start);
|
||||
time64 time1{time64_from_gdate(&prev_year_start, DayPart::start)};
|
||||
ASSERT_TRUE(m_db->set_option("foo", "bar", time1));
|
||||
EXPECT_EQ(time1, m_db->find_option("foo", "bar")->get().get_value<time64>());
|
||||
EXPECT_EQ(time1, m_db->find_option("foo", "bar")->get_value<time64>());
|
||||
}
|
||||
|
||||
TEST_F(GncOptionDBTest, test_register_absolute_date_option)
|
||||
@ -217,7 +217,7 @@ TEST_F(GncOptionDBTest, test_register_absolute_date_option)
|
||||
gnc_gdate_set_prev_year_start(&prev_year_start);
|
||||
ASSERT_TRUE(m_db->set_option("foo", "bar", time1));
|
||||
EXPECT_EQ(time1,
|
||||
m_db->find_option("foo", "bar")->get().get_value<time64>());
|
||||
m_db->find_option("foo", "bar")->get_value<time64>());
|
||||
}
|
||||
|
||||
/* Copied from gnc-optiondb.cpp for the purpose of finding the index of the
|
||||
@ -243,15 +243,15 @@ TEST_F(GncOptionDBTest, test_register_start_date_option)
|
||||
gnc_gdate_set_prev_year_start(&prev_year_start);
|
||||
time64 time1{time64_from_gdate(&prev_year_start, DayPart::start)};
|
||||
EXPECT_EQ(RelativeDatePeriod::START_ACCOUNTING_PERIOD,
|
||||
m_db->find_option("foo", "bar")->get().get_value<RelativeDatePeriod>());
|
||||
m_db->find_option("foo", "bar")->get_value<RelativeDatePeriod>());
|
||||
ASSERT_TRUE(m_db->set_option("foo", "bar", time1));
|
||||
EXPECT_EQ(time1,
|
||||
m_db->find_option("foo", "bar")->get().get_value<time64>());
|
||||
m_db->find_option("foo", "bar")->get_value<time64>());
|
||||
EXPECT_EQ(RelativeDatePeriod::ABSOLUTE,
|
||||
m_db->find_option("foo", "bar")->get().get_value<RelativeDatePeriod>());
|
||||
m_db->find_option("foo", "bar")->get_value<RelativeDatePeriod>());
|
||||
m_db->set_option("foo", "bar", RelativeDatePeriod::START_THIS_MONTH);
|
||||
EXPECT_EQ(RelativeDatePeriod::START_THIS_MONTH,
|
||||
m_db->find_option("foo", "bar")->get().get_value<RelativeDatePeriod>());
|
||||
m_db->find_option("foo", "bar")->get_value<RelativeDatePeriod>());
|
||||
|
||||
auto index(std::find(begin_dates.begin(), begin_dates.end(),
|
||||
RelativeDatePeriod::START_THIS_MONTH) - begin_dates.begin());
|
||||
@ -259,12 +259,12 @@ TEST_F(GncOptionDBTest, test_register_start_date_option)
|
||||
* gnc-optiondb.cpp.
|
||||
*/
|
||||
EXPECT_EQ(index,
|
||||
m_db->find_option("foo", "bar")->get().get_value<size_t>());
|
||||
m_db->find_option("foo", "bar")->get_value<size_t>());
|
||||
m_db->set_option("foo", "bar", RelativeDatePeriod::END_THIS_MONTH);
|
||||
EXPECT_EQ(RelativeDatePeriod::START_THIS_MONTH,
|
||||
m_db->find_option("foo", "bar")->get().get_value<RelativeDatePeriod>());
|
||||
m_db->find_option("foo", "bar")->get_value<RelativeDatePeriod>());
|
||||
m_db->set_option("foo", "bar", static_cast<size_t>(5));
|
||||
EXPECT_EQ(5, m_db->find_option("foo", "bar")->get().get_value<size_t>());
|
||||
EXPECT_EQ(5, m_db->find_option("foo", "bar")->get_value<size_t>());
|
||||
|
||||
}
|
||||
|
||||
@ -335,7 +335,7 @@ TEST_F(GncOptionDBIOTest, test_option_scheme_output)
|
||||
oss.clear();
|
||||
m_db->set_option("foo", "sausage", std::string{"pepper"});
|
||||
EXPECT_STREQ("pepper", m_db->lookup_string_option("foo", "sausage").c_str());
|
||||
EXPECT_TRUE(m_db->find_option("foo", "sausage")->get().is_changed());
|
||||
EXPECT_TRUE(m_db->find_option("foo", "sausage")->is_changed());
|
||||
oss.flush();
|
||||
m_db->save_option_scheme(oss, "option", "foo", "sausage");
|
||||
EXPECT_STREQ("(let ((option (gnc:lookup-option option\n"
|
||||
@ -373,7 +373,7 @@ TEST_F(GncOptionDBIOTest, test_date_interval_option_scheme_input)
|
||||
gnc_gdate_set_month_end(&month_end);
|
||||
auto time1 = time64_from_gdate(&month_end, DayPart::end);
|
||||
m_db->load_option_scheme(iss);
|
||||
EXPECT_EQ(time1, m_db->find_option("pork", "garply")->get().get_value<time64>());
|
||||
EXPECT_EQ(time1, m_db->find_option("pork", "garply")->get_value<time64>());
|
||||
|
||||
}
|
||||
|
||||
@ -389,10 +389,10 @@ TEST_F(GncOptionDBIOTest, test_account_list_option_scheme_input)
|
||||
input += hpe_guid + "\" \"";
|
||||
input += msft_guid + "\")))) option))\n\n";
|
||||
std::istringstream iss{input};
|
||||
EXPECT_EQ(acclist[1], m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>()[0]);
|
||||
EXPECT_EQ(acclist[1], m_db->find_option("quux", "xyzzy")->get_value<GncOptionAccountList>()[0]);
|
||||
m_db->load_option_scheme(iss);
|
||||
EXPECT_EQ(acclist[2], m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>()[1]);
|
||||
EXPECT_EQ(2, m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>().size());
|
||||
EXPECT_EQ(acclist[2], m_db->find_option("quux", "xyzzy")->get_value<GncOptionAccountList>()[1]);
|
||||
EXPECT_EQ(2, m_db->find_option("quux", "xyzzy")->get_value<GncOptionAccountList>().size());
|
||||
|
||||
}
|
||||
|
||||
@ -427,12 +427,12 @@ TEST_F(GncOptionDBIOTest, test_multiple_options_scheme_input)
|
||||
g_date_subtract_months(&month_end, 1);
|
||||
gnc_gdate_set_month_end(&month_end);
|
||||
auto time1 = time64_from_gdate(&month_end, DayPart::end);
|
||||
EXPECT_EQ(acclist[1], m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>()[0]);
|
||||
EXPECT_EQ(acclist[1], m_db->find_option("quux", "xyzzy")->get_value<GncOptionAccountList>()[0]);
|
||||
m_db->load_from_scheme(iss);
|
||||
EXPECT_STREQ("pepper", m_db->lookup_string_option("foo", "sausage").c_str());
|
||||
EXPECT_EQ(time1, m_db->find_option("pork", "garply")->get().get_value<time64>());
|
||||
EXPECT_EQ(acclist[2], m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>()[1]);
|
||||
EXPECT_EQ(2, m_db->find_option("quux", "xyzzy")->get().get_value<GncOptionAccountList>().size());
|
||||
EXPECT_EQ(time1, m_db->find_option("pork", "garply")->get_value<time64>());
|
||||
EXPECT_EQ(acclist[2], m_db->find_option("quux", "xyzzy")->get_value<GncOptionAccountList>()[1]);
|
||||
EXPECT_EQ(2, m_db->find_option("quux", "xyzzy")->get_value<GncOptionAccountList>().size());
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user