diff --git a/libgnucash/app-utils/gnc-option.cpp b/libgnucash/app-utils/gnc-option.cpp index 36a5981071..8543efa20c 100644 --- a/libgnucash/app-utils/gnc-option.cpp +++ b/libgnucash/app-utils/gnc-option.cpp @@ -25,68 +25,3 @@ #include "gnc-option.hpp" #include -template<> SCM -scm_from_value(std::string value) -{ - return scm_from_utf8_string(value.c_str()); -} - -template<> SCM -scm_from_value(bool value) -{ - return value ? SCM_BOOL_T : SCM_BOOL_F; -} - -template<> SCM -scm_from_value(int64_t value) -{ - return scm_from_int64(value); -} - -template<> SCM -scm_from_value(int value) -{ - return scm_from_int(value); -} - -template<> SCM -scm_from_value(QofInstance* value) -{ - auto guid = guid_to_string(qof_instance_get_guid(value)); - auto scm_guid = scm_from_utf8_string(guid); - g_free(guid); - return scm_guid; -} - -template<> SCM -scm_from_value(QofQuery* value) -{ - return SCM_BOOL_F; -} - -template<> SCM -scm_from_value(GncNumeric value) -{ - return SCM_BOOL_F; -} - -template<> SCM -scm_from_value>(std::vector value) -{ - SCM s_list; - for (auto guid : value) - { - auto guid_s = guid_to_string(qof_instance_get_guid(&guid)); - auto scm_guid = scm_from_utf8_string(guid_s); - auto scm_guid_list1 = scm_list_1(scm_guid); - s_list = scm_append(scm_list_2(s_list, scm_guid_list1)); - g_free(guid_s); - } - return s_list; -} - -template<> SCM -scm_from_value(GncMultiChoiceOptionChoices value) -{ - return SCM_BOOL_F; -} diff --git a/libgnucash/app-utils/gnc-option.hpp b/libgnucash/app-utils/gnc-option.hpp index 9c5daa5440..045bd55413 100644 --- a/libgnucash/app-utils/gnc-option.hpp +++ b/libgnucash/app-utils/gnc-option.hpp @@ -32,11 +32,14 @@ extern "C" #include } #include +#include #include #include +#include +#include #include #include -#include +#include /* * Unused base class to document the structure of the current Scheme option @@ -141,8 +144,8 @@ class GncOptionUIItem; class OptionUIItem { public: - GncOptionUIType get_ui_type() { return m_ui_type; } - GncOptionUIItem* const get_ui_item() {return m_ui_item; } + GncOptionUIType get_ui_type() const { return m_ui_type; } + GncOptionUIItem* const get_ui_item() const {return m_ui_item; } void clear_ui_item() { m_ui_item = nullptr; } void set_ui_item(GncOptionUIItem* ui_item) { @@ -175,8 +178,71 @@ private: GncOptionUIType m_ui_type; }; -template -SCM scm_from_value(ValueType); +inline SCM +scm_from_value(std::string value) +{ + return scm_from_utf8_string(value.c_str()); +} + +inline SCM +scm_from_value(bool value) +{ + return value ? SCM_BOOL_T : SCM_BOOL_F; +} + +inline SCM +scm_from_value(int64_t value) +{ + return scm_from_int64(value); +} + +inline SCM +scm_from_value(int value) +{ + return scm_from_int(value); +} + +inline SCM +scm_from_value(QofInstance* value) +{ + auto guid = guid_to_string(qof_instance_get_guid(value)); + auto scm_guid = scm_from_utf8_string(guid); + g_free(guid); + return scm_guid; +} + +inline SCM +scm_from_value(QofQuery* value) +{ + return SCM_BOOL_F; +} + +inline SCM +scm_from_value(GncNumeric value) +{ + return SCM_BOOL_F; +} + +inline SCM +scm_from_value(std::vector value) +{ + SCM s_list; + for (auto guid : value) + { + auto guid_s = guid_to_string(qof_instance_get_guid(&guid)); + auto scm_guid = scm_from_utf8_string(guid_s); + auto scm_guid_list1 = scm_list_1(scm_guid); + s_list = scm_append(scm_list_2(s_list, scm_guid_list1)); + g_free(guid_s); + } + return s_list; +} + +inline SCM +scm_from_value(GncMultiChoiceOptionChoices value) +{ + return SCM_BOOL_F; +} template class GncOptionValue : @@ -190,6 +256,10 @@ public: OptionClassifier{section, name, key, doc_string}, OptionUIItem(ui_type), m_value{value}, m_default_value{value} {} + GncOptionValue(const GncOptionValue&) = default; + GncOptionValue(GncOptionValue&&) = default; + GncOptionValue& operator=(const GncOptionValue&) = default; + GncOptionValue& operator=(GncOptionValue&&) = default; ValueType get_value() const { return m_value; } ValueType get_default_value() const { return m_default_value; } void set_value(ValueType new_value) { m_value = new_value; } @@ -227,6 +297,10 @@ public: if (!this->validate(value)) throw std::invalid_argument("Attempt to create GncValidatedOption with bad value."); } + GncOptionValidatedValue(const GncOptionValidatedValue&) = default; + GncOptionValidatedValue(GncOptionValidatedValue&&) = default; + GncOptionValidatedValue& operator=(const GncOptionValidatedValue&) = default; + GncOptionValidatedValue& operator=(GncOptionValidatedValue&&) = default; ValueType get_value() const { return m_value; } ValueType get_default_value() const { return m_default_value; } bool validate(ValueType value) { return m_validator(value); } @@ -259,6 +333,10 @@ public: m_default_value{value >= min && value <= max ? value : min}, m_min{min}, m_step{step} {} + GncOptionRangeValue(const GncOptionRangeValue&) = default; + GncOptionRangeValue(GncOptionRangeValue&&) = default; + GncOptionRangeValue& operator=(const GncOptionRangeValue&) = default; + GncOptionRangeValue& operator=(GncOptionRangeValue&&) = default; ValueType get_value() const { return m_value; } ValueType get_default_value() const { return m_default_value; } bool validate(ValueType value) { return value >= m_min && value <= m_max; } @@ -277,7 +355,7 @@ private: ValueType m_step; }; -using GncOptionVariant = boost::variant, +using GncOptionVariant = std::variant, GncOptionValue, GncOptionValue, GncOptionValue, @@ -303,185 +381,93 @@ public: template ValueType get_value() const { - return boost::apply_visitor(GetValueVisitor(), m_option); + return std::visit([](const auto& option)->ValueType { + if constexpr (std::is_same_v) + return option.get_value(); + else + return ValueType {}; + }, m_option); } template ValueType get_default_value() const { - return boost::apply_visitor(GetDefaultValueVisitor(), m_option); + return std::visit([](const auto& option)->ValueType { + if constexpr (std::is_same_v) + return option.get_default_value(); + else + return ValueType {}; + }, m_option); + } SCM get_scm_value() const { - return boost::apply_visitor(GetSCMVisitor(), m_option); + return std::visit([](const auto& option)->SCM { + auto value{option.get_value()}; + return scm_from_value(value); + }, m_option); } SCM get_scm_default_value() const { - return boost::apply_visitor(GetSCMDefaultVisitor(), m_option); + return std::visit([](const auto& option)->SCM { + auto value{option.get_default_value()}; + return scm_from_value(value); + }, m_option); } template void set_value(ValueType value) { - boost::apply_visitor(SetValueVisitor(value), m_option); + std::visit([value](auto& option) { + if constexpr (std::is_same_v) + option.set_value(value); + }, m_option); } const std::string& get_section() const { - return boost::apply_visitor(GetSectionVisitor(), m_option); + return std::visit([](const auto& option)->const std::string& { + return option.m_section; + }, m_option); } const std::string& get_name() const { - return boost::apply_visitor(GetNameVisitor(), m_option); + return std::visit([](const auto& option)->const std::string& { + return option.m_name; + }, m_option); } const std::string& get_key() const { - return boost::apply_visitor(GetKeyVisitor(), m_option); + return std::visit([](const auto& option)->const std::string& { + return option.m_sort_tag; + }, m_option); } const std::string& get_docstring() const { - return boost::apply_visitor(GetDocstringVisitor(), m_option); + return std::visit([](const auto& option)->const std::string& { + return option.m_doc_string; + }, m_option); } void set_ui_item(GncOptionUIItem* ui_elem) { - return boost::apply_visitor(SetUIItemVisitor(ui_elem), m_option); + std::visit([ui_elem](auto& option) { + option.set_ui_item(ui_elem); + }, m_option); } - const GncOptionUIType get_ui_type() + const GncOptionUIType get_ui_type() const { - return boost::apply_visitor(GetUITypeVisitor(), m_option); + return std::visit([](const auto& option)->GncOptionUIType { + return option.get_ui_type(); + }, m_option); } - GncOptionUIItem* const get_ui_item() + GncOptionUIItem* const get_ui_item() const { - return boost::apply_visitor(GetUIItemVisitor(), m_option); + return std::visit([](const auto& option)->GncOptionUIItem* { + return option.get_ui_item(); + }, m_option); } void make_internal() { - return boost::apply_visitor(MakeInternalVisitor(), m_option); + std::visit([](auto& option) { + option.make_internal(); + }, m_option); } private: - template - struct GetValueVisitor : public boost::static_visitor - { - ValueType operator()(const GncOptionValue& option) const { - return option.get_value(); - } - ValueType operator()(const GncOptionValidatedValue& option) const { - return option.get_value(); - } - template - ValueType operator()(OptionType& option) const { - return ValueType{}; - } - }; - template - struct GetDefaultValueVisitor : public boost::static_visitor - { - ValueType operator()(const GncOptionValue& option) const { - return option.get_default_value(); - } - ValueType operator()(const GncOptionValidatedValue& option) const { - return option.get_default_value(); - } - template - ValueType operator()(OptionType& option) const { - return ValueType(); - } - }; - template - struct SetValueVisitor : public boost::static_visitor<> - { - SetValueVisitor(ValueType value) : m_value{value} {} - void operator()(GncOptionValue& option) const { - option.set_value(m_value); - } - void operator()(GncOptionValidatedValue& option) const { - option.set_value(m_value); - } - template - void operator()(OptionType& option) const { - std::string msg{"Attempt to set option of type "}; - msg += typeid(OptionType).name(); - msg += " with value of type "; - msg += typeid(m_value).name(); - throw std::invalid_argument(msg); - } - private: - ValueType m_value; - }; - struct GetSCMVisitor : public boost::static_visitor - { - template - SCM operator()(OptionType& option) const { - auto value{option.get_value()}; - return scm_from_value(value); - } - }; - struct GetSCMDefaultVisitor : public boost::static_visitor - { - template - SCM operator()(OptionType& option) const { - auto value{option.get_value()}; - return scm_from_value(value); - } - }; - struct GetSectionVisitor : public boost::static_visitor - { - template - const std::string& operator()(OptionType& option) const { - return option.m_section; - } - }; - struct GetNameVisitor : public boost::static_visitor - { - template - const std::string& operator()(OptionType& option) const { - return option.m_name; - } - }; - struct GetKeyVisitor : public boost::static_visitor - { - template - const std::string& operator()(OptionType& option) const { - return option.m_sort_tag; - } - }; - struct GetDocstringVisitor : - public boost::static_visitor - { - template - const std::string& operator()(OptionType& option) const { - return option.m_doc_string; - } - }; - struct SetUIItemVisitor : public boost::static_visitor<> - { - SetUIItemVisitor(GncOptionUIItem* ui_item) : m_ui_item{ui_item} {} - template - void operator()(OptionType& option) const { - option.set_ui_item(m_ui_item); - } - private: - GncOptionUIItem* m_ui_item; - }; - struct GetUITypeVisitor : - public boost::static_visitor - { - template - const GncOptionUIType operator()(OptionType& option) const { - return option.get_ui_type(); - } - }; - struct GetUIItemVisitor : - public boost::static_visitor - { - template - GncOptionUIItem* const operator()(OptionType& option) const { - return option.get_ui_item(); - } - }; - struct MakeInternalVisitor : public boost::static_visitor<> - { - template - void operator()(OptionType& option) const { - return option.make_internal(); - } - }; - GncOptionVariant m_option; }; diff --git a/libgnucash/app-utils/gnc-optiondb.cpp b/libgnucash/app-utils/gnc-optiondb.cpp index 169228c060..9276513ac1 100644 --- a/libgnucash/app-utils/gnc-optiondb.cpp +++ b/libgnucash/app-utils/gnc-optiondb.cpp @@ -23,7 +23,7 @@ #include "gnc-optiondb.hpp" -GncOptionDB::GncOptionDB() : m_default_section{boost::none} {} +GncOptionDB::GncOptionDB() : m_default_section{std::nullopt} {} GncOptionDB::GncOptionDB(QofBook* book) : GncOptionDB() {} @@ -39,7 +39,7 @@ GncOptionDB::register_option(const char* section, GncOption&& option) if (db_section) { - db_section->second.emplace_back(std::move(option)); + db_section->get().second.emplace_back(std::move(option)); return; } @@ -55,9 +55,9 @@ GncOptionDB::unregister_option(const char* section, const char* name) auto db_section = find_section(section); if (db_section) { - db_section->second.erase( + db_section->get().second.erase( std::remove_if( - db_section->second.begin(), db_section->second.end(), + db_section->get().second.begin(), db_section->get().second.end(), [name](const GncOption& option) -> bool { return option.get_name() == std::string{name}; @@ -75,7 +75,7 @@ const GncOptionSection* const GncOptionDB::get_default_section() const noexcept { if (m_default_section) - return &(m_default_section.get()); + return &(m_default_section.value().get()); return nullptr; } @@ -85,7 +85,7 @@ GncOptionDB::set_ui_item(const char* section, const char* name, { auto option = find_option(section, name); if (!option) return; - option->set_ui_item(ui_item); + option->get().set_ui_item(ui_item); } GncOptionUIItem* const @@ -93,7 +93,7 @@ GncOptionDB::get_ui_item(const char* section, const char* name) { auto option = find_option(section, name); if (!option) return nullptr; - return option->get_ui_item(); + return option->get().get_ui_item(); } GncOptionUIType @@ -101,7 +101,7 @@ GncOptionDB::get_ui_type(const char* section, const char* name) { auto option = find_option(section, name); if (!option) return GncOptionUIType::INTERNAL; - return option->get_ui_type(); + return option->get().get_ui_type(); } void @@ -110,7 +110,7 @@ GncOptionDB::set_ui_from_option(const char* section, const char* name, { auto option = find_option(section, name); if (!option) return; - func(option.get()); + func(option->get()); } void @@ -119,11 +119,11 @@ GncOptionDB::set_option_from_ui(const char* section, const char* name, { auto option = find_option(section, name); if (!option) return; - func(option.get()); + func(option->get()); } -boost::optional +std::optional> GncOptionDB::find_section(const char* section) { auto db_section = std::find_if( @@ -133,24 +133,24 @@ GncOptionDB::find_section(const char* section) return sect.first == std::string{section}; }); if (db_section == m_sections.end()) - return boost::none; + return std::nullopt; return *db_section; } -boost::optional +std::optional> GncOptionDB::find_option(const char* section, const char* name) { auto db_section = find_section(section); if (!db_section) - return boost::none; + return std::nullopt; auto db_opt = std::find_if( - db_section->second.begin(), db_section->second.end(), + db_section->get().second.begin(), db_section->get().second.end(), [name](GncOption& option) -> bool { return option.get_name() == std::string{name}; }); - if (db_opt == db_section->second.end()) - return boost::none; + if (db_opt == db_section->get().second.end()) + return std::nullopt; return *db_opt; } @@ -162,7 +162,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_value(); + return db_opt->get().get_value(); } void @@ -170,7 +170,7 @@ GncOptionDB::make_internal(const char* section, const char* name) { auto db_opt = find_option(section, name); if (db_opt) - db_opt->make_internal(); + db_opt->get().make_internal(); } void diff --git a/libgnucash/app-utils/gnc-optiondb.hpp b/libgnucash/app-utils/gnc-optiondb.hpp index 1b2de18f39..2a9c597a2d 100644 --- a/libgnucash/app-utils/gnc-optiondb.hpp +++ b/libgnucash/app-utils/gnc-optiondb.hpp @@ -27,7 +27,7 @@ #include "gnc-option.hpp" #include #include -#include +#include extern "C" { #include @@ -70,7 +70,7 @@ public: auto option{find_option(section, name)}; if (!option) return false; - option->set_value(value); + option->get().set_value(value); return true; } catch(const std::invalid_argument& err) @@ -82,10 +82,10 @@ public: // void set_selectable(const char* section, const char* name); void make_internal(const char* section, const char* name); void commit(); - boost::optional find_section(const char* section); - boost::optional find_option(const char* section, const char* name); + std::optional> find_section(const char* section); + std::optional> find_option(const char* section, const char* name); private: - boost::optional m_default_section; + std::optional> m_default_section; std::vector m_sections; bool m_dirty = false; diff --git a/libgnucash/app-utils/gnc-optiondb.i b/libgnucash/app-utils/gnc-optiondb.i index ad81c0450d..7d8af2f715 100644 --- a/libgnucash/app-utils/gnc-optiondb.i +++ b/libgnucash/app-utils/gnc-optiondb.i @@ -59,7 +59,7 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB); auto db_opt = $self->find_option(section, name); if (!db_opt) return SCM_BOOL_F; - return db_opt->get_scm_value(); + return db_opt->get().get_scm_value(); } %template(set_option_string) set_option;