Skip to content

Commit

Permalink
Integrate gnc-option into app-utils.
Browse files Browse the repository at this point in the history
Includes converting swig-app-utils-guile to c++.
  • Loading branch information
jralls committed Jul 11, 2021
1 parent 81c5ac6 commit cbf7d70
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 91 deletions.
20 changes: 15 additions & 5 deletions libgnucash/app-utils/CMakeLists.txt
Expand Up @@ -11,6 +11,8 @@ set (app_utils_noinst_HEADERS
calculation/finproto.h
calculation/fin_spl_protos.h
calculation/fin_static_proto.h
gnc-option-impl.hpp
gnc-optiondb-impl.hpp
)

set (app_utils_HEADERS
Expand All @@ -27,19 +29,24 @@ set (app_utils_HEADERS
gnc-gsettings.h
gnc-help-utils.h
gnc-helpers.h
gnc-option.hpp
gnc-optiondb.hpp
gnc-prefs-utils.h
gnc-state.h
gnc-state.h
gnc-sx-instance-model.h
gnc-ui-util.h
gnc-ui-balances.h
option-util.h
)

# Command to generate the swig-app-utils-guile.c wrapper file
gnc_add_swig_guile_command (swig-apputils-guile-c
SWIG_APP_UTILS_GUILE_C swig-app-utils-guile.c
${CMAKE_CURRENT_SOURCE_DIR}/app-utils.i ""
set(SWIG_ARGS "-c++" "-procdoc" "sw-gnc-option-doc" "-procdocformat" "plain")
gnc_add_swig_guile_command (swig-apputils-guile-cpp
SWIG_APP_UTILS_GUILE_CPP swig-app-utils-guile.cpp
${CMAKE_CURRENT_SOURCE_DIR}/app-utils.i
${CMAKE_CURRENT_SOURCE_DIR}/gnc-optiondb.i ""
)
unset(SWIG_ARGS)

# Command to generate the swig-app-utils-python.c wrapper file
gnc_add_swig_python_command (swig-app-utils-python
Expand All @@ -62,6 +69,9 @@ set (app_utils_SOURCES
gnc-exp-parser.c
gnc-gsettings.c
gnc-helpers.c
gnc-option.cpp
gnc-option-impl.cpp
gnc-optiondb.cpp
gnc-prefs-utils.c
gnc-sx-instance-model.c
gnc-state.c
Expand Down Expand Up @@ -103,7 +113,7 @@ if (WIN32)
endif()


add_library (gnc-app-utils ${app_utils_ALL_SOURCES} ${SWIG_APP_UTILS_GUILE_C})
add_library (gnc-app-utils ${app_utils_ALL_SOURCES} ${SWIG_APP_UTILS_GUILE_CPP})
target_link_libraries(gnc-app-utils ${app_utils_ALL_LIBRARIES})

target_include_directories (gnc-app-utils
Expand Down
16 changes: 14 additions & 2 deletions libgnucash/app-utils/app-utils.i
Expand Up @@ -21,6 +21,10 @@
%module sw_app_utils
%{
/* Includes the header in the wrapper code */
#ifdef __cplusplus
extern "C"
{
#endif
#include <config.h>
#include <option-util.h>
#include <gnc-euro.h>
Expand All @@ -33,14 +37,22 @@
#include <gnc-sx-instance-model.h>

#include "gnc-engine-guile.h"
#ifdef __cplusplus
}
#endif
%}

#if defined(SWIGGUILE)
#if defined(SWIGGUILE) //Always C++
%{
extern "C"
{
#include "guile-mappings.h"

SCM scm_init_sw_app_utils_module (void);
}
%}

%include "gnc-optiondb.i"
#endif

#if defined(SWIGPYTHON)
Expand Down Expand Up @@ -76,7 +88,7 @@ void gnc_option_db_set_option_selectable_by_name(SCM guile_option,
GList *node;

for (node = $1; node; node = node->next)
list = scm_cons(gnc_quoteinfo2scm(node->data), list);
list = scm_cons(gnc_quoteinfo2scm(static_cast<gnc_commodity*>(node->data)), list);

$result = scm_reverse(list);
}
Expand Down
77 changes: 40 additions & 37 deletions libgnucash/app-utils/gnc-optiondb.i
Expand Up @@ -3,6 +3,7 @@
*
* unique_ptr SWIG wrapper from https://stackoverflow.com/questions/27693812/how-to-handle-unique-ptrs-with-swig
*/
#if defined(SWIGGUILE)

namespace std {
%feature("novaluewrapper") unique_ptr;
Expand Down Expand Up @@ -37,17 +38,11 @@ namespace std {

%enddef

%module sw_gnc_optiondb
//%module sw_gnc_optiondb
%{
extern "C"
{
#include <config.h>
#include <libguile.h>
#include <gnc-engine-guile.h>
}
#include "gnc-optiondb.hpp"
#include "gnc-optiondb-impl.hpp"
extern "C" SCM scm_init_sw_gnc_optiondb_module(void);
SCM scm_init_sw_gnc_optiondb_module(void);
%}

%include <std_string.i>
Expand Down Expand Up @@ -118,27 +113,6 @@ scm_from_value<const QofInstance*>(const QofInstance* value)
return scm_guid;
}

/* Account is actually a typedef for struct account_s and SWIG insists on using
* the struct name (i.e. account_s) in C++ and the alias (i.e. Account) in
* C. Oddly the compiler's type resolution also fails to consider them the same
* so we have to use the struct name here to get the template to resolve
* correctly.
*/
using GncOptionAccount_sList = std::vector<const account_s*>;

template <>inline SCM
scm_from_value<GncOptionAccount_sList>(GncOptionAccount_sList value)
{
SCM s_list = SCM_EOL;
for (auto acct : value)
{
SCM elem = scm_list_1(SWIG_NewPointerObj(acct, SWIGTYPE_p_account_s, 0));
s_list = scm_append(scm_list_2(s_list, elem));
}

return s_list;
}

template <typename ValueType> inline ValueType
scm_to_value(SCM new_value)
{
Expand Down Expand Up @@ -166,19 +140,34 @@ scm_to_value<int64_t>(SCM new_value)
return scm_to_int64(new_value);
}

template <>inline SCM
scm_from_value<GncOptionAccountList>(GncOptionAccountList value)
{
SCM s_list = SCM_EOL;
for (auto acct : value)
{
SCM elem = scm_list_1(SWIG_NewPointerObj(acct, SWIGTYPE_p_Account, 0));
s_list = scm_append(scm_list_2(s_list, elem));
}

return s_list;
}

QofBook* gnc_option_test_book_new();
void gnc_option_test_book_destroy(QofBook*);

QofBook*
qof_book_new()
gnc_option_test_book_new()
{
return static_cast<QofBook*>(g_object_new(QOF_TYPE_BOOK, nullptr));
}

void
qof_book_destroy(QofBook* book)
gnc_option_test_book_destroy(QofBook* book)
{
g_object_unref(book);
}

using Account = struct account_s;
%}

%ignore OptionClassifier;
Expand Down Expand Up @@ -228,6 +217,19 @@ using Account = struct account_s;
$1 = &choices;
}


%typemap(in) GncOptionAccountList
{
auto len = scm_to_size_t(scm_length($input));
for (std::size_t i = 0; i < len; ++i)
{
SCM s_account = scm_list_ref($input, scm_from_size_t(i));
Account* acct = (Account*)SWIG_MustGetPtr(s_account,
SWIGTYPE_p_Account, 1, 0);
$1.push_back(acct);
}
}

%typemap(in) GncOptionAccountTypeList& (GncOptionAccountTypeList types)
{
auto len = scm_to_size_t(scm_length($input));
Expand Down Expand Up @@ -259,7 +261,7 @@ using Account = struct account_s;
{
SCM s_account = scm_list_ref($input, scm_from_size_t(i));
Account* acct = (Account*)SWIG_MustGetPtr(s_account,
SWIGTYPE_p_account_s, 1, 0);
SWIGTYPE_p_Account, 1, 0);
$1.push_back(acct);
}
}
Expand All @@ -271,7 +273,7 @@ using Account = struct account_s;
{
SCM s_account = scm_list_ref($input, scm_from_size_t(i));
Account* acct = (Account*)SWIG_MustGetPtr(s_account,
SWIGTYPE_p_account_s, 1, 0);
SWIGTYPE_p_Account, 1, 0);
acclist.push_back(acct);
}
$1 = &acclist;
Expand All @@ -282,7 +284,7 @@ using Account = struct account_s;
$result = SCM_EOL;
for (auto acct : $1)
{
SCM elem = scm_list_1(SWIG_NewPointerObj(acct, SWIGTYPE_p_account_s, 0));
SCM elem = scm_list_1(SWIG_NewPointerObj(acct, SWIGTYPE_p_Account, 0));
$result = scm_append(scm_list_2($result, elem));
}
}
Expand All @@ -292,7 +294,7 @@ using Account = struct account_s;
$result = SCM_EOL;
for (auto acct : *$1)
{
SCM elem = scm_list_1(SWIG_NewPointerObj(acct, SWIGTYPE_p_account_s, 0));
SCM elem = scm_list_1(SWIG_NewPointerObj(acct, SWIGTYPE_p_Account, 0));
$result = scm_append(scm_list_2($result, elem));
}
}
Expand Down Expand Up @@ -326,7 +328,6 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
}, *($self->_get_option()));
}
};

