Extract all SCM functions to gnc-optiondb.i.

So that it can be moved to bindings/ when gnc-module-load-begone is ready.
This commit is contained in:
John Ralls 2019-10-15 16:33:55 -07:00
parent d544f85256
commit 8eedcb6d6d
3 changed files with 122 additions and 110 deletions

View File

@ -178,72 +178,6 @@ private:
GncOptionUIType m_ui_type;
};
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<GncGUID> 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 <typename ValueType>
class GncOptionValue :
public OptionClassifier, public OptionUIItem
@ -388,6 +322,7 @@ public:
return ValueType {};
}, m_option);
}
template <typename ValueType> ValueType get_default_value() const
{
return std::visit([](const auto& option)->ValueType {
@ -398,20 +333,7 @@ public:
}, m_option);
}
SCM get_scm_value() const
{
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 std::visit([](const auto& option)->SCM {
auto value{option.get_default_value()};
return scm_from_value(value);
}, m_option);
}
template <typename ValueType> void set_value(ValueType value)
{
std::visit([value](auto& option) {
@ -467,6 +389,7 @@ public:
option.make_internal();
}, m_option);
}
const GncOptionVariant& _get_option() const { return m_option; }
private:
GncOptionVariant m_option;
};

View File

@ -46,22 +46,139 @@ extern "C" SCM scm_init_sw_gnc_optiondb_module(void);
%include <std_string.i>
%inline %{
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<GncGUID> 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(std::vector<std::pair<std::string, std::string>>)
{
return SCM_BOOL_F;
}
%}
%ignore OptionClassifier;
%ignore OptionUIItem;
%ignore GncOption;
%nodefaultctor GncOption;
wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
%include "gnc-option.hpp"
%include "gnc-optiondb.hpp"
%extend GncOption {
SCM get_scm_value() const
{
return std::visit([](const auto& option)->SCM {
auto value{option.get_value()};
return scm_from_value(value);
}, $self->_get_option());
}
SCM get_scm_default_value() const
{
return std::visit([](const auto& option)->SCM {
auto value{option.get_default_value()};
return scm_from_value(value);
}, $self->_get_option());
}
};
%extend GncOptionDB {
SCM lookup_option(const char* section, const char* name)
{
auto db_opt = $self->find_option(section, name);
if (!db_opt)
return SCM_BOOL_F;
return db_opt->get().get_scm_value();
return GncOption_get_scm_value(&(db_opt->get()));
}
%template(set_option_string) set_option<std::string>;
%template(set_option_int) set_option<int>;
};
/*
TEST(GncOption, test_string_scm_functions)
{
GncOption option("foo", "bar", "baz", "Phony Option", std::string{"waldo"});
auto scm_value = option.get_scm_value();
auto str_value = scm_to_utf8_string(scm_value);
EXPECT_STREQ("waldo", str_value);
g_free(str_value);
scm_value = option.get_scm_default_value();
str_value = scm_to_utf8_string(scm_value);
EXPECT_STREQ("waldo", str_value);
g_free(str_value);
}
TEST(GNCOption, test_budget_scm_functions)
{
auto book = qof_book_new();
auto budget = gnc_budget_new(book);
GncOption option("foo", "bar", "baz", "Phony Option",
QOF_INSTANCE(budget));
auto scm_budget = option.get_scm_value();
auto str_value = scm_to_utf8_string(scm_budget);
auto guid = guid_to_string(qof_instance_get_guid(budget));
EXPECT_STREQ(guid, str_value);
g_free(guid);
gnc_budget_destroy(budget);
qof_book_destroy(book);
}
*/

View File

@ -67,19 +67,6 @@ TEST(GncOption, test_int64_t_value)
EXPECT_EQ(INT64_C(987654321), option.get_value<int64_t>());
}
TEST(GncOption, test_string_scm_functions)
{
GncOption option("foo", "bar", "baz", "Phony Option", std::string{"waldo"});
auto scm_value = option.get_scm_value();
auto str_value = scm_to_utf8_string(scm_value);
EXPECT_STREQ("waldo", str_value);
g_free(str_value);
scm_value = option.get_scm_default_value();
str_value = scm_to_utf8_string(scm_value);
EXPECT_STREQ("waldo", str_value);
g_free(str_value);
}
TEST(GNCOption, test_budget_ctor)
{
auto book = qof_book_new();
@ -92,21 +79,6 @@ TEST(GNCOption, test_budget_ctor)
qof_book_destroy(book);
}
TEST(GNCOption, test_budget_scm_functions)
{
auto book = qof_book_new();
auto budget = gnc_budget_new(book);
GncOption option("foo", "bar", "baz", "Phony Option",
QOF_INSTANCE(budget));
auto scm_budget = option.get_scm_value();
auto str_value = scm_to_utf8_string(scm_budget);
auto guid = guid_to_string(qof_instance_get_guid(budget));
EXPECT_STREQ(guid, str_value);
g_free(guid);
gnc_budget_destroy(budget);
qof_book_destroy(book);
}
TEST(GNCOption, test_commodity_ctor)
{
auto book = qof_book_new();