« Return to Thread: Another reference problem

Re: Another reference problem

by wsfulton :: Rate this Message:

Reply to Author | View in Thread

Marco Dinacci wrote:

> Hi,
>
> I've a problem passing a Python object to a method that expects a
> reference value, I read lots of previous messages but I couldn't find
> one that fit my situation so here is my problem:
>
> Python class:
>
> import mymodule as mm
> class PyFoo(mm.Foo): pass
> class PyBar(mm.Bar): pass
>
> pf = PyFoo()
> pb = PyBar()
> pb.test(0, pf) # the problem manifests here
>
> Foo and Bar are C++ classes and Bar::test has the following signature:
>
> void test(my_enum_type, Foo&);
>
directorin won't be used unless this method is virtual. Also
my_enum_type must be a global enum. You must also make sure that
directors are turned on for the class and the test method, turn it on
globally:
%feature("director") ;
The typemap must also be declared before SWIG parses test. Here is a
full example:

%module(directors="1") example

%feature("director") ;

%typemap(directorin) (my_enum_type et, Foo &foo) {
// $input - $1 - "$1_name" - $2 - "$2_name"
}

%inline %{
class Foo {
public:
   virtual ~Foo() {}
};
enum my_enum_type { a, b };
class Bar {
public:
   virtual ~Bar() {}
   virtual void test(my_enum_type et, Foo &foo) {}
};
%}

You'll have to fill in the contents of the directorin typemap with
whatever you are hoping to achieve.

> The error message I have is:
> TypeError: "in method Bar_test, argument 3 of type 'Foo &'"
>
> I tried to define a typemap like this:
> %typemap(directorin) (my_enum_type et, Foo &foo) {
> $1 = et;
> $2 = &foo;
> }
>
For special variables, the python typemaps use $input and for some
strange reason $1_name is used instead of $1.

William

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

 « Return to Thread: Another reference problem