Apply typemap to single method

View: New views
2 Messages — Rating Filter:   Alert me  

Apply typemap to single method

by eodabash :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is it possible to create a typemap that applies only to a single method in a single class? For instance suppose I had the following class in C++.

class MyClass
{
public:

void MyMethod(param1, param2, param3);

}

If I wanted to apply a specific typemap to the parameters in MyMethod and no other methods/classes what would be the SWIG syntax to do this (if there is any)? For instance I can do a typemap based on a specific parameter type and name, but this will modify all such parameters in all methods and classes. I want this same behavior, but scoped to a single class + method combination.


Thanks,
Evan

Re: Apply typemap to single method

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

eodabash wrote:

> Is it possible to create a typemap that applies only to a single method in a
> single class? For instance suppose I had the following class in C++.
>
> class MyClass
> {
> public:
>
> void MyMethod(param1, param2, param3);
>
> }
>
> If I wanted to apply a specific typemap to the parameters in MyMethod and no
> other methods/classes what would be the SWIG syntax to do this (if there is
> any)? For instance I can do a typemap based on a specific parameter type and
> name, but this will modify all such parameters in all methods and classes. I
> want this same behavior, but scoped to a single class + method combination.
>
This isn't possible as the matching rules for typemaps are on the
*type*, and so can only be narrowed down to the name associated with the
type. Instead you can try one of this workaround which effectively
renames 'int param1' into 'int p1' allowing a named typemap for the
parameter:

// interface file:
%{
#include "MyClass.h"
%}

%typemap(in) int p1 {...}
%extend MyClass {
   void MyMethod(int p1, int p2, int p3) {
     $self->MyMethod(p1, p2, p3);
   }
}
%ignore MyClass::MyMethod;

%include "MyClass.h"

// MyClass.h file:
class MyClass {
public:
   void MyMethod(int param1, int param2, int param3);
};

You'd use the above if you didn't want to modify anything in the header
MyClass.h file. Otherwise, don't %include the header file, just define
the method in your interface file with modified parameter names:

%{
#include "MyClass.h"
%}

%typemap(in) int p1 {...}

class MyClass {
public:
   void MyMethod(int p1, int p2, int p3);
};


Or if you have a typemap, you can clear it so that it no longer takes
effect:

%{
#include "MyClass.h"
%}

class MyClass {
public:
   %typemap(in) int param1 { ... }
   void MyMethod(int param1, int param2, int param3);

   %clear int param1;
   void MyMethod2(int param1, int param2, int param3);
};

so then the typemap is used for the 1st parameter in MyMethod, but not
MyMethod2.

William

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user