Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegates in SWIG - C#

( complete duplicate of http://old.nabble.com/C%2B%2B-pointer-to-method-as-parameter-to-C--td17645155.html , but couldn't make the proposed macro work)

I've got the following C++ function (simplified) :

InputPort addInputPort(void(*callback)(InputPort));

Problem : the signature of the generated C# function is :

public InputPort addInputPort(SWIGTYPE_p_f__InputPort____void callback)

SWIGTYPE_p_f__InputPort____void is not a delegate (and has no public constructor anyway), so I can't use addInputPort.

How do I tell SWIG to use a delegate instead ? If the solution involves %typemap, please be extra patient with me...

like image 619
Calvin1602 Avatar asked Jul 06 '11 15:07

Calvin1602


1 Answers

The solution involves a %typemap. The effect you are seeing is the default useless mapping for unknown types. In your case, the unknown type is the function type void *(InputPort).

In fact, all of SWIG's translation is based on %typemaps which have already been written and live in standard SWIG libraries. You can investigate the initial %typemaps for C# in this Subversion location.

In particular, look for SWIGStringHelper in csharphead.swg. That code maps a delegate to a C callback. The trick is to add a helper callback implementation in the SWIG module.

like image 187
Pedro Lamarão Avatar answered Oct 13 '22 23:10

Pedro Lamarão