William S Fulton | 4 Sep 2010 14:04
Picon
Favicon
Gravatar

Re: Detecting the constructor wrapper

Daniel Russel wrote:
> I have certain types of objects (namely the non-reference counted ones), which I want to disallow being
passed by pointer since that makes memory management with python very difficult. I can add a typemap for
the pointer, with a static assert, but that is also activated by the constructor wrapper (which I do want to
allow). What is the best way to detect that I am in a constructor wrapper (from within a typemap)? Thanks.

Run swig -debug-tmsearch and then you can see the typemap matching rules 
that will work, for example:

%inline %{
struct HeyHo {};
HeyHo* thing() { return 0; }
%}

You will see three 'out' typemaps:

example.i:7: Searching for a suitable 'out' typemap for: HeyHo *HeyHo::HeyHo
   Looking for: HeyHo *HeyHo::HeyHo
   Looking for: HeyHo *
   Looking for: SWIGTYPE *HeyHo::HeyHo
   Looking for: SWIGTYPE *
   Using: %typemap(out) SWIGTYPE *

...

example.i:7: Searching for a suitable 'out' typemap for: void HeyHo::~HeyHo
   Looking for: void HeyHo::~HeyHo
   Looking for: void
   Using: %typemap(out) void

...

example.i:8: Searching for a suitable 'out' typemap for: HeyHo *thing
   Looking for: HeyHo *thing
   Looking for: HeyHo *
   Looking for: SWIGTYPE *thing
   Looking for: SWIGTYPE *
   Using: %typemap(out) SWIGTYPE *

Hopefulyl it is clear that the first is for the constructor, the second 
for the destructor (which returns void) and the third is for the 'thing' 
method.

Now you can apply the typemap search rules to target just the 
constructor with this' named typemap' ...

%typemap(out) HeyHo* HeyHo::HeyHo "..."

If you rerun the -debug-tmsearch, you'll see it being used (just for the 
constructor):

example.i:7: Searching for a suitable 'out' typemap for: HeyHo *HeyHo::HeyHo
   Looking for: HeyHo *HeyHo::HeyHo
   Using: %typemap(out) HeyHo *HeyHo::HeyHo

Alternatively, take a look at the %refobject, also called the "ref" 
feature, which adds code to constructor wrappers.

William

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd

Gmane