%extend GncOptionDB {
%template(set_option_string) set_option<std::string>;
%template(set_option_int) set_option<int>;
Expand Down Expand Up @@ -369,3 +370,5 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
GncOption_set_value_from_scm(&(db_opt->get()), new_value);
}
%}

#endif //SWIGGUILE
45 changes: 2 additions & 43 deletions libgnucash/app-utils/test/CMakeLists.txt
Expand Up @@ -30,9 +30,6 @@ gnc_add_test_with_guile(test-scm-query-string test-scm-query-string.cpp
add_app_utils_test(test-sx test-sx.cpp)

set(gtest_gnc_option_SOURCES
${MODULEPATH}/gnc-option.cpp
${MODULEPATH}/gnc-option-impl.cpp
${MODULEPATH}/gnc-optiondb.cpp
gtest-gnc-option.cpp
gtest-gnc-optiondb.cpp)

Expand Down Expand Up @@ -91,49 +88,10 @@ if (HAVE_SRFI64)
FALSE
)

set(SWIG_ARGS "-c++" "-procdoc" "sw-gnc-option-doc" "-procdocformat" "plain")
gnc_add_swig_guile_command(swig-gnc-optiondb-guile
SWIG_GNC_OPTIONDB_GUILE_CPP swig-gnc-optiondb-guile.cpp
${MODULEPATH}/gnc-optiondb.i
""
)
add_library(swig-gnc-optiondb MODULE
${MODULEPATH}/gnc-option.cpp
${MODULEPATH}/gnc-option-impl.cpp
${MODULEPATH}/gnc-optiondb.cpp
${SWIG_GNC_OPTIONDB_GUILE_CPP}
)
set(swig_gnc_optiondb_INCLUDES
${MODULEPATH}
${CMAKE_SOURCE_DIR}/bindings/guile
${CMAKE_SOURCE_DIR}/libgnucash/engine
${CMAKE_BINARY_DIR}/common # for config.h
${GLIB2_INCLUDE_DIRS}
${GUILE_INCLUDE_DIRS}
)

