Lindley French | 25 Jun 20:32 2015

dynamic_cast support

I can see in the SWIG codebase several attempts to support downcasting in various ways. Java downcasting just doesn't work, and the solution offered in the docs, which is to enumerate all the various subclasses in the out typemap, is a maintenance problem.

I see some attempt to create feature:javadowncast, but it seems to only work with directors. (If you try to use it with everything, it'll generate compile errors for some types.) I see a dynamic_cast test suite which is extremely basic. I don't see any comprehensive solution to the problem.

dynamic_cast in C++ is trivial. The only *real* problem is that it requires the target type to be specified at compile time, which some languages don't support well. There's no way to pass a Java Class object to a C++ dynamic_cast, for instance.

It seems to me that the solution is to add a feature which causes all C++ subclasses of Base to be %extend'ed with a method:
static Derived* DynamicCast(Base* b) {
  return dynamic_cast<Derived*>(b);

This could be done manually with %extend, but again that's a maintenance problem. It seems like there should be a way to write an %extend that applies individually to all *subclasses* of a given type, with the concrete type specified in a variable, perhaps $derived.

I don't mind figuring out the code myself, but I thought I'd ask to see if I'm missing anything or if there's a better solution to this problem available.
Monitor 25 network devices or servers for free with OpManager!
OpManager is web-based network management software that monitors 
network devices and physical & virtual servers, alerts via email & sms 
for fault. Monitor 25 devices for free with no restriction. Download now;119417398;o
Swig-user mailing list
Swig-user <at>
Peter Toft | 23 Jun 19:56 2015

-fpic needed for C -> Python?

Hi all

I often happily use swig to wrap C/C++ for Python.
I can see that the nice swig documentation
writes how to do that. E.g. section 6.4

Side-remark: Some time ago I also wrote a small article about that (in 

I have a problem with a pre-compiled 64 bit C/C++ static library - 
apparently not compiled with -fpic/-fPIC.
Is there any way e.g. with wrappers that I can make a Linux shared 
object (plus a .pyinterface)
which can be used in a vanilla Linux Python?



Peter Toft <pto <at>>

Monitor 25 network devices or servers for free with OpManager!
OpManager is web-based network management software that monitors 
network devices and physical & virtual servers, alerts via email & sms 
for fault. Monitor 25 devices for free with no restriction. Download now;119417398;o
Emre Metin | 22 Jun 13:33 2015


I'm tring to wrap my c++ code to c# with using Swig. If the output
destination of C# files is class library , there is no error and
succesfully build.

However I want to support both windows phone 8.1 and windows 8.1
because of this reason I'm using portable class library rather than
normal class library. In this situation I'm getting error that says;

The type or namespace name 'HandleRef' does not exist in the namespace
'System.Runtime.InteropServices' (are you missing an assembly

I really don't know what I am missing. What is the solution of this
problem? My guess windows phone 8.1 and windows 8.1 does not support
HandleRef but I'm not sure. If it so, What should I do?

Stackoverflow link;

Monitor 25 network devices or servers for free with OpManager!
OpManager is web-based network management software that monitors 
network devices and physical & virtual servers, alerts via email & sms 
for fault. Monitor 25 devices for free with no restriction. Download now;119417398;o
Kris Lamb | 19 Jun 15:36 2015

Dependency checking

Hi, I've found the -M option for checking the dependencies from the interface file, is there a similar option to know what files will be generated by swig?

I'm trying to create a tool that will only perform the swig build step if any of the input dependencies have been modified since the last build or if any of the output files are missing.

For instance when wrapping to csharp you get the module_wrap.cxx and then a whole bunch of .cs files for each class that has been wrapped.


Swig-user mailing list
Swig-user <at>
Christoph Müller | 19 Jun 08:55 2015

WG: Re: Swig C# NullException problem

> Date: Wed, 10 Jun 2015 22:38:59 +0100
> From: William S Fulton <wsf <at>>
> Subject: Re: [Swig-user] Swig C# NullException problem
> To: Linda Schey <linda.schey <at>>
> Cc: swig-user <swig-user <at>>
> Message-ID:
> 	<CANGqftDaEe0AHJPo+5uqsJ9s5tKTm_BrWjjxa7erAGzx6eYbGg <at>>
> Content-Type: text/plain; charset=UTF-8
> On 8 June 2015 at 16:12, Linda Schey <linda.schey <at>> wrote:
> > Hello,
> >
> > we have encountered a problem with swig for which we couln't find 
> > any solution in the documentation nor in forum posts.
> > We are right now developing an universal plugin which will connect 
> > the 3d program Blender with the 3d engine of our university. 
> > Blenders API is written in c/c++ whereas our engine is developed in c#.
> > We need to swig a lot of classes which lead us to write typemaps 
> > that won't address individual cases but set up rules for all custom
types etc.
> >
> > It's a huge learning process for us but right now we are having 
> > troubles with the exception handling or at least it seems like this 
> > is the error source.
> > Swig will always create a default exception in the generated c++ 
> > file which results in zero as a return value. But this will create a 
> > compile error with the method of our custom class, that can't have zero
as a return value.
> >
> > We've already gone through the documentation chapters about 
> > exception handling and couldn't find a solution for our problem.
> >
> > How can we set up a custom exception that will result in a default 
> > value if the object is null? Is there an obvious mistake we're just 
> > too narrow minded to see? Can the error be fixed in another way? 
> > (The object can never be null which means we really don't need this
> > exception.)
> >
> > Please find below our sample code which includes our typemaps (for 
> > the exception handling and other custom types) and the generated c++
> >
> > A huge thank you in advance for any clues and with best wishes,
> >
> > Linda Schey and Sarah Haefele
> > Computer Science in Media Master, Furtwangen University Germany
> >
> > --------------------------------------
> > cpp_file.h
> > --------------------------------------
> > std::array<float, 3> FooInArrayOut(Foo f);
> >
> > --------------------------------------
> > cpp_file.i
> > --------------------------------------
> > //Map std::array<float,3> To Fusee.Math.float3
> > %typemap(ctype)  std::array<float, 3> "std::array<float, 3> /* 
> > std::array<float, 3>_ctype */"
> > %typemap(imtype) std::array<float, 3> "Fusee.Math.float3 /* 
> > Fusee.Math.float3*_imtype */"
> > %typemap(cstype) std::array<float, 3> "Fusee.Math.float3 /* 
> > iFusee.Math.float3_cstype */"
> > %typemap(csin)   std::array<float, 3> "$csinput /*
> > */"
> > %typemap(in)     std::array<float, 3> %{ $1 = $input /*
> > in*/; %}
> > %typemap(out)    std::array<float, 3> %{ /* Fusee.Math.float3
out*/$result =
> > $1/* Fusee.Math.float3 out*/; %}
> > %typemap(csout)  std::array<float, 3> { return $imcall/*
> > Fusee.Math.float3 csout*/; }
> >
> > --------------------------------------
> > cpp_file_wrap.cxx
> > --------------------------------------
> >  SWIGEXPORT std::array<float, 3> /* std::array<float, 3>_ctype */ 
> > SWIGSTDCALL CSharp_cpp_file_FooInArrayOut(void * jarg1, void * jarg2) {
> >   std::array<float, 3> /* std::array<float, 3>_ctype */ jresult ;
> >   cpp_file *arg1 = (cpp_file *) 0 ;
> >   Foo arg2 ;
> >   Foo *argp2 ;
> >   std::array< float,3 > result;
> >
> >   arg1 = (cpp_file *)jarg1;
> >   argp2 = (Foo *)jarg2;
> >   if (!argp2) {
> >     
> > SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullExcept
> > io n, "Attempt to dereference null Foo", 0);
> >     return 0;
> >   }
> >   arg2 = *argp2;
> >   result = (arg1)->FooInArrayOut(arg2);
> >   /* Fusee.Math.float3 out*/jresult = result/* Fusee.Math.float3 out*/;
> >   return jresult;
> > }
> >
> > --------------------------------------
> > Error Msg
> > --------------------------------------
> > error C2440: 'return' : cannot convert from 'int' to
> I don't think you can use std::array<float, 3> as a return type in wrapper
function. How is the CLI going to know how to marshal this type to
Fusee.Math.float3 ?
> The exception handling is written such that the return type return type
will be void or a type that will accept zero. I don't think any other type
will easily be handling without overriding quite a bit of default code
> William

The CLI is capable of marshalling any C/C++ POD (plain-old-datatype -
classes or structs without methods, con- or destructors) to a respective C#
struct with the same memory layout. We are using this scheme to wrap c++
methods returning instances (not pointers) to c# methods returning structs.
It works very well in a big project where we SWIGged the C++ API of Cinema4D
(currently more than 700 classes wrapped -
and we're very happy with it. The funny thing is: This only works because
just by chance none of the numerous C++methods returning a POD on the stack
takes a pointer to a class as an argument:

A method like this works with the approach mentioned by Linda:
Vector TestReturnStructOnStack(); // (WORKS JUST FINE)

And a method like that doesn't - because the SWIG-generated C++-Stub tries
to return 0 (which is not castable to Vector):
Vector TestReturnStructOnStack(Foo *foo); // (C++ COMPILE ERROR in
SWIG-C++-Stub - cannot cast int to Vector)

I doubt that this is by design. There should be a customizable typemap-entry
for "Vector" allowing to change the default return value of any method
returning "Vector".
I tried to add a constructor taking an int to Vector. This cures the
compiler error but then Vector is not a POD anymore and the p/invoke will
Thanks for any help - we have been struggling with this for weeks now.

Prof. Christoph Müller
Computergrafik – Game-Engineering
Fakultät Digitale Medien
Hochschule Furtwangen

Lindley French | 15 Jun 23:07 2015

Should Java own parameters passed by value?

A week ago, I created to cause Java to own copies of director method parameters passed by value, in a manner analogous to how Java owns copies of objects returned by value.

The idea was that if it's being passed by value the object is (probably) copyable anyway, and it's less surprising if the reference handed to Java isn't invalidated after the call returns, since Java might want to store the data for later without having to unmarshall it or clone the object explicitly.

However, I think it's worth asking whether or not the list thinks this is a good idea in general.

First, there's the fact that this makes an extra copy: One when the object is passed by value, and the second when it is copied onto the heap for Java to own. Second, there's the problem of move-only types, which normally can be passed into methods taking arguments by value, but would not allow an explicit copy onto the heap. (It's worth noting this second issue applies to the return-by-value typemap as well....except possibly in the 'optimal mode' case.)

Both of these would be solved by using std::move(), but my understanding is that SWIG wants to avoid using any C++11 constructs for now. I don't suppose there's a macro that resolves to std::move() if and only if C++11 support is detected, is there?

Swig-user mailing list
Swig-user <at>
Joop Renes | 13 Jun 06:26 2015

please unsubscribe

Swig-user mailing list
Swig-user <at>
Lindley French | 12 Jun 23:24 2015

Typemap in director

I'm having problems defining the directorin typemap for a smart pointer in a macro.

It looks like this:
%typemap(directorin, descriptor="L$packagepath/$&javaclassname;") SmartPtr< TYPE > {

  const SmartPtr< TYPE >& smartptr = $1;

  if (smartptr.Get()) {


    $input = ($typemap(jni, TYPE))smartptr.Get();

  } else {

    $input = 0;



The problem is that $javaclassname is giving me garbage. I also tried defining it as:

%typemap(directorin, descriptor="L$packagepath/$&typemap(jstype, TYPE);") SmartPtr< TYPE >

However, the typemap is not being expanded within the string.

Swig-user mailing list
Swig-user <at>
Joel Andersson | 11 Jun 21:05 2015

Have data member getter function return a copy


If I wrap a structure such as

struct MyStruct {
 MyClass foo;

with "%immutable" feature, it will create a get-function for foo returning essentially a reference to foo. Is there any way of having this get-function return a copy of foo instead of a reference to foo?

That is, a get function:
const MyClass foo_get() const;
instead of
const MyClass& foo_get() const;

I am worried about the case where a reference to foo will outlive the structure pointed at. And I don't worry about the extra overhead of copying foo.

Best regards,

Joel Andersson, PhD
Ptge. Busquets 11-13, atico 3
E-08940 Cornella de Llobregat (Barcelona), Spain
Home: +34-93-6034011
Mobile: +34-63-4408800 (in Sweden also +46-707-360512)
Swig-user mailing list
Swig-user <at>
Don Bigler | 10 Jun 22:26 2015

std::vector and typemap(argout)

Currently std::vector defaults to in place assignment for the following example:

// C++ Code
void halve_in_place(std::vector<double>& v) {
    // would you believe this is the same as the above?

# Python Code

I have a function as follows:

// C++ code
void inputOutputExample ( const Vector& input1, const Vector& input2, std::vector<double>& dVector ) const;

I’d like to use the function in Python so that it matches the functions that use OUTPUT typemaps as follows:

# Python Code
v = example.inputOutputExample(in1,in2)

I’ve tried the following typemap definition:

%typemap(in, numinputs=0) std::vector<double>& OUTPUT(std::vector<double> temp) { $1 = &temp; } %typemap(argout) std::vector<double>& OUTPUT { %append_output(swig::from(*$1)); } %apply std::vector<double> & OUTPUT { std::vector<double>& dVector}; %typemap(argout) const std::vector<double>& dVector ""; %typemap(in, numinputs=0) const std::vector<double>& dVector ";
However this results in compilation errors after wrapping due to the following lines of code:

if (SWIG_IsNewObj(res2)) delete arg2; return resultobj; fail: if (SWIG_IsNewObj(res2)) delete arg2; return NULL;

The reason is res2 is not defined.  The error occurs in functions with  const std::vector<double>& dVector instead of std::vector<double>& dVector.  Why am I getting this error and how do I eliminate it?

Thank you,
Don Bigler

Swig-user mailing list
Swig-user <at>
Bojan Petrovic | 10 Jun 20:49 2015

Golang and C++ with MSVC compiler

I am trying to wrap a c++ library compiled with MSVC to use it from Go.

The *_wrap.cxx file produced by swig contains "__attribute__ ((weak))" which doesn't compile with MSVC. Is MSVC not supported or am I doing something wrong?


Swig-user mailing list
Swig-user <at>