C# mapping for "long" incorrect in typemaps.i

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

C# mapping for "long" incorrect in typemaps.i

Valient Gough-2

Hi,

I'm using SWIG 1.3.27 to create a wrapper for some third-party open-source software. 

The size of a 'long' in C/C++ depends on the system (32bit vs 64bit).  In C#, a long is always 64bit.  This is probably why in typemaps.i, a long is mapped to an integer, however that is also incorrect.

A 'long' can hold a pointer on both system types, so sizeof(long) == sizeof(void*).  The only type like that in C# is an IntPtr.  The software I'm wrapping has an inode pointer which is typedefed as an unsigned long.  So I wrapped this as an IntPtr in SWIG by doing this:

%typemap(ctype) fuse_ino_t "unsigned long"
%typemap(imtype) fuse_ino_t "IntPtr"
%typemap(cstype) fuse_ino_t "IntPtr"

That was fine, except when I later tried to fix up some output types and so I included typemaps.i.  That caused many interfaces to get redefined incorrectly, which I attribute to mappings from long -> 32bit int inside of typemaps.i.    This would work on a 32bit system, but it means that my C# code would only work on 32bit machines, and not 64bit.  Actually, it is worst then that -- the SWIG P/Invoke interfaces would expect 32bit data, but the C libraries would expect 64bit.  Not good.

So, what are the options?

1)  change the definition of long in typemaps.i for csharp target.  Do other C# users agree that this should be changed (either by removing the long typemap, or using IntPtr, etc)

2) I make a local copy of typemaps.i that I distribute with my code, with the long typemap removed.

3) is there a way to override the mapping from typemaps.i?  I've tried moving the %include before or after my %typemap definitions, but I can't seem to override what happens in typemaps.i.

regards,
Valient


Reply | Threaded
Open this post in threaded view
|

Re: C# mapping for "long" incorrect in typemaps.i

William S Fulton
Valient Gough wrote:

>
> Hi,
>
> I'm using SWIG 1.3.27 to create a wrapper for some third-party
> open-source software.
>
> The size of a 'long' in C/C++ depends on the system (32bit vs 64bit).  
> In C#, a long is always 64bit.  This is probably why in typemaps.i, a
> long is mapped to an integer, however that is also incorrect.
>
> A 'long' can hold a pointer on both system types, so sizeof(long) ==
> sizeof(void*).  The only type like that in C# is an IntPtr.  The
> software I'm wrapping has an inode pointer which is typedefed as an
> unsigned long.  So I wrapped this as an IntPtr in SWIG by doing this:
>
> %typemap(ctype) fuse_ino_t "unsigned long"
> %typemap(imtype) fuse_ino_t "IntPtr"
> %typemap(cstype) fuse_ino_t "IntPtr"
>
> That was fine, except when I later tried to fix up some output types and
> so I included typemaps.i.  That caused many interfaces to get redefined
> incorrectly, which I attribute to mappings from long -> 32bit int inside
> of typemaps.i.    This would work on a 32bit system, but it means that
> my C# code would only work on 32bit machines, and not 64bit.  Actually,
> it is worst then that -- the SWIG P/Invoke interfaces would expect 32bit
> data, but the C libraries would expect 64bit.  Not good.
>
> So, what are the options?
>
> 1)  change the definition of long in typemaps.i for csharp target.  Do
> other C# users agree that this should be changed (either by removing the
> long typemap, or using IntPtr, etc)
>
> 2) I make a local copy of typemaps.i that I distribute with my code,
> with the long typemap removed.
>
> 3) is there a way to override the mapping from typemaps.i?  I've tried
> moving the %include before or after my %typemap definitions, but I can't
> seem to override what happens in typemaps.i.
>
3) is the easiest and best. Just write your own typemaps after your
%include <typemaps.i>. That way the typemaps which were parsed last will
be used. So I suggest the easy way to 'write' the typemaps is to just
add in your own versions of the INPUT_TYPEMAP, OUTPUT_TYPEMAP and
INOUT_TYPEMAP macros with the types you want.

William


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user