set(swig_gnc_optiondb_LIBS
gnc-engine
gnc-app-utils
gnucash-guile
${GLIB2_LDFLAGS}
${GUILE_LDFLAGS}
)

target_link_libraries(swig-gnc-optiondb ${swig_gnc_optiondb_LIBS})
target_include_directories(swig-gnc-optiondb
PRIVATE ${swig_gnc_optiondb_INCLUDES})

install(TARGETS swig-gnc-optiondb
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

gnc_add_scheme_test_targets(scm-test-gnc-optiondb
"test-gnc-optiondb.scm"
"tests"
"swig-gnc-optiondb;scm-srfi64-extras"
"swig-apputils-guile-cpp;scm-srfi64-extras"
FALSE
)
gnc_add_scheme_tests("test-gnc-optiondb.scm")
Expand Down Expand Up @@ -173,4 +131,5 @@ set_dist_list(test_app_utils_DIST
${test_app_utils_scheme_SOURCES}
${test_app_utils_SOURCES}
${test_autoclear_SOURCES}
${gtest_gnc_option_SOURCES}
)
8 changes: 4 additions & 4 deletions libgnucash/app-utils/test/test-gnc-optiondb.scm
Expand Up @@ -26,10 +26,10 @@
(use-modules (gnucash gnc-module))
(eval-when
(compile load eval expand)
(load-extension "libswig-gnc-optiondb" "scm_init_sw_gnc_optiondb_module"))
(load-extension "_sw_app_utils" "scm_init_sw_app_utils_module"))

(use-modules (gnucash engine))
(use-modules (sw_gnc_optiondb))
(use-modules (sw_app_utils))

(define (run-test)
(test-runner-factory gnc:test-runner)
Expand Down Expand Up @@ -84,7 +84,7 @@

(define (cleanup book root)
;; Destroying the book destroys the account tree too
(qof-book-destroy book))
(gnc-option-test-book-destroy book))

(define (test-make-account-list-option book)
(test-group "test-make-account-list-option"
Expand Down Expand Up @@ -122,7 +122,7 @@
(let ((acct (gnc-option-value optiondb "salt" "pork")))
(test-equal (list (cadr acctlist)) acct)))))

(let* ((book (qof-book-new))
(let* ((book (gnc-option-test-book-new))
(root-account (gnc-account-create-root book)))
(test-group-with-cleanup "test-gnc-make-account-list-options"
(make-account-tree book root-account)
Expand Down

0 comments on commit cbf7d70

Please sign in to comment.