Fix up the SWIG wrapper for GncOptionDBPtr.

Thanks to Flexo@stackoverflow for
https://stackoverflow.com/questions/27693812/how-to-handle-unique-ptrs-with-swig
5 years later, why isn't this in SWIG yet?
This commit is contained in:
John Ralls 2019-09-29 12:12:32 -07:00
parent 4146251cc7
commit 0a13b4c518
2 changed files with 39 additions and 3 deletions

View File

@ -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 <typename Type>
struct unique_ptr {
typedef Type* pointer;
explicit unique_ptr( pointer Ptr );
unique_ptr (unique_ptr&& Right);
template<class Type2, Class Del2> unique_ptr( unique_ptr<Type2, Del2>&& 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<Type>;
%newobject std::unique_ptr<Type>::release;
%typemap(out) std::unique_ptr<Type> %{
$result = SWIG_NewPointerObj(new $1_ltype(std::move($1)), $&1_descriptor, SWIG_POINTER_OWN);
%}
%enddef
%module sw_gnc_optiondb
%{
#include <libguile.h>
@ -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"

View File

@ -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"))