William S Fulton | 20 Aug 08:29 2010
Picon

Re: Having problems with csdirectorin with C# 'out' parameters.

Chris Haukap wrote:
> Hello everyone
> 
>  
> 
> I have a C++ base class that I need to derive from in C#.  The function 
> I need to wrap takes a reference to a structure.  I have defined 
> typemaps to turn this into an ‘out’ parameter on the C# side.
> 
> The problem is there appears to be type mismatches between the wrapped 
> class and its corresponding director.
> 
>  
> 
> Here are my defined typemaps:
> 
>  
> 
> %typemap(imtype)  FS *OUTPUT,
> 
>                   FS &OUTPUT "out IntPtr /*Blah_imtype*/"
> 
> %typemap(cstype, inattributes="out") FS *OUTPUT,
> 
>                  FS &OUTPUT " FS /*Blah_cstype*/"
> 
> %typemap(csin,
> 
>          pre= "    IntPtr handle = IntPtr.Zero;",
> 
>          post="    $csinput =  new FS(handle, false);")
> 
>             FS *OUTPUT, FS &OUTPUT "out handle /*Blah_csin*/"
> 
> //%typemap(csdirectorin,
> 
> //         pre= "    FS temp = null;",
> 
> //         post="    $iminput = FS.getCPtr(temp).Handle;")
> 
> //            FS *OUTPUT, FS &OUTPUT "out temp /*Blah_csdirectorin*/"
> 
> %apply FS & OUTPUT { FS & };
> 
>  
> 
> Here’s the C++ class ‘Blah’:
> 
>  
> 
> class Blah
> 
> {
> 
> public:
> 
>    virtual ~ Blah(){}
> 
>    virtual void createImageStage(FS& fs);
> 
> };
> 
>  
> 
>  
> 
> Here’s the C# definition of createImageStage (in the generated Blah.cs 
> class):
> 
>  
> 
>   public virtual void createImageStage(out FS /*Blah_cstype*/ fs) {
> 
>     IntPtr handle = IntPtr.Zero;
> 
>     try {
> 
>       if (this.GetType() == typeof(Blah)) 
> RasterSDKPINVOKE.Blah_createImageStage(swigCPtr, out handle 
> /*Blah_csin*/); else 
> RasterSDKPINVOKE.Blah_createImageStageSwigExplicitBlah(swigCPtr, out 
> handle /*Blah_csin*/);
> 
>       if (RasterSDKPINVOKE.SWIGPendingException.Pending) throw 
> RasterSDKPINVOKE.SWIGPendingException.Retrieve();
> 
>     } finally {
> 
>     fs =  new FS(handle, false);
> 
>     }
> 
>   }
> 
>  
> 
> Here’s the problem code (in the generated Blah.cs class) with 
> ‘csdirectorin’ turned off (note that the call to createImageStage is 
> expecting an ‘out FS’ and is being given ‘new FS’):
> 
>  
> 
>   private void SwigDirectorcreateImageStage(out IntPtr /*Blah_imtype*/ fs) {
> 
>     createImageStage(new FS(fs, false));
> 
>   }
> 
>  
> 
> Here’s the problem code again (in the generated Blah.cs class) with 
> ‘csdirectorin’ turned on  (note ‘SwigDirectorcreateImageStage’ takes an 
> ‘out IntPtr’ – which comes from the imtype typemap which does not sync 
> up well with the ‘out FS’ that createImageStage wants.  So I’ve defined 
> some pre/post attributes to do a quick and dirty conversion but those 
> attributes are not being applied here for some reason so ‘temp’ is 
> undefined):
> 
>  
> 
>   private void SwigDirectorcreateImageStage(out IntPtr /*Blah_imtype*/ fs) {
> 
>     createImageStage(out temp /*Blah_csdirectorin*/);
> 
>   }
> 
>  
> 
> Should this be working?  Am I approaching this in the wrong way?  Is 
> there a better solution?  Any help would be appreciated, Thank you.  A 
> full (not) compiling example is available upon request.
> 

Directors havn't been tested with the post and pre attributes. Arguably 
the csdirectorin typemap should support these attributes, but this is 
not implemented. The code changes would go in Source/Modules/csharp.cxx. 
Please raise a bug report and if possible supply a patch.

William

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

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 

Gmane