#1271 $null ignores %typemap(ctype) <Type> "void"

closed-invalid
csharp (36)
5
2012-12-11
2012-09-10
Eric Cabot
No

Context : porting C++ to C#

Hello, I have many C++ methods that are of the following form:

Status foo( ... );

Status is more-or-less an enum with values like STATUS_OK, STATUS_FAILURE, STATUS_OUT_OF_MEMORY, etc.

It's similar to Microsoft's HRESULT type that so many functions return.

In our port to C# we were requested to get rid of Status and use exceptions instead

So I came up with typemaps like the following:
%typemap(ctype) Status "void"
%typemap(out) Status
{
if ( $1 == STATUS_FAILURE )
SWIG_CSharpSetPendingException( SWIG_CSharpApplicationException, $1.str() );
else if ( $1 == STATUS_OUTOFMEMORY )
SWIG_CSharpSetPendingException( SWIG_CSharpOutOfMemoryException, $1.str() );
...
}
etc

But now I stumbled over a function that is of this form:

Status foo( SomeClass & arg );
The typemap for the argument "arg" is this one from csharp.swg:

%typemap(in, canthrow=1) SWIGTYPE & %{ $1 = ($1_ltype)$input;
if (!$1) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "$1_type type is null", 0);
return $null;
} %}

$null is replaced by 0 here so the file ..._wrap.cxx doesnt compile ! ('void' function returning a value)

The documentation says that $null will be "nothing" if the function doesnt return a value.

In my case the native method does return a value but the wrapped version in ..._wrap.cxx doesn't (it's returning void because of %typemap(ctype) Status "void")

Note: I cannot simply removed "Status" from my interface file otherwise I would miss the code in the %typemap(out) Status.

Thanks

Discussion

  • William Fulton
    William Fulton
    2012-12-11

    There is the 'null' attribute you can use in the 'out' typemap to overcome this difficulty. I have just documented it in CSharp.html for swig-2.0.9. Change your typemap to:

    %typemap(out, null="") Status { ... }

    then the empty string is used when a non-void type is the return type of the function being wrapped.

     
  • William Fulton
    William Fulton
    2012-12-11

    • status: open --> closed-invalid