diff --git a/libgnucash/app-utils/gnc-optiondb.i b/libgnucash/app-utils/gnc-optiondb.i index 01689ef041..88d690f54d 100644 --- a/libgnucash/app-utils/gnc-optiondb.i +++ b/libgnucash/app-utils/gnc-optiondb.i @@ -1,7 +1,42 @@ /* * Temporary swig interface file while developing C++ options. + * + * unique_ptr SWIG wrapper from https://stackoverflow.com/questions/27693812/how-to-handle-unique-ptrs-with-swig */ +namespace std { + %feature("novaluewrapper") unique_ptr; + template + struct unique_ptr { + typedef Type* pointer; + + explicit unique_ptr( pointer Ptr ); + unique_ptr (unique_ptr&& Right); + template unique_ptr( unique_ptr&& Right ); + unique_ptr( const unique_ptr& Right) = delete; + + + pointer operator-> () const; + pointer release (); + void reset (pointer __p=pointer()); + void swap (unique_ptr &__u); + pointer get () const; + operator bool () const; + + ~unique_ptr(); + }; +} + +%define wrap_unique_ptr(Name, Type) + %template(Name) std::unique_ptr; + %newobject std::unique_ptr::release; + + %typemap(out) std::unique_ptr %{ + $result = SWIG_NewPointerObj(new $1_ltype(std::move($1)), $&1_descriptor, SWIG_POINTER_OWN); + %} + +%enddef + %module sw_gnc_optiondb %{ #include @@ -15,4 +50,6 @@ extern "C" SCM scm_init_sw_gnc_optiondb_module(void); %ignore OptionUIItem; %ignore GncOption; +wrap_unique_ptr(GncOptionDBPtr, GncOptionDB); + %include "gnc-optiondb.hpp" diff --git a/libgnucash/app-utils/test/test-gnc-optiondb.scm b/libgnucash/app-utils/test/test-gnc-optiondb.scm index 4d26a803f7..f46b4303e4 100644 --- a/libgnucash/app-utils/test/test-gnc-optiondb.scm +++ b/libgnucash/app-utils/test/test-gnc-optiondb.scm @@ -37,10 +37,9 @@ (define (test-gnc-make-text-option) (test-begin "test-gnc-test-string-option") - (let* ((option-db (new-GncOptionDB)) + (let* ((option-db (gnc-option-db-new)) (string-opt (gnc-register-string-option option-db "foo" "bar" "baz" "Phony Option" "waldo"))) - (test-equal (GncOptionDB-lookup-option option-db "foo" "bar") "waldo") - (delete-GncOptionDB option-db)) + (test-equal (GncOptionDB-lookup-option (GncOptionDBPtr-get option-db) "foo" "bar") "waldo")) (test-end "test-gnc-make-string-option